Skip to content

Commit

Permalink
CFE-1065: Add range validation for placementGroupPartition
Browse files Browse the repository at this point in the history
Signed-off-by: chiragkyal <ckyal@redhat.com>
  • Loading branch information
chiragkyal committed Jul 2, 2024
1 parent 456f651 commit 5e77ccd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pkg/webhooks/machine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,19 @@ func validateAWS(m *machinev1beta1.Machine, config *admissionConfig) (bool, []st
)
}

// placementGroupPartition must be between 1 and 7 if it's not 0 (default).
if providerSpec.PlacementGroupPartition < 0 || providerSpec.PlacementGroupPartition > 7 {
errs = append(
errs,
field.Invalid(
field.NewPath("providerSpec", "placementGroupPartition"),
providerSpec.PlacementGroupPartition,
"placementGroupPartition must be between 1 and 7",
),
)
}

// If placementGroupPartition is not 0 (default), placementGroupName must be set.
if providerSpec.PlacementGroupName == "" && providerSpec.PlacementGroupPartition != 0 {
errs = append(
errs,
Expand Down
20 changes: 19 additions & 1 deletion pkg/webhooks/machine_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,24 @@ func TestValidateAWSProviderSpec(t *testing.T) {
expectedOk: false,
expectedError: "providerSpec.placementGroupPartition: Invalid value: 2: providerSpec.placementGroupPartition is set but providerSpec.placementGroupName is empty",
},
{
testCase: "fail if placementGroupPartition is outside 1-7 range (lower)",
modifySpec: func(p *machinev1beta1.AWSMachineProviderConfig) {
p.PlacementGroupName = "placement-group"
p.PlacementGroupPartition = -1
},
expectedOk: false,
expectedError: "providerSpec.placementGroupPartition: Invalid value: -1: placementGroupPartition must be between 1 and 7",
},
{
testCase: "fail if placementGroupPartition is outside 1-7 range (upper)",
modifySpec: func(p *machinev1beta1.AWSMachineProviderConfig) {
p.PlacementGroupName = "placement-group"
p.PlacementGroupPartition = 8
},
expectedOk: false,
expectedError: "providerSpec.placementGroupPartition: Invalid value: 8: placementGroupPartition must be between 1 and 7",
},
{
testCase: "allow if only placementGroupName is set",
modifySpec: func(p *machinev1beta1.AWSMachineProviderConfig) {
Expand All @@ -2150,7 +2168,7 @@ func TestValidateAWSProviderSpec(t *testing.T) {
expectedOk: true,
},
{
testCase: "allow if both placementGroupName and placementGroupPartition are set",
testCase: "allow if correct placementGroupName and placementGroupPartition are set",
modifySpec: func(p *machinev1beta1.AWSMachineProviderConfig) {
p.PlacementGroupName = "placement-group"
p.PlacementGroupPartition = 2
Expand Down

0 comments on commit 5e77ccd

Please sign in to comment.