Skip to content

Commit

Permalink
Added the support for capacity blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
athiruma committed Jan 2, 2025
1 parent dc86e24 commit a1fa3fd
Show file tree
Hide file tree
Showing 24 changed files with 870 additions and 58 deletions.
1 change: 1 addition & 0 deletions api/v1beta1/awscluster_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (src *AWSCluster) ConvertTo(dstRaw conversion.Hub) error {
dst.Status.Bastion.PrivateDNSName = restored.Status.Bastion.PrivateDNSName
dst.Status.Bastion.PublicIPOnLaunch = restored.Status.Bastion.PublicIPOnLaunch
dst.Status.Bastion.CapacityReservationID = restored.Status.Bastion.CapacityReservationID
dst.Status.Bastion.MarketType = restored.Status.Bastion.MarketType
}
dst.Spec.Partition = restored.Spec.Partition

Expand Down
2 changes: 2 additions & 0 deletions api/v1beta1/awsmachine_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (src *AWSMachine) ConvertTo(dstRaw conversion.Hub) error {
dst.Spec.PrivateDNSName = restored.Spec.PrivateDNSName
dst.Spec.SecurityGroupOverrides = restored.Spec.SecurityGroupOverrides
dst.Spec.CapacityReservationID = restored.Spec.CapacityReservationID
dst.Spec.MarketType = restored.Spec.MarketType
if restored.Spec.ElasticIPPool != nil {
if dst.Spec.ElasticIPPool == nil {
dst.Spec.ElasticIPPool = &infrav1.ElasticIPPool{}
Expand Down Expand Up @@ -104,6 +105,7 @@ func (r *AWSMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
dst.Spec.Template.Spec.PrivateDNSName = restored.Spec.Template.Spec.PrivateDNSName
dst.Spec.Template.Spec.SecurityGroupOverrides = restored.Spec.Template.Spec.SecurityGroupOverrides
dst.Spec.Template.Spec.CapacityReservationID = restored.Spec.Template.Spec.CapacityReservationID
dst.Spec.Template.Spec.MarketType = restored.Spec.Template.Spec.MarketType
if restored.Spec.Template.Spec.ElasticIPPool != nil {
if dst.Spec.Template.Spec.ElasticIPPool == nil {
dst.Spec.Template.Spec.ElasticIPPool = &infrav1.ElasticIPPool{}
Expand Down
2 changes: 2 additions & 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.

9 changes: 9 additions & 0 deletions api/v1beta2/awsmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ type AWSMachineSpec struct {
// CapacityReservationID specifies the target Capacity Reservation into which the instance should be launched.
// +optional
CapacityReservationID *string `json:"capacityReservationId,omitempty"`

// marketType specifies the type of market for the EC2 instance. Valid values include:
// "OnDemand" (default): The instance runs as a standard OnDemand instance.
// "Spot": The instance runs as a Spot instance. When spotMarketOptions is provided, the marketType defaults to "Spot".
// "CapacityBlock": The instance utilizes pre-purchased compute capacity (capacity blocks) with AWS Capacity Reservations.
// If this value is selected, CapacityReservationID must be specified to identify the target reservation.
// If marketType is not specified and spotMarketOptions is provided, the marketType defaults to "Spot".
// +optional
MarketType MarketType `json:"marketType,omitempty"`
}

// CloudInit defines options related to the bootstrapping systems where
Expand Down
12 changes: 12 additions & 0 deletions api/v1beta2/awsmachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (r *AWSMachine) ValidateCreate() (admission.Warnings, error) {
allErrs = append(allErrs, r.validateAdditionalSecurityGroups()...)
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
allErrs = append(allErrs, r.validateNetworkElasticIPPool()...)
allErrs = append(allErrs, r.validateInstanceMarketType()...)

return nil, aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
}
Expand Down Expand Up @@ -361,6 +362,17 @@ func (r *AWSMachine) validateNetworkElasticIPPool() field.ErrorList {
return allErrs
}

func (r *AWSMachine) validateInstanceMarketType() field.ErrorList {
var allErrs field.ErrorList
if r.Spec.MarketType == MarketTypeCapacityBlock && r.Spec.SpotMarketOptions != nil {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "marketType"), "marketType set to CapacityBlock and spotMarketOptions cannot be used together"))
}
if r.Spec.MarketType == MarketTypeOnDemand && r.Spec.SpotMarketOptions != nil {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "marketType"), "setting marketType to OnDemand and spotMarketOptions cannot be used together"))
}
return allErrs
}

