From b0b14bc7f0b3269b7e9e3152f8d3db9258680ff6 Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Wed, 27 Feb 2019 08:39:39 -0800 Subject: [PATCH] Add foregroundDeletion only if cluster is nil, move label in types (#785) Signed-off-by: Vince Prignano --- pkg/apis/cluster/v1alpha1/machine_types.go | 9 +++++++-- pkg/controller/machine/controller.go | 11 +++++------ pkg/controller/machine/controller_test.go | 6 +++--- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/pkg/apis/cluster/v1alpha1/machine_types.go b/pkg/apis/cluster/v1alpha1/machine_types.go index 02df8c021e1b..60fd59ab1c1c 100644 --- a/pkg/apis/cluster/v1alpha1/machine_types.go +++ b/pkg/apis/cluster/v1alpha1/machine_types.go @@ -23,8 +23,13 @@ import ( "sigs.k8s.io/cluster-api/pkg/apis/cluster/common" ) -// Finalizer is set on PrepareForCreate callback -const MachineFinalizer = "machine.cluster.k8s.io" +const ( + // MachineFinalizer is set on PrepareForCreate callback. + MachineFinalizer = "machine.cluster.k8s.io" + + // MachineClusterLabelName is the label set on machines linked to a cluster. + MachineClusterLabelName = "cluster.k8s.io/cluster-name" +) // +genclient // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object diff --git a/pkg/controller/machine/controller.go b/pkg/controller/machine/controller.go index 795bd088f26f..d939944685d5 100644 --- a/pkg/controller/machine/controller.go +++ b/pkg/controller/machine/controller.go @@ -37,8 +37,7 @@ import ( ) const ( - NodeNameEnvVar = "NODE_NAME" - MachineClusterLabelName = "cluster.k8s.io/cluster-name" + NodeNameEnvVar = "NODE_NAME" ) var DefaultActuator Actuator @@ -137,7 +136,7 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul if m.ObjectMeta.DeletionTimestamp.IsZero() { finalizerCount := len(m.Finalizers) - if !util.Contains(m.Finalizers, metav1.FinalizerDeleteDependents) { + if cluster != nil && !util.Contains(m.Finalizers, metav1.FinalizerDeleteDependents) { m.Finalizers = append(m.ObjectMeta.Finalizers, metav1.FinalizerDeleteDependents) } @@ -226,15 +225,15 @@ func (r *ReconcileMachine) Reconcile(request reconcile.Request) (reconcile.Resul } func (r *ReconcileMachine) getCluster(ctx context.Context, machine *clusterv1.Machine) (*clusterv1.Cluster, error) { - if machine.Labels[MachineClusterLabelName] == "" { - klog.Infof("Machine %q in namespace %q doesn't specify %q label, assuming nil cluster", machine.Name, MachineClusterLabelName, machine.Namespace) + if machine.Labels[clusterv1.MachineClusterLabelName] == "" { + klog.Infof("Machine %q in namespace %q doesn't specify %q label, assuming nil cluster", machine.Name, clusterv1.MachineClusterLabelName, machine.Namespace) return nil, nil } cluster := &clusterv1.Cluster{} key := client.ObjectKey{ Namespace: machine.Namespace, - Name: machine.Labels[MachineClusterLabelName], + Name: machine.Labels[clusterv1.MachineClusterLabelName], } if err := r.Client.Get(ctx, key, cluster); err != nil { diff --git a/pkg/controller/machine/controller_test.go b/pkg/controller/machine/controller_test.go index a2d91249adb9..49cf6128ff3c 100644 --- a/pkg/controller/machine/controller_test.go +++ b/pkg/controller/machine/controller_test.go @@ -42,7 +42,7 @@ func TestReconcileRequest(t *testing.T) { Namespace: "default", Finalizers: []string{v1alpha1.MachineFinalizer, metav1.FinalizerDeleteDependents}, Labels: map[string]string{ - MachineClusterLabelName: "testcluster", + v1alpha1.MachineClusterLabelName: "testcluster", }, }, } @@ -55,7 +55,7 @@ func TestReconcileRequest(t *testing.T) { Namespace: "default", Finalizers: []string{v1alpha1.MachineFinalizer, metav1.FinalizerDeleteDependents}, Labels: map[string]string{ - MachineClusterLabelName: "testcluster", + v1alpha1.MachineClusterLabelName: "testcluster", }, }, } @@ -70,7 +70,7 @@ func TestReconcileRequest(t *testing.T) { Finalizers: []string{v1alpha1.MachineFinalizer, metav1.FinalizerDeleteDependents}, DeletionTimestamp: &time, Labels: map[string]string{ - MachineClusterLabelName: "testcluster", + v1alpha1.MachineClusterLabelName: "testcluster", }, }, }