From dab38c8abebda5e6041115bee13a607e6e6fe916 Mon Sep 17 00:00:00 2001 From: Himanshu Pandey Date: Tue, 11 Feb 2020 10:53:31 -0800 Subject: [PATCH] Moved MachineDeployment Cluster Label Name to webhook fix the import issue Minor change to the test gofmted the file Added separate test case for labels Modified the MachineDeployment reconcile test, added labels, selectors --- api/v1alpha3/machinedeployment_webhook.go | 3 +++ api/v1alpha3/machinedeployment_webhook_test.go | 18 ++++++++++++++++++ controllers/machinedeployment_controller.go | 4 ---- .../machinedeployment_controller_test.go | 17 ++++++++++++++--- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/api/v1alpha3/machinedeployment_webhook.go b/api/v1alpha3/machinedeployment_webhook.go index 5ce67f553eb7..88fa1ef30e98 100644 --- a/api/v1alpha3/machinedeployment_webhook.go +++ b/api/v1alpha3/machinedeployment_webhook.go @@ -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 } diff --git a/api/v1alpha3/machinedeployment_webhook_test.go b/api/v1alpha3/machinedeployment_webhook_test.go index efb475e7eafe..1d7ac73ab2c9 100644 --- a/api/v1alpha3/machinedeployment_webhook_test.go +++ b/api/v1alpha3/machinedeployment_webhook_test.go @@ -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")) +} diff --git a/controllers/machinedeployment_controller.go b/controllers/machinedeployment_controller.go index d318aa331768..7fe99f7ae52e 100644 --- a/controllers/machinedeployment_controller.go +++ b/controllers/machinedeployment_controller.go @@ -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(), diff --git a/controllers/machinedeployment_controller_test.go b/controllers/machinedeployment_controller_test.go index b947d3b7ebc6..ccf1fc86ae8a 100644 --- a/controllers/machinedeployment_controller_test.go +++ b/controllers/machinedeployment_controller_test.go @@ -62,19 +62,29 @@ var _ = Describe("MachineDeployment Reconciler", func() { }) It("Should reconcile a MachineDeployment", func() { - labels := map[string]string{"foo": "bar"} + labels := map[string]string{ + "foo": "bar", + clusterv1.ClusterLabelName: testCluster.Name, + } version := "1.10.3" deployment := &clusterv1.MachineDeployment{ ObjectMeta: metav1.ObjectMeta{ GenerateName: "md-", Namespace: namespace.Name, + Labels: map[string]string{ + clusterv1.ClusterLabelName: testCluster.Name, + }, }, Spec: clusterv1.MachineDeploymentSpec{ ClusterName: testCluster.Name, MinReadySeconds: pointer.Int32Ptr(0), Replicas: pointer.Int32Ptr(2), RevisionHistoryLimit: pointer.Int32Ptr(0), - Selector: metav1.LabelSelector{}, + Selector: metav1.LabelSelector{ + MatchLabels: map[string]string{ + clusterv1.ClusterLabelName: testCluster.Name, + }, + }, Strategy: &clusterv1.MachineDeploymentStrategy{ Type: clusterv1.RollingUpdateMachineDeploymentStrategyType, RollingUpdate: &clusterv1.MachineRollingUpdateDeployment{ @@ -306,7 +316,8 @@ var _ = Describe("MachineDeployment Reconciler", func() { oldLabels[clusterv1.MachineDeploymentLabelName] = deployment.Name newLabels := map[string]string{ - "new-key": "new-value", + "new-key": "new-value", + clusterv1.ClusterLabelName: testCluster.Name, } By("Updating MachineDeployment label")