Skip to content

Commit

Permalink
Moved MachineDeployment Cluster Label Name to webhook
Browse files Browse the repository at this point in the history
fix the import issue

Minor change to the test

gofmted the file

Added separate test case for labels
  • Loading branch information
Himanshu Pandey committed Feb 19, 2020
1 parent e939e07 commit 0bedc08
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions api/v1alpha3/machinedeployment_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,7 @@ func PopulateDefaultsMachineDeployment(d *MachineDeployment) {
d.Spec.Selector.MatchLabels[MachineDeploymentLabelName] = d.Name
d.Spec.Template.Labels[MachineDeploymentLabelName] = d.Name
}
// Make sure selector and template to be in the same cluster.
d.Spec.Selector.MatchLabels[ClusterLabelName] = d.Spec.ClusterName
d.Spec.Template.Labels[ClusterLabelName] = d.Spec.ClusterName
}
18 changes: 18 additions & 0 deletions api/v1alpha3/machinedeployment_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,21 @@ func TestMachineDeploymentValidation(t *testing.T) {
})
}
}

func TestMachineDeploymentWithSpec(t *testing.T) {
g := NewWithT(t)
md := MachineDeployment{
Spec: MachineDeploymentSpec{
ClusterName: "test-cluster",
Template: MachineTemplateSpec{
Spec: MachineSpec{
ClusterName: "test-cluster",
},
},
},
}

md.Default()
g.Expect(md.Spec.Selector.MatchLabels).To(HaveKeyWithValue(ClusterLabelName, "test-cluster"))
g.Expect(md.Spec.Template.Labels).To(HaveKeyWithValue(ClusterLabelName, "test-cluster"))
}
4 changes: 0 additions & 4 deletions controllers/machinedeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ func (r *MachineDeploymentReconciler) reconcile(_ context.Context, cluster *clus

d.Labels[clusterv1.ClusterLabelName] = d.Spec.ClusterName

// Make sure selector and template to be in the same cluster.
d.Spec.Selector.MatchLabels[clusterv1.ClusterLabelName] = d.Spec.ClusterName
d.Spec.Template.Labels[clusterv1.ClusterLabelName] = d.Spec.ClusterName

if r.shouldAdopt(d) {
d.OwnerReferences = util.EnsureOwnerRef(d.OwnerReferences, metav1.OwnerReference{
APIVersion: clusterv1.GroupVersion.String(),
Expand Down

0 comments on commit 0bedc08

Please sign in to comment.