@@ -578,6 +578,36 @@ func generatePodAntiAffinity(podAffinityTerm v1.PodAffinityTerm, preferredDuring
578578 return podAntiAffinity
579579}
580580
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+
581611func tolerations (tolerationsSpec * []v1.Toleration , podToleration map [string ]string ) []v1.Toleration {
582612 // allow to override tolerations by postgresql manifest
583613 if len (* tolerationsSpec ) > 0 {
@@ -791,6 +821,7 @@ func (c *Cluster) generatePodTemplate(
791821 podAntiAffinity bool ,
792822 podAntiAffinityTopologyKey string ,
793823 podAntiAffinityPreferredDuringScheduling bool ,
824+ topologySpreadConstraints * []v1.TopologySpreadConstraint ,
794825 additionalSecretMount string ,
795826 additionalSecretMountPath string ,
796827 additionalVolumes []acidv1.AdditionalVolume ,
@@ -846,6 +877,10 @@ func (c *Cluster) generatePodTemplate(
846877 podSpec .PriorityClassName = priorityClassName
847878 }
848879
880+ if len (topologySpreadConstraints ) > 0 {
881+ podSpec .TopologySpreadConstraints = generateTopologySpreadConstraints (labels , topologySpreadConstraints )
882+ }
883+
849884 if sharePgSocketWithSidecars != nil && * sharePgSocketWithSidecars {
850885 addVarRunVolume (& podSpec )
851886 }
@@ -1447,6 +1482,7 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
14471482 c .OpConfig .EnablePodAntiAffinity ,
14481483 c .OpConfig .PodAntiAffinityTopologyKey ,
14491484 c .OpConfig .PodAntiAffinityPreferredDuringScheduling ,
1485+ spec .TopologySpreadConstraints ,
14501486 c .OpConfig .AdditionalSecretMount ,
14511487 c .OpConfig .AdditionalSecretMountPath ,
14521488 additionalVolumes )
@@ -2282,6 +2318,7 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1.CronJob, error) {
22822318 false ,
22832319 "" ,
22842320 false ,
2321+ & []v1.TopologySpreadConstraint {},
22852322 c .OpConfig .AdditionalSecretMount ,
22862323 c .OpConfig .AdditionalSecretMountPath ,
22872324 []acidv1.AdditionalVolume {}); err != nil {
0 commit comments