Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions api/v1beta1/awsmachine_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func (src *AWSMachine) ConvertTo(dstRaw conversion.Hub) error {
}
}

dst.Spec.HostAffinity = restored.Spec.HostAffinity
dst.Spec.HostID = restored.Spec.HostID

return nil
}

Expand Down Expand Up @@ -119,6 +122,8 @@ func (r *AWSMachineTemplate) ConvertTo(dstRaw conversion.Hub) error {
dst.Spec.Template.Spec.ElasticIPPool.PublicIpv4PoolFallBackOrder = restored.Spec.Template.Spec.ElasticIPPool.PublicIpv4PoolFallBackOrder
}
}
dst.Spec.Template.Spec.HostAffinity = restored.Spec.Template.Spec.HostAffinity
dst.Spec.Template.Spec.HostID = restored.Spec.Template.Spec.HostID

return nil
}
Expand Down
10 changes: 10 additions & 0 deletions api/v1beta2/awsmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ 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 should be launched.
// +optional
HostID *string `json:"hostId,omitempty"`

// Affinity specifies the dedicated host affinity setting for the instance.
// When affinity is set to Host, an instance launched onto a specific host always restarts on the same host if stopped.
// +optional
// +kubebuilder:validation:Enum:=Defailt;Host
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// +kubebuilder:validation:Enum:=Defailt;Host
// +kubebuilder:validation:Enum:=Default;Host

HostAffinity *string `json:"hostAffinity,omitempty"`
}

// CloudInit defines options related to the bootstrapping systems where
Expand Down
8 changes: 8 additions & 0 deletions api/v1beta2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ type Instance 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 should be launched
// +optional
HostID *string `json:"hostID,omitempty"`

// Affinity specifies the dedicated host affinity setting for the instance.
// +optional
HostAffinity *string `json:"hostAffinity,omitempty"`
}

// MarketType describes the market type of an Instance
Expand Down
17 changes: 17 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,19 @@ 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, "affinity", i.HostAffinity)

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
2 changes: 1 addition & 1 deletion test/e2e/shared/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,4 @@ func ReleaseResources(request *TestResource, nodeNum int, fileLock *flock.Flock)
return nil
}
return errors.New("giving up on releasing resource due to timeout")
}
}
149 changes: 149 additions & 0 deletions test/e2e/suites/unmanaged/unmanaged_CAPA_dedicated_host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
//go:build e2e
// +build e2e

/*
Copyright 2025 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package unmanaged

import (
"context"

"github.com/gofrs/flock"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"

"sigs.k8s.io/cluster-api-provider-aws/v2/test/e2e/shared"
capi_e2e "sigs.k8s.io/cluster-api/test/e2e"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)

// setupNamespace initializes the namespace for the test.
func setupNamespace(ctx context.Context, e2eCtx *shared.E2EContext) *corev1.Namespace {
Expect(e2eCtx.Environment.BootstrapClusterProxy).ToNot(BeNil(), "Invalid argument. BootstrapClusterProxy can't be nil")
return shared.SetupSpecNamespace(ctx, "capa-dedicate-host", e2eCtx)
}

func allocateDedicatedHost() string {
// Is this the proper way to create a session?
sess := session.Must(session.NewSession())
ec2Client := ec2.New(sess)

// Can that be retrieved from test configuration?
input := &ec2.AllocateHostsInput{
AvailabilityZone: aws.String("us-east-1a"),
InstanceType: aws.String("m5.large"),
Quantity: aws.Int64(1),
}

output, err := ec2Client.AllocateHosts(input)
Expect(err).ToNot(HaveOccurred(), "Failed to allocate dedicated host")
Expect(len(output.HostIds)).To(BeNumerically(">", 0), "No dedicated host ID returned")

return *output.HostIds[0]
}

// setupRequiredResources allocates the required resources for the test.
func setupRequiredResources(e2eCtx *shared.E2EContext) *shared.TestResource {
requiredResources := &shared.TestResource{
EC2Normal: 2 * e2eCtx.Settings.InstanceVCPU,
IGW: 1,
NGW: 1,
VPC: 1,
ClassicLB: 1,
EIP: 3,
EventBridgeRules: 50,
}
requiredResources.WriteRequestedResources(e2eCtx, "capa-dedicated-hosts-test")

Expect(shared.AcquireResources(requiredResources, ginkgo.GinkgoParallelProcess(), flock.New(shared.ResourceQuotaFilePath))).To(Succeed())
return requiredResources
}

// releaseDedicatedHost releases the allocated dedicated host.
func releaseDedicatedHost(hostID string) {
sess := session.Must(session.NewSession())
ec2Client := ec2.New(sess)

input := &ec2.ReleaseHostsInput{
HostIds: []*string{aws.String(hostID)},
}

_, err := ec2Client.ReleaseHosts(input)
Expect(err).ToNot(HaveOccurred(), "Failed to release dedicated host")
}

// releaseResources releases the resources allocated for the test.
func releaseResources(requiredResources *shared.TestResource, e2eCtx *shared.E2EContext) {

Check failure on line 94 in test/e2e/suites/unmanaged/unmanaged_CAPA_dedicated_host.go

View workflow job for this annotation

GitHub Actions / lint

`releaseResources` - `e2eCtx` is unused (unparam)
shared.ReleaseResources(requiredResources, ginkgo.GinkgoParallelProcess(), flock.New(shared.ResourceQuotaFilePath))

Check failure on line 95 in test/e2e/suites/unmanaged/unmanaged_CAPA_dedicated_host.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `shared.ReleaseResources` is not checked (errcheck)
}

// runQuickStartSpec executes the QuickStartSpec test.
func runQuickStartSpec(e2eCtx *shared.E2EContext) {
capi_e2e.QuickStartSpec(context.TODO(), func() capi_e2e.QuickStartSpecInput {
return capi_e2e.QuickStartSpecInput{
E2EConfig: e2eCtx.E2EConfig,
ClusterctlConfigPath: e2eCtx.Environment.ClusterctlConfigPath,
BootstrapClusterProxy: e2eCtx.Environment.BootstrapClusterProxy,
ArtifactFolder: e2eCtx.Settings.ArtifactFolder,
SkipCleanup: e2eCtx.Settings.SkipCleanup,
}
})
}

// cleanupNamespace cleans up the namespace and dumps resources.
func cleanupNamespace(ctx context.Context, namespace *corev1.Namespace, e2eCtx *shared.E2EContext) {
shared.DumpSpecResourcesAndCleanup(ctx, "", namespace, e2eCtx)
}

var _ = ginkgo.Context("[unmanaged] [dedicated-host]", func() {
var (
namespace *corev1.Namespace
ctx context.Context
requiredResources *shared.TestResource
dedicatedHostID string
)

ginkgo.BeforeEach(func() {
ctx = context.TODO()
namespace = setupNamespace(ctx, e2eCtx)
dedicatedHostID = allocateDedicatedHost()
})

ginkgo.Describe("Running the dedicated-hosts spec", func() {
ginkgo.BeforeEach(func() {
requiredResources = setupRequiredResources(e2eCtx)
// e2eCtx.Settings.DedicatedHostID = dedicatedHostID
})

ginkgo.It("should run the QuickStartSpec", func() {
runQuickStartSpec(e2eCtx)
})

ginkgo.AfterEach(func() {
releaseDedicatedHost(dedicatedHostID)
releaseResources(requiredResources, e2eCtx)
})
})

ginkgo.AfterEach(func() {
cleanupNamespace(ctx, namespace, e2eCtx)
})
})
Loading