diff --git a/internal/controllers/cryostat_controller.go b/internal/controllers/cryostat_controller.go index c5ef72af..973826e6 100644 --- a/internal/controllers/cryostat_controller.go +++ b/internal/controllers/cryostat_controller.go @@ -64,6 +64,7 @@ import ( corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" "k8s.io/apimachinery/pkg/api/errors" + kerrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -571,6 +572,10 @@ func (r *CryostatReconciler) deleteClusterRoleBinding(ctx context.Context, cr *o clusterBinding := resources.NewClusterRoleBindingForCR(cr) err := r.Delete(ctx, clusterBinding) if err != nil { + if kerrors.IsNotFound(err) { + reqLogger.Info("ClusterRoleBinding not found, proceeding with deletion", "bindingName", clusterBinding.Name) + return nil + } reqLogger.Error(err, "failed to delete ClusterRoleBinding", "bindingName", clusterBinding.Name) return err } @@ -639,6 +644,10 @@ func (r *CryostatReconciler) deleteConsoleLink(ctx context.Context, cr *operator link := resources.NewConsoleLink(cr, "") err := r.Client.Delete(ctx, link) if err != nil { + if kerrors.IsNotFound(err) { + reqLogger.Info("ConsoleLink not found, proceeding with deletion", "linkName", link.Name) + return nil + } reqLogger.Error(err, "failed to delete ConsoleLink", "linkName", link.Name) return err } diff --git a/internal/controllers/cryostat_controller_test.go b/internal/controllers/cryostat_controller_test.go index 87b171bd..b20132bb 100644 --- a/internal/controllers/cryostat_controller_test.go +++ b/internal/controllers/cryostat_controller_test.go @@ -448,16 +448,28 @@ var _ = Describe("CryostatController", func() { t.objs = append(t.objs, test.NewCryostat()) }) JustBeforeEach(func() { - t.reconcileDeletedCryostat() + t.reconcileCryostatFully() }) - It("should delete the ClusterRoleBinding", func() { - t.checkClusterRoleBindingDeleted() + Context("ClusterRoleBinding exists", func() { + JustBeforeEach(func() { + t.reconcileDeletedCryostat() + }) + It("should delete the ClusterRoleBinding", func() { + t.checkClusterRoleBindingDeleted() + }) + It("should remove the finalizer", func() { + t.expectCryostatFinalizerAbsent() + }) }) - It("should remove the finalizer", func() { - cr := &operatorv1beta1.Cryostat{} - err := t.Client.Get(context.Background(), types.NamespacedName{Name: "cryostat", Namespace: "default"}, cr) - Expect(err).ToNot(HaveOccurred()) - Expect(cr.GetFinalizers()).ToNot(ContainElement("operator.cryostat.io/cryostat.finalizer")) + Context("ClusterRoleBinding does not exist", func() { + JustBeforeEach(func() { + err := t.Client.Delete(context.Background(), test.NewClusterRoleBinding()) + Expect(err).ToNot(HaveOccurred()) + t.reconcileDeletedCryostat() + }) + It("should remove the finalizer", func() { + t.expectCryostatFinalizerAbsent() + }) }) }) Context("on OpenShift", func() { @@ -475,10 +487,7 @@ var _ = Describe("CryostatController", func() { Expect(link.Spec).To(Equal(expectedLink.Spec)) }) It("should add the finalizer", func() { - cr := &operatorv1beta1.Cryostat{} - err := t.Client.Get(context.Background(), types.NamespacedName{Name: "cryostat", Namespace: "default"}, cr) - Expect(err).ToNot(HaveOccurred()) - Expect(cr.GetFinalizers()).To(ContainElement("operator.cryostat.io/cryostat.finalizer")) + t.expectCryostatFinalizerPresent() }) Context("with restricted SCC", func() { BeforeEach(func() { @@ -497,14 +506,29 @@ var _ = Describe("CryostatController", func() { }) }) Context("when deleted", func() { - JustBeforeEach(func() { - t.reconcileDeletedCryostat() + Context("ConsoleLink exists", func() { + JustBeforeEach(func() { + t.reconcileDeletedCryostat() + }) + It("should delete the ConsoleLink", func() { + link := &consolev1.ConsoleLink{} + expectedLink := test.NewConsoleLink() + err := t.Client.Get(context.Background(), types.NamespacedName{Name: expectedLink.Name}, link) + Expect(kerrors.IsNotFound(err)).To(BeTrue()) + }) + It("should remove the finalizer", func() { + t.expectCryostatFinalizerAbsent() + }) }) - It("should delete the ConsoleLink", func() { - link := &consolev1.ConsoleLink{} - expectedLink := test.NewConsoleLink() - err := t.Client.Get(context.Background(), types.NamespacedName{Name: expectedLink.Name}, link) - Expect(kerrors.IsNotFound(err)).To(BeTrue()) + Context("ConsoleLink does not exist", func() { + JustBeforeEach(func() { + err := t.Client.Delete(context.Background(), test.NewConsoleLink()) + Expect(err).ToNot(HaveOccurred()) + t.reconcileDeletedCryostat() + }) + It("should remove the finalizer", func() { + t.expectCryostatFinalizerAbsent() + }) }) }) }) @@ -1131,6 +1155,20 @@ func (t *cryostatTestInput) expectIdempotence() { Expect(obj2.Spec).To(Equal(obj.Spec)) } +func (t *cryostatTestInput) expectCryostatFinalizerPresent() { + cr := &operatorv1beta1.Cryostat{} + err := t.Client.Get(context.Background(), types.NamespacedName{Name: "cryostat", Namespace: "default"}, cr) + Expect(err).ToNot(HaveOccurred()) + Expect(cr.GetFinalizers()).To(ContainElement("operator.cryostat.io/cryostat.finalizer")) +} + +func (t *cryostatTestInput) expectCryostatFinalizerAbsent() { + cr := &operatorv1beta1.Cryostat{} + err := t.Client.Get(context.Background(), types.NamespacedName{Name: "cryostat", Namespace: "default"}, cr) + Expect(err).ToNot(HaveOccurred()) + Expect(cr.GetFinalizers()).ToNot(ContainElement("operator.cryostat.io/cryostat.finalizer")) +} + func (t *cryostatTestInput) checkGrafanaService() { service := &corev1.Service{} err := t.Client.Get(context.Background(), types.NamespacedName{Name: "cryostat-grafana", Namespace: "default"}, service)