Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #410: fix reconcile if foreground cascading deletion is used #530

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 21 additions & 11 deletions controllers/zookeepercluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"strconv"
"time"

"errors"

"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/predicate"
Expand All @@ -28,7 +30,7 @@
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/api/errors"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -49,6 +51,8 @@

var _ reconcile.Reconciler = &ZookeeperClusterReconciler{}

var ErrFinalizerDone = errors.New("finalizer already done")

// ZookeeperClusterReconciler reconciles a ZookeeperCluster object
type ZookeeperClusterReconciler struct {
Client client.Client
Expand All @@ -72,7 +76,7 @@
instance := &zookeeperv1beta1.ZookeeperCluster{}
err := r.Client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
if errors.IsNotFound(err) {
if kerrors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile
// request. Owned objects are automatically garbage collected. For
// additional cleanup logic use finalizers.
Expand Down Expand Up @@ -100,8 +104,14 @@
}
return reconcile.Result{Requeue: true}, nil
}
if err := r.reconcileFinalizers(instance); err != nil {
if errors.Is(err, ErrFinalizerDone) {
err = nil

Check warning on line 109 in controllers/zookeepercluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/zookeepercluster_controller.go#L108-L109

Added lines #L108 - L109 were not covered by tests
}
return reconcile.Result{}, err

Check warning on line 111 in controllers/zookeepercluster_controller.go

View check run for this annotation

Codecov / codecov/patch

controllers/zookeepercluster_controller.go#L111

Added line #L111 was not covered by tests
}

for _, fun := range []reconcileFun{
r.reconcileFinalizers,
r.reconcileConfigMap,
r.reconcileStatefulSet,
r.reconcileClientService,
Expand Down Expand Up @@ -198,7 +208,7 @@
// Check if this ServiceAccount already exists
foundServiceAccount := &corev1.ServiceAccount{}
err = r.Client.Get(context.TODO(), types.NamespacedName{Name: serviceAccount.Name, Namespace: serviceAccount.Namespace}, foundServiceAccount)
if err != nil && errors.IsNotFound(err) {
if err != nil && kerrors.IsNotFound(err) {
r.Log.Info("Creating a new ServiceAccount", "ServiceAccount.Namespace", serviceAccount.Namespace, "ServiceAccount.Name", serviceAccount.Name)
err = r.Client.Create(context.TODO(), serviceAccount)
if err != nil {
Expand All @@ -224,7 +234,7 @@
Name: sts.Name,
Namespace: sts.Namespace,
}, foundSts)
if err != nil && errors.IsNotFound(err) {
if err != nil && kerrors.IsNotFound(err) {
r.Log.Info("Creating a new Zookeeper StatefulSet",
"StatefulSet.Namespace", sts.Namespace,
"StatefulSet.Name", sts.Name)
Expand Down Expand Up @@ -386,7 +396,7 @@
Name: svc.Name,
Namespace: svc.Namespace,
}, foundSvc)
if err != nil && errors.IsNotFound(err) {
if err != nil && kerrors.IsNotFound(err) {
r.Log.Info("Creating new client service",
"Service.Namespace", svc.Namespace,
"Service.Name", svc.Name)
Expand Down Expand Up @@ -433,7 +443,7 @@
Name: svc.Name,
Namespace: svc.Namespace,
}, foundSvc)
if err != nil && errors.IsNotFound(err) {
if err != nil && kerrors.IsNotFound(err) {
r.Log.Info("Creating new headless service",
"Service.Namespace", svc.Namespace,
"Service.Name", svc.Name)
Expand Down Expand Up @@ -467,7 +477,7 @@
Name: svc.Name,
Namespace: svc.Namespace,
}, foundSvc)
if err != nil && errors.IsNotFound(err) {
if err != nil && kerrors.IsNotFound(err) {
r.Log.Info("Creating admin server service",
"Service.Namespace", svc.Namespace,
"Service.Name", svc.Name)
Expand Down Expand Up @@ -501,7 +511,7 @@
Name: pdb.Name,
Namespace: pdb.Namespace,
}, foundPdb)
if err != nil && errors.IsNotFound(err) {
if err != nil && kerrors.IsNotFound(err) {
r.Log.Info("Creating new pod-disruption-budget",
"PodDisruptionBudget.Namespace", pdb.Namespace,
"PodDisruptionBudget.Name", pdb.Name)
Expand All @@ -526,7 +536,7 @@
Name: cm.Name,
Namespace: cm.Namespace,
}, foundCm)
if err != nil && errors.IsNotFound(err) {
if err != nil && kerrors.IsNotFound(err) {
r.Log.Info("Creating a new Zookeeper Config Map",
"ConfigMap.Namespace", cm.Namespace,
"ConfigMap.Name", cm.Name)
Expand Down Expand Up @@ -721,7 +731,7 @@
}
}
}
return nil
return fmt.Errorf("%w, do not continue reconcile", ErrFinalizerDone)
}

func (r *ZookeeperClusterReconciler) getPVCCount(instance *zookeeperv1beta1.ZookeeperCluster) (pvcCount int, err error) {
Expand Down
8 changes: 7 additions & 1 deletion controllers/zookeepercluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,11 +649,17 @@ var _ = Describe("ZookeeperCluster Controller", func() {
now := metav1.Now()
z.SetDeletionTimestamp(&now)
cl.Update(context.TODO(), z)
err = r.reconcileFinalizers(z)
})
It("should not raise an error", func() {
err = r.reconcileFinalizers(z)
Ω(err).To(BeNil())
})
It("should not raise an error", func() {
z.Spec.Persistence.VolumeReclaimPolicy = v1beta1.VolumeReclaimPolicyDelete
cl.Update(context.TODO(), z)
err = r.reconcileFinalizers(z)
Ω(err.Error()).To(Equal("finalizer already done, do not continue reconcile"))
})
})

Context("reconcileFinalizers", func() {
Expand Down
Loading