Skip to content

Commit 9fffe34

Browse files
authored
support skip reconciliation annotation on state machine reconciler (#2903)
1 parent ac11e93 commit 9fffe34

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

pkg/controller/state/reconciler.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"sigs.k8s.io/controller-runtime/pkg/log"
3535
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3636

37+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/customresource"
3738
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/generated/translate"
3839
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/finalizer"
3940
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/state"
@@ -103,13 +104,6 @@ func NewStateReconciler[T any](target StateHandler[T], options ...ReconcilerOpti
103104
return r
104105
}
105106

106-
func NewUnstructuredStateReconciler(target UnstructuredStateReconciler, gvk schema.GroupVersionKind) *Reconciler[unstructured.Unstructured] {
107-
return &Reconciler[unstructured.Unstructured]{
108-
reconciler: target,
109-
unstructuredGVK: gvk,
110-
}
111-
}
112-
113107
func (r *Reconciler[T]) SetupWithManager(mgr ctrl.Manager, defaultOptions controller.Options) error {
114108
r.cluster = mgr
115109
return r.reconciler.SetupWithManager(mgr, r, defaultOptions)
@@ -142,6 +136,17 @@ func (r *Reconciler[T]) Reconcile(ctx context.Context, req ctrl.Request) (reconc
142136
currentStatus := newStatusObject(obj)
143137
currentState := state.GetState(currentStatus.Status.Conditions)
144138

139+
if customresource.ReconciliationShouldBeSkipped(clientObj) {
140+
logger.Info(fmt.Sprintf("Skipping reconciliation by annotation %s=%s", customresource.ReconciliationPolicyAnnotation, customresource.ReconciliationPolicySkip))
141+
if currentState == state.StateDeleted {
142+
if err := finalizer.UnsetFinalizers(ctx, r.cluster.GetClient(), clientObj, "mongodb.com/finalizer"); err != nil {
143+
return ctrl.Result{}, fmt.Errorf("failed to unset finalizer: %w", err)
144+
}
145+
}
146+
147+
return ctrl.Result{}, nil
148+
}
149+
145150
logger.Info("reconcile started", "currentState", currentState)
146151
if err := finalizer.EnsureFinalizers(ctx, r.cluster.GetClient(), clientObj, "mongodb.com/finalizer"); err != nil {
147152
return ctrl.Result{}, fmt.Errorf("failed to manage finalizers: %w", err)

pkg/controller/state/reconciler_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"sigs.k8s.io/controller-runtime/pkg/controller"
3838
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3939

40+
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/customresource"
4041
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/state"
4142
)
4243

@@ -304,6 +305,20 @@ func TestReconcile(t *testing.T) {
304305
return Result{NextState: "Initial"}, nil
305306
},
306307
},
308+
{
309+
name: "should skip reconcile request",
310+
existingObj: baseObj.WithSkipReconciliationAnnotation(),
311+
handleState: func(ctx context.Context, do *dummyObject) (Result, error) {
312+
return Result{Result: reconcile.Result{}}, nil
313+
},
314+
},
315+
{
316+
name: "should skip reconcile request on deleted object",
317+
existingObj: baseObj.WithSkipReconciliationAnnotation().WithDeletedStaze(),
318+
handleState: func(ctx context.Context, do *dummyObject) (Result, error) {
319+
return Result{Result: reconcile.Result{}}, nil
320+
},
321+
},
307322
}
308323

309324
for _, tc := range tests {
@@ -534,6 +549,33 @@ func (do *dummyObject) DeepCopy() *dummyObject {
534549
}
535550
}
536551

552+
func (do *dummyObject) WithSkipReconciliationAnnotation() *dummyObject {
553+
copyOfDo := do.DeepCopy()
554+
555+
if copyOfDo.Annotations == nil {
556+
copyOfDo.Annotations = make(map[string]string)
557+
}
558+
copyOfDo.Annotations[customresource.ReconciliationPolicyAnnotation] = customresource.ReconciliationPolicySkip
559+
560+
return copyOfDo
561+
}
562+
563+
func (do *dummyObject) WithDeletedStaze() *dummyObject {
564+
copyOfDo := do.DeepCopy()
565+
566+
if len(copyOfDo.Status.Conditions) == 0 {
567+
copyOfDo.Status.Conditions = []metav1.Condition{}
568+
}
569+
570+
copyOfDo.Status.Conditions = append(copyOfDo.Status.Conditions, metav1.Condition{
571+
Type: "State",
572+
Status: metav1.ConditionTrue,
573+
Reason: "Deleted",
574+
})
575+
576+
return copyOfDo
577+
}
578+
537579
func prevStatusObject(state state.ResourceState, observedGen int64) StatusObject {
538580
return newDummyObject(metav1.ObjectMeta{}, []metav1.Condition{
539581
newStateCondition(state, metav1.ConditionTrue, observedGen),

0 commit comments

Comments
 (0)