Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/apis/machine/v1beta1/machine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
21 changes: 17 additions & 4 deletions pkg/apis/machine/v1beta1/machine_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestMachineCreation(t *testing.T) {
name string
platformType osconfigv1.PlatformType
clusterID string
presetClusterID bool
expectedError string
providerSpecValue *runtime.RawExtension
}{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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() {
Expand All @@ -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())
}
})
Expand Down
8 changes: 6 additions & 2 deletions pkg/apis/machine/v1beta1/machineset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 23 additions & 5 deletions pkg/apis/machine/v1beta1/machineset_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestMachineSetCreation(t *testing.T) {
name string
platformType osconfigv1.PlatformType
clusterID string
presetClusterID bool
expectedError string
providerSpecValue *runtime.RawExtension
}{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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() {
Expand All @@ -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())
}
})
Expand Down