Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions pkg/controller/state/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/customresource"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/generated/translate"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/finalizer"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/state"
Expand Down Expand Up @@ -103,13 +104,6 @@ func NewStateReconciler[T any](target StateHandler[T], options ...ReconcilerOpti
return r
}

func NewUnstructuredStateReconciler(target UnstructuredStateReconciler, gvk schema.GroupVersionKind) *Reconciler[unstructured.Unstructured] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌
We are indeed not using this at all, so ok to remove it.

return &Reconciler[unstructured.Unstructured]{
reconciler: target,
unstructuredGVK: gvk,
}
}

func (r *Reconciler[T]) SetupWithManager(mgr ctrl.Manager, defaultOptions controller.Options) error {
r.cluster = mgr
return r.reconciler.SetupWithManager(mgr, r, defaultOptions)
Expand Down Expand Up @@ -142,6 +136,17 @@ func (r *Reconciler[T]) Reconcile(ctx context.Context, req ctrl.Request) (reconc
currentStatus := newStatusObject(obj)
currentState := state.GetState(currentStatus.Status.Conditions)

if customresource.ReconciliationShouldBeSkipped(clientObj) {
logger.Info(fmt.Sprintf("Skipping reconciliation by annotation %s=%s", customresource.ReconciliationPolicyAnnotation, customresource.ReconciliationPolicySkip))
if currentState == state.StateDeleted {
if err := finalizer.UnsetFinalizers(ctx, r.cluster.GetClient(), clientObj, "mongodb.com/finalizer"); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to unset finalizer: %w", err)
}
}

return ctrl.Result{}, nil
}

logger.Info("reconcile started", "currentState", currentState)
if err := finalizer.EnsureFinalizers(ctx, r.cluster.GetClient(), clientObj, "mongodb.com/finalizer"); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to manage finalizers: %w", err)
Expand Down
42 changes: 42 additions & 0 deletions pkg/controller/state/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/customresource"
"github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/state"
)

Expand Down Expand Up @@ -304,6 +305,20 @@ func TestReconcile(t *testing.T) {
return Result{NextState: "Initial"}, nil
},
},
{
name: "should skip reconcile request",
existingObj: baseObj.WithSkipReconciliationAnnotation(),
handleState: func(ctx context.Context, do *dummyObject) (Result, error) {
return Result{Result: reconcile.Result{}}, nil
},
},
{
name: "should skip reconcile request on deleted object",
existingObj: baseObj.WithSkipReconciliationAnnotation().WithDeletedStaze(),
handleState: func(ctx context.Context, do *dummyObject) (Result, error) {
return Result{Result: reconcile.Result{}}, nil
},
},
}

for _, tc := range tests {
Expand Down Expand Up @@ -534,6 +549,33 @@ func (do *dummyObject) DeepCopy() *dummyObject {
}
}

func (do *dummyObject) WithSkipReconciliationAnnotation() *dummyObject {
copyOfDo := do.DeepCopy()

if copyOfDo.Annotations == nil {
copyOfDo.Annotations = make(map[string]string)
}
copyOfDo.Annotations[customresource.ReconciliationPolicyAnnotation] = customresource.ReconciliationPolicySkip

return copyOfDo
}

func (do *dummyObject) WithDeletedStaze() *dummyObject {
copyOfDo := do.DeepCopy()

if len(copyOfDo.Status.Conditions) == 0 {
copyOfDo.Status.Conditions = []metav1.Condition{}
}

copyOfDo.Status.Conditions = append(copyOfDo.Status.Conditions, metav1.Condition{
Type: "State",
Status: metav1.ConditionTrue,
Reason: "Deleted",
})

return copyOfDo
}

func prevStatusObject(state state.ResourceState, observedGen int64) StatusObject {
return newDummyObject(metav1.ObjectMeta{}, []metav1.Condition{
newStateCondition(state, metav1.ConditionTrue, observedGen),
Expand Down