Skip to content

Commit ef110e3

Browse files
K8SPSMDB-227: Add topologySpreadConstraints (#1280)
* K8SPSMDB-227: Add `topologySpreadConstraints` https://jira.percona.com/browse/K8SPSMDB-227 * fix tests * fix defaults * fix `upgrade-consistency` test * update `cr.yaml` * Revert "fix tests" This reverts commit 6796ce7. * don't add topologySpreadConstraints by default * fix `TestSetSafeDefault` --------- Co-authored-by: Inel Pandzic <[email protected]>
1 parent 06ffb2d commit ef110e3

File tree

12 files changed

+1903
-17
lines changed

12 files changed

+1903
-17
lines changed

config/crd/bases/psmdb.percona.com_perconaservermongodbs.yaml

Lines changed: 364 additions & 0 deletions
Large diffs are not rendered by default.

deploy/bundle.yaml

Lines changed: 364 additions & 0 deletions
Large diffs are not rendered by default.

deploy/cr.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ spec:
4949
- name: rs0
5050
size: 3
5151
# terminationGracePeriodSeconds: 300
52+
# topologySpreadConstraints:
53+
# - labelSelector:
54+
# matchLabels:
55+
# app.kubernetes.io/name: percona-server-mongodb
56+
# maxSkew: 1
57+
# topologyKey: kubernetes.io/hostname
58+
# whenUnsatisfiable: DoNotSchedule
5259
# externalNodes:
5360
# - host: 34.124.76.90
5461
# - host: 34.124.76.91
@@ -310,6 +317,13 @@ spec:
310317
configsvrReplSet:
311318
size: 3
312319
# terminationGracePeriodSeconds: 300
320+
# topologySpreadConstraints:
321+
# - labelSelector:
322+
# matchLabels:
323+
# app.kubernetes.io/name: percona-server-mongodb
324+
# maxSkew: 1
325+
# topologyKey: kubernetes.io/hostname
326+
# whenUnsatisfiable: DoNotSchedule
313327
# externalNodes:
314328
# - host: 34.124.76.93
315329
# - host: 34.124.76.94
@@ -407,6 +421,13 @@ spec:
407421
mongos:
408422
size: 3
409423
# terminationGracePeriodSeconds: 300
424+
# topologySpreadConstraints:
425+
# - labelSelector:
426+
# matchLabels:
427+
# app.kubernetes.io/name: percona-server-mongodb
428+
# maxSkew: 1
429+
# topologyKey: kubernetes.io/hostname
430+
# whenUnsatisfiable: DoNotSchedule
410431
# # for more configuration fields refer to https://docs.mongodb.com/manual/reference/configuration-options/
411432
# configuration: |
412433
# systemLog:

deploy/crd.yaml

Lines changed: 364 additions & 0 deletions
Large diffs are not rendered by default.

deploy/cw-bundle.yaml

Lines changed: 364 additions & 0 deletions
Large diffs are not rendered by default.

e2e-tests/version-service/conf/crd.yaml

Lines changed: 364 additions & 0 deletions
Large diffs are not rendered by default.

pkg/apis/psmdb/v1/psmdb_defaults.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func (cr *PerconaServerMongoDB) CheckNSetDefaults(platform version.Platform, log
273273
cr.Spec.Sharding.Mongos.ReadinessProbe.FailureThreshold = 3
274274
}
275275

276-
cr.Spec.Sharding.Mongos.reconcileOpts()
276+
cr.Spec.Sharding.Mongos.reconcileOpts(cr)
277277

278278
if err := cr.Spec.Sharding.Mongos.Configuration.SetDefaults(); err != nil {
279279
return errors.Wrap(err, "failed to set configuration defaults")
@@ -558,10 +558,10 @@ func (rs *ReplsetSpec) SetDefaults(platform version.Platform, cr *PerconaServerM
558558
rs.Expose.ExposeType = corev1.ServiceTypeClusterIP
559559
}
560560

561-
rs.MultiAZ.reconcileOpts()
561+
rs.MultiAZ.reconcileOpts(cr)
562562

563563
if rs.Arbiter.Enabled {
564-
rs.Arbiter.MultiAZ.reconcileOpts()
564+
rs.Arbiter.MultiAZ.reconcileOpts(cr)
565565
}
566566

567567
if !cr.Spec.UnsafeConf && cr.DeletionTimestamp == nil {
@@ -694,7 +694,7 @@ func (nv *NonVotingSpec) SetDefaults(cr *PerconaServerMongoDB, rs *ReplsetSpec)
694694
nv.ServiceAccountName = WorkloadSA
695695
}
696696

697-
nv.MultiAZ.reconcileOpts()
697+
nv.MultiAZ.reconcileOpts(cr)
698698

699699
if nv.ContainerSecurityContext == nil {
700700
nv.ContainerSecurityContext = rs.ContainerSecurityContext
@@ -741,8 +741,9 @@ func (rs *ReplsetSpec) setSafeDefaults(log logr.Logger) {
741741
}
742742
}
743743

744-
func (m *MultiAZ) reconcileOpts() {
744+
func (m *MultiAZ) reconcileOpts(cr *PerconaServerMongoDB) {
745745
m.reconcileAffinityOpts()
746+
m.reconcileTopologySpreadConstraints(cr)
746747

747748
if m.PodDisruptionBudget == nil {
748749
defaultMaxUnavailable := intstr.FromInt(1)
@@ -786,6 +787,24 @@ func (m *MultiAZ) reconcileAffinityOpts() {
786787
}
787788
}
788789

790+
func (m *MultiAZ) reconcileTopologySpreadConstraints(cr *PerconaServerMongoDB) {
791+
if cr.CompareVersion("1.15.0") < 0 {
792+
return
793+
}
794+
795+
for i := range m.TopologySpreadConstraints {
796+
if m.TopologySpreadConstraints[i].MaxSkew == 0 {
797+
m.TopologySpreadConstraints[i].MaxSkew = 1
798+
}
799+
if m.TopologySpreadConstraints[i].TopologyKey == "" {
800+
m.TopologySpreadConstraints[i].TopologyKey = defaultAffinityTopologyKey
801+
}
802+
if m.TopologySpreadConstraints[i].WhenUnsatisfiable == "" {
803+
m.TopologySpreadConstraints[i].WhenUnsatisfiable = corev1.DoNotSchedule
804+
}
805+
}
806+
}
807+
789808
func (v *VolumeSpec) reconcileOpts() error {
790809
if v.EmptyDir == nil && v.HostPath == nil && v.PersistentVolumeClaim.PersistentVolumeClaimSpec == nil {
791810
v.PersistentVolumeClaim.PersistentVolumeClaimSpec = &corev1.PersistentVolumeClaimSpec{}

pkg/apis/psmdb/v1/psmdb_defaults_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,9 @@ func TestSetSafeDefault(t *testing.T) {
226226
cr := &api.PerconaServerMongoDB{
227227
ObjectMeta: metav1.ObjectMeta{Name: "psmdb-mock", Namespace: "psmdb"},
228228
Spec: api.PerconaServerMongoDBSpec{
229-
Replsets: []*api.ReplsetSpec{{Name: "rs0", Size: 3}, {Name: "rs1", Size: 3}},
230-
Sharding: api.Sharding{Enabled: true, Mongos: &api.MongosSpec{Size: 3}},
229+
CRVersion: version.Version,
230+
Replsets: []*api.ReplsetSpec{{Name: "rs0", Size: 3}, {Name: "rs1", Size: 3}},
231+
Sharding: api.Sharding{Enabled: true, Mongos: &api.MongosSpec{Size: 3}},
231232
},
232233
}
233234

pkg/apis/psmdb/v1/psmdb_types.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,16 +287,17 @@ func (spec *PMMSpec) ShouldUseAPIKeyAuth(secret *corev1.Secret) bool {
287287
}
288288

289289
type MultiAZ struct {
290-
Affinity *PodAffinity `json:"affinity,omitempty"`
291-
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
292-
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
293-
PriorityClassName string `json:"priorityClassName,omitempty"`
294-
ServiceAccountName string `json:"serviceAccountName,omitempty"`
295-
Annotations map[string]string `json:"annotations,omitempty"`
296-
Labels map[string]string `json:"labels,omitempty"`
297-
PodDisruptionBudget *PodDisruptionBudgetSpec `json:"podDisruptionBudget,omitempty"`
298-
TerminationGracePeriodSeconds *int64 `json:"terminationGracePeriodSeconds,omitempty"`
299-
RuntimeClassName *string `json:"runtimeClassName,omitempty"`
290+
Affinity *PodAffinity `json:"affinity,omitempty"`
291+
TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`
292+
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
293+
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
294+
PriorityClassName string `json:"priorityClassName,omitempty"`
295+
ServiceAccountName string `json:"serviceAccountName,omitempty"`
296+
Annotations map[string]string `json:"annotations,omitempty"`
297+
Labels map[string]string `json:"labels,omitempty"`
298+
PodDisruptionBudget *PodDisruptionBudgetSpec `json:"podDisruptionBudget,omitempty"`
299+
TerminationGracePeriodSeconds *int64 `json:"terminationGracePeriodSeconds,omitempty"`
300+
RuntimeClassName *string `json:"runtimeClassName,omitempty"`
300301

301302
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
302303

pkg/apis/psmdb/v1/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)