Skip to content

Commit dc07755

Browse files
committed
Added the CapacityReservation support
Fixed the lint Fixed the description Removed the API in v1
1 parent 3b67f4f commit dc07755

9 files changed

+88
-0
lines changed

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ type AWSMachineSpec struct {
193193
// PrivateDNSName is the options for the instance hostname.
194194
// +optional
195195
PrivateDNSName *PrivateDNSName `json:"privateDnsName,omitempty"`
196+
197+
// CapacityReservationID specifies the instance that should be launched in the
198+
// reserved compute capacity.
199+
// +optional
200+
CapacityReservationID string `json:"capacityReservationId,omitempty"`
196201
}
197202

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

api/v1beta2/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ type Instance struct {
249249
// PublicIPOnLaunch is the option to associate a public IP on instance launch
250250
// +optional
251251
PublicIPOnLaunch *bool `json:"publicIPOnLaunch,omitempty"`
252+
253+
// CapacityReservationID specifies the instance that should be launched in the
254+
// reserved compute capacity.
255+
// +optional
256+
CapacityReservationID string `json:"capacityReservationId,omitempty"`
252257
}
253258

254259
// InstanceMetadataState describes the state of InstanceMetadataOptions.HttpEndpoint and InstanceMetadataOptions.InstanceMetadataTags

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,11 @@ spec:
10901090
availabilityZone:
10911091
description: Availability zone of instance
10921092
type: string
1093+
capacityReservationId:
1094+
description: |-
1095+
CapacityReservationID specifies the instance that should be launched in the
1096+
reserved compute capacity.
1097+
type: string
10931098
ebsOptimized:
10941099
description: Indicates whether the instance is optimized for Amazon
10951100
EBS I/O.
@@ -3094,6 +3099,11 @@ spec:
30943099
availabilityZone:
30953100
description: Availability zone of instance
30963101
type: string
3102+
capacityReservationId:
3103+
description: |-
3104+
CapacityReservationID specifies the instance that should be launched in the
3105+
reserved compute capacity.
3106+
type: string
30973107
ebsOptimized:
30983108
description: Indicates whether the instance is optimized for Amazon
30993109
EBS I/O.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,11 @@ spec:
20752075
availabilityZone:
20762076
description: Availability zone of instance
20772077
type: string
2078+
capacityReservationId:
2079+
description: |-
2080+
CapacityReservationID specifies the instance that should be launched in the
2081+
reserved compute capacity.
2082+
type: string
20782083
ebsOptimized:
20792084
description: Indicates whether the instance is optimized for Amazon
20802085
EBS I/O.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,11 @@ spec:
624624
description: ID of resource
625625
type: string
626626
type: object
627+
capacityReservationId:
628+
description: |-
629+
CapacityReservationID specifies the instance that should be launched in the
630+
reserved compute capacity.
631+
type: string
627632
cloudInit:
628633
description: |-
629634
CloudInit defines options related to the bootstrapping systems where

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,11 @@ spec:
554554
description: ID of resource
555555
type: string
556556
type: object
557+
capacityReservationId:
558+
description: |-
559+
CapacityReservationID specifies the instance that should be launched in the
560+
reserved compute capacity.
561+
type: string
557562
cloudInit:
558563
description: |-
559564
CloudInit defines options related to the bootstrapping systems where

pkg/cloud/services/ec2/instances.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ func (s *Service) CreateInstance(scope *scope.MachineScope, userData []byte, use
251251

252252
input.PrivateDNSName = scope.AWSMachine.Spec.PrivateDNSName
253253

254+
input.CapacityReservationID = scope.AWSMachine.Spec.CapacityReservationID
255+
254256
s.scope.Debug("Running instance", "machine-role", scope.Role())
255257
s.scope.Debug("Running instance with instance metadata options", "metadata options", input.InstanceMetadataOptions)
256258
out, err := s.runInstance(scope.Role(), input)
@@ -636,6 +638,7 @@ func (s *Service) runInstance(role string, i *infrav1.Instance) (*infrav1.Instan
636638
input.InstanceMarketOptions = getInstanceMarketOptionsRequest(i.SpotMarketOptions)
637639
input.MetadataOptions = getInstanceMetadataOptionsRequest(i.InstanceMetadataOptions)
638640
input.PrivateDnsNameOptions = getPrivateDNSNameOptionsRequest(i.PrivateDNSName)
641+
input.CapacityReservationSpecification = getCapacityReservationSpecification(i.CapacityReservationID)
639642

640643
if i.Tenancy != "" {
641644
input.Placement = &ec2.Placement{
@@ -1119,6 +1122,22 @@ func filterGroups(list []string, strToFilter string) (newList []string) {
11191122
return
11201123
}
11211124

1125+
func getCapacityReservationSpecification(capacityReservationID string) *ec2.CapacityReservationSpecification {
1126+
if capacityReservationID == "" {
1127+
// Instance is not a CapacityReservation instance
1128+
return nil
1129+
}
1130+
1131+
// Set required values for CapacityReservation
1132+
capacityReservationTargetOptions := &ec2.CapacityReservationTarget{}
1133+
capacityReservationTargetOptions.SetCapacityReservationId(capacityReservationID)
1134+
1135+
capacityReservationSpecification := &ec2.CapacityReservationSpecification{}
1136+
capacityReservationSpecification.SetCapacityReservationTarget(capacityReservationTargetOptions)
1137+
1138+
return capacityReservationSpecification
1139+
}
1140+
11221141
func getInstanceMarketOptionsRequest(spotMarketOptions *infrav1.SpotMarketOptions) *ec2.InstanceMarketOptionsRequest {
11231142
if spotMarketOptions == nil {
11241143
// Instance is not a Spot instance

pkg/cloud/services/ec2/instances_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5320,3 +5320,35 @@ func mockedGetPrivateDNSDomainNameFromDHCPOptionsEmptyCalls(m *mocks.MockEC2APIM
53205320
},
53215321
}, nil)
53225322
}
5323+
5324+
func TestGetCapacityReservationSpecification(t *testing.T) {
5325+
mockCapacityReservationID := "cr-123"
5326+
testCases := []struct {
5327+
name string
5328+
capacityReservationID string
5329+
expectedRequest *ec2.CapacityReservationSpecification
5330+
}{
5331+
{
5332+
name: "with no CapacityReservationID options specified",
5333+
capacityReservationID: "",
5334+
expectedRequest: nil,
5335+
},
5336+
{
5337+
name: "with an valid CapacityReservationID specified",
5338+
capacityReservationID: *aws.String(mockCapacityReservationID),
5339+
expectedRequest: &ec2.CapacityReservationSpecification{
5340+
CapacityReservationTarget: &ec2.CapacityReservationTarget{
5341+
CapacityReservationId: aws.String(mockCapacityReservationID),
5342+
},
5343+
},
5344+
},
5345+
}
5346+
for _, tc := range testCases {
5347+
t.Run(tc.name, func(t *testing.T) {
5348+
request := getCapacityReservationSpecification(tc.capacityReservationID)
5349+
if !cmp.Equal(request, tc.expectedRequest) {
5350+
t.Errorf("Case: %s. Got: %v, expected: %v", tc.name, request, tc.expectedRequest)
5351+
}
5352+
})
5353+
}
5354+
}

0 commit comments

Comments
 (0)