-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmod.rs
More file actions
125 lines (110 loc) · 3.68 KB
/
Copy pathmod.rs
File metadata and controls
125 lines (110 loc) · 3.68 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
use stackable_operator::{
k8s_openapi::api::{apps::v1::Deployment, core::v1::Toleration},
kube::api::{DynamicObject, GroupVersionKind},
};
use crate::data::get_or_create;
/// Copies the pod tolerations from the `source` to the `target`.
/// Does nothing if there are no tolerations or if the `target` is not
/// a DaemonSet or a Deployment.
/// Admins can configure Subscription tolerations when installing the operator
/// and these need to be copied over to the objects created by the deployer.
pub(super) fn maybe_copy_tolerations(
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(tolerations) = deployment_tolerations(source) {
let path = "template/spec/tolerations".split("/");
*get_or_create(target.data.pointer_mut("/spec").unwrap(), path)? = serde_json::json!(
tolerations
.iter()
.map(|t| serde_json::json!(t))
.collect::<Vec<serde_json::Value>>()
);
}
}
Ok(())
}
fn deployment_tolerations(deployment: &Deployment) -> Option<&Vec<Toleration>> {
deployment
.spec
.as_ref()
.and_then(|s| s.template.spec.as_ref())
.and_then(|ps| ps.tolerations.as_ref())
}
#[cfg(test)]
mod test {
use std::sync::LazyLock;
use anyhow::Result;
use serde::Deserialize;
use super::*;
use crate::tolerations::{deployment_tolerations, maybe_copy_tolerations};
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"
"#;
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"
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_tolerations() -> Result<()> {
let gvk: GroupVersionKind = GroupVersionKind {
kind: "DaemonSet".to_string(),
version: "v1".to_string(),
group: "apps".to_string(),
};
let mut daemonset = DAEMONSET.clone();
maybe_copy_tolerations(&DEPLOYMENT, &mut daemonset, &gvk)?;
let expected = serde_json::json!(
deployment_tolerations(&DEPLOYMENT)
.unwrap()
.iter()
.map(|t| serde_json::json!(t))
.collect::<Vec<serde_json::Value>>()
);
assert_eq!(
daemonset.data.pointer("/spec/template/spec/tolerations"),
Some(&expected)
);
Ok(())
}
}