Skip to content

Commit 1fd80ae

Browse files
committed
copy subscription env to op deployment too
1 parent 02e791c commit 1fd80ae

2 files changed

Lines changed: 42 additions & 24 deletions

File tree

rust/olm-deployer/src/data.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ pub fn container<'a>(
3636
}
3737
}
3838

39+
pub fn containers<'a>(
40+
target: &'a mut DynamicObject,
41+
) -> anyhow::Result<&'a mut Vec<serde_json::Value>> {
42+
let tname = target.name_any();
43+
let path = "template/spec/containers".split("/");
44+
match get_or_create(target.data.pointer_mut("/spec").unwrap(), path)? {
45+
serde_json::Value::Array(containers) => Ok(containers),
46+
_ => anyhow::bail!("no containers found in object {tname}"),
47+
}
48+
}
49+
3950
/// Returns the object nested in `root` by traversing the `path` of nested keys.
4051
/// Creates any missing objects in path.
4152
/// In case of success, the returned value is either the existing object or

rust/olm-deployer/src/env/mod.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,46 @@ use stackable_operator::{
66
},
77
};
88

9-
use crate::data::container;
9+
use crate::data::containers;
1010

1111
/// Copy the environment from the "secret-operator-deployer" container in `source`
12-
/// to the container "secret-operator" in `target`.
13-
/// The `target` must be a DaemonSet object, otherwise this is a no-op.
12+
/// to *all* containers in target.
13+
/// The target must be a DaemonSet or Deployment, otherwise this function is a no-op.
14+
/// This function allows OLM Subscription objects to configure the environment
15+
/// of operator containers.
1416
pub(super) fn maybe_copy_env(
1517
source: &Deployment,
1618
target: &mut DynamicObject,
1719
target_gvk: &GroupVersionKind,
1820
) -> anyhow::Result<()> {
19-
if target_gvk.kind == "DaemonSet" {
21+
let target_kind_set = ["DaemonSet", "Deployment"];
22+
if target_kind_set.contains(&target_gvk.kind.as_str()) {
2023
if let Some(env) = deployer_env_var(source) {
21-
match container(target, "secret-operator")? {
22-
serde_json::Value::Object(c) => {
23-
let json_env = env
24-
.iter()
25-
.map(|e| serde_json::json!(e))
26-
.collect::<Vec<serde_json::Value>>();
27-
28-
match c.get_mut("env") {
29-
Some(env) => match env {
30-
v @ serde_json::Value::Null => {
31-
*v = serde_json::json!(json_env);
24+
for container in containers(target)? {
25+
match container {
26+
serde_json::Value::Object(c) => {
27+
let json_env = env
28+
.iter()
29+
.map(|e| serde_json::json!(e))
30+
.collect::<Vec<serde_json::Value>>();
31+
32+
match c.get_mut("env") {
33+
Some(env) => match env {
34+
v @ serde_json::Value::Null => {
35+
*v = serde_json::json!(json_env);
36+
}
37+
serde_json::Value::Array(container_env) => {
38+
container_env.extend_from_slice(&json_env)
39+
}
40+
_ => anyhow::bail!("env is not null or an array"),
41+
},
42+
None => {
43+
c.insert("env".to_string(), serde_json::json!(json_env));
3244
}
33-
serde_json::Value::Array(container_env) => {
34-
container_env.extend_from_slice(&json_env)
35-
}
36-
_ => anyhow::bail!("env is not null or an array"),
37-
},
38-
None => {
39-
c.insert("env".to_string(), serde_json::json!(json_env));
4045
}
4146
}
47+
_ => anyhow::bail!("no containers found in object {}", target.name_any()),
4248
}
43-
_ => anyhow::bail!("no containers found in object {}", target.name_any()),
4449
}
4550
}
4651
}
@@ -149,7 +154,9 @@ spec:
149154
},
150155
]);
151156
assert_eq!(
152-
container(&mut daemonset, "secret-operator")?
157+
containers(&mut daemonset)?
158+
.first()
159+
.expect("daemonset has no containers")
153160
.get("env")
154161
.unwrap(),
155162
&expected

0 commit comments

Comments
 (0)