Skip to content

Commit 9bbcf75

Browse files
bors[bot]andlaz
andauthored
Merge #298
298: PodBuilder can now set pod affinity r=maltesander a=andlaz ## Description ## Review Checklist - [x] Code contains useful comments - [x] (Integration-)Test cases added (or not applicable) - [x] Documentation added (or not applicable) - [x] Changelog updated (or not applicable) Co-authored-by: andlaz <andras.szerdahelyi@gmail.com>
2 parents e58dab4 + 7ccf0e2 commit 9bbcf75

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

src/builder.rs

+44-9
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use k8s_openapi::api::core::v1::{
99
Affinity, ConfigMap, ConfigMapVolumeSource, Container, ContainerPort, DownwardAPIVolumeSource,
1010
EmptyDirVolumeSource, EnvVar, Event, EventSource, HostPathVolumeSource, Node, NodeAffinity,
1111
NodeSelector, NodeSelectorRequirement, NodeSelectorTerm, ObjectReference,
12-
PersistentVolumeClaimVolumeSource, Pod, PodCondition, PodSecurityContext, PodSpec, PodStatus,
13-
PodTemplateSpec, Probe, ProjectedVolumeSource, SELinuxOptions, SeccompProfile,
12+
PersistentVolumeClaimVolumeSource, Pod, PodAffinity, PodCondition, PodSecurityContext, PodSpec,
13+
PodStatus, PodTemplateSpec, Probe, ProjectedVolumeSource, SELinuxOptions, SeccompProfile,
1414
SecretVolumeSource, Sysctl, Toleration, Volume, VolumeMount, WindowsSecurityContextOptions,
1515
};
1616
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
@@ -914,6 +914,7 @@ pub struct PodBuilder {
914914
metadata: Option<ObjectMeta>,
915915
node_name: Option<String>,
916916
node_selector: Option<LabelSelector>,
917+
pod_affinity: Option<PodAffinity>,
917918
status: Option<PodStatus>,
918919
security_context: Option<PodSecurityContext>,
919920
tolerations: Option<Vec<Toleration>>,
@@ -960,6 +961,11 @@ impl PodBuilder {
960961
self
961962
}
962963

964+
pub fn pod_affinity(&mut self, affinity: PodAffinity) -> &mut Self {
965+
self.pod_affinity = Some(affinity);
966+
self
967+
}
968+
963969
pub fn node_selector(&mut self, node_selector: LabelSelector) -> &mut Self {
964970
self.node_selector = Some(node_selector);
965971
self
@@ -1033,6 +1039,7 @@ impl PodBuilder {
10331039
}) => (match_labels, match_expressions),
10341040
None => (None, None),
10351041
};
1042+
10361043
let node_affinity = node_label_exprs.map(|node_label_exprs| NodeAffinity {
10371044
required_during_scheduling_ignored_during_execution: Some(NodeSelector {
10381045
node_selector_terms: vec![NodeSelectorTerm {
@@ -1071,10 +1078,18 @@ impl PodBuilder {
10711078
init_containers: self.init_containers.clone(),
10721079
node_name: self.node_name.clone(),
10731080
node_selector: node_selector_labels,
1074-
affinity: node_affinity.map(|node_affinity| Affinity {
1075-
node_affinity: Some(node_affinity),
1076-
..Affinity::default()
1077-
}),
1081+
affinity: node_affinity
1082+
.map(|node_affinity| Affinity {
1083+
node_affinity: Some(node_affinity),
1084+
pod_affinity: self.pod_affinity.clone(),
1085+
..Affinity::default()
1086+
})
1087+
.or_else(|| {
1088+
Some(Affinity {
1089+
pod_affinity: self.pod_affinity.clone(),
1090+
..Affinity::default()
1091+
})
1092+
}),
10781093
security_context: self.security_context.clone(),
10791094
tolerations: self.tolerations.clone(),
10801095
volumes: self.volumes.clone(),
@@ -1334,11 +1349,13 @@ mod tests {
13341349
VolumeMountBuilder,
13351350
};
13361351
use k8s_openapi::api::core::v1::{
1337-
EnvVar, Pod, PodSecurityContext, SELinuxOptions, SeccompProfile, Sysctl, VolumeMount,
1338-
WindowsSecurityContextOptions,
1352+
EnvVar, Pod, PodAffinity, PodAffinityTerm, PodSecurityContext, SELinuxOptions,
1353+
SeccompProfile, Sysctl, VolumeMount, WindowsSecurityContextOptions,
13391354
};
13401355
use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
1341-
use k8s_openapi::apimachinery::pkg::apis::meta::v1::OwnerReference;
1356+
use k8s_openapi::apimachinery::pkg::apis::meta::v1::{
1357+
LabelSelector, LabelSelectorRequirement, OwnerReference,
1358+
};
13421359
use std::collections::BTreeMap;
13431360

13441361
#[test]
@@ -1605,7 +1622,24 @@ mod tests {
16051622
.args(vec!["12345".to_string()])
16061623
.build();
16071624

1625+
let pod_affinity = PodAffinity {
1626+
preferred_during_scheduling_ignored_during_execution: None,
1627+
required_during_scheduling_ignored_during_execution: Some(vec![PodAffinityTerm {
1628+
label_selector: Some(LabelSelector {
1629+
match_expressions: Some(vec![LabelSelectorRequirement {
1630+
key: "security".to_string(),
1631+
operator: "In".to_string(),
1632+
values: Some(vec!["S1".to_string()]),
1633+
}]),
1634+
match_labels: None,
1635+
}),
1636+
topology_key: "topology.kubernetes.io/zone".to_string(),
1637+
..Default::default()
1638+
}]),
1639+
};
1640+
16081641
let pod = PodBuilder::new()
1642+
.pod_affinity(pod_affinity.clone())
16091643
.metadata(ObjectMetaBuilder::new().name("testpod").build())
16101644
.add_container(container)
16111645
.add_init_container(init_container)
@@ -1620,6 +1654,7 @@ mod tests {
16201654

16211655
let pod_spec = pod.spec.unwrap();
16221656

1657+
assert_eq!(pod_spec.affinity.unwrap().pod_affinity, Some(pod_affinity));
16231658
assert_eq!(pod.metadata.name.unwrap(), "testpod");
16241659
assert_eq!(
16251660
pod_spec.node_name.as_ref().unwrap(),

0 commit comments

Comments
 (0)