From 5e77ccd39f21deb80282e6e74a1253f12a75cb8c Mon Sep 17 00:00:00 2001 From: chiragkyal Date: Tue, 2 Jul 2024 12:56:22 +0530 Subject: [PATCH] CFE-1065: Add range validation for placementGroupPartition Signed-off-by: chiragkyal --- pkg/webhooks/machine_webhook.go | 13 +++++++++++++ pkg/webhooks/machine_webhook_test.go | 20 +++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/pkg/webhooks/machine_webhook.go b/pkg/webhooks/machine_webhook.go index 5f371658f8..5d70f8817b 100644 --- a/pkg/webhooks/machine_webhook.go +++ b/pkg/webhooks/machine_webhook.go @@ -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, diff --git a/pkg/webhooks/machine_webhook_test.go b/pkg/webhooks/machine_webhook_test.go index 426e060d34..0279a1a5bd 100644 --- a/pkg/webhooks/machine_webhook_test.go +++ b/pkg/webhooks/machine_webhook_test.go @@ -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) { @@ -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