Skip to content

Commit

Permalink
Add topologySpreadConstraints configuration to pod spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
laiminhtrung1997 committed Feb 4, 2024
1 parent 8a1b2f4 commit b948c94
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/apis/acid.zalan.do/v1/postgresql_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ type PostgresSpec struct {
// deprecated json tags
InitContainersOld []v1.Container `json:"init_containers,omitempty"`
PodPriorityClassNameOld string `json:"pod_priority_class_name,omitempty"`

TopologySpreadConstraints []v1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
5 changes: 5 additions & 0 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,11 @@ func (c *Cluster) compareStatefulSetWith(statefulSet *appsv1.StatefulSet) *compa
needsRollUpdate = true
reasons = append(reasons, "new statefulset's pod affinity does not match the current one")
}
if !reflect.DeepEqual(c.Statefulset.Spec.Template.Spec.TopologySpreadConstraints, statefulSet.Spec.Template.Spec.TopologySpreadConstraints) {
needsReplace = true
needsRollUpdate = true
reasons = append(reasons, "new statefulset's pod topologySpreadConstraints does not match the current one")
}
if len(c.Statefulset.Spec.Template.Spec.Tolerations) != len(statefulSet.Spec.Template.Spec.Tolerations) {
needsReplace = true
needsRollUpdate = true
Expand Down
37 changes: 37 additions & 0 deletions pkg/cluster/k8sres.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,36 @@ func generatePodAntiAffinity(podAffinityTerm v1.PodAffinityTerm, preferredDuring
return podAntiAffinity
}

func generateTopologySpreadConstraints(labels labels.Set, topologySpreadConstraintObjs []*v1.TopologySpreadConstraint) []v1.TopologySpreadConstraint {
var topologySpreadConstraints []v1.TopologySpreadConstraint
var nodeAffinityPolicy *v1.NodeInclusionPolicy
var nodeTaintsPolicy *v1.NodeInclusionPolicy
for _, topologySpreadConstraintObj := range topologySpreadConstraintObjs {
if topologySpreadConstraintObj.NodeAffinityPolicy != nil {
nodeAffinityPolicy = (*v1.NodeInclusionPolicy)(topologySpreadConstraintObj.NodeAffinityPolicy)
}
if topologySpreadConstraintObj.NodeTaintsPolicy != nil {
nodeTaintsPolicy = (*v1.NodeInclusionPolicy)(topologySpreadConstraintObj.NodeTaintsPolicy)
}
topologySpreadConstraint := v1.TopologySpreadConstraint{
MaxSkew: topologySpreadConstraintObj.MaxSkew,
TopologyKey: topologySpreadConstraintObj.TopologyKey,
WhenUnsatisfiable: v1.UnsatisfiableConstraintAction(topologySpreadConstraintObj.WhenUnsatisfiable),
LabelSelector: &metav1.LabelSelector{
MatchLabels: labels,
},
MinDomains: topologySpreadConstraintObj.MinDomains,
NodeAffinityPolicy: nodeAffinityPolicy,
NodeTaintsPolicy: nodeTaintsPolicy,
MatchLabelKeys: topologySpreadConstraintObj.MatchLabelKeys,
}
topologySpreadConstraints = append(topologySpreadConstraints, topologySpreadConstraint)
nodeAffinityPolicy = nil
nodeTaintsPolicy = nil
}
return topologySpreadConstraints
}

func tolerations(tolerationsSpec *[]v1.Toleration, podToleration map[string]string) []v1.Toleration {
// allow to override tolerations by postgresql manifest
if len(*tolerationsSpec) > 0 {
Expand Down Expand Up @@ -791,6 +821,7 @@ func (c *Cluster) generatePodTemplate(
podAntiAffinity bool,
podAntiAffinityTopologyKey string,
podAntiAffinityPreferredDuringScheduling bool,
topologySpreadConstraints *[]v1.TopologySpreadConstraint,
additionalSecretMount string,
additionalSecretMountPath string,
additionalVolumes []acidv1.AdditionalVolume,
Expand Down Expand Up @@ -846,6 +877,10 @@ func (c *Cluster) generatePodTemplate(
podSpec.PriorityClassName = priorityClassName
}

if len(topologySpreadConstraints) > 0 {

Check failure on line 880 in pkg/cluster/k8sres.go

View workflow job for this annotation

GitHub Actions / End-2-End tests

invalid argument: topologySpreadConstraints (variable of type *[]"k8s.io/api/core/v1".TopologySpreadConstraint) for len

Check failure on line 880 in pkg/cluster/k8sres.go

View workflow job for this annotation

GitHub Actions / Unit tests and coverage

invalid argument: topologySpreadConstraints (variable of type *[]"k8s.io/api/core/v1".TopologySpreadConstraint) for len
podSpec.TopologySpreadConstraints = generateTopologySpreadConstraints(labels, topologySpreadConstraints)

Check failure on line 881 in pkg/cluster/k8sres.go

View workflow job for this annotation

GitHub Actions / End-2-End tests

cannot use topologySpreadConstraints (variable of type *[]"k8s.io/api/core/v1".TopologySpreadConstraint) as []*"k8s.io/api/core/v1".TopologySpreadConstraint value in argument to generateTopologySpreadConstraints

Check failure on line 881 in pkg/cluster/k8sres.go

View workflow job for this annotation

GitHub Actions / Unit tests and coverage

cannot use topologySpreadConstraints (variable of type *[]"k8s.io/api/core/v1".TopologySpreadConstraint) as []*"k8s.io/api/core/v1".TopologySpreadConstraint value in argument to generateTopologySpreadConstraints
}

if sharePgSocketWithSidecars != nil && *sharePgSocketWithSidecars {
addVarRunVolume(&podSpec)
}
Expand Down Expand Up @@ -1447,6 +1482,7 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*appsv1.Statef
c.OpConfig.EnablePodAntiAffinity,
c.OpConfig.PodAntiAffinityTopologyKey,
c.OpConfig.PodAntiAffinityPreferredDuringScheduling,
spec.TopologySpreadConstraints,

Check failure on line 1485 in pkg/cluster/k8sres.go

View workflow job for this annotation

GitHub Actions / End-2-End tests

cannot use spec.TopologySpreadConstraints (variable of type []"k8s.io/api/core/v1".TopologySpreadConstraint) as *[]"k8s.io/api/core/v1".TopologySpreadConstraint value in argument to c.generatePodTemplate

Check failure on line 1485 in pkg/cluster/k8sres.go

View workflow job for this annotation

GitHub Actions / Unit tests and coverage

cannot use spec.TopologySpreadConstraints (variable of type []"k8s.io/api/core/v1".TopologySpreadConstraint) as *[]"k8s.io/api/core/v1".TopologySpreadConstraint value in argument to c.generatePodTemplate
c.OpConfig.AdditionalSecretMount,
c.OpConfig.AdditionalSecretMountPath,
additionalVolumes)
Expand Down Expand Up @@ -2282,6 +2318,7 @@ func (c *Cluster) generateLogicalBackupJob() (*batchv1.CronJob, error) {
false,
"",
false,
&[]v1.TopologySpreadConstraint{},
c.OpConfig.AdditionalSecretMount,
c.OpConfig.AdditionalSecretMountPath,
[]acidv1.AdditionalVolume{}); err != nil {
Expand Down

0 comments on commit b948c94

Please sign in to comment.