Skip to content

Commit

Permalink
Added the CapacityReservation support
Browse files Browse the repository at this point in the history
Fixed the lint

Fixed the description

Removed the API in v1
  • Loading branch information
athiruma committed Jul 15, 2024
1 parent 3b67f4f commit 11045bb
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/v1beta1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions api/v1beta2/awsmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ type AWSMachineSpec struct {
// PrivateDNSName is the options for the instance hostname.
// +optional
PrivateDNSName *PrivateDNSName `json:"privateDnsName,omitempty"`

// CapacityReservationID specifies the instance that should be launched in the
// reserved compute capacity.
// +optional
CapacityReservationID string `json:"capacityReservationId,omitempty"`
}

// CloudInit defines options related to the bootstrapping systems where
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ type Instance struct {
// PublicIPOnLaunch is the option to associate a public IP on instance launch
// +optional
PublicIPOnLaunch *bool `json:"publicIPOnLaunch,omitempty"`

// CapacityReservationID specifies the instance that should be launched in the
// reserved compute capacity.
// +optional
CapacityReservationID string `json:"capacityReservationId,omitempty"`
}

// InstanceMetadataState describes the state of InstanceMetadataOptions.HttpEndpoint and InstanceMetadataOptions.InstanceMetadataTags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,11 @@ spec:
availabilityZone:
description: Availability zone of instance
type: string
capacityReservationId:
description: |-
CapacityReservationID specifies the instance that should be launched in the
reserved compute capacity.
type: string
ebsOptimized:
description: Indicates whether the instance is optimized for Amazon
EBS I/O.
Expand Down Expand Up @@ -3094,6 +3099,11 @@ spec:
availabilityZone:
description: Availability zone of instance
type: string
capacityReservationId:
description: |-
CapacityReservationID specifies the instance that should be launched in the
reserved compute capacity.
type: string
ebsOptimized:
description: Indicates whether the instance is optimized for Amazon
EBS I/O.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2075,6 +2075,11 @@ spec:
availabilityZone:
description: Availability zone of instance
type: string
capacityReservationId:
description: |-
CapacityReservationID specifies the instance that should be launched in the
reserved compute capacity.
type: string
ebsOptimized:
description: Indicates whether the instance is optimized for Amazon
EBS I/O.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,11 @@ spec:
description: ID of resource
type: string
type: object
capacityReservationId:
description: |-
CapacityReservationID specifies the instance that should be launched in the
reserved compute capacity.
type: string
cloudInit:
description: |-
CloudInit defines options related to the bootstrapping systems where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,11 @@ spec:
description: ID of resource
type: string
type: object
capacityReservationId:
description: |-
CapacityReservationID specifies the instance that should be launched in the
reserved compute capacity.
type: string
cloudInit:
description: |-
CloudInit defines options related to the bootstrapping systems where
Expand Down
19 changes: 19 additions & 0 deletions pkg/cloud/services/ec2/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ func (s *Service) CreateInstance(scope *scope.MachineScope, userData []byte, use

input.PrivateDNSName = scope.AWSMachine.Spec.PrivateDNSName

input.CapacityReservationID = scope.AWSMachine.Spec.CapacityReservationID

s.scope.Debug("Running instance", "machine-role", scope.Role())
s.scope.Debug("Running instance with instance metadata options", "metadata options", input.InstanceMetadataOptions)
out, err := s.runInstance(scope.Role(), input)
Expand Down Expand Up @@ -636,6 +638,7 @@ func (s *Service) runInstance(role string, i *infrav1.Instance) (*infrav1.Instan
input.InstanceMarketOptions = getInstanceMarketOptionsRequest(i.SpotMarketOptions)
input.MetadataOptions = getInstanceMetadataOptionsRequest(i.InstanceMetadataOptions)
input.PrivateDnsNameOptions = getPrivateDNSNameOptionsRequest(i.PrivateDNSName)
input.CapacityReservationSpecification = getCapacityReservationSpecification(i.CapacityReservationID)

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

func getCapacityReservationSpecification(capacityReservationID string) *ec2.CapacityReservationSpecification {
if capacityReservationID == "" {
// Instance is not a CapacityReservation instance
return nil
}

// Set required values for CapacityReservation
capacityReservationTargetOptions := &ec2.CapacityReservationTarget{}
capacityReservationTargetOptions.SetCapacityReservationId(capacityReservationID)

capacityReservationSpecification := &ec2.CapacityReservationSpecification{}
capacityReservationSpecification.SetCapacityReservationTarget(capacityReservationTargetOptions)

return capacityReservationSpecification
}

func getInstanceMarketOptionsRequest(spotMarketOptions *infrav1.SpotMarketOptions) *ec2.InstanceMarketOptionsRequest {
if spotMarketOptions == nil {
// Instance is not a Spot instance
Expand Down
32 changes: 32 additions & 0 deletions pkg/cloud/services/ec2/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5320,3 +5320,35 @@ func mockedGetPrivateDNSDomainNameFromDHCPOptionsEmptyCalls(m *mocks.MockEC2APIM
},
}, nil)
}

func TestGetCapacityReservationSpecification(t *testing.T) {
mockCapacityReservationID := "cr-123"
testCases := []struct {
name string
capacityReservationID string
expectedRequest *ec2.CapacityReservationSpecification
}{
{
name: "with no CapacityReservationID options specified",
capacityReservationID: "",
expectedRequest: nil,
},
{
name: "with an valid CapacityReservationID specified",
capacityReservationID: *aws.String(mockCapacityReservationID),
expectedRequest: &ec2.CapacityReservationSpecification{
CapacityReservationTarget: &ec2.CapacityReservationTarget{
CapacityReservationId: aws.String(mockCapacityReservationID),
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
request := getCapacityReservationSpecification(tc.capacityReservationID)
if !cmp.Equal(request, tc.expectedRequest) {
t.Errorf("Case: %s. Got: %v, expected: %v", tc.name, request, tc.expectedRequest)
}
})
}
}

0 comments on commit 11045bb

Please sign in to comment.