Skip to content

Commit 0fe91d1

Browse files
committed
remove hard coded container names
1 parent b7ce10f commit 0fe91d1

File tree

3 files changed

+51
-57
lines changed

3 files changed

+51
-57
lines changed

rust/olm-deployer/src/data.rs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
11
use anyhow::{Context, anyhow};
22
use stackable_operator::kube::{ResourceExt, api::DynamicObject};
33

4-
pub fn container<'a>(
4+
pub fn containers<'a>(
55
target: &'a mut DynamicObject,
6-
container_name: &str,
7-
) -> anyhow::Result<&'a mut serde_json::Value> {
6+
) -> anyhow::Result<&'a mut Vec<serde_json::Value>> {
87
let tname = target.name_any();
98
let path = "template/spec/containers".split("/");
10-
match get_or_create(
11-
target
12-
.data
13-
.pointer_mut("/spec")
14-
.context(anyhow!("object [{tname}] has no .spec property"))?,
15-
path,
16-
)? {
17-
serde_json::Value::Array(containers) => {
18-
for c in containers {
19-
if c.is_object() {
20-
if let Some(serde_json::Value::String(name)) = c.get("name") {
21-
if container_name == name {
22-
return Ok(c);
23-
}
24-
}
25-
} else {
26-
anyhow::bail!("container is not a object: {:?}", c);
27-
}
28-
}
29-
anyhow::bail!("container named {container_name} not found");
30-
}
9+
match get_or_create(target.data.pointer_mut("/spec").unwrap(), path)? {
10+
serde_json::Value::Array(containers) => Ok(containers),
3111
_ => anyhow::bail!("no containers found in object {tname}"),
3212
}
3313
}

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 "listener-operator-deployer" container in `source`
12-
/// to the container "listener-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, "listener-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
}
@@ -151,7 +156,9 @@ spec:
151156
},
152157
]);
153158
assert_eq!(
154-
container(&mut daemonset, "listener-operator")?
159+
containers(&mut daemonset)?
160+
.first()
161+
.expect("daemonset has no containers")
155162
.get("env")
156163
.unwrap(),
157164
&expected

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,28 @@ use stackable_operator::{
66
},
77
};
88

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

1111
/// Copies the resources of the container named "listener-operator-deployer" from `source`
12-
/// to the container "listener-operator" in `target`.
13-
/// Does nothing if there are no resources or if the `target` is not a DaemonSet.
12+
/// to *all* containers in `target`.
13+
/// Does nothing if there are no resources or if the `target` is not a DaemonSet or a Deployment.
14+
/// This function allows OLM Subscription objects to configure the resources
15+
/// of operator containers.
1416
pub(super) fn maybe_copy_resources(
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(res) = deployment_resources(source) {
21-
match container(target, "listener-operator")? {
22-
serde_json::Value::Object(c) => {
23-
c.insert("resources".to_string(), serde_json::json!(res));
24+
for container in containers(target)? {
25+
match container {
26+
serde_json::Value::Object(c) => {
27+
c.insert("resources".to_string(), serde_json::json!(res));
28+
}
29+
_ => anyhow::bail!("no containers found in object {}", target.name_any()),
2430
}
25-
_ => anyhow::bail!("no containers found in object {}", target.name_any()),
2631
}
2732
}
2833
}
@@ -152,7 +157,9 @@ spec:
152157
..ResourceRequirements::default()
153158
});
154159
assert_eq!(
155-
container(&mut daemonset, "listener-operator")?
160+
containers(&mut daemonset)?
161+
.first()
162+
.expect("daemonset has no containers")
156163
.get("resources")
157164
.unwrap(),
158165
&expected

0 commit comments

Comments
 (0)