Skip to content

Commit dcfaef0

Browse files
authored
Merge pull request #5633 from BraeTroutman/OCPBUGS-60943
🐛 Add validation to prevent Spot instances with CapacityReservationsOnly preference
2 parents 8ab8731 + fa170d4 commit dcfaef0

8 files changed

+40
-10
lines changed

api/v1beta2/awsmachine_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ type AWSMachineSpec struct {
249249
// CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
250250
// "Open": The instance may make use of open Capacity Reservations that match its AZ and InstanceType
251251
// "None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
252-
// "CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
252+
// "CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation. Note that this is incompatible with a MarketType of `Spot`
253253
// +kubebuilder:validation:Enum="";None;CapacityReservationsOnly;Open
254254
// +optional
255255
CapacityReservationPreference CapacityReservationPreference `json:"capacityReservationPreference,omitempty"`

api/v1beta2/awsmachine_webhook.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,13 @@ func (r *AWSMachine) validateNetworkElasticIPPool() field.ErrorList {
384384
func (r *AWSMachine) validateCapacityReservation() field.ErrorList {
385385
var allErrs field.ErrorList
386386
if r.Spec.CapacityReservationID != nil && r.Spec.CapacityReservationPreference != CapacityReservationPreferenceOnly && r.Spec.CapacityReservationPreference != "" {
387-
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "capacityReservationPreference"), "when a reservation ID is specified, capacityReservationPreference may only be `capacity-reservations-only` or empty"))
387+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "capacityReservationPreference"), "when a reservation ID is specified, capacityReservationPreference may only be 'CapacityReservationsOnly' or empty"))
388+
}
389+
if r.Spec.CapacityReservationPreference == CapacityReservationPreferenceOnly && r.Spec.MarketType == MarketTypeSpot {
390+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "capacityReservationPreference"), "when MarketType is set to 'Spot', capacityReservationPreference cannot be set to 'CapacityReservationsOnly'"))
391+
}
392+
if r.Spec.CapacityReservationPreference == CapacityReservationPreferenceOnly && r.Spec.SpotMarketOptions != nil {
393+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "capacityReservationPreference"), "when capacityReservationPreference is 'CapacityReservationsOnly', SpotMarketOptions cannot be set (which implies MarketType: 'Spot')"))
388394
}
389395
return allErrs
390396
}

api/v1beta2/awsmachine_webhook_test.go

Lines changed: 25 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,30 @@ func TestAWSMachineCreate(t *testing.T) {
293293
},
294294
wantErr: false,
295295
},
296+
{
297+
name: "invalid CapacityReservationPreference is `CapacityReservationsOnly` 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+
},
308+
{
309+
name: "invalid CapacityReservationPreference is `CapacityReservationsOnly` and SpotMarketOptions is non-nil",
310+
machine: &AWSMachine{
311+
Spec: AWSMachineSpec{
312+
InstanceType: "test",
313+
CapacityReservationID: aws.String("cr-12345678901234567"),
314+
CapacityReservationPreference: CapacityReservationPreferenceOnly,
315+
SpotMarketOptions: &SpotMarketOptions{},
316+
},
317+
},
318+
wantErr: true,
319+
},
296320
{
297321
name: "empty instance type not allowed",
298322
machine: &AWSMachine{

api/v1beta2/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ type Instance struct {
289289
// CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
290290
// "Open": The instance may make use of open Capacity Reservations that match its AZ and InstanceType
291291
// "None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
292-
// "CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
292+
// "CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation. Note that this is incompatible with a MarketType of `Spot`
293293
// +kubebuilder:validation:Enum="";None;CapacityReservationsOnly;Open
294294
// +optional
295295
CapacityReservationPreference CapacityReservationPreference `json:"capacityReservationPreference,omitempty"`
@@ -304,7 +304,7 @@ const (
304304
// CapacityReservationPreferenceNone the instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
305305
CapacityReservationPreferenceNone CapacityReservationPreference = "None"
306306

307-
// CapacityReservationPreferenceOnly the instance will only run if matched or targeted to a Capacity Reservation
307+
// CapacityReservationPreferenceOnly the instance will only run if matched or targeted to a Capacity Reservation. Note that this is incompatible with a MarketType of `Spot`
308308
CapacityReservationPreferenceOnly CapacityReservationPreference = "CapacityReservationsOnly"
309309

310310
// CapacityReservationPreferenceOpen the instance may make use of open Capacity Reservations that match its AZ and InstanceType.

config/crd/bases/controlplane.cluster.x-k8s.io_awsmanagedcontrolplanes.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ spec:
12301230
CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
12311231
"Open": The instance may make use of open Capacity Reservations that match its AZ and InstanceType
12321232
"None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
1233-
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
1233+
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation. Note that this is incompatible with a MarketType of `Spot`
12341234
type: string
12351235
ebsOptimized:
12361236
description: Indicates whether the instance is optimized for Amazon
@@ -3444,7 +3444,7 @@ spec:
34443444
CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
34453445
"Open": The instance may make use of open Capacity Reservations that match its AZ and InstanceType
34463446
"None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
3447-
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
3447+
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation. Note that this is incompatible with a MarketType of `Spot`
34483448
type: string
34493449
ebsOptimized:
34503450
description: Indicates whether the instance is optimized for Amazon

config/crd/bases/infrastructure.cluster.x-k8s.io_awsclusters.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2213,7 +2213,7 @@ spec:
22132213
CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
22142214
"Open": The instance may make use of open Capacity Reservations that match its AZ and InstanceType
22152215
"None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
2216-
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
2216+
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation. Note that this is incompatible with a MarketType of `Spot`
22172217
type: string
22182218
ebsOptimized:
22192219
description: Indicates whether the instance is optimized for Amazon

config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachines.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ spec:
657657
CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
658658
"Open": The instance may make use of open Capacity Reservations that match its AZ and InstanceType
659659
"None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
660-
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
660+
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation. Note that this is incompatible with a MarketType of `Spot`
661661
type: string
662662
cloudInit:
663663
description: |-

config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachinetemplates.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ spec:
576576
CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
577577
"Open": The instance may make use of open Capacity Reservations that match its AZ and InstanceType
578578
"None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
579-
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
579+
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation. Note that this is incompatible with a MarketType of `Spot`
580580
type: string
581581
cloudInit:
582582
description: |-

0 commit comments

Comments
 (0)