Skip to content

Commit 4890746

Browse files
committed
Add validation to prevent Spot instances with capacity-reservations-only preference
Adds webhook validation to block conflicting configuration where CapacityReservationPreference is set to 'capacity-reservations-only' while MarketType is 'Spot', as this combination is not supported by AWS.
1 parent 8ab8731 commit 4890746

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

api/v1beta2/awsmachine_webhook.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ func (r *AWSMachine) validateCapacityReservation() field.ErrorList {
386386
if r.Spec.CapacityReservationID != nil && r.Spec.CapacityReservationPreference != CapacityReservationPreferenceOnly && r.Spec.CapacityReservationPreference != "" {
387387
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "capacityReservationPreference"), "when a reservation ID is specified, capacityReservationPreference may only be `capacity-reservations-only` or empty"))
388388
}
389+
if r.Spec.CapacityReservationPreference == CapacityReservationPreferenceOnly && r.Spec.SpotMarketOptions != nil && r.Spec.MarketType == MarketTypeSpot {
390+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "capacityReservationPreference"), "when capacityReservationPreference is 'capacity-reservations-only', MarketType may not be 'Spot'"))
391+
}
389392
return allErrs
390393
}
391394

api/v1beta2/awsmachine_webhook_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func TestAWSMachineCreate(t *testing.T) {
241241
wantErr: true,
242242
},
243243
{
244-
name: "valid MarketType set to MarketTypeCapacityBlock is specified and CapacityReservationId is not provided",
244+
name: "invalid MarketType set to MarketTypeCapacityBlock is specified and CapacityReservationId is not provided",
245245
machine: &AWSMachine{
246246
Spec: AWSMachineSpec{
247247
MarketType: MarketTypeCapacityBlock,
@@ -293,6 +293,18 @@ func TestAWSMachineCreate(t *testing.T) {
293293
},
294294
wantErr: false,
295295
},
296+
{
297+
name: "invalid CapacityReservationPreference is `capacity-reservation-only` and MarketType is `Spot`",
298+
machine: &AWSMachine{
299+
Spec: AWSMachineSpec{
300+
InstanceType: "test",
301+
CapacityReservationID: aws.String("cr-12345678901234567"),
302+
CapacityReservationPreference: CapacityReservationPreferenceOnly,
303+
MarketType: MarketTypeSpot,
304+
},
305+
},
306+
wantErr: true,
307+
},
296308
{
297309
name: "empty instance type not allowed",
298310
machine: &AWSMachine{

0 commit comments

Comments
 (0)