Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/v1beta1/awscluster_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func (src *AWSCluster) ConvertTo(dstRaw conversion.Hub) error {
dst.Status.Bastion.NetworkInterfaceType = restored.Status.Bastion.NetworkInterfaceType
dst.Status.Bastion.CapacityReservationID = restored.Status.Bastion.CapacityReservationID
dst.Status.Bastion.MarketType = restored.Status.Bastion.MarketType
dst.Status.Bastion.HostAffinity = restored.Status.Bastion.HostAffinity
dst.Status.Bastion.HostID = restored.Status.Bastion.HostID
}
dst.Spec.Partition = restored.Spec.Partition

Expand Down
4 changes: 4 additions & 0 deletions api/v1beta1/awsmachine_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func (src *AWSMachine) ConvertTo(dstRaw conversion.Hub) error {
dst.Spec.SecurityGroupOverrides = restored.Spec.SecurityGroupOverrides
dst.Spec.CapacityReservationID = restored.Spec.CapacityReservationID
dst.Spec.MarketType = restored.Spec.MarketType
dst.Spec.HostID = restored.Spec.HostID
dst.Spec.HostAffinity = restored.Spec.HostAffinity
dst.Spec.NetworkInterfaceType = restored.Spec.NetworkInterfaceType
if restored.Spec.ElasticIPPool != nil {
if dst.Spec.ElasticIPPool == nil {
Expand Down Expand Up @@ -107,6 +109,8 @@ func (r *AWSMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
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
dst.Spec.Template.Spec.HostID = restored.Spec.Template.Spec.HostID
dst.Spec.Template.Spec.HostAffinity = restored.Spec.Template.Spec.HostAffinity
dst.Spec.Template.Spec.NetworkInterfaceType = restored.Spec.Template.Spec.NetworkInterfaceType
if restored.Spec.Template.Spec.ElasticIPPool != nil {
if dst.Spec.Template.Spec.ElasticIPPool == nil {
Expand Down
4 changes: 4 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.

12 changes: 12 additions & 0 deletions api/v1beta2/awsmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ type AWSMachineSpec struct {
// If marketType is not specified and spotMarketOptions is provided, the marketType defaults to "Spot".
// +optional
MarketType MarketType `json:"marketType,omitempty"`

// HostID specifies the Dedicated Host on which the instance must be started.
// +optional
HostID *string `json:"hostID,omitempty"`

// HostAffinity specifies the dedicated host affinity setting for the instance.
// When hostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
// When hostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.
// When HostAffinity is defined, HostID is required.
// +optional
// +kubebuilder:validation:Enum:=default;host
HostAffinity *string `json:"hostAffinity,omitempty"`
}

// CloudInit defines options related to the bootstrapping systems where
Expand Down
14 changes: 14 additions & 0 deletions api/v1beta2/awsmachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
allErrs = append(allErrs, r.validateNonRootVolumes()...)
allErrs = append(allErrs, r.validateSSHKeyName()...)
allErrs = append(allErrs, r.validateAdditionalSecurityGroups()...)
allErrs = append(allErrs, r.validateHostAffinity()...)
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
allErrs = append(allErrs, r.validateNetworkElasticIPPool()...)
allErrs = append(allErrs, r.validateInstanceMarketType()...)
Expand Down Expand Up @@ -91,6 +92,8 @@
allErrs = append(allErrs, r.validateCloudInitSecret()...)
allErrs = append(allErrs, r.validateAdditionalSecurityGroups()...)
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
allErrs = append(allErrs, r.validateHostAffinity()...)

Check failure on line 96 in api/v1beta2/awsmachine_webhook.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (gci)

newAWSMachineSpec := newAWSMachine["spec"].(map[string]interface{})
oldAWSMachineSpec := oldAWSMachine["spec"].(map[string]interface{})
Expand Down Expand Up @@ -433,6 +436,17 @@
return allErrs
}

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

if r.Spec.HostAffinity != nil {
if r.Spec.HostID == nil || len(*r.Spec.HostID) == 0 {
allErrs = append(allErrs, field.Required(field.NewPath("spec.hostID"), "hostID must be set when hostAffinity is configured"))
}
}
return allErrs
}

func (r *AWSMachine) validateSSHKeyName() field.ErrorList {
return validateSSHKeyName(r.Spec.SSHKeyName)
}
31 changes: 31 additions & 0 deletions api/v1beta2/awsmachine_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,37 @@
},
wantErr: true,
},
{
name: "configure host affinity with Host ID",
machine: &AWSMachine{
Spec: AWSMachineSpec{
InstanceType: "test",
HostAffinity: ptr.To("default"),
HostID: ptr.To("h-09dcf61cb388b0149"),

Check failure on line 420 in api/v1beta2/awsmachine_webhook_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (gci)
},
},
wantErr: false,
},
{
name: "configure host affinity with invalid affinity",
machine: &AWSMachine{
Spec: AWSMachineSpec{
InstanceType: "test",
HostAffinity: ptr.To("invalid"),
},
},
wantErr: true,
},
{
name: "configure host affinity without Host ID",
machine: &AWSMachine{
Spec: AWSMachineSpec{
InstanceType: "test",
HostAffinity: ptr.To("default"),
},
},
wantErr: true,
},
{
name: "create with valid BYOIPv4",
machine: &AWSMachine{
Expand Down
12 changes: 12 additions & 0 deletions api/v1beta2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,18 @@ type Instance struct {
// If marketType is not specified and spotMarketOptions is provided, the marketType defaults to "Spot".
// +optional
MarketType MarketType `json:"marketType,omitempty"`

// HostAffinity specifies the dedicated host affinity setting for the instance.
// When hostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
// When hostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.
// When HostAffinity is defined, HostID is required.
// +optional
// +kubebuilder:validation:Enum:=default;host
HostAffinity *string `json:"hostAffinity,omitempty"`

// HostID specifies the dedicated host on which the instance should be started
// +optional
HostID *string `json:"hostID,omitempty"`
}

// MarketType describes the market type of an Instance
Expand Down
20 changes: 20 additions & 0 deletions api/v1beta2/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,20 @@ spec:
description: Specifies whether enhanced networking with ENA is
enabled.
type: boolean
hostAffinity:
description: |-
HostAffinity specifies the dedicated host affinity setting for the instance.
When hostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
When hostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.
When HostAffinity is defined, HostID is required.
enum:
- default
- host
type: string
hostID:
description: HostID specifies the dedicated host on which the
instance should be started
type: string
iamProfile:
description: The name of the IAM instance profile associated with
the instance, if applicable.
Expand Down Expand Up @@ -3378,6 +3392,20 @@ spec:
description: Specifies whether enhanced networking with ENA is
enabled.
type: boolean
hostAffinity:
description: |-
HostAffinity specifies the dedicated host affinity setting for the instance.
When hostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
When hostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.
When HostAffinity is defined, HostID is required.
enum:
- default
- host
type: string
hostID:
description: HostID specifies the dedicated host on which the
instance should be started
type: string
iamProfile:
description: The name of the IAM instance profile associated with
the instance, if applicable.
Expand Down
14 changes: 14 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 @@ -2180,6 +2180,20 @@ spec:
description: Specifies whether enhanced networking with ENA is
enabled.
type: boolean
hostAffinity:
description: |-
HostAffinity specifies the dedicated host affinity setting for the instance.
When hostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
When hostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.
When HostAffinity is defined, HostID is required.
enum:
- default
- host
type: string
hostID:
description: HostID specifies the dedicated host on which the
instance should be started
type: string
iamProfile:
description: The name of the IAM instance profile associated with
the instance, if applicable.
Expand Down
14 changes: 14 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 @@ -686,6 +686,20 @@ spec:
- message: allowed values are 'none' and 'amazon-pool'
rule: self in ['none','amazon-pool']
type: object
hostAffinity:
description: |-
HostAffinity specifies the dedicated host affinity setting for the instance.
When hostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
When hostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.
When HostAffinity is defined, HostID is required.
enum:
- default
- host
type: string
hostID:
description: HostID specifies the Dedicated Host on which the instance
must be started.
type: string
iamInstanceProfile:
description: IAMInstanceProfile is a name of an IAM instance profile
to assign to the instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,20 @@ spec:
- message: allowed values are 'none' and 'amazon-pool'
rule: self in ['none','amazon-pool']
type: object
hostAffinity:
description: |-
HostAffinity specifies the dedicated host affinity setting for the instance.
When hostAffinity is set to host, an instance started onto a specific host always restarts on the same host if stopped.
When hostAffinity is set to default, and you stop and restart the instance, it can be restarted on any available host.
When HostAffinity is defined, HostID is required.
enum:
- default
- host
type: string
hostID:
description: HostID specifies the Dedicated Host on which
the instance must be started.
type: string
iamInstanceProfile:
description: IAMInstanceProfile is a name of an IAM instance
profile to assign to the instance
Expand Down
25 changes: 25 additions & 0 deletions pkg/cloud/services/ec2/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ func (s *Service) CreateInstance(scope *scope.MachineScope, userData []byte, use

input.MarketType = scope.AWSMachine.Spec.MarketType

input.HostID = scope.AWSMachine.Spec.HostID

input.HostAffinity = scope.AWSMachine.Spec.HostAffinity

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 @@ -674,6 +678,27 @@ func (s *Service) runInstance(role string, i *infrav1.Instance) (*infrav1.Instan
}
}

if i.HostID != nil {
if i.HostAffinity == nil {
i.HostAffinity = aws.String("Default")
}
s.scope.Debug("Running instance with dedicated host placement",
"hostId", i.HostID,
"hostAffinity", i.HostAffinity)
if input.Placement != nil {
s.scope.Warn("Placement already set for instance, overwriting with dedicated host placement",
"hostId", i.HostID,
"hostAffinity", i.HostAffinity,
"placement", input.Placement.GoString())
}

input.Placement = &ec2.Placement{
Tenancy: aws.String("host"),
Affinity: i.HostAffinity,
HostId: i.HostID,
}
}

out, err := s.EC2Client.RunInstancesWithContext(context.TODO(), input)
if err != nil {
return nil, errors.Wrap(err, "failed to run instance")
Expand Down
1 change: 1 addition & 0 deletions test/e2e/data/e2e_conf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ providers:
- sourcePath: "./shared/v1beta2_provider/metadata.yaml"
- sourcePath: "./infrastructure-aws/withoutclusterclass/generated/cluster-template-ignition.yaml"
- sourcePath: "./infrastructure-aws/withoutclusterclass/generated/cluster-template-upgrade-to-external-cloud-provider.yaml"
- sourcePath: "./infrastructure-aws/withoutclusterclass/generated/cluster-template-dedicated-host.yaml"
replacements:
# To allow bugs to be catched.
- old: "failureThreshold: 3"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
apiVersion: cluster.x-k8s.io/v1beta1
kind: MachineDeployment
metadata:
name: "${CLUSTER_NAME}-md-dh"
spec:
clusterName: "${CLUSTER_NAME}"
replicas: 1
selector:
matchLabels:
template:
spec:
clusterName: "${CLUSTER_NAME}"
version: "${KUBERNETES_VERSION}"
bootstrap:
configRef:
name: "${CLUSTER_NAME}-md-dh"
apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
kind: KubeadmConfigTemplate
infrastructureRef:
name: "${CLUSTER_NAME}-md-dh"
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
kind: AWSMachineTemplate
---
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
kind: AWSMachineTemplate
metadata:
name: "${CLUSTER_NAME}-md-dh"
spec:
template:
spec:
instanceType: "${AWS_NODE_MACHINE_TYPE}"
iamInstanceProfile: "nodes.cluster-api-provider-aws.sigs.k8s.io"
sshKeyName: "${AWS_SSH_KEY_NAME}"
hostID: "${HOST_ID}"
hostAffinity: "${HOST_AFFINITY}"
---
apiVersion: bootstrap.cluster.x-k8s.io/v1beta1
kind: KubeadmConfigTemplate
metadata:
name: "${CLUSTER_NAME}-md-dh"
spec:
template:
spec:
joinConfiguration:
nodeRegistration:
name: '{{ ds.meta_data.local_hostname }}'
kubeletExtraArgs:
cloud-provider: aws
Loading
Loading