Skip to content

Commit b948c94

Browse files
Add topologySpreadConstraints configuration to pod spec.
1 parent 8a1b2f4 commit b948c94

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

pkg/apis/acid.zalan.do/v1/postgresql_type.go

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ type PostgresSpec struct {
9191
// deprecated json tags
9292
InitContainersOld []v1.Container `json:"init_containers,omitempty"`
9393
PodPriorityClassNameOld string `json:"pod_priority_class_name,omitempty"`
94+
95+
TopologySpreadConstraints []v1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
9496
}
9597

9698
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

pkg/cluster/cluster.go

+5
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,11 @@ func (c *Cluster) compareStatefulSetWith(statefulSet *appsv1.StatefulSet) *compa
453453
needsRollUpdate = true
454454
reasons = append(reasons, "new statefulset's pod affinity does not match the current one")
455455
}
456+
if !reflect.DeepEqual(c.Statefulset.Spec.Template.Spec.TopologySpreadConstraints, statefulSet.Spec.Template.Spec.TopologySpreadConstraints) {
457+
needsReplace = true
458+
needsRollUpdate = true
459+
reasons = append(reasons, "new statefulset's pod topologySpreadConstraints does not match the current one")
460+
}
456461
if len(c.Statefulset.Spec.Template.Spec.Tolerations) != len(statefulSet.Spec.Template.Spec.Tolerations) {
457462
needsReplace = true
458463
needsRollUpdate = true

pkg/cluster/k8sres.go

+37
Original file line numberDiff line numberDiff line change
@@ -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+
581611
func 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

Comments
 (0)