Skip to content

Commit 819e410

Browse files
authored
refactor podAffinity generation (#2156)
1 parent d7e1fb5 commit 819e410

File tree

11 files changed

+145
-112
lines changed

11 files changed

+145
-112
lines changed

charts/postgres-operator/crds/operatorconfigurations.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,12 @@ spec:
278278
pdb_name_format:
279279
type: string
280280
default: "postgres-{cluster}-pdb"
281-
pod_antiaffinity_topology_key:
282-
type: string
283-
default: "kubernetes.io/hostname"
284281
pod_antiaffinity_preferred_during_scheduling:
285282
type: boolean
286283
default: false
284+
pod_antiaffinity_topology_key:
285+
type: string
286+
default: "kubernetes.io/hostname"
287287
pod_environment_configmap:
288288
type: string
289289
pod_environment_secret:

charts/postgres-operator/values.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ configKubernetes:
165165

166166
# defines the template for PDB (Pod Disruption Budget) names
167167
pdb_name_format: "postgres-{cluster}-pdb"
168+
# switches pod anti affinity type to `preferredDuringSchedulingIgnoredDuringExecution`
169+
pod_antiaffinity_preferred_during_scheduling: false
168170
# override topology key for pod anti affinity
169171
pod_antiaffinity_topology_key: "kubernetes.io/hostname"
170-
# switches pod anti affinity type to `preferredDuringSchedulingIgnoredDuringExecution`
171-
# pod_antiaffinity_preferred_during_scheduling: true
172172
# namespaced name of the ConfigMap with environment variables to populate on every pod
173173
# pod_environment_configmap: "default/my-custom-config"
174174
# name of the Secret (in cluster namespace) with environment variables to populate on every pod

manifests/configmap.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ data:
109109
# password_rotation_interval: "90"
110110
# password_rotation_user_retention: "180"
111111
pdb_name_format: "postgres-{cluster}-pdb"
112+
# pod_antiaffinity_preferred_during_scheduling: "false"
112113
# pod_antiaffinity_topology_key: "kubernetes.io/hostname"
113114
pod_deletion_wait_timeout: 10m
114115
# pod_environment_configmap: "default/my-custom-config"

manifests/operatorconfiguration.crd.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ spec:
276276
pdb_name_format:
277277
type: string
278278
default: "postgres-{cluster}-pdb"
279+
pod_antiaffinity_preferred_during_scheduling:
280+
type: boolean
281+
default: false
279282
pod_antiaffinity_topology_key:
280283
type: string
281284
default: "kubernetes.io/hostname"

manifests/postgresql-operator-default-configuration.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ configuration:
8484
# node_readiness_label_merge: "OR"
8585
oauth_token_secret_name: postgresql-operator
8686
pdb_name_format: "postgres-{cluster}-pdb"
87+
pod_antiaffinity_preferred_during_scheduling: false
8788
pod_antiaffinity_topology_key: "kubernetes.io/hostname"
8889
# pod_environment_configmap: "default/my-custom-config"
8990
# pod_environment_secret: "my-custom-secret"
@@ -95,6 +96,7 @@ configuration:
9596
# pod_service_account_role_binding_definition: ""
9697
pod_terminate_grace_period: 5m
9798
secret_name_template: "{username}.{cluster}.credentials.{tprkind}.{tprgroup}"
99+
share_pgsocket_with_sidecars: false
98100
spilo_allow_privilege_escalation: true
99101
# spilo_runasuser: 101
100102
# spilo_runasgroup: 103

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,12 +1372,12 @@ var OperatorConfigCRDResourceValidation = apiextv1.CustomResourceValidation{
13721372
"pdb_name_format": {
13731373
Type: "string",
13741374
},
1375-
"pod_antiaffinity_topology_key": {
1376-
Type: "string",
1377-
},
13781375
"pod_antiaffinity_preferred_during_scheduling": {
13791376
Type: "boolean",
13801377
},
1378+
"pod_antiaffinity_topology_key": {
1379+
Type: "string",
1380+
},
13811381
"pod_environment_configmap": {
13821382
Type: "string",
13831383
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ type KubernetesMetaConfiguration struct {
9797
PodPriorityClassName string `json:"pod_priority_class_name,omitempty"`
9898
MasterPodMoveTimeout Duration `json:"master_pod_move_timeout,omitempty"`
9999
EnablePodAntiAffinity bool `json:"enable_pod_antiaffinity,omitempty"`
100-
PodAntiAffinityTopologyKey string `json:"pod_antiaffinity_topology_key,omitempty"`
101100
PodAntiAffinityPreferredDuringScheduling bool `json:"pod_antiaffinity_preferred_during_scheduling,omitempty"`
101+
PodAntiAffinityTopologyKey string `json:"pod_antiaffinity_topology_key,omitempty"`
102102
PodManagementPolicy string `json:"pod_management_policy,omitempty"`
103-
EnableReadinessProbe bool `json:"enable_readiness_probe,omitempty"`
103+
EnableReadinessProbe bool `json:"enable_readiness_probe,omitempty"`
104104
EnableCrossNamespaceSecret bool `json:"enable_cross_namespace_secret,omitempty"`
105105
}
106106

pkg/cluster/connection_pooler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,12 @@ func (c *Cluster) generateConnectionPoolerPodTemplate(role PostgresRole) (
354354
nodeAffinity := c.nodeAffinity(c.OpConfig.NodeReadinessLabel, spec.NodeAffinity)
355355
if c.OpConfig.EnablePodAntiAffinity {
356356
labelsSet := labels.Set(c.connectionPoolerLabels(role, false).MatchLabels)
357-
podTemplate.Spec.Affinity = generatePodAffinity(
357+
podTemplate.Spec.Affinity = podAffinity(
358358
labelsSet,
359359
c.OpConfig.PodAntiAffinityTopologyKey,
360360
nodeAffinity,
361361
c.OpConfig.PodAntiAffinityPreferredDuringScheduling,
362+
true,
362363
)
363364
} else if nodeAffinity != nil {
364365
podTemplate.Spec.Affinity = nodeAffinity

pkg/cluster/k8sres.go

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

528557
func 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

Comments
 (0)