Skip to content

Commit

Permalink
Add foregroundDeletion only if cluster is nil, move label in types (k…
Browse files Browse the repository at this point in the history
…ubernetes-sigs#785)

Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri authored and k8s-ci-robot committed Feb 27, 2019
1 parent a16ceae commit b0b14bc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
9 changes: 7 additions & 2 deletions pkg/apis/cluster/v1alpha1/machine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions pkg/controller/machine/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ import (
)

const (
NodeNameEnvVar = "NODE_NAME"
MachineClusterLabelName = "cluster.k8s.io/cluster-name"
NodeNameEnvVar = "NODE_NAME"
)

var DefaultActuator Actuator
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
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 @@ -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",
},
},
}
Expand All @@ -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",
},
},
}
Expand All @@ -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",
},
},
}
Expand Down

0 comments on commit b0b14bc

Please sign in to comment.