Skip to content

Commit 949d8f3

Browse files
bug(wrap): add oom_score_adj to exec specs (#124)
This value can be forwarded from a CRI and ensures an appropriate OOM score is set for processes in cases where high memory use is seen. Signed-off-by: Alexander Merritt <alexander@edera.dev>
1 parent 17e5773 commit 949d8f3

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ pub struct ExecutableSpec {
8585
/// Requires `no_new_privs = true`.
8686
#[serde(default)]
8787
pub seccomp: Option<SeccompFilter>,
88+
89+
/// An optional out-of-memory score adjustment value.
90+
pub oom_score_adj: Option<i32>,
8891
}
8992

9093
#[derive(Default, Debug, Serialize, Deserialize)]

src/runner.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ impl AttachRequestBuilder {
9797
self
9898
}
9999

100+
pub fn set_oom_score_adj(mut self, score: i32) -> AttachRequestBuilder {
101+
self.config.exec.oom_score_adj = Some(score);
102+
self
103+
}
104+
100105
pub fn push_namespace(mut self, ns: Namespace) -> AttachRequestBuilder {
101106
if self.config.namespaces.is_none() {
102107
self.config.namespaces = vec![].into();
@@ -211,6 +216,11 @@ impl CreateRequestBuilder {
211216
self
212217
}
213218

219+
pub fn set_oom_score_adj(mut self, score: i32) -> CreateRequestBuilder {
220+
self.config.exec.oom_score_adj = Some(score);
221+
self
222+
}
223+
214224
pub fn set_hostname(mut self, hostname: &str) -> CreateRequestBuilder {
215225
self.config.hostname = hostname.to_string().into();
216226
self

src/wrap.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,11 @@ impl Wrappable for CreateRequest {
639639

640640
debug!("mount tree finalized, doing final prep");
641641

642+
// Ensure the process receives the desired out-of-memory score adjustment.
643+
if let Some(score) = self.exec.oom_score_adj {
644+
fs::write("/proc/self/oom_score_adj", score.to_string())?;
645+
}
646+
642647
// We need to toggle SECBIT before we change UID/GID,
643648
// or else changing UID/GID may cause us to lose the capabilities
644649
// we need to explicitly drop capabilities later on.
@@ -841,6 +846,11 @@ impl Wrappable for AttachRequest {
841846

842847
apply_capabilities(self.capabilities.as_ref())?;
843848

849+
// Ensure the process receives the desired out-of-memory score adjustment.
850+
if let Some(score) = self.exec.oom_score_adj {
851+
fs::write("/proc/self/oom_score_adj", score.to_string())?;
852+
}
853+
844854
debug!("all namespaces joined -- forking child");
845855
fork_and_wait()?;
846856

0 commit comments

Comments
 (0)