Skip to content

Commit 4566a9a

Browse files
Merge pull request #1477 from vr4manta/SPLAT-2679
SPLAT-2680: Fixed MAO to not allow dedicated host configurations for control plane nodes
2 parents d8d9ab7 + 21400fe commit 4566a9a

2 files changed

Lines changed: 93 additions & 2 deletions

File tree

pkg/webhooks/machine_webhook.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ func validateAWS(m *machinev1beta1.Machine, config *admissionConfig) (bool, []st
831831
}
832832
}
833833

834-
errs = append(errs, processAWSPlacementTenancy(providerSpec.Placement)...)
834+
errs = append(errs, processAWSPlacementTenancy(m, providerSpec.Placement)...)
835835

836836
if providerSpec.PlacementGroupPartition != nil {
837837
partition := *providerSpec.PlacementGroupPartition
@@ -928,9 +928,26 @@ func validateAWS(m *machinev1beta1.Machine, config *admissionConfig) (bool, []st
928928
return true, warnings, nil
929929
}
930930

931+
const (
932+
machineRoleLabel = "machine.openshift.io/cluster-api-machine-role"
933+
machineTypeLabel = "machine.openshift.io/cluster-api-machine-type"
934+
machineMasterRole = "master"
935+
)
936+
937+
// isControlPlaneMachine checks if a machine is a control plane/master machine by examining its labels.
938+
func isControlPlaneMachine(m *machinev1beta1.Machine) bool {
939+
if m.Labels == nil {
940+
return false
941+
}
942+
role, hasRole := m.Labels[machineRoleLabel]
943+
machineType, hasType := m.Labels[machineTypeLabel]
944+
945+
return (hasRole && role == machineMasterRole) || (hasType && machineType == machineMasterRole)
946+
}
947+
931948
// processAWSPlacement analyzes the Placement field in relation to Tenancy and host placement. These are analyzed
932949
// together based based on their relations to one another.
933-
func processAWSPlacementTenancy(placement machinev1beta1.Placement) field.ErrorList {
950+
func processAWSPlacementTenancy(m *machinev1beta1.Machine, placement machinev1beta1.Placement) field.ErrorList {
934951
var errs field.ErrorList
935952

936953
switch placement.Tenancy {
@@ -940,6 +957,12 @@ func processAWSPlacementTenancy(placement machinev1beta1.Placement) field.ErrorL
940957
errs = append(errs, field.Forbidden(field.NewPath("spec.placement.host"), "host may only be specified when tenancy is 'host'"))
941958
}
942959
case machinev1beta1.HostTenancy:
960+
// Dedicated hosts are not supported for control plane machines
961+
if isControlPlaneMachine(m) {
962+
errs = append(errs, field.Forbidden(field.NewPath("spec.placement.tenancy"), "dedicated host tenancy is not supported for control plane machines"))
963+
return errs
964+
}
965+
943966
if placement.Host != nil {
944967
klog.V(4).Infof("Validating AWS Host Placement")
945968

pkg/webhooks/machine_webhook_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func TestMachineCreation(t *testing.T) {
123123
platformType osconfigv1.PlatformType
124124
clusterID string
125125
presetClusterID bool
126+
isMasterMachine bool
126127
expectedError string
127128
disconnected bool
128129
providerSpecValue *kruntime.RawExtension
@@ -1033,6 +1034,66 @@ func TestMachineCreation(t *testing.T) {
10331034
},
10341035
expectedError: "",
10351036
},
1037+
{
1038+
name: "control plane machine with dedicated host should fail",
1039+
platformType: osconfigv1.AWSPlatformType,
1040+
clusterID: "aws-cluster",
1041+
isMasterMachine: true,
1042+
providerSpecValue: &kruntime.RawExtension{
1043+
Object: &machinev1beta1.AWSMachineProviderConfig{
1044+
AMI: machinev1beta1.AWSResourceReference{ID: ptr.To[string]("ami")},
1045+
InstanceType: "test",
1046+
Placement: machinev1beta1.Placement{
1047+
Tenancy: machinev1beta1.HostTenancy,
1048+
Host: &machinev1beta1.HostPlacement{
1049+
Affinity: ptr.To(machinev1beta1.HostAffinityAnyAvailable),
1050+
},
1051+
},
1052+
},
1053+
},
1054+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.tenancy: Forbidden: dedicated host tenancy is not supported for control plane machines",
1055+
},
1056+
{
1057+
name: "control plane machine with dedicated host and ID should fail",
1058+
platformType: osconfigv1.AWSPlatformType,
1059+
clusterID: "aws-cluster",
1060+
isMasterMachine: true,
1061+
providerSpecValue: &kruntime.RawExtension{
1062+
Object: &machinev1beta1.AWSMachineProviderConfig{
1063+
AMI: machinev1beta1.AWSResourceReference{ID: ptr.To[string]("ami")},
1064+
InstanceType: "test",
1065+
Placement: machinev1beta1.Placement{
1066+
Tenancy: machinev1beta1.HostTenancy,
1067+
Host: &machinev1beta1.HostPlacement{
1068+
Affinity: ptr.To(machinev1beta1.HostAffinityDedicatedHost),
1069+
DedicatedHost: &machinev1beta1.DedicatedHost{
1070+
ID: "h-1234567890abcdef0",
1071+
},
1072+
},
1073+
},
1074+
},
1075+
},
1076+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: spec.placement.tenancy: Forbidden: dedicated host tenancy is not supported for control plane machines",
1077+
},
1078+
{
1079+
name: "worker machine with dedicated host should succeed",
1080+
platformType: osconfigv1.AWSPlatformType,
1081+
clusterID: "aws-cluster",
1082+
isMasterMachine: false,
1083+
providerSpecValue: &kruntime.RawExtension{
1084+
Object: &machinev1beta1.AWSMachineProviderConfig{
1085+
AMI: machinev1beta1.AWSResourceReference{ID: ptr.To[string]("ami")},
1086+
InstanceType: "test",
1087+
Placement: machinev1beta1.Placement{
1088+
Tenancy: machinev1beta1.HostTenancy,
1089+
Host: &machinev1beta1.HostPlacement{
1090+
Affinity: ptr.To(machinev1beta1.HostAffinityAnyAvailable),
1091+
},
1092+
},
1093+
},
1094+
},
1095+
expectedError: "",
1096+
},
10361097
{
10371098
name: "with VolumeType set to gp3 and Throughput set under minium value",
10381099
platformType: osconfigv1.AWSPlatformType,
@@ -2243,6 +2304,13 @@ func TestMachineCreation(t *testing.T) {
22432304
m.Labels = make(map[string]string)
22442305
m.Labels[machinev1beta1.MachineClusterIDLabel] = presetClusterID
22452306
}
2307+
if tc.isMasterMachine {
2308+
if m.Labels == nil {
2309+
m.Labels = make(map[string]string)
2310+
}
2311+
m.Labels[machineRoleLabel] = "master"
2312+
m.Labels[machineTypeLabel] = "master"
2313+
}
22462314

22472315
err = c.Create(ctx, m)
22482316
if err == nil {

0 commit comments

Comments
 (0)