Skip to content

Commit b9b8bc3

Browse files
authored
fix(driver-kubernetes): propagate log_level as OPENSHELL_LOG_LEVEL env var (#1310)
The sandbox_to_k8s_spec function was inserting logLevel and environment fields at the Sandbox CR spec level. Neither field exists in the agents.x-k8s.io/v1alpha1 CRD schema, so Kubernetes silently dropped them and the log_level value never reached the sandbox pod. Remove the dead CR-level insertions. Build a merged environment map that includes OPENSHELL_LOG_LEVEL when log_level is set, and pass it to sandbox_template_to_k8s so the value flows through build_env_list into the pod container spec. Add a unit test verifying OPENSHELL_LOG_LEVEL appears in the container env and that logLevel is absent from the CR spec. Fixes #1265
1 parent 59475aa commit b9b8bc3

1 file changed

Lines changed: 22 additions & 20 deletions

File tree

  • crates/openshell-driver-kubernetes/src

‎crates/openshell-driver-kubernetes/src/driver.rs‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,14 @@ struct SandboxPodParams<'a> {
981981
enable_user_namespaces: bool,
982982
}
983983

984+
fn spec_pod_env(spec: Option<&SandboxSpec>) -> std::collections::HashMap<String, String> {
985+
let mut env = spec.map_or_else(Default::default, |s| s.environment.clone());
986+
if let Some(s) = spec.filter(|s| !s.log_level.is_empty()) {
987+
env.insert("OPENSHELL_LOG_LEVEL".to_string(), s.log_level.clone());
988+
}
989+
env
990+
}
991+
984992
fn sandbox_to_k8s_spec(
985993
spec: Option<&SandboxSpec>,
986994
params: &SandboxPodParams<'_>,
@@ -999,25 +1007,11 @@ fn sandbox_to_k8s_spec(
9991007
let inject_workspace = !user_has_vct;
10001008

10011009
if let Some(spec) = spec {
1002-
if !spec.log_level.is_empty() {
1003-
root.insert("logLevel".to_string(), serde_json::json!(spec.log_level));
1004-
}
1005-
if !spec.environment.is_empty() {
1006-
root.insert(
1007-
"environment".to_string(),
1008-
serde_json::json!(spec.environment),
1009-
);
1010-
}
1010+
let pod_env = spec_pod_env(Some(spec));
10111011
if let Some(template) = spec.template.as_ref() {
10121012
root.insert(
10131013
"podTemplate".to_string(),
1014-
sandbox_template_to_k8s(
1015-
template,
1016-
spec.gpu,
1017-
&spec.environment,
1018-
inject_workspace,
1019-
params,
1020-
),
1014+
sandbox_template_to_k8s(template, spec.gpu, &pod_env, inject_workspace, params),
10211015
);
10221016
if !template.agent_socket_path.is_empty() {
10231017
root.insert(
@@ -1044,14 +1038,13 @@ fn sandbox_to_k8s_spec(
10441038

10451039
// podTemplate is required by the Kubernetes CRD - ensure it's always present
10461040
if !root.contains_key("podTemplate") {
1047-
let empty_env = std::collections::HashMap::new();
1048-
let spec_env = spec.as_ref().map_or(&empty_env, |s| &s.environment);
1041+
let pod_env = spec_pod_env(spec);
10491042
root.insert(
10501043
"podTemplate".to_string(),
10511044
sandbox_template_to_k8s(
10521045
&SandboxTemplate::default(),
1053-
spec.as_ref().is_some_and(|s| s.gpu),
1054-
spec_env,
1046+
spec.is_some_and(|s| s.gpu),
1047+
&pod_env,
10551048
inject_workspace,
10561049
params,
10571050
),
@@ -2312,4 +2305,13 @@ mod tests {
23122305

23132306
assert_eq!(platform_config_bool(&template, "a_string"), None);
23142307
}
2308+
2309+
#[test]
2310+
fn log_level_propagates_as_env_var_to_sandbox_pod() {
2311+
let spec = SandboxSpec { log_level: "debug".to_string(), ..SandboxSpec::default() };
2312+
let cr = sandbox_to_k8s_spec(Some(&spec), &SandboxPodParams::default());
2313+
let env = cr["spec"]["podTemplate"]["spec"]["containers"][0]["env"].as_array().unwrap();
2314+
assert!(env.iter().any(|e| e["name"] == "OPENSHELL_LOG_LEVEL" && e["value"] == "debug"));
2315+
assert!(cr["spec"].get("logLevel").is_none());
2316+
}
23152317
}

0 commit comments

Comments
 (0)