@@ -578,6 +578,36 @@ func generatePodAntiAffinity(podAffinityTerm v1.PodAffinityTerm, preferredDuring
578
578
return podAntiAffinity
579
579
}
580
580
581
+ func generateTopologySpreadConstraints (labels labels.Set , topologySpreadConstraintObjs []* v1.TopologySpreadConstraint ) []v1.TopologySpreadConstraint {
582
+ var topologySpreadConstraints []v1.TopologySpreadConstraint
583
+ var nodeAffinityPolicy * v1.NodeInclusionPolicy
584
+ var nodeTaintsPolicy * v1.NodeInclusionPolicy
585
+ for _ , topologySpreadConstraintObj := range topologySpreadConstraintObjs {
586
+ if topologySpreadConstraintObj .NodeAffinityPolicy != nil {
587
+ nodeAffinityPolicy = (* v1 .NodeInclusionPolicy )(topologySpreadConstraintObj .NodeAffinityPolicy )
588
+ }
589
+ if topologySpreadConstraintObj .NodeTaintsPolicy != nil {
590
+ nodeTaintsPolicy = (* v1 .NodeInclusionPolicy )(topologySpreadConstraintObj .NodeTaintsPolicy )
591
+ }
592
+ topologySpreadConstraint := v1.TopologySpreadConstraint {
593
+ MaxSkew : topologySpreadConstraintObj .MaxSkew ,
594
+ TopologyKey : topologySpreadConstraintObj .TopologyKey ,
595
+ WhenUnsatisfiable : v1 .UnsatisfiableConstraintAction (topologySpreadConstraintObj .WhenUnsatisfiable ),
596
+ LabelSelector : & metav1.LabelSelector {
597
+ MatchLabels : labels ,
598
+ },
599
+ MinDomains : topologySpreadConstraintObj .MinDomains ,
600
+ NodeAffinityPolicy : nodeAffinityPolicy ,
601
+ NodeTaintsPolicy : nodeTaintsPolicy ,
602
+ MatchLabelKeys : topologySpreadConstraintObj .MatchLabelKeys ,
603
+ }
604
+ topologySpreadConstraints = append (topologySpreadConstraints , topologySpreadConstraint )
605
+ nodeAffinityPolicy = nil
606
+ nodeTaintsPolicy = nil
607
+ }
608
+ return topologySpreadConstraints
609
+ }
610
+
581
611
func tolerations (tolerationsSpec * []v1.Toleration , podToleration map [string ]string ) []v1.Toleration {
582
612
// allow to override tolerations by postgresql manifest
583
613
if len (* tolerationsSpec ) > 0 {
@@ -791,6 +821,7 @@ func (c *Cluster) generatePodTemplate(
791
821
podAntiAffinity bool ,
792
822
podAntiAffinityTopologyKey string ,
793
823
podAntiAffinityPreferredDuringScheduling bool ,
824
+ topologySpreadConstraints * []v1.TopologySpreadConstraint ,
794
825
additionalSecretMount string ,
795
826
additionalSecretMountPath string ,
796
827
additionalVolumes []acidv1.AdditionalVolume ,
@@ -846,6 +877,10 @@ func (c *Cluster) generatePodTemplate(
846
877
podSpec .PriorityClassName = priorityClassName
847
878
}
848
879
880
+ if len (topologySpreadConstraints ) > 0 {
881
+ podSpec .TopologySpreadConstraints = generateTopologySpreadConstraints (labels , topologySpreadConstraints )
882
+ }
883
+
849
884
if sharePgSocketWithSidecars != nil && * sharePgSocketWithSidecars {
850
885
addVarRunVolume (& podSpec )
851
886
}
@@ -1447,6 +1482,7 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
1447
1482
c .OpConfig .EnablePodAntiAffinity ,
1448
1483
c .OpConfig .PodAntiAffinityTopologyKey ,
1449
1484
c .OpConfig .PodAntiAffinityPreferredDuringScheduling ,
1485
+ spec .TopologySpreadConstraints ,
1450
1486
c .OpConfig .AdditionalSecretMount ,
1451
1487
c .OpConfig .AdditionalSecretMountPath ,
1452
1488
additionalVolumes )
@@ -2282,6 +2318,7 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1.CronJob, error) {
2282
2318
false ,
2283
2319
"" ,
2284
2320
false ,
2321
+ & []v1.TopologySpreadConstraint {},
2285
2322
c .OpConfig .AdditionalSecretMount ,
2286
2323
c .OpConfig .AdditionalSecretMountPath ,
2287
2324
[]acidv1.AdditionalVolume {}); err != nil {
0 commit comments