-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathaffinity.rs
More file actions
120 lines (107 loc) · 4.23 KB
/
affinity.rs
File metadata and controls
120 lines (107 loc) · 4.23 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
use stackable_operator::{
commons::affinity::{StackableAffinityFragment, affinity_between_role_pods},
k8s_openapi::api::core::v1::PodAntiAffinity,
};
use crate::crd::{APP_NAME, ZookeeperRole};
pub fn get_affinity(cluster_name: &str, role: &ZookeeperRole) -> StackableAffinityFragment {
let affinity_between_role_pods =
affinity_between_role_pods(APP_NAME, cluster_name, &role.to_string(), 70);
StackableAffinityFragment {
pod_affinity: None,
pod_anti_affinity: Some(PodAntiAffinity {
preferred_during_scheduling_ignored_during_execution: Some(vec![
affinity_between_role_pods,
]),
required_during_scheduling_ignored_during_execution: None,
}),
node_affinity: None,
node_selector: None,
}
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use stackable_operator::{
commons::affinity::StackableAffinity,
k8s_openapi::{
api::core::v1::{PodAffinityTerm, PodAntiAffinity, WeightedPodAffinityTerm},
apimachinery::pkg::apis::meta::v1::LabelSelector,
},
kube::runtime::reflector::ObjectRef,
role_utils::RoleGroupRef,
};
use crate::crd::{affinity::ZookeeperRole, v1alpha1};
#[test]
fn test_affinity_defaults() {
let input = r#"
apiVersion: zookeeper.stackable.tech/v1alpha1
kind: ZookeeperCluster
metadata:
name: simple-zk
spec:
image:
productVersion: 3.9.4
clusterConfig:
authentication:
- authenticationClass: zk-client-tls
tls:
serverSecretClass: tls
quorumSecretClass: tls
servers:
roleGroups:
default:
replicas: 3
"#;
let zk: v1alpha1::ZookeeperCluster =
serde_yaml::from_str(input).expect("illegal test input");
let rolegroup_ref = RoleGroupRef {
cluster: ObjectRef::from_obj(&zk),
role: ZookeeperRole::Server.to_string(),
role_group: "default".to_string(),
};
let expected: StackableAffinity = StackableAffinity {
pod_affinity: None,
pod_anti_affinity: Some(PodAntiAffinity {
required_during_scheduling_ignored_during_execution: None,
preferred_during_scheduling_ignored_during_execution: Some(vec![
WeightedPodAffinityTerm {
pod_affinity_term: PodAffinityTerm {
label_selector: Some(LabelSelector {
match_expressions: None,
match_labels: Some(BTreeMap::from([
(
"app.kubernetes.io/name".to_string(),
"zookeeper".to_string(),
),
(
"app.kubernetes.io/instance".to_string(),
"simple-zk".to_string(),
),
(
"app.kubernetes.io/component".to_string(),
"server".to_string(),
),
])),
}),
namespace_selector: None,
namespaces: None,
topology_key: "kubernetes.io/hostname".to_string(),
// NOTE (@Techassi): Both these fields were added in
// Kubernetes 1.30, and cannot be used for now.
match_label_keys: None,
mismatch_label_keys: None,
},
weight: 70,
},
]),
}),
node_affinity: None,
node_selector: None,
};
let affinity = zk
.merged_config(&ZookeeperRole::Server, &rolegroup_ref)
.unwrap()
.affinity;
assert_eq!(affinity, expected);
}
}