Skip to content

Commit

Permalink
Add support for dedicated instance tenancy
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Demichev committed Oct 13, 2020
1 parent 6588351 commit 8c1e136
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/actuators/machine/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,25 @@ func launchInstance(machine *machinev1.Machine, machineProviderConfig *awsprovid
}
}

instanceTenancy := machineProviderConfig.Tenancy

switch instanceTenancy {
case "":
// Do nothing when not set
case awsproviderv1.DefaultTenancy, awsproviderv1.DedicatedTenancy, awsproviderv1.HostTenancy:
if placement == nil {
placement = &ec2.Placement{}
}
tenancy := string(machineProviderConfig.Tenancy)
placement.Tenancy = &tenancy
default:
return nil, mapierrors.CreateMachine("invalid instance tenancy: %s. Allowed options are: %s,%s,%s",
instanceTenancy,
awsproviderv1.DefaultTenancy,
awsproviderv1.DedicatedTenancy,
awsproviderv1.HostTenancy)
}

inputConfig := ec2.RunInstancesInput{
ImageId: amiID,
InstanceType: aws.String(machineProviderConfig.InstanceType),
Expand Down
43 changes: 43 additions & 0 deletions pkg/actuators/machine/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,49 @@ func TestLaunchInstance(t *testing.T) {
name: "AMI not specified",
providerConfig: stubPCAMI(awsproviderv1.AWSResourceReference{}),
},
{
name: "Dedicated instance tenancy",
providerConfig: stubDedicatedInstanceTenancy(),
runInstancesInput: &ec2.RunInstancesInput{
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
Name: aws.String(*providerConfig.IAMInstanceProfile.ID),
},
ImageId: aws.String(*providerConfig.AMI.ID),
InstanceType: &providerConfig.InstanceType,
MinCount: aws.Int64(1),
MaxCount: aws.Int64(1),
KeyName: providerConfig.KeyName,
TagSpecifications: []*ec2.TagSpecification{{
ResourceType: aws.String("instance"),
Tags: stubTagList,
}, {
ResourceType: aws.String("volume"),
Tags: stubTagList,
}},
NetworkInterfaces: []*ec2.InstanceNetworkInterfaceSpecification{
{
DeviceIndex: aws.Int64(providerConfig.DeviceIndex),
AssociatePublicIpAddress: providerConfig.PublicIP,
SubnetId: providerConfig.Subnet.ID,
Groups: []*string{
aws.String("sg-00868b02fbe29de17"),
aws.String("sg-0a4658991dc5eb40a"),
aws.String("sg-009a70e28fa4ba84e"),
aws.String("sg-07323d56fb932c84c"),
aws.String("sg-08b1ffd32874d59a2"),
},
},
},
UserData: aws.String(""),
Placement: &ec2.Placement{
Tenancy: aws.String("dedicated"),
},
},
},
{
name: "Dedicated instance tenancy",
providerConfig: stubInvalidInstanceTenancy(),
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions pkg/actuators/machine/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ func stubPCAMI(ami awsproviderv1.AWSResourceReference) *awsproviderv1.AWSMachine
return pc
}

func stubDedicatedInstanceTenancy() *awsproviderv1.AWSMachineProviderConfig {
pc := stubProviderConfig()
pc.Tenancy = awsproviderv1.DedicatedTenancy
return pc
}

func stubInvalidInstanceTenancy() *awsproviderv1.AWSMachineProviderConfig {
pc := stubProviderConfig()
pc.Tenancy = "invalid"
return pc
}

func stubDescribeLoadBalancersOutput() *elbv2.DescribeLoadBalancersOutput {
return &elbv2.DescribeLoadBalancersOutput{
LoadBalancers: []*elbv2.LoadBalancer{
Expand Down
16 changes: 16 additions & 0 deletions pkg/apis/awsprovider/v1beta1/awsproviderconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ type AWSMachineProviderConfig struct {

// SpotMarketOptions allows users to configure instances to be run using AWS Spot instances.
SpotMarketOptions *SpotMarketOptions `json:"spotMarketOptions,omitempty"`

// Tenancy indicates if instance should run on shared or single-tenant hardware. There are
// supported 3 options: default, dedicated and host.
Tenancy InstanceTenancy `json:"tenancy,omitempty"`
}

// BlockDeviceMappingSpec describes a block device mapping
Expand Down Expand Up @@ -235,6 +239,18 @@ type LoadBalancerReference struct {
// an instance with load balancers specified in LoadBalancerNames
type AWSLoadBalancerType string

// InstanceTenancy indicates if instance should run on shared or single-tenant hardware.
type InstanceTenancy string

const (
// DefaultTenancy instance runs on shared hardware
DefaultTenancy InstanceTenancy = "default"
// DedicatedTenancy instance runs on single-tenant hardware
DedicatedTenancy InstanceTenancy = "dedicated"
// HostTenancy instance runs on a Dedicated Host, which is an isolated server with configurations that you can control.
HostTenancy InstanceTenancy = "host"
)

// Possible values for AWSLoadBalancerType. Add to this list as other types
// of load balancer are supported by the actuator.
const (
Expand Down

0 comments on commit 8c1e136

Please sign in to comment.