Skip to content

Commit e959540

Browse files
Merge pull request #1265 from chiragkyal/add-validation
CFE-1065: Add range validation for placementGroupPartition
2 parents 456f651 + 5e77ccd commit e959540

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

pkg/webhooks/machine_webhook.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,19 @@ func validateAWS(m *machinev1beta1.Machine, config *admissionConfig) (bool, []st
746746
)
747747
}
748748

749+
// placementGroupPartition must be between 1 and 7 if it's not 0 (default).
750+
if providerSpec.PlacementGroupPartition < 0 || providerSpec.PlacementGroupPartition > 7 {
751+
errs = append(
752+
errs,
753+
field.Invalid(
754+
field.NewPath("providerSpec", "placementGroupPartition"),
755+
providerSpec.PlacementGroupPartition,
756+
"placementGroupPartition must be between 1 and 7",
757+
),
758+
)
759+
}
760+
761+
// If placementGroupPartition is not 0 (default), placementGroupName must be set.
749762
if providerSpec.PlacementGroupName == "" && providerSpec.PlacementGroupPartition != 0 {
750763
errs = append(
751764
errs,

pkg/webhooks/machine_webhook_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,24 @@ func TestValidateAWSProviderSpec(t *testing.T) {
21422142
expectedOk: false,
21432143
expectedError: "providerSpec.placementGroupPartition: Invalid value: 2: providerSpec.placementGroupPartition is set but providerSpec.placementGroupName is empty",
21442144
},
2145+
{
2146+
testCase: "fail if placementGroupPartition is outside 1-7 range (lower)",
2147+
modifySpec: func(p *machinev1beta1.AWSMachineProviderConfig) {
2148+
p.PlacementGroupName = "placement-group"
2149+
p.PlacementGroupPartition = -1
2150+
},
2151+
expectedOk: false,
2152+
expectedError: "providerSpec.placementGroupPartition: Invalid value: -1: placementGroupPartition must be between 1 and 7",
2153+
},
2154+
{
2155+
testCase: "fail if placementGroupPartition is outside 1-7 range (upper)",
2156+
modifySpec: func(p *machinev1beta1.AWSMachineProviderConfig) {
2157+
p.PlacementGroupName = "placement-group"
2158+
p.PlacementGroupPartition = 8
2159+
},
2160+
expectedOk: false,
2161+
expectedError: "providerSpec.placementGroupPartition: Invalid value: 8: placementGroupPartition must be between 1 and 7",
2162+
},
21452163
{
21462164
testCase: "allow if only placementGroupName is set",
21472165
modifySpec: func(p *machinev1beta1.AWSMachineProviderConfig) {
@@ -2150,7 +2168,7 @@ func TestValidateAWSProviderSpec(t *testing.T) {
21502168
expectedOk: true,
21512169
},
21522170
{
2153-
testCase: "allow if both placementGroupName and placementGroupPartition are set",
2171+
testCase: "allow if correct placementGroupName and placementGroupPartition are set",
21542172
modifySpec: func(p *machinev1beta1.AWSMachineProviderConfig) {
21552173
p.PlacementGroupName = "placement-group"
21562174
p.PlacementGroupPartition = 2

0 commit comments

Comments
 (0)