func (r *AWSMachine) validateNonRootVolumes() field.ErrorList {
var allErrs field.ErrorList

Expand Down
32 changes: 32 additions & 0 deletions api/v1beta2/awsmachine_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,38 @@ func TestAWSMachineCreate(t *testing.T) {
},
wantErr: false,
},
{
name: "invalid case, MarketType set to MarketTypeCapacityBlock and spotMarketOptions are specified",
machine: &AWSMachine{
Spec: AWSMachineSpec{
MarketType: MarketTypeCapacityBlock,
SpotMarketOptions: &SpotMarketOptions{},
InstanceType: "test",
},
},
wantErr: true,
},
{
name: "invalid case, MarketType set to MarketTypeOnDemand and spotMarketOptions are specified",
machine: &AWSMachine{
Spec: AWSMachineSpec{
MarketType: MarketTypeOnDemand,
SpotMarketOptions: &SpotMarketOptions{},
InstanceType: "test",
},
},
wantErr: true,
},
{
name: "valid MarketType set to MarketTypeCapacityBlock is specified",
machine: &AWSMachine{
Spec: AWSMachineSpec{
MarketType: MarketTypeCapacityBlock,
InstanceType: "test",
},
},
wantErr: false,
},
{
name: "empty instance type not allowed",
machine: &AWSMachine{
Expand Down
24 changes: 24 additions & 0 deletions api/v1beta2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,32 @@ type Instance struct {
// CapacityReservationID specifies the target Capacity Reservation into which the instance should be launched.
// +optional
CapacityReservationID *string `json:"capacityReservationId,omitempty"`

// marketType specifies the type of market for the EC2 instance. Valid values include:
// "OnDemand" (default): The instance runs as a standard OnDemand instance.
// "Spot": The instance runs as a Spot instance. When spotMarketOptions is provided, the marketType defaults to "Spot".
// "CapacityBlock": The instance utilizes pre-purchased compute capacity (capacity blocks) with AWS Capacity Reservations.
// If this value is selected, CapacityReservationID must be specified to identify the target reservation.
// If marketType is not specified and spotMarketOptions is provided, the marketType defaults to "Spot".
// +optional
MarketType MarketType `json:"marketType,omitempty"`
}

// MarketType describes the market type of an Instance
// +kubebuilder:validation:Enum:=OnDemand;Spot;CapacityBlock
type MarketType string

const (
// MarketTypeOnDemand is a MarketType enum value
MarketTypeOnDemand MarketType = "OnDemand"

// MarketTypeSpot is a MarketType enum value
MarketTypeSpot MarketType = "Spot"

// MarketTypeCapacityBlock is a MarketType enum value
MarketTypeCapacityBlock MarketType = "CapacityBlock"
)

// InstanceMetadataState describes the state of InstanceMetadataOptions.HttpEndpoint and InstanceMetadataOptions.InstanceMetadataTags
type InstanceMetadataState string

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,19 @@ spec:
instanceState:
description: The current state of the instance.
type: string
marketType:
description: |-
marketType specifies the type of market for the EC2 instance. Valid values include:
"OnDemand" (default): The instance runs as a standard OnDemand instance.
"Spot": The instance runs as a Spot instance. When spotMarketOptions is provided, the marketType defaults to "Spot".
"CapacityBlock": The instance utilizes pre-purchased compute capacity (capacity blocks) with AWS Capacity Reservations.
If this value is selected, CapacityReservationID must be specified to identify the target reservation.
If marketType is not specified and spotMarketOptions is provided, the marketType defaults to "Spot".
enum:
- OnDemand
- Spot
- CapacityBlock
type: string
networkInterfaces:
description: Specifies ENIs attached to instance
items:
Expand Down Expand Up @@ -3250,6 +3263,19 @@ spec:
instanceState:
description: The current state of the instance.
type: string
marketType:
description: |-
marketType specifies the type of market for the EC2 instance. Valid values include:
"OnDemand" (default): The instance runs as a standard OnDemand instance.
"Spot": The instance runs as a Spot instance. When spotMarketOptions is provided, the marketType defaults to "Spot".
"CapacityBlock": The instance utilizes pre-purchased compute capacity (capacity blocks) with AWS Capacity Reservations.
If this value is selected, CapacityReservationID must be specified to identify the target reservation.
If marketType is not specified and spotMarketOptions is provided, the marketType defaults to "Spot".
enum:
- OnDemand
- Spot
- CapacityBlock
type: string
networkInterfaces:
description: Specifies ENIs attached to instance
items:
Expand Down
13 changes: 13 additions & 0 deletions config/crd/bases/infrastructure.cluster.x-k8s.io_awsclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2177,6 +2177,19 @@ spec:
instanceState:
description: The current state of the instance.
type: string
marketType:
description: |-
marketType specifies the type of market for the EC2 instance. Valid values include:
"OnDemand" (default): The instance runs as a standard OnDemand instance.
"Spot": The instance runs as a Spot instance. When spotMarketOptions is provided, the marketType defaults to "Spot".
"CapacityBlock": The instance utilizes pre-purchased compute capacity (capacity blocks) with AWS Capacity Reservations.
If this value is selected, CapacityReservationID must be specified to identify the target reservation.
If marketType is not specified and spotMarketOptions is provided, the marketType defaults to "Spot".
enum:
- OnDemand
- Spot
- CapacityBlock
type: string
networkInterfaces:
description: Specifies ENIs attached to instance
items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,10 @@ spec:
description: ID of resource
type: string
type: object
capacityReservationId:
description: CapacityReservationID specifies the target Capacity
Reservation into which the instance should be launched.
type: string
iamInstanceProfile:
description: |-
The name or the Amazon Resource Name (ARN) of the instance profile associated
Expand Down Expand Up @@ -724,6 +728,19 @@ spec:
description: 'InstanceType is the type of instance to create.
Example: m4.xlarge'
type: string
marketType:
description: |-
marketType specifies the type of market for the EC2 instance. Valid values include:
"OnDemand" (default): The instance runs as a standard OnDemand instance.
"Spot": The instance runs as a Spot instance. When spotMarketOptions is provided, the marketType defaults to "Spot".
"CapacityBlock": The instance utilizes pre-purchased compute capacity (capacity blocks) with AWS Capacity Reservations.
If this value is selected, CapacityReservationID must be specified to identify the target reservation.
If marketType is not specified and spotMarketOptions is provided, the marketType defaults to "Spot".
enum:
- OnDemand
- Spot
- CapacityBlock
type: string
name:
description: The name of the launch template.
type: string
Expand Down
13 changes: 13 additions & 0 deletions config/crd/bases/infrastructure.cluster.x-k8s.io_awsmachines.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,19 @@ spec:
m4.xlarge'
minLength: 2
type: string
marketType:
description: |-
marketType specifies the type of market for the EC2 instance. Valid values include:
"OnDemand" (default): The instance runs as a standard OnDemand instance.
"Spot": The instance runs as a Spot instance. When spotMarketOptions is provided, the marketType defaults to "Spot".
"CapacityBlock": The instance utilizes pre-purchased compute capacity (capacity blocks) with AWS Capacity Reservations.
If this value is selected, CapacityReservationID must be specified to identify the target reservation.
If marketType is not specified and spotMarketOptions is provided, the marketType defaults to "Spot".
enum:
- OnDemand
- Spot
- CapacityBlock
type: string
networkInterfaces:
description: |-
NetworkInterfaces is a list of ENIs to associate with the instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,19 @@ spec:
Example: m4.xlarge'
minLength: 2
type: string
marketType:
description: |-
marketType specifies the type of market for the EC2 instance. Valid values include:
"OnDemand" (default): The instance runs as a standard OnDemand instance.
"Spot": The instance runs as a Spot instance. When spotMarketOptions is provided, the marketType defaults to "Spot".
"CapacityBlock": The instance utilizes pre-purchased compute capacity (capacity blocks) with AWS Capacity Reservations.
If this value is selected, CapacityReservationID must be specified to identify the target reservation.
If marketType is not specified and spotMarketOptions is provided, the marketType defaults to "Spot".
enum:
- OnDemand
- Spot
- CapacityBlock
type: string
networkInterfaces:
description: |-
NetworkInterfaces is a list of ENIs to associate with the instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,10 @@ spec:
description: ID of resource
type: string
type: object
capacityReservationId:
description: CapacityReservationID specifies the target Capacity
Reservation into which the instance should be launched.
type: string
iamInstanceProfile:
description: |-
The name or the Amazon Resource Name (ARN) of the instance profile associated
Expand Down Expand Up @@ -720,6 +724,19 @@ spec:
description: 'InstanceType is the type of instance to create.
Example: m4.xlarge'
type: string
marketType:
description: |-
marketType specifies the type of market for the EC2 instance. Valid values include:
"OnDemand" (default): The instance runs as a standard OnDemand instance.
"Spot": The instance runs as a Spot instance. When spotMarketOptions is provided, the marketType defaults to "Spot".
"CapacityBlock": The instance utilizes pre-purchased compute capacity (capacity blocks) with AWS Capacity Reservations.
If this value is selected, CapacityReservationID must be specified to identify the target reservation.
If marketType is not specified and spotMarketOptions is provided, the marketType defaults to "Spot".
enum:
- OnDemand
- Spot
- CapacityBlock
type: string
name:
description: The name of the launch template.
type: string
Expand Down
18 changes: 17 additions & 1 deletion exp/api/v1beta1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,16 @@ func (src *AWSMachinePool) ConvertTo(dstRaw conversion.Hub) error {
dst.Spec.AWSLaunchTemplate.PrivateDNSName = restored.Spec.AWSLaunchTemplate.PrivateDNSName
}

if restored.Spec.AWSLaunchTemplate.CapacityReservationID != nil {
dst.Spec.AWSLaunchTemplate.CapacityReservationID = restored.Spec.AWSLaunchTemplate.CapacityReservationID
}

if restored.Spec.AWSLaunchTemplate.MarketType != "" {
dst.Spec.AWSLaunchTemplate.MarketType = restored.Spec.AWSLaunchTemplate.MarketType
}

dst.Spec.DefaultInstanceWarmup = restored.Spec.DefaultInstanceWarmup
dst.Spec.AWSLaunchTemplate.NonRootVolumes = restored.Spec.AWSLaunchTemplate.NonRootVolumes

return nil
}

Expand Down Expand Up @@ -109,6 +116,15 @@ func (src *AWSManagedMachinePool) ConvertTo(dstRaw conversion.Hub) error {
if restored.Spec.AWSLaunchTemplate.PrivateDNSName != nil {
dst.Spec.AWSLaunchTemplate.PrivateDNSName = restored.Spec.AWSLaunchTemplate.PrivateDNSName
}

if restored.Spec.AWSLaunchTemplate.CapacityReservationID != nil {
dst.Spec.AWSLaunchTemplate.CapacityReservationID = restored.Spec.AWSLaunchTemplate.CapacityReservationID
}

if restored.Spec.AWSLaunchTemplate.MarketType != "" {
dst.Spec.AWSLaunchTemplate.MarketType = restored.Spec.AWSLaunchTemplate.MarketType
}

}
if restored.Spec.AvailabilityZoneSubnetType != nil {
dst.Spec.AvailabilityZoneSubnetType = restored.Spec.AvailabilityZoneSubnetType
Expand Down
2 changes: 2 additions & 0 deletions exp/api/v1beta1/zz_generated.conversion.go

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

Loading

0 comments on commit a1fa3fd

Please sign in to comment.