Skip to content

Commit 592e975

Browse files
committed
update v1beta2 with CapacityReservationPreference
update all the API types that map to EC2 RunInstance operations, allowing for the specification of CapacityReservationPreference as well as validations to ensure that these values play well with AWS specifications for use of CapacityReservationPreference with CapacityReservationID
1 parent 49f7c86 commit 592e975

20 files changed

+234
-13
lines changed

api/v1beta1/awscluster_conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func (src *AWSCluster) ConvertTo(dstRaw conversion.Hub) error {
6363
dst.Status.Bastion.NetworkInterfaceType = restored.Status.Bastion.NetworkInterfaceType
6464
dst.Status.Bastion.CapacityReservationID = restored.Status.Bastion.CapacityReservationID
6565
dst.Status.Bastion.MarketType = restored.Status.Bastion.MarketType
66+
dst.Status.Bastion.CapacityReservationPreference = restored.Status.Bastion.CapacityReservationPreference
6667
}
6768
dst.Spec.Partition = restored.Spec.Partition
6869

api/v1beta1/awsmachine_conversion.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func (src *AWSMachine) ConvertTo(dstRaw conversion.Hub) error {
4444
dst.Spec.SecurityGroupOverrides = restored.Spec.SecurityGroupOverrides
4545
dst.Spec.CapacityReservationID = restored.Spec.CapacityReservationID
4646
dst.Spec.MarketType = restored.Spec.MarketType
47+
dst.Spec.CapacityReservationPreference = restored.Spec.CapacityReservationPreference
4748
dst.Spec.NetworkInterfaceType = restored.Spec.NetworkInterfaceType
4849
if restored.Spec.ElasticIPPool != nil {
4950
if dst.Spec.ElasticIPPool == nil {
@@ -108,6 +109,7 @@ func (r *AWSMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
108109
dst.Spec.Template.Spec.SecurityGroupOverrides = restored.Spec.Template.Spec.SecurityGroupOverrides
109110
dst.Spec.Template.Spec.CapacityReservationID = restored.Spec.Template.Spec.CapacityReservationID
110111
dst.Spec.Template.Spec.MarketType = restored.Spec.Template.Spec.MarketType
112+
dst.Spec.Template.Spec.CapacityReservationPreference = restored.Spec.Template.Spec.CapacityReservationPreference
111113
dst.Spec.Template.Spec.NetworkInterfaceType = restored.Spec.Template.Spec.NetworkInterfaceType
112114
if restored.Spec.Template.Spec.ElasticIPPool != nil {
113115
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ type AWSMachineSpec struct {
233233
// If marketType is not specified and spotMarketOptions is provided, the marketType defaults to "Spot".
234234
// +optional
235235
MarketType MarketType `json:"marketType,omitempty"`
236+
237+
// CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
238+
// "Open" (default): The instance may make make use of open Capacity Reservations that match its AZ and InstanceType
239+
// "None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
240+
// "CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
241+
// +optional
242+
CapacityReservationPreference CapacityReservationPreference `json:"capacityReservationPreference,omitempty"`
236243
}
237244

238245
// 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
@@ -78,6 +78,7 @@ func (*awsMachineWebhook) ValidateCreate(_ context.Context, obj runtime.Object)
7878
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
7979
allErrs = append(allErrs, r.validateNetworkElasticIPPool()...)
8080
allErrs = append(allErrs, r.validateInstanceMarketType()...)
81+
allErrs = append(allErrs, r.validateCapacityReservation()...)
8182

8283
return nil, aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
8384
}
@@ -378,6 +379,14 @@ func (r *AWSMachine) validateNetworkElasticIPPool() field.ErrorList {
378379
return allErrs
379380
}
380381

382+
func (r *AWSMachine) validateCapacityReservation() field.ErrorList {
383+
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"), "when a reservation ID is specified, capacityReservationPreference may only be `capacity-reservations-only` or empty"))
386+
}
387+
return allErrs
388+
}
389+
381390
func (r *AWSMachine) validateInstanceMarketType() field.ErrorList {
382391
var allErrs field.ErrorList
383392
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,31 @@ type Instance struct {
273273
// If marketType is not specified and spotMarketOptions is provided, the marketType defaults to "Spot".
274274
// +optional
275275
MarketType MarketType `json:"marketType,omitempty"`
276+
277+
// CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
278+
// "Open" (default): The instance may make make use of open Capacity Reservations that match its AZ and InstanceType
279+
// "None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
280+
// "CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
281+
// +optional
282+
CapacityReservationPreference CapacityReservationPreference `json:"capacityReservationPreference,omitempty"`
276283
}
277284

285+
// CapacityReservationPreference describes the preferred use of capacity reservations
286+
// of an instance
287+
// +kubebuilder:validation:Enum:=None;CapacityReservationsOnly;Open
288+
type CapacityReservationPreference string
289+
290+
const (
291+
// CapacityReservationPreferenceNone is a CapacityReservationPreference enum value
292+
CapacityReservationPreferenceNone CapacityReservationPreference = "None"
293+
294+
// CapacityReservationPreferenceOnly is a CapacityReservationPreference enum value
295+
CapacityReservationPreferenceOnly CapacityReservationPreference = "CapacityReservationsOnly"
296+
297+
// CapacityReservationPreferenceOpen is a CapacityReservationPreference enum value
298+
CapacityReservationPreferenceOpen CapacityReservationPreference = "Open"
299+
)
300+
278301
// MarketType describes the market type of an Instance
279302
// +kubebuilder:validation:Enum:=OnDemand;Spot;CapacityBlock
280303
type MarketType string

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,17 @@ spec:
12141214
description: CapacityReservationID specifies the target Capacity
12151215
Reservation into which the instance should be launched.
12161216
type: string
1217+
capacityReservationPreference:
1218+
description: |-
1219+
CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
1220+
"Open" (default): The instance may make make use of open Capacity Reservations that match its AZ and InstanceType
1221+
"None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
1222+
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
1223+
enum:
1224+
- None
1225+
- CapacityReservationsOnly
1226+
- Open
1227+
type: string
12171228
ebsOptimized:
12181229
description: Indicates whether the instance is optimized for Amazon
12191230
EBS I/O.
@@ -3395,6 +3406,17 @@ spec:
33953406
description: CapacityReservationID specifies the target Capacity
33963407
Reservation into which the instance should be launched.
33973408
type: string
3409+
capacityReservationPreference:
3410+
description: |-
3411+
CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
3412+
"Open" (default): The instance may make make use of open Capacity Reservations that match its AZ and InstanceType
3413+
"None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
3414+
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
3415+
enum:
3416+
- None
3417+
- CapacityReservationsOnly
3418+
- Open
3419+
type: string
33983420
ebsOptimized:
33993421
description: Indicates whether the instance is optimized for Amazon
34003422
EBS I/O.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,6 +2197,17 @@ spec:
21972197
description: CapacityReservationID specifies the target Capacity
21982198
Reservation into which the instance should be launched.
21992199
type: string
2200+
capacityReservationPreference:
2201+
description: |-
2202+
CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
2203+
"Open" (default): The instance may make make use of open Capacity Reservations that match its AZ and InstanceType
2204+
"None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
2205+
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
2206+
enum:
2207+
- None
2208+
- CapacityReservationsOnly
2209+
- Open
2210+
type: string
22002211
ebsOptimized:
22012212
description: Indicates whether the instance is optimized for Amazon
22022213
EBS I/O.

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

0 commit comments

Comments
 (0)