Skip to content

Commit

Permalink
don't process NSTemplateTier that is being deleted (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatousJobanek authored Jan 23, 2025
1 parent a1bcf67 commit fc112fb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
7 changes: 6 additions & 1 deletion controllers/nstemplatetier/nstemplatetier_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
"github.com/codeready-toolchain/host-operator/controllers/toolchainconfig"
"github.com/redhat-cop/operator-utils/pkg/util"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -54,6 +55,10 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
logger.Error(err, "unable to get the current NSTemplateTier")
return reconcile.Result{}, fmt.Errorf("unable to get the current NSTemplateTier: %w", err)
}
// if the NSTemplateTier is being deleted, then do nothing
if util.IsBeingDeleted(tier) {
return reconcile.Result{}, nil
}

_, err := toolchainconfig.GetToolchainConfig(r.Client)
if err != nil {
Expand Down Expand Up @@ -211,7 +216,7 @@ func NewTTR(tierTmpl *toolchainv1alpha1.TierTemplate, nsTmplTier *toolchainv1alp
ttr := &toolchainv1alpha1.TierTemplateRevision{
ObjectMeta: metav1.ObjectMeta{
Namespace: tierTmpl.GetNamespace(),
GenerateName: newTTRName + "-",
GenerateName: newTTRName,
Labels: labels,
},
Spec: toolchainv1alpha1.TierTemplateRevisionSpec{
Expand Down
31 changes: 30 additions & 1 deletion controllers/nstemplatetier/nstemplatetier_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,35 @@ func TestReconcile(t *testing.T) {
})

})

t.Run("if being deleted, then do nothign", func(t *testing.T) {
// given
tierBeingDeleted := base1nsTier.DeepCopy()
tierBeingDeleted.DeletionTimestamp = &metav1.Time{Time: time.Now()}
tierBeingDeleted.Finalizers = []string{"dummy"}
r, req, cl := prepareReconcile(t, tierBeingDeleted.Name, tierBeingDeleted)
called := false
cl.MockGet = func(ctx context.Context, key runtimeclient.ObjectKey, obj runtimeclient.Object, opts ...runtimeclient.GetOption) error {
if called {
return fmt.Errorf("should not call Get more than once")
}
called = true
return cl.Client.Get(ctx, key, obj, opts...)
}
cl.MockCreate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.CreateOption) error {
return fmt.Errorf("should not call Create")
}
cl.MockStatusUpdate = func(ctx context.Context, obj runtimeclient.Object, opts ...runtimeclient.SubResourceUpdateOption) error {
return fmt.Errorf("should not call StatusUpdate")
}

// when
res, err := r.Reconcile(context.TODO(), req)

// then
require.NoError(t, err)
assert.Empty(t, res.Requeue)
})
})

}
Expand All @@ -325,7 +354,7 @@ func initTierTemplates(t *testing.T, withTemplateObjects []runtime.RawExtension,
return tierTemplates
}

func prepareReconcile(t *testing.T, name string, initObjs ...runtimeclient.Object) (reconcile.Reconciler, reconcile.Request, *test.FakeClient) {
func prepareReconcile(t *testing.T, name string, initObjs ...runtimeclient.Object) (*nstemplatetier.Reconciler, reconcile.Request, *test.FakeClient) {
os.Setenv("WATCH_NAMESPACE", test.HostOperatorNs)
s := scheme.Scheme
err := apis.AddToScheme(s)
Expand Down

0 comments on commit fc112fb

Please sign in to comment.