Skip to content

Commit d83d106

Browse files
committed
update exp apis with preference fields
1 parent 4c2802e commit d83d106

8 files changed

+63
-3
lines changed

api/v1beta2/awsmachine_webhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ func (r *AWSMachine) validateNetworkElasticIPPool() field.ErrorList {
381381

382382
func (r *AWSMachine) validateCapacityReservation() field.ErrorList {
383383
var allErrs field.ErrorList
384-
if r.Spec.CapacityReservationID != nil && r.Spec.CapacityReservationPreference != CapacityReservationPreferenceOnly || r.Spec.CapacityReservationPreference == "" {
385-
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "capacityReservationPreference"), "capacityReservationPreference may only be set to capacity-reservations-only when a target reservation is specified"))
384+
if r.Spec.CapacityReservationID != nil && r.Spec.CapacityReservationPreference != CapacityReservationPreferenceOnly && r.Spec.CapacityReservationPreference != "" {
385+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "capacityReservationPreference"), "when a reservation ID is specified, capacityReservationPreference may only be `capacity-reservations-only` or empty"))
386386
}
387387
return allErrs
388388
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,17 @@ spec:
644644
description: CapacityReservationID specifies the target Capacity
645645
Reservation into which the instance should be launched.
646646
type: string
647+
capacityReservationPreference:
648+
description: |-
649+
CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
650+
"Open" (default): The instance may make make use of open Capacity Reservations that match its AZ and InstanceType
651+
"None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
652+
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
653+
enum:
654+
- None
655+
- CapacityReservationsOnly
656+
- Open
657+
type: string
647658
iamInstanceProfile:
648659
description: |-
649660
The name or the Amazon Resource Name (ARN) of the instance profile associated

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,17 @@ spec:
653653
description: CapacityReservationID specifies the target Capacity
654654
Reservation into which the instance should be launched.
655655
type: string
656+
capacityReservationPreference:
657+
description: |-
658+
CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
659+
"Open" (default): The instance may make make use of open Capacity Reservations that match its AZ and InstanceType
660+
"None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
661+
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
662+
enum:
663+
- None
664+
- CapacityReservationsOnly
665+
- Open
666+
type: string
656667
iamInstanceProfile:
657668
description: |-
658669
The name or the Amazon Resource Name (ARN) of the instance profile associated

exp/api/v1beta1/conversion.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ func (src *AWSMachinePool) ConvertTo(dstRaw conversion.Hub) error {
7272
dst.Spec.AWSLaunchTemplate.MarketType = restored.Spec.AWSLaunchTemplate.MarketType
7373
}
7474

75+
if preference := restored.Spec.AWSLaunchTemplate.CapacityReservationPreference; preference != "" {
76+
dst.Spec.AWSLaunchTemplate.CapacityReservationPreference = preference
77+
}
78+
7579
dst.Spec.DefaultInstanceWarmup = restored.Spec.DefaultInstanceWarmup
7680
dst.Spec.AWSLaunchTemplate.NonRootVolumes = restored.Spec.AWSLaunchTemplate.NonRootVolumes
7781
return nil
@@ -130,6 +134,10 @@ func (src *AWSManagedMachinePool) ConvertTo(dstRaw conversion.Hub) error {
130134
if restored.Spec.AWSLaunchTemplate.MarketType != "" {
131135
dst.Spec.AWSLaunchTemplate.MarketType = restored.Spec.AWSLaunchTemplate.MarketType
132136
}
137+
138+
if preference := restored.Spec.AWSLaunchTemplate.CapacityReservationPreference; preference != "" {
139+
dst.Spec.AWSLaunchTemplate.CapacityReservationPreference = preference
140+
}
133141
}
134142
if restored.Spec.AvailabilityZoneSubnetType != nil {
135143
dst.Spec.AvailabilityZoneSubnetType = restored.Spec.AvailabilityZoneSubnetType

exp/api/v1beta1/zz_generated.conversion.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exp/api/v1beta2/awsmachinepool_webhook.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ func (*AWSMachinePoolWebhook) ValidateCreate(_ context.Context, obj runtime.Obje
211211
allErrs = append(allErrs, r.validateSpotInstances()...)
212212
allErrs = append(allErrs, r.validateRefreshPreferences()...)
213213
allErrs = append(allErrs, r.validateInstanceMarketType()...)
214+
allErrs = append(allErrs, r.validateCapacityReservation()...)
214215
allErrs = append(allErrs, r.validateLifecycleHooks()...)
215216
allErrs = append(allErrs, r.validateIgnition()...)
216217

@@ -225,6 +226,16 @@ func (*AWSMachinePoolWebhook) ValidateCreate(_ context.Context, obj runtime.Obje
225226
)
226227
}
227228

229+
func (r *AWSMachinePool) validateCapacityReservation() field.ErrorList {
230+
var allErrs field.ErrorList
231+
if r.Spec.AWSLaunchTemplate.CapacityReservationID != nil &&
232+
r.Spec.AWSLaunchTemplate.CapacityReservationPreference != infrav1.CapacityReservationPreferenceOnly &&
233+
r.Spec.AWSLaunchTemplate.CapacityReservationPreference != "" {
234+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "capacityReservationPreference"), "when a reservation ID is specified, capacityReservationPreference may only be `capacity-reservations-only` or empty"))
235+
}
236+
return allErrs
237+
}
238+
228239
func (r *AWSMachinePool) validateInstanceMarketType() field.ErrorList {
229240
var allErrs field.ErrorList
230241
if r.Spec.AWSLaunchTemplate.MarketType == infrav1.MarketTypeCapacityBlock && r.Spec.AWSLaunchTemplate.SpotMarketOptions != nil {

exp/api/v1beta2/awsmachinepool_webhook_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,18 @@ func TestAWSMachinePoolValidateCreate(t *testing.T) {
303303
},
304304
wantErrToContain: ptr.To("cannot be set to when CapacityReservationID is specified"),
305305
},
306-
306+
{
307+
name: "with CapacityReservationPreference of `none` and CapacityReservationID is specified",
308+
pool: &AWSMachinePool{
309+
Spec: AWSMachinePoolSpec{
310+
AWSLaunchTemplate: AWSLaunchTemplate{
311+
CapacityReservationID: aws.String("cr-123"),
312+
CapacityReservationPreference: infrav1.CapacityReservationPreferenceNone,
313+
},
314+
},
315+
},
316+
wantErrToContain: ptr.To("when a reservation ID is specified, capacityReservationPreference may only be `capacity-reservations-only` or empty"),
317+
},
307318
{
308319
name: "invalid, MarketType set to MarketTypeCapacityBlock and spotMarketOptions are specified",
309320
pool: &AWSMachinePool{

exp/api/v1beta2/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ type AWSLaunchTemplate struct {
146146
// If marketType is not specified and spotMarketOptions is provided, the marketType defaults to "Spot".
147147
// +optional
148148
MarketType infrav1.MarketType `json:"marketType,omitempty"`
149+
150+
// CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
151+
// "Open" (default): The instance may make make use of open Capacity Reservations that match its AZ and InstanceType
152+
// "None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
153+
// "CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
154+
// +optional
155+
CapacityReservationPreference infrav1.CapacityReservationPreference `json:"capacityReservationPreference,omitempty"`
149156
}
150157

151158
// Overrides are used to override the instance type specified by the launch template with multiple

0 commit comments

Comments
 (0)