Skip to content

Commit 57f828a

Browse files
Merge pull request #1329 from athiruma/validate_capacity_blocks
OCPCLOUD-2780: Validate aws marketType webhook
2 parents 709f4c3 + 902ba24 commit 57f828a

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

pkg/webhooks/machine_webhook.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,12 @@ func validateAWS(m *machinev1beta1.Machine, config *admissionConfig) (bool, []st
782782
}
783783
}
784784

785+
if providerSpec.MarketType != "" {
786+
if err := validateAWSInstanceMarketType(providerSpec); err != nil {
787+
errs = append(errs, field.Invalid(field.NewPath("providerSpec", "marketType"), providerSpec.MarketType, err.Error()))
788+
}
789+
}
790+
785791
// TODO(alberto): Validate providerSpec.BlockDevices.
786792
// https://github.com/openshift/cluster-api-provider-aws/pull/299#discussion_r433920532
787793

@@ -2412,3 +2418,16 @@ func validateAwsCapacityReservationId(capacityReservationId string) error {
24122418
}
24132419
return nil
24142420
}
2421+
2422+
func validateAWSInstanceMarketType(providerSpec *machinev1beta1.AWSMachineProviderConfig) error {
2423+
if providerSpec.MarketType == machinev1beta1.MarketTypeCapacityBlock && providerSpec.SpotMarketOptions != nil {
2424+
return errors.New("invalid marketType: marketType set to CapacityBlock and spotMarketOptions cannot be used together")
2425+
}
2426+
if providerSpec.MarketType == machinev1beta1.MarketTypeOnDemand && providerSpec.SpotMarketOptions != nil {
2427+
return errors.New("invalid marketType: setting marketType to OnDemand and spotMarketOptions cannot be used together")
2428+
}
2429+
if providerSpec.MarketType == machinev1beta1.MarketTypeCapacityBlock && len(providerSpec.CapacityReservationID) == 0 {
2430+
return errors.New("capacityReservationID is required when CapacityBlock is provided")
2431+
}
2432+
return nil
2433+
}

pkg/webhooks/machine_webhook_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,66 @@ func TestMachineCreation(t *testing.T) {
198198
},
199199
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: providerSpec.capacityReservationId: Invalid value: \"cr-123\": invalid value for capacityReservationId: \"cr-123\", it must start with 'cr-' and be exactly 20 characters long with 17 hexadecimal characters",
200200
},
201+
{
202+
name: "with MarketType set to MarketTypeCapacityBlock and spotMarketOptions are specified",
203+
platformType: osconfigv1.AWSPlatformType,
204+
clusterID: "aws-cluster",
205+
providerSpecValue: &kruntime.RawExtension{
206+
Object: &machinev1beta1.AWSMachineProviderConfig{
207+
AMI: machinev1beta1.AWSResourceReference{
208+
ID: ptr.To[string]("ami"),
209+
},
210+
CapacityReservationID: "cr-12345678901234567",
211+
MarketType: machinev1beta1.MarketTypeCapacityBlock,
212+
SpotMarketOptions: &machinev1beta1.SpotMarketOptions{},
213+
},
214+
},
215+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: providerSpec.marketType: Invalid value: \"CapacityBlock\": invalid marketType: marketType set to CapacityBlock and spotMarketOptions cannot be used together",
216+
},
217+
{
218+
name: "with MarketType set to MarketTypeOnDemand and spotMarketOptions are specified",
219+
platformType: osconfigv1.AWSPlatformType,
220+
clusterID: "aws-cluster",
221+
providerSpecValue: &kruntime.RawExtension{
222+
Object: &machinev1beta1.AWSMachineProviderConfig{
223+
AMI: machinev1beta1.AWSResourceReference{
224+
ID: ptr.To[string]("ami"),
225+
},
226+
MarketType: machinev1beta1.MarketTypeOnDemand,
227+
SpotMarketOptions: &machinev1beta1.SpotMarketOptions{},
228+
},
229+
},
230+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: providerSpec.marketType: Invalid value: \"OnDemand\": invalid marketType: setting marketType to OnDemand and spotMarketOptions cannot be used together",
231+
},
232+
{
233+
name: "with MarketType set to MarketTypeCapacityBlock is specified and CapacityReservationId is not provided",
234+
platformType: osconfigv1.AWSPlatformType,
235+
clusterID: "aws-cluster",
236+
providerSpecValue: &kruntime.RawExtension{
237+
Object: &machinev1beta1.AWSMachineProviderConfig{
238+
AMI: machinev1beta1.AWSResourceReference{
239+
ID: ptr.To[string]("ami"),
240+
},
241+
MarketType: machinev1beta1.MarketTypeCapacityBlock,
242+
},
243+
},
244+
expectedError: "admission webhook \"validation.machine.machine.openshift.io\" denied the request: providerSpec.marketType: Invalid value: \"CapacityBlock\": capacityReservationID is required when CapacityBlock is provided",
245+
},
246+
{
247+
name: "with MarketType set to MarketTypeCapacityBlock and CapacityReservationId are provided",
248+
platformType: osconfigv1.AWSPlatformType,
249+
clusterID: "aws-cluster",
250+
providerSpecValue: &kruntime.RawExtension{
251+
Object: &machinev1beta1.AWSMachineProviderConfig{
252+
AMI: machinev1beta1.AWSResourceReference{
253+
ID: ptr.To[string]("ami"),
254+
},
255+
CapacityReservationID: "cr-12345678901234567",
256+
MarketType: machinev1beta1.MarketTypeCapacityBlock,
257+
},
258+
},
259+
expectedError: "",
260+
},
201261
{
202262
name: "with Azure and a nil provider spec value",
203263
platformType: osconfigv1.AzurePlatformType,

0 commit comments

Comments
 (0)