Skip to content

Commit

Permalink
Merge pull request #2308 from hpandeycodeit/md_label
Browse files Browse the repository at this point in the history
✨ Moved MachineDeployment Cluster Label Name to webhook
  • Loading branch information
k8s-ci-robot committed Feb 21, 2020
2 parents 19bb154 + dab38c8 commit 1435eed
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 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(ctx context.Context, cluster *cl

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
17 changes: 14 additions & 3 deletions controllers/machinedeployment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -305,7 +315,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")
Expand Down

0 comments on commit 1435eed

Please sign in to comment.