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

Backport foreground deletion #989

Merged
Merged
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
30 changes: 21 additions & 9 deletions pkg/controller/cluster/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
Expand Down Expand Up @@ -86,20 +87,31 @@ func (r *ReconcileCluster) Reconcile(request reconcile.Request) (reconcile.Resul
}

name := cluster.Name
klog.Infof("Running reconcile Cluster for %s\n", name)
klog.Infof("Running reconcile Cluster for %q", name)

// If object hasn't been deleted and doesn't have a finalizer, add one
// Add a finalizer to newly created objects.
if cluster.ObjectMeta.DeletionTimestamp.IsZero() &&
!util.Contains(cluster.ObjectMeta.Finalizers, clusterv1.ClusterFinalizer) {
cluster.Finalizers = append(cluster.Finalizers, clusterv1.ClusterFinalizer)
if err = r.Update(context.Background(), cluster); err != nil {
klog.Infof("failed to add finalizer to cluster object %v due to error %v.", name, err)
return reconcile.Result{}, err
if cluster.ObjectMeta.DeletionTimestamp.IsZero() {
finalizerCount := len(cluster.Finalizers)

if !util.Contains(cluster.Finalizers, metav1.FinalizerDeleteDependents) {
cluster.Finalizers = append(cluster.ObjectMeta.Finalizers, metav1.FinalizerDeleteDependents)
}

if !util.Contains(cluster.Finalizers, clusterv1.ClusterFinalizer) {
cluster.Finalizers = append(cluster.ObjectMeta.Finalizers, clusterv1.ClusterFinalizer)
}

if len(cluster.Finalizers) > finalizerCount {
if err := r.Update(context.Background(), cluster); err != nil {
klog.Infof("Failed to add finalizer to cluster %q: %v", name, err)
return reconcile.Result{}, err
}

// Since adding the finalizer updates the object return to avoid later update issues.
return reconcile.Result{Requeue: true}, nil
}

// Since adding the finalizer updates the object return to avoid later update issues
return reconcile.Result{}, nil
}

if !cluster.ObjectMeta.DeletionTimestamp.IsZero() {
Expand Down