diff --git a/controllers/nstemplatetier/nstemplatetier_controller.go b/controllers/nstemplatetier/nstemplatetier_controller.go index fe27f36d2..c6c729a84 100644 --- a/controllers/nstemplatetier/nstemplatetier_controller.go +++ b/controllers/nstemplatetier/nstemplatetier_controller.go @@ -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" @@ -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 { @@ -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{ diff --git a/controllers/nstemplatetier/nstemplatetier_controller_test.go b/controllers/nstemplatetier/nstemplatetier_controller_test.go index 1835c8e2b..538259764 100644 --- a/controllers/nstemplatetier/nstemplatetier_controller_test.go +++ b/controllers/nstemplatetier/nstemplatetier_controller_test.go @@ -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) + }) }) } @@ -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)