Skip to content

Commit 8564630

Browse files
authored
Add validation to ensure consistent replicas (#1263)
Signed-off-by: hoyhbx <[email protected]>
1 parent f12c953 commit 8564630

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

controllers/replicaset_controller_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,19 @@ func TestCustomDataDir_Configuration(t *testing.T) {
11841184
}
11851185
}
11861186

1187+
func TestInconsistentReplicas(t *testing.T) {
1188+
mdb := newTestReplicaSet()
1189+
stsReplicas := new(int32)
1190+
*stsReplicas = 3
1191+
mdb.Spec.StatefulSetConfiguration.SpecWrapper.Spec.Replicas = stsReplicas
1192+
mdb.Spec.Members = 4
1193+
1194+
mgr := client.NewManager(&mdb)
1195+
r := NewReconciler(mgr)
1196+
_, err := r.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Namespace: mdb.Namespace, Name: mdb.Name}})
1197+
assert.NoError(t, err)
1198+
}
1199+
11871200
func assertVolumeMountPath(t *testing.T, mounts []corev1.VolumeMount, name, path string) {
11881201
for _, v := range mounts {
11891202
if v.Name == name {

controllers/validation/validation.go

+14
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func validateSpec(mdb mdbv1.MongoDBCommunity, log *zap.SugaredLogger) error {
3737
return err
3838
}
3939

40+
if err := validateStatefulSet(mdb); err != nil {
41+
return err
42+
}
43+
4044
return nil
4145
}
4246

@@ -125,3 +129,13 @@ func validateAuthModeSpec(mdb mdbv1.MongoDBCommunity, log *zap.SugaredLogger) er
125129

126130
return nil
127131
}
132+
133+
func validateStatefulSet(mdb mdbv1.MongoDBCommunity) error {
134+
stsReplicas := mdb.Spec.StatefulSetConfiguration.SpecWrapper.Spec.Replicas
135+
136+
if stsReplicas != nil && *stsReplicas != int32(mdb.Spec.Members) {
137+
return fmt.Errorf("spec.statefulset.spec.replicas has to be equal to spec.members")
138+
}
139+
140+
return nil
141+
}

0 commit comments

Comments
 (0)