Skip to content

Commit 456f00d

Browse files
authored
Merge pull request #5609 from BraeTroutman/capacity-reservation-preferences
✨ Capacity reservation preferences
2 parents 5f62ff6 + 108a81f commit 456f00d

23 files changed

+544
-88
lines changed

api/v1beta1/awscluster_conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (src *AWSCluster) ConvertTo(dstRaw conversion.Hub) error {
6565
dst.Status.Bastion.MarketType = restored.Status.Bastion.MarketType
6666
dst.Status.Bastion.HostAffinity = restored.Status.Bastion.HostAffinity
6767
dst.Status.Bastion.HostID = restored.Status.Bastion.HostID
68+
dst.Status.Bastion.CapacityReservationPreference = restored.Status.Bastion.CapacityReservationPreference
6869
}
6970
dst.Spec.Partition = restored.Spec.Partition
7071

api/v1beta1/awsmachine_conversion.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (src *AWSMachine) ConvertTo(dstRaw conversion.Hub) error {
4646
dst.Spec.MarketType = restored.Spec.MarketType
4747
dst.Spec.HostID = restored.Spec.HostID
4848
dst.Spec.HostAffinity = restored.Spec.HostAffinity
49+
dst.Spec.CapacityReservationPreference = restored.Spec.CapacityReservationPreference
4950
dst.Spec.NetworkInterfaceType = restored.Spec.NetworkInterfaceType
5051
if restored.Spec.ElasticIPPool != nil {
5152
if dst.Spec.ElasticIPPool == nil {
@@ -112,6 +113,7 @@ func (r *AWSMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
112113
dst.Spec.Template.Spec.MarketType = restored.Spec.Template.Spec.MarketType
113114
dst.Spec.Template.Spec.HostID = restored.Spec.Template.Spec.HostID
114115
dst.Spec.Template.Spec.HostAffinity = restored.Spec.Template.Spec.HostAffinity
116+
dst.Spec.Template.Spec.CapacityReservationPreference = restored.Spec.Template.Spec.CapacityReservationPreference
115117
dst.Spec.Template.Spec.NetworkInterfaceType = restored.Spec.Template.Spec.NetworkInterfaceType
116118
if restored.Spec.Template.Spec.ElasticIPPool != nil {
117119
if dst.Spec.Template.Spec.ElasticIPPool == nil {

api/v1beta1/zz_generated.conversion.go

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

api/v1beta2/awsmachine_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ type AWSMachineSpec struct {
245245
// +optional
246246
// +kubebuilder:validation:Enum:=default;host
247247
HostAffinity *string `json:"hostAffinity,omitempty"`
248+
249+
// CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
250+
// "Open": The instance may make use of open Capacity Reservations that match its AZ and InstanceType
251+
// "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
253+
// +kubebuilder:validation:Enum="";None;CapacityReservationsOnly;Open
254+
// +optional
255+
CapacityReservationPreference CapacityReservationPreference `json:"capacityReservationPreference,omitempty"`
248256
}
249257

250258
// CloudInit defines options related to the bootstrapping systems where

api/v1beta2/awsmachine_webhook.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func (*awsMachineWebhook) ValidateCreate(_ context.Context, obj runtime.Object)
7979
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
8080
allErrs = append(allErrs, r.validateNetworkElasticIPPool()...)
8181
allErrs = append(allErrs, r.validateInstanceMarketType()...)
82+
allErrs = append(allErrs, r.validateCapacityReservation()...)
8283

8384
return nil, aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
8485
}
@@ -380,6 +381,14 @@ func (r *AWSMachine) validateNetworkElasticIPPool() field.ErrorList {
380381
return allErrs
381382
}
382383

384+
func (r *AWSMachine) validateCapacityReservation() field.ErrorList {
385+
var allErrs field.ErrorList
386+
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"))
388+
}
389+
return allErrs
390+
}
391+
383392
func (r *AWSMachine) validateInstanceMarketType() field.ErrorList {
384393
var allErrs field.ErrorList
385394
if r.Spec.MarketType == MarketTypeCapacityBlock && r.Spec.SpotMarketOptions != nil {

api/v1beta2/awsmachine_webhook_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,38 @@ func TestAWSMachineCreate(t *testing.T) {
261261
},
262262
wantErr: false,
263263
},
264+
{
265+
name: "invalid case, CapacityReservationId is set and CapacityReservationPreference is not `capacity-reservation-only`",
266+
machine: &AWSMachine{
267+
Spec: AWSMachineSpec{
268+
InstanceType: "test",
269+
CapacityReservationID: aws.String("cr-12345678901234567"),
270+
CapacityReservationPreference: CapacityReservationPreferenceNone,
271+
},
272+
},
273+
wantErr: true,
274+
},
275+
{
276+
name: "valid CapacityReservationId is set and CapacityReservationPreference is not specified",
277+
machine: &AWSMachine{
278+
Spec: AWSMachineSpec{
279+
InstanceType: "test",
280+
CapacityReservationID: aws.String("cr-12345678901234567"),
281+
},
282+
},
283+
wantErr: false,
284+
},
285+
{
286+
name: "valid CapacityReservationId is set and CapacityReservationPreference is `capacity-reservation-only`",
287+
machine: &AWSMachine{
288+
Spec: AWSMachineSpec{
289+
InstanceType: "test",
290+
CapacityReservationID: aws.String("cr-12345678901234567"),
291+
CapacityReservationPreference: CapacityReservationPreferenceOnly,
292+
},
293+
},
294+
wantErr: false,
295+
},
264296
{
265297
name: "empty instance type not allowed",
266298
machine: &AWSMachine{

api/v1beta2/types.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,32 @@ type Instance struct {
285285
// HostID specifies the dedicated host on which the instance should be started.
286286
// +optional
287287
HostID *string `json:"hostID,omitempty"`
288+
289+
// CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
290+
// "Open": The instance may make use of open Capacity Reservations that match its AZ and InstanceType
291+
// "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
293+
// +kubebuilder:validation:Enum="";None;CapacityReservationsOnly;Open
294+
// +optional
295+
CapacityReservationPreference CapacityReservationPreference `json:"capacityReservationPreference,omitempty"`
288296
}
289297

298+
// CapacityReservationPreference describes the preferred use of capacity reservations
299+
// of an instance
300+
// +kubebuilder:validation:Enum:="";None;CapacityReservationsOnly;Open
301+
type CapacityReservationPreference string
302+
303+
const (
304+
// CapacityReservationPreferenceNone the instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
305+
CapacityReservationPreferenceNone CapacityReservationPreference = "None"
306+
307+
// CapacityReservationPreferenceOnly the instance will only run if matched or targeted to a Capacity Reservation
308+
CapacityReservationPreferenceOnly CapacityReservationPreference = "CapacityReservationsOnly"
309+
310+
// CapacityReservationPreferenceOpen the instance may make use of open Capacity Reservations that match its AZ and InstanceType.
311+
CapacityReservationPreferenceOpen CapacityReservationPreference = "Open"
312+
)
313+
290314
// MarketType describes the market type of an Instance
291315
// +kubebuilder:validation:Enum:=OnDemand;Spot;CapacityBlock
292316
type MarketType string

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,24 @@ spec:
12141214
description: CapacityReservationID specifies the target Capacity
12151215
Reservation into which the instance should be launched.
12161216
type: string
1217+
capacityReservationPreference:
1218+
allOf:
1219+
- enum:
1220+
- ""
1221+
- None
1222+
- CapacityReservationsOnly
1223+
- Open
1224+
- enum:
1225+
- ""
1226+
- None
1227+
- CapacityReservationsOnly
1228+
- Open
1229+
description: |-
1230+
CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
1231+
"Open": The instance may make use of open Capacity Reservations that match its AZ and InstanceType
1232+
"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
1234+
type: string
12171235
ebsOptimized:
12181236
description: Indicates whether the instance is optimized for Amazon
12191237
EBS I/O.
@@ -3410,6 +3428,24 @@ spec:
34103428
description: CapacityReservationID specifies the target Capacity
34113429
Reservation into which the instance should be launched.
34123430
type: string
3431+
capacityReservationPreference:
3432+
allOf:
3433+
- enum:
3434+
- ""
3435+
- None
3436+
- CapacityReservationsOnly
3437+
- Open
3438+
- enum:
3439+
- ""
3440+
- None
3441+
- CapacityReservationsOnly
3442+
- Open
3443+
description: |-
3444+
CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
3445+
"Open": The instance may make use of open Capacity Reservations that match its AZ and InstanceType
3446+
"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
3448+
type: string
34133449
ebsOptimized:
34143450
description: Indicates whether the instance is optimized for Amazon
34153451
EBS I/O.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,24 @@ spec:
21972197
description: CapacityReservationID specifies the target Capacity
21982198
Reservation into which the instance should be launched.
21992199
type: string
2200+
capacityReservationPreference:
2201+
allOf:
2202+
- enum:
2203+
- ""
2204+
- None
2205+
- CapacityReservationsOnly
2206+
- Open
2207+
- enum:
2208+
- ""
2209+
- None
2210+
- CapacityReservationsOnly
2211+
- Open
2212+
description: |-
2213+
CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
2214+
"Open": The instance may make use of open Capacity Reservations that match its AZ and InstanceType
2215+
"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
2217+
type: string
22002218
ebsOptimized:
22012219
description: Indicates whether the instance is optimized for Amazon
22022220
EBS I/O.

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

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

0 commit comments

Comments
 (0)