-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmod.rs
More file actions
166 lines (152 loc) · 4.8 KB
/
Copy pathmod.rs
File metadata and controls
166 lines (152 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use stackable_operator::{
k8s_openapi::api::{apps::v1::Deployment, core::v1::ResourceRequirements},
kube::{
ResourceExt,
api::{DynamicObject, GroupVersionKind},
},
};
use crate::data::containers;
/// Copies the resources of the container named "secret-operator-deployer" from `source`
/// to *all* containers in `target`.
/// Does nothing if there are no resources or if the `target` is not a DaemonSet or a Deployment.
/// This function allows OLM Subscription objects to configure the resources
/// of operator containers.
pub(super) fn maybe_copy_resources(
source: &Deployment,
target: &mut DynamicObject,
target_gvk: &GroupVersionKind,
) -> anyhow::Result<()> {
let target_kind_set = ["DaemonSet", "Deployment"];
if target_kind_set.contains(&target_gvk.kind.as_str()) {
if let Some(res) = deployment_resources(source) {
for container in containers(target)? {
match container {
serde_json::Value::Object(c) => {
c.insert("resources".to_string(), serde_json::json!(res));
}
_ => anyhow::bail!("no containers found in object {}", target.name_any()),
}
}
}
}
Ok(())
}
fn deployment_resources(deployment: &Deployment) -> Option<&ResourceRequirements> {
deployment
.spec
.as_ref()
.and_then(|ds| ds.template.spec.as_ref())
.map(|ts| ts.containers.iter())
.into_iter()
.flatten()
.rfind(|c| c.name == "secret-operator-deployer")
.and_then(|c| c.resources.as_ref())
}
#[cfg(test)]
mod test {
use std::sync::LazyLock;
use anyhow::Result;
use serde::Deserialize;
use stackable_operator::k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use super::*;
static DAEMONSET: LazyLock<DynamicObject> = LazyLock::new(|| {
const STR_DAEMONSET: &str = r#"
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: secret-operator-daemonset
spec:
template:
spec:
containers:
- name: secret-operator
image: "quay.io/stackable/secret-operator@sha256:bb5063aa67336465fd3fa80a7c6fd82ac6e30ebe3ffc6dba6ca84c1f1af95bfe"
env:
- name: NAME1
value: value1
resources:
limits:
cpu: 500m
memory: 2Mi
requests:
cpu: 200m
memory: 1Mi
"#;
let data =
serde_yaml::Value::deserialize(serde_yaml::Deserializer::from_str(STR_DAEMONSET))
.unwrap();
serde_yaml::from_value(data).unwrap()
});
static DEPLOYMENT: LazyLock<Deployment> = LazyLock::new(|| {
const STR_DEPLOYMENT: &str = r#"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: secret-operator-deployer
uid: d9287d0a-3069-47c3-8c90-b714dc6d1af5
spec:
template:
spec:
containers:
- name: secret-operator-deployer
image: "quay.io/stackable/tools@sha256:bb02df387d8f614089fe053373f766e21b7a9a1ad04cb3408059014cb0f1388e"
env:
- name: NAME2
value: value2
resources:
limits:
cpu: 1000m
memory: 1Gi
requests:
cpu: 100m
memory: 512Mi
tolerations:
- key: keep-out
value: "yes"
operator: Equal
effect: NoSchedule
"#;
let data =
serde_yaml::Value::deserialize(serde_yaml::Deserializer::from_str(STR_DEPLOYMENT))
.unwrap();
serde_yaml::from_value(data).unwrap()
});
#[test]
fn test_copy_env_var() -> Result<()> {
let gvk: GroupVersionKind = GroupVersionKind {
kind: "DaemonSet".to_string(),
version: "v1".to_string(),
group: "apps".to_string(),
};
let mut daemonset = DAEMONSET.clone();
maybe_copy_resources(&DEPLOYMENT, &mut daemonset, &gvk)?;
let expected = serde_json::json!(ResourceRequirements {
limits: Some(
[
("cpu".to_string(), Quantity("1000m".to_string())),
("memory".to_string(), Quantity("1Gi".to_string()))
]
.into()
),
requests: Some(
[
("cpu".to_string(), Quantity("100m".to_string())),
("memory".to_string(), Quantity("512Mi".to_string()))
]
.into()
),
..ResourceRequirements::default()
});
assert_eq!(
containers(&mut daemonset)?
.first()
.expect("daemonset has no containers")
.get("resources")
.unwrap(),
&expected
);
Ok(())
}
}