diff --git a/pkg/apis/machine/v1beta1/machine_webhook.go b/pkg/apis/machine/v1beta1/machine_webhook.go index 4dab8e2abe..2fa1558194 100644 --- a/pkg/apis/machine/v1beta1/machine_webhook.go +++ b/pkg/apis/machine/v1beta1/machine_webhook.go @@ -452,7 +452,9 @@ func (h *machineDefaulterHandler) Handle(ctx context.Context, req admission.Requ if m.Labels == nil { m.Labels = make(map[string]string) } - m.Labels[MachineClusterIDLabel] = h.clusterID + if _, ok := m.Labels[MachineClusterIDLabel]; !ok { + m.Labels[MachineClusterIDLabel] = h.clusterID + } if ok, err := h.webhookOperations(m, h.clusterID); !ok { return admission.Denied(err.Error()) diff --git a/pkg/apis/machine/v1beta1/machine_webhook_test.go b/pkg/apis/machine/v1beta1/machine_webhook_test.go index c89bb34a1c..9f5eb7bbf2 100644 --- a/pkg/apis/machine/v1beta1/machine_webhook_test.go +++ b/pkg/apis/machine/v1beta1/machine_webhook_test.go @@ -50,6 +50,7 @@ func TestMachineCreation(t *testing.T) { name string platformType osconfigv1.PlatformType clusterID string + presetClusterID bool expectedError string providerSpecValue *runtime.RawExtension }{ @@ -176,9 +177,10 @@ func TestMachineCreation(t *testing.T) { expectedError: "[providerSpec.template: Required value: template must be provided, providerSpec.workspace: Required value: workspace must be provided, providerSpec.network.devices: Required value: at least 1 network device must be provided]", }, { - name: "with vSphere and the template, workspace and network devices set", - platformType: osconfigv1.VSpherePlatformType, - clusterID: "vsphere-cluster", + name: "with vSphere and the template, workspace and network devices set", + platformType: osconfigv1.VSpherePlatformType, + clusterID: "vsphere-cluster", + presetClusterID: true, providerSpecValue: &runtime.RawExtension{ Object: &vsphere.VSphereMachineProviderSpec{ Template: "template", @@ -255,6 +257,13 @@ func TestMachineCreation(t *testing.T) { }, }, } + + presetClusterID := "anything" + if tc.presetClusterID { + m.Labels = make(map[string]string) + m.Labels[MachineClusterIDLabel] = presetClusterID + } + err = c.Create(ctx, m) if err == nil { defer func() { @@ -266,7 +275,11 @@ func TestMachineCreation(t *testing.T) { gs.Expect(err).ToNot(BeNil()) gs.Expect(apierrors.ReasonForError(err)).To(BeEquivalentTo(tc.expectedError)) } else { - gs.Expect(m.Labels[MachineClusterIDLabel]).To(BeIdenticalTo(tc.clusterID)) + if tc.presetClusterID { + gs.Expect(m.Labels[MachineClusterIDLabel]).To(BeIdenticalTo(presetClusterID)) + } else { + gs.Expect(m.Labels[MachineClusterIDLabel]).To(BeIdenticalTo(tc.clusterID)) + } gs.Expect(err).To(BeNil()) } }) diff --git a/pkg/apis/machine/v1beta1/machineset_webhook.go b/pkg/apis/machine/v1beta1/machineset_webhook.go index 6e3bd90cfd..c79a7250a9 100644 --- a/pkg/apis/machine/v1beta1/machineset_webhook.go +++ b/pkg/apis/machine/v1beta1/machineset_webhook.go @@ -131,12 +131,16 @@ func (h *machineSetDefaulterHandler) defaultMachineSet(ms *MachineSet) (bool, ut if ms.Spec.Selector.MatchLabels == nil { ms.Spec.Selector.MatchLabels = make(map[string]string) } - ms.Spec.Selector.MatchLabels[MachineClusterIDLabel] = h.clusterID + if _, ok := ms.Spec.Selector.MatchLabels[MachineClusterIDLabel]; !ok { + ms.Spec.Selector.MatchLabels[MachineClusterIDLabel] = h.clusterID + } if ms.Spec.Template.Labels == nil { ms.Spec.Template.Labels = make(map[string]string) } - ms.Spec.Template.Labels[MachineClusterIDLabel] = h.clusterID + if _, ok := ms.Spec.Template.Labels[MachineClusterIDLabel]; !ok { + ms.Spec.Template.Labels[MachineClusterIDLabel] = h.clusterID + } // Restore the defaulted template ms.Spec.Template.Spec = m.Spec diff --git a/pkg/apis/machine/v1beta1/machineset_webhook_test.go b/pkg/apis/machine/v1beta1/machineset_webhook_test.go index f738c6c43d..a3de21a224 100644 --- a/pkg/apis/machine/v1beta1/machineset_webhook_test.go +++ b/pkg/apis/machine/v1beta1/machineset_webhook_test.go @@ -47,6 +47,7 @@ func TestMachineSetCreation(t *testing.T) { name string platformType osconfigv1.PlatformType clusterID string + presetClusterID bool expectedError string providerSpecValue *runtime.RawExtension }{ @@ -173,9 +174,10 @@ func TestMachineSetCreation(t *testing.T) { expectedError: "[providerSpec.template: Required value: template must be provided, providerSpec.workspace: Required value: workspace must be provided, providerSpec.network.devices: Required value: at least 1 network device must be provided]", }, { - name: "with vSphere and the template, workspace and network devices set", - platformType: osconfigv1.VSpherePlatformType, - clusterID: "vsphere-cluster", + name: "with vSphere and the template, workspace and network devices set", + platformType: osconfigv1.VSpherePlatformType, + clusterID: "vsphere-cluster", + presetClusterID: true, providerSpecValue: &runtime.RawExtension{ Object: &vsphere.VSphereMachineProviderSpec{ Template: "template", @@ -256,6 +258,16 @@ func TestMachineSetCreation(t *testing.T) { }, }, } + + presetClusterID := "anything" + if tc.presetClusterID { + ms.Spec.Selector.MatchLabels = make(map[string]string) + ms.Spec.Selector.MatchLabels[MachineClusterIDLabel] = presetClusterID + + ms.Spec.Template.Labels = make(map[string]string) + ms.Spec.Template.Labels[MachineClusterIDLabel] = presetClusterID + } + err = c.Create(ctx, ms) if err == nil { defer func() { @@ -267,8 +279,14 @@ func TestMachineSetCreation(t *testing.T) { gs.Expect(err).ToNot(BeNil()) gs.Expect(apierrors.ReasonForError(err)).To(BeEquivalentTo(tc.expectedError)) } else { - gs.Expect(ms.Spec.Selector.MatchLabels[MachineClusterIDLabel]).To(BeIdenticalTo(tc.clusterID)) - gs.Expect(ms.Spec.Template.Labels[MachineClusterIDLabel]).To(BeIdenticalTo(tc.clusterID)) + if tc.presetClusterID { + gs.Expect(ms.Spec.Selector.MatchLabels[MachineClusterIDLabel]).To(BeIdenticalTo(presetClusterID)) + gs.Expect(ms.Spec.Template.Labels[MachineClusterIDLabel]).To(BeIdenticalTo(presetClusterID)) + + } else { + gs.Expect(ms.Spec.Selector.MatchLabels[MachineClusterIDLabel]).To(BeIdenticalTo(tc.clusterID)) + gs.Expect(ms.Spec.Template.Labels[MachineClusterIDLabel]).To(BeIdenticalTo(tc.clusterID)) + } gs.Expect(err).To(BeNil()) } })