@@ -495,8 +495,14 @@ func (c *Cluster) nodeAffinity(nodeReadinessLabel map[string]string, nodeAffinit
495495 }
496496}
497497
498- func generatePodAffinity (labels labels.Set , topologyKey string , nodeAffinity * v1.Affinity , preferredDuringScheduling bool ) * v1.Affinity {
499- // generate pod anti-affinity to avoid multiple pods of the same Postgres cluster in the same topology , e.g. node
498+ func podAffinity (
499+ labels labels.Set ,
500+ topologyKey string ,
501+ nodeAffinity * v1.Affinity ,
502+ preferredDuringScheduling bool ,
503+ anti bool ) * v1.Affinity {
504+
505+ var podAffinity v1.Affinity
500506
501507 podAffinityTerm := v1.PodAffinityTerm {
502508 LabelSelector : & metav1.LabelSelector {
@@ -505,24 +511,47 @@ func generatePodAffinity(labels labels.Set, topologyKey string, nodeAffinity *v1
505511 TopologyKey : topologyKey ,
506512 }
507513
508- podAffinity := v1.Affinity {
509- PodAntiAffinity : & v1.PodAntiAffinity {},
514+ if anti {
515+ podAffinity .PodAntiAffinity = generatePodAntiAffinity (podAffinityTerm , preferredDuringScheduling )
516+ } else {
517+ podAffinity .PodAffinity = generatePodAffinity (podAffinityTerm , preferredDuringScheduling )
510518 }
511519
520+ if nodeAffinity != nil && nodeAffinity .NodeAffinity != nil {
521+ podAffinity .NodeAffinity = nodeAffinity .NodeAffinity
522+ }
523+
524+ return & podAffinity
525+ }
526+
527+ func generatePodAffinity (podAffinityTerm v1.PodAffinityTerm , preferredDuringScheduling bool ) * v1.PodAffinity {
528+ podAffinity := & v1.PodAffinity {}
529+
512530 if preferredDuringScheduling {
513- podAffinity .PodAntiAffinity . PreferredDuringSchedulingIgnoredDuringExecution = []v1.WeightedPodAffinityTerm {{
531+ podAffinity .PreferredDuringSchedulingIgnoredDuringExecution = []v1.WeightedPodAffinityTerm {{
514532 Weight : 1 ,
515533 PodAffinityTerm : podAffinityTerm ,
516534 }}
517535 } else {
518- podAffinity .PodAntiAffinity . RequiredDuringSchedulingIgnoredDuringExecution = []v1.PodAffinityTerm {podAffinityTerm }
536+ podAffinity .RequiredDuringSchedulingIgnoredDuringExecution = []v1.PodAffinityTerm {podAffinityTerm }
519537 }
520538
521- if nodeAffinity != nil && nodeAffinity .NodeAffinity != nil {
522- podAffinity .NodeAffinity = nodeAffinity .NodeAffinity
539+ return podAffinity
540+ }
541+
542+ func generatePodAntiAffinity (podAffinityTerm v1.PodAffinityTerm , preferredDuringScheduling bool ) * v1.PodAntiAffinity {
543+ podAntiAffinity := & v1.PodAntiAffinity {}
544+
545+ if preferredDuringScheduling {
546+ podAntiAffinity .PreferredDuringSchedulingIgnoredDuringExecution = []v1.WeightedPodAffinityTerm {{
547+ Weight : 1 ,
548+ PodAffinityTerm : podAffinityTerm ,
549+ }}
550+ } else {
551+ podAntiAffinity .RequiredDuringSchedulingIgnoredDuringExecution = []v1.PodAffinityTerm {podAffinityTerm }
523552 }
524553
525- return & podAffinity
554+ return podAntiAffinity
526555}
527556
528557func tolerations (tolerationsSpec * []v1.Toleration , podToleration map [string ]string ) []v1.Toleration {
@@ -778,11 +807,12 @@ func (c *Cluster) generatePodTemplate(
778807 }
779808
780809 if podAntiAffinity {
781- podSpec .Affinity = generatePodAffinity (
810+ podSpec .Affinity = podAffinity (
782811 labels ,
783812 podAntiAffinityTopologyKey ,
784813 nodeAffinity ,
785814 podAntiAffinityPreferredDuringScheduling ,
815+ true ,
786816 )
787817 } else if nodeAffinity != nil {
788818 podSpec .Affinity = nodeAffinity
@@ -2100,20 +2130,15 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1.CronJob, error) {
21002130 c .OpConfig .ClusterNameLabel : c .Name ,
21012131 "application" : "spilo-logical-backup" ,
21022132 }
2103- podAffinityTerm := v1.PodAffinityTerm {
2104- LabelSelector : & metav1.LabelSelector {
2105- MatchLabels : labels ,
2106- },
2107- TopologyKey : "kubernetes.io/hostname" ,
2108- }
2109- podAffinity := v1.Affinity {
2110- PodAffinity : & v1.PodAffinity {
2111- PreferredDuringSchedulingIgnoredDuringExecution : []v1.WeightedPodAffinityTerm {{
2112- Weight : 1 ,
2113- PodAffinityTerm : podAffinityTerm ,
2114- },
2115- },
2116- }}
2133+
2134+ nodeAffinity := c .nodeAffinity (c .OpConfig .NodeReadinessLabel , nil )
2135+ podAffinity := podAffinity (
2136+ labels ,
2137+ "kubernetes.io/hostname" ,
2138+ nodeAffinity ,
2139+ true ,
2140+ false ,
2141+ )
21172142
21182143 annotations := c .generatePodAnnotations (& c .Spec )
21192144
@@ -2147,7 +2172,7 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1.CronJob, error) {
21472172 }
21482173
21492174 // overwrite specific params of logical backups pods
2150- podTemplate .Spec .Affinity = & podAffinity
2175+ podTemplate .Spec .Affinity = podAffinity
21512176 podTemplate .Spec .RestartPolicy = "Never" // affects containers within a pod
21522177
21532178 // configure a batch job
0 commit comments