Skip to content

Commit

Permalink
Set Machines ownerRef to cluster with foregroundDeletion (#757)
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri authored and k8s-ci-robot committed Feb 21, 2019
1 parent 0252bc6 commit c64edfe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
50 changes: 40 additions & 10 deletions pkg/controller/machine/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

corev1 "k8s.io/api/core/v1"
apierrors "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 @@ -120,30 +121,55 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul
if err != nil {
return reconcile.Result{}, err
}

// Set the ownerRef with foreground deletion if there is a linked cluster.
if cluster != nil && len(m.OwnerReferences) == 0 {
blockOwnerDeletion := true
m.OwnerReferences = append(m.OwnerReferences, metav1.OwnerReference{
APIVersion: cluster.APIVersion,
Kind: cluster.Kind,
Name: cluster.Name,
UID: cluster.UID,
BlockOwnerDeletion: &blockOwnerDeletion,
})
}

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

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

// Since adding the finalizer updates the object return to avoid later update issues
return reconcile.Result{}, nil
if !util.Contains(m.Finalizers, clusterv1.MachineFinalizer) {
m.Finalizers = append(m.ObjectMeta.Finalizers, clusterv1.MachineFinalizer)
}

if len(m.Finalizers) > finalizerCount {
if err := r.Client.Update(ctx, m); err != nil {
klog.Infof("failed to add finalizers to machine object %v due to error %v.", name, err)
return reconcile.Result{}, err
}

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

if !m.ObjectMeta.DeletionTimestamp.IsZero() {
// no-op if finalizer has been removed.
if !util.Contains(m.ObjectMeta.Finalizers, clusterv1.MachineFinalizer) {
if !util.Contains(m.Finalizers, clusterv1.MachineFinalizer) {
klog.Infof("reconciling machine object %v causes a no-op as there is no finalizer.", name)
return reconcile.Result{}, nil
}

if !r.isDeleteAllowed(m) {
klog.Infof("Skipping reconciling of machine object %v", name)
return reconcile.Result{}, nil
}

klog.Infof("reconciling machine object %v triggers delete.", name)
if err := r.actuator.Delete(ctx, cluster, m); err != nil {
klog.Errorf("Error deleting machine object %v; %v", name, err)
Expand All @@ -156,11 +182,12 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul

// Remove finalizer on successful deletion.
klog.Infof("machine object %v deletion successful, removing finalizer.", name)
m.ObjectMeta.Finalizers = util.Filter(m.ObjectMeta.Finalizers, clusterv1.MachineFinalizer)
m.Finalizers = util.Filter(m.Finalizers, clusterv1.MachineFinalizer)
if err := r.Client.Update(context.Background(), m); err != nil {
klog.Errorf("Error removing finalizer from machine object %v; %v", name, err)
return reconcile.Result{}, err
}

return reconcile.Result{}, nil
}

Expand All @@ -169,6 +196,7 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul
klog.Errorf("Error checking existence of machine instance for machine object %v; %v", name, err)
return reconcile.Result{}, err
}

if exist {
klog.Infof("Reconciling machine object %v triggers idempotent update.", name)
if err := r.actuator.Update(ctx, cluster, m); err != nil {
Expand All @@ -180,6 +208,7 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul
}
return reconcile.Result{}, nil
}

// Machine resource created. Machine does not yet exist.
klog.Infof("Reconciling machine object %v triggers idempotent create.", m.ObjectMeta.Name)
if err := r.actuator.Create(ctx, cluster, m); err != nil {
Expand All @@ -190,6 +219,7 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul
}
return reconcile.Result{}, err
}

return reconcile.Result{}, nil
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/machine/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestReconcileRequest(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "create",
Namespace: "default",
Finalizers: []string{v1alpha1.MachineFinalizer},
Finalizers: []string{v1alpha1.MachineFinalizer, metav1.FinalizerDeleteDependents},
Labels: map[string]string{
MachineClusterLabelName: "testcluster",
},
Expand All @@ -49,7 +49,7 @@ func TestReconcileRequest(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "update",
Namespace: "default",
Finalizers: []string{v1alpha1.MachineFinalizer},
Finalizers: []string{v1alpha1.MachineFinalizer, metav1.FinalizerDeleteDependents},
Labels: map[string]string{
MachineClusterLabelName: "testcluster",
},
Expand All @@ -63,7 +63,7 @@ func TestReconcileRequest(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "delete",
Namespace: "default",
Finalizers: []string{v1alpha1.MachineFinalizer},
Finalizers: []string{v1alpha1.MachineFinalizer, metav1.FinalizerDeleteDependents},
DeletionTimestamp: &time,
Labels: map[string]string{
MachineClusterLabelName: "testcluster",
Expand Down

0 comments on commit c64edfe

Please sign in to comment.