@@ -9,8 +9,8 @@ use k8s_openapi::api::core::v1::{
9
9
Affinity , ConfigMap , ConfigMapVolumeSource , Container , ContainerPort , DownwardAPIVolumeSource ,
10
10
EmptyDirVolumeSource , EnvVar , Event , EventSource , HostPathVolumeSource , Node , NodeAffinity ,
11
11
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 ,
14
14
SecretVolumeSource , Sysctl , Toleration , Volume , VolumeMount , WindowsSecurityContextOptions ,
15
15
} ;
16
16
use k8s_openapi:: apimachinery:: pkg:: api:: resource:: Quantity ;
@@ -914,6 +914,7 @@ pub struct PodBuilder {
914
914
metadata : Option < ObjectMeta > ,
915
915
node_name : Option < String > ,
916
916
node_selector : Option < LabelSelector > ,
917
+ pod_affinity : Option < PodAffinity > ,
917
918
status : Option < PodStatus > ,
918
919
security_context : Option < PodSecurityContext > ,
919
920
tolerations : Option < Vec < Toleration > > ,
@@ -960,6 +961,11 @@ impl PodBuilder {
960
961
self
961
962
}
962
963
964
+ pub fn pod_affinity ( & mut self , affinity : PodAffinity ) -> & mut Self {
965
+ self . pod_affinity = Some ( affinity) ;
966
+ self
967
+ }
968
+
963
969
pub fn node_selector ( & mut self , node_selector : LabelSelector ) -> & mut Self {
964
970
self . node_selector = Some ( node_selector) ;
965
971
self
@@ -1033,6 +1039,7 @@ impl PodBuilder {
1033
1039
} ) => ( match_labels, match_expressions) ,
1034
1040
None => ( None , None ) ,
1035
1041
} ;
1042
+
1036
1043
let node_affinity = node_label_exprs. map ( |node_label_exprs| NodeAffinity {
1037
1044
required_during_scheduling_ignored_during_execution : Some ( NodeSelector {
1038
1045
node_selector_terms : vec ! [ NodeSelectorTerm {
@@ -1071,10 +1078,18 @@ impl PodBuilder {
1071
1078
init_containers : self . init_containers . clone ( ) ,
1072
1079
node_name : self . node_name . clone ( ) ,
1073
1080
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
+ } ) ,
1078
1093
security_context : self . security_context . clone ( ) ,
1079
1094
tolerations : self . tolerations . clone ( ) ,
1080
1095
volumes : self . volumes . clone ( ) ,
@@ -1334,11 +1349,13 @@ mod tests {
1334
1349
VolumeMountBuilder ,
1335
1350
} ;
1336
1351
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 ,
1339
1354
} ;
1340
1355
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
+ } ;
1342
1359
use std:: collections:: BTreeMap ;
1343
1360
1344
1361
#[ test]
@@ -1605,7 +1622,24 @@ mod tests {
1605
1622
. args ( vec ! [ "12345" . to_string( ) ] )
1606
1623
. build ( ) ;
1607
1624
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
+
1608
1641
let pod = PodBuilder :: new ( )
1642
+ . pod_affinity ( pod_affinity. clone ( ) )
1609
1643
. metadata ( ObjectMetaBuilder :: new ( ) . name ( "testpod" ) . build ( ) )
1610
1644
. add_container ( container)
1611
1645
. add_init_container ( init_container)
@@ -1620,6 +1654,7 @@ mod tests {
1620
1654
1621
1655
let pod_spec = pod. spec . unwrap ( ) ;
1622
1656
1657
+ assert_eq ! ( pod_spec. affinity. unwrap( ) . pod_affinity, Some ( pod_affinity) ) ;
1623
1658
assert_eq ! ( pod. metadata. name. unwrap( ) , "testpod" ) ;
1624
1659
assert_eq ! (
1625
1660
pod_spec. node_name. as_ref( ) . unwrap( ) ,
0 commit comments