Skip to content

Commit

Permalink
fix(finalizer): remove finalizers when objects to delete not found (#258
Browse files Browse the repository at this point in the history
) (#259)

(cherry picked from commit 4c813f3)

Co-authored-by: Elliott Baron <ebaron@redhat.com>
  • Loading branch information
mergify[bot] and ebaron committed Sep 15, 2021
1 parent 4662141 commit 73fc033
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 19 deletions.
9 changes: 9 additions & 0 deletions internal/controllers/cryostat_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
76 changes: 57 additions & 19 deletions internal/controllers/cryostat_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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()
})
})
})
})
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 73fc033

Please sign in to comment.