Skip to content

Commit 24ec913

Browse files
committed
Add annotation to disable reconciliation
The new `mongodb.com/v1.disable-reconciliation` annotation allows disabling reconciliation for `MongoDBMultiCluster` objects. This will be used later when migrating multi-cluster replica sets from `MongoDBMultiCluster` to `MongoDB`.
1 parent 749c1a3 commit 24ec913

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

controllers/operator/mongodbmultireplicaset_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ func (r *ReconcileMongoDbMultiReplicaSet) Reconcile(ctx context.Context, request
135135
return reconcileResult, err
136136
}
137137

138+
if val, ok := mrs.Annotations[util.DisableReconciliation]; ok && val == util.DisableReconciliationValue {
139+
log.Info("Reconciliation disabled")
140+
return r.updateStatus(ctx, &mrs, workflow.Disabled(), log)
141+
}
142+
138143
if !architectures.IsRunningStaticArchitecture(mrs.Annotations) {
139144
agents.UpgradeAllIfNeeded(ctx, agents.ClientSecret{Client: r.client, SecretClient: r.SecretClient}, r.omConnectionFactory, GetWatchedNamespace(), true)
140145
}

controllers/operator/mongodbmultireplicaset_controller_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"maps"
89
"sort"
910
"testing"
1011

@@ -1425,6 +1426,75 @@ func TestValidationsRunOnReconcile(t *testing.T) {
14251426
assert.Equal(t, fmt.Sprintf("Multiple clusters with the same name (%s) are not allowed", duplicateName), mrs.Status.Message)
14261427
}
14271428

1429+
func TestReconcileDisableReconciliationAnnotation(t *testing.T) {
1430+
tests := []struct {
1431+
name string
1432+
annotations map[string]string
1433+
expectedPhase status.Phase
1434+
expectedReconcileResult reconcile.Result
1435+
}{
1436+
{
1437+
name: "reconciliation disabled when annotation is true",
1438+
annotations: map[string]string{util.DisableReconciliation: util.DisableReconciliationValue},
1439+
expectedPhase: status.PhaseDisabled,
1440+
expectedReconcileResult: reconcile.Result{Requeue: false, RequeueAfter: 0},
1441+
},
1442+
{
1443+
name: "reconciliation proceeds when annotation is false",
1444+
annotations: map[string]string{util.DisableReconciliation: "false"},
1445+
expectedPhase: status.PhaseRunning,
1446+
expectedReconcileResult: reconcile.Result{RequeueAfter: util.TWENTY_FOUR_HOURS},
1447+
},
1448+
{
1449+
name: "reconciliation proceeds when annotation is empty string",
1450+
annotations: map[string]string{util.DisableReconciliation: ""},
1451+
expectedPhase: status.PhaseRunning,
1452+
expectedReconcileResult: reconcile.Result{RequeueAfter: util.TWENTY_FOUR_HOURS},
1453+
},
1454+
{
1455+
name: "reconciliation proceeds when annotation is arbitrary value",
1456+
annotations: map[string]string{util.DisableReconciliation: "some-other-value"},
1457+
expectedPhase: status.PhaseRunning,
1458+
expectedReconcileResult: reconcile.Result{RequeueAfter: util.TWENTY_FOUR_HOURS},
1459+
},
1460+
{
1461+
name: "reconciliation proceeds when annotation is absent",
1462+
expectedPhase: status.PhaseRunning,
1463+
expectedReconcileResult: reconcile.Result{RequeueAfter: util.TWENTY_FOUR_HOURS},
1464+
},
1465+
}
1466+
1467+
for _, tt := range tests {
1468+
t.Run(tt.name, func(t *testing.T) {
1469+
ctx := context.Background()
1470+
mrs := mdbmulti.DefaultMultiReplicaSetBuilder().SetClusterSpecList(clusters).Build()
1471+
1472+
// Set up test annotations
1473+
if mrs.Annotations == nil {
1474+
mrs.Annotations = map[string]string{}
1475+
}
1476+
maps.Copy(mrs.Annotations, tt.annotations)
1477+
1478+
reconciler, client, _, _ := defaultMultiReplicaSetReconciler(ctx, nil, "", "", mrs)
1479+
1480+
// Update the resource with the annotation
1481+
err := client.Update(ctx, mrs)
1482+
assert.NoError(t, err)
1483+
1484+
// Reconcile should return without error and with expected result
1485+
result, err := reconciler.Reconcile(ctx, requestFromObject(mrs))
1486+
assert.NoError(t, err)
1487+
assert.Equal(t, tt.expectedReconcileResult, result)
1488+
1489+
// Fetch the updated resource to check the status
1490+
err = client.Get(ctx, kube.ObjectKey(mrs.Namespace, mrs.Name), mrs)
1491+
assert.NoError(t, err)
1492+
1493+
assert.Equal(t, tt.expectedPhase, mrs.Status.Phase)
1494+
})
1495+
}
1496+
}
1497+
14281498
func assertClusterpresent(t *testing.T, m map[string]int, specs mdb.ClusterSpecList, arr []int) {
14291499
tmp := make([]int, 0)
14301500
for _, s := range specs {

pkg/util/constants.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ const (
285285
LastAchievedSpec = "mongodb.com/v1.lastSuccessfulConfiguration"
286286
LastAchievedRsMemberIds = "mongodb.com/v1.lastAchievedRsMemberIds"
287287

288+
DisableReconciliation = "mongodb.com/v1.disableReconciliation"
289+
DisableReconciliationValue = "true"
290+
288291
// SecretVolumeName is the name of the volume resource.
289292
SecretVolumeName = "secret-certs"
290293

0 commit comments

Comments
 (0)