Skip to content

Commit a2d76a7

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 5f62ff6 commit a2d76a7

22 files changed

+488
-49
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ 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" (default): The instance may make 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+
// +optional
254+
CapacityReservationPreference CapacityReservationPreference `json:"capacityReservationPreference,omitempty"`
248255
}
249256

250257
// 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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,31 @@ 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" (default): The instance may make 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+
// +optional
294+
CapacityReservationPreference CapacityReservationPreference `json:"capacityReservationPreference,omitempty"`
288295
}
289296

297+
// CapacityReservationPreference describes the preferred use of capacity reservations
298+
// of an instance
299+
// +kubebuilder:validation:Enum:=None;CapacityReservationsOnly;Open
300+
type CapacityReservationPreference string
301+
302+
const (
303+
// CapacityReservationPreferenceNone is a CapacityReservationPreference enum value
304+
CapacityReservationPreferenceNone CapacityReservationPreference = "None"
305+
306+
// CapacityReservationPreferenceOnly is a CapacityReservationPreference enum value
307+
CapacityReservationPreferenceOnly CapacityReservationPreference = "CapacityReservationsOnly"
308+
309+
// CapacityReservationPreferenceOpen is a CapacityReservationPreference enum value
310+
CapacityReservationPreferenceOpen CapacityReservationPreference = "Open"
311+
)
312+
290313
// MarketType describes the market type of an Instance
291314
// +kubebuilder:validation:Enum:=OnDemand;Spot;CapacityBlock
292315
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.
@@ -3410,6 +3421,17 @@ spec:
34103421
description: CapacityReservationID specifies the target Capacity
34113422
Reservation into which the instance should be launched.
34123423
type: string
3424+
capacityReservationPreference:
3425+
description: |-
3426+
CapacityReservationPreference specifies the preference for use of Capacity Reservations by the instance. Valid values include:
3427+
"Open" (default): The instance may make make use of open Capacity Reservations that match its AZ and InstanceType
3428+
"None": The instance may not make use of any Capacity Reservations. This is to conserve open reservations for desired workloads
3429+
"CapacityReservationsOnly": The instance will only run if matched or targeted to a Capacity Reservation
3430+
enum:
3431+
- None
3432+
- CapacityReservationsOnly
3433+
- Open
3434+
type: string
34133435
ebsOptimized:
34143436
description: Indicates whether the instance is optimized for Amazon
34153437
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)