Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ spec:
publicIp:
description: The public IPv4 address assigned to the instance, if applicable.
type: string
rootDeviceSize:
description: Specifies size (in Gi) of the root storage device
format: int64
type: integer
securityGroupIds:
description: SecurityGroupIDs are one or more security group IDs this
instance belongs to.
Expand Down
4 changes: 4 additions & 0 deletions config/crds/awsprovider_v1alpha1_awsmachineproviderspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ spec:
IP. Precedence for this setting is as follows: 1. This field if set 2.
Cluster/flavor setting 3. Subnet default'
type: boolean
rootDeviceSize:
description: RootDeviceSize is the size of the root volume.
format: int64
type: integer
subnet:
description: Subnet is a reference to the subnet to use for this instance.
If not specified, the cluster subnet will be used.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ type AWSMachineProviderSpec struct {
// +optional
KeyName string `json:"keyName,omitempty"`

// RootDeviceSize is the size of the root volume.
// +optional
RootDeviceSize int64 `json:"rootDeviceSize,omitempty"`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ah! forgot to run make generate-crds. thanks Seth! updated.


// KubeadmConfiguration holds the kubeadm configuration options
// +optional
KubeadmConfiguration KubeadmConfiguration `json:"kubeadmConfiguration,omitempty"`
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/awsprovider/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ type Instance struct {
// Indicates whether the instance is optimized for Amazon EBS I/O.
EBSOptimized *bool `json:"ebsOptimized,omitempty"`

// Specifies size (in Gi) of the root storage device
RootDeviceSize int64 `json:"rootDeviceSize,omitempty"`

// The tags associated with the instance.
Tags map[string]string `json:"tags,omitempty"`
}
5 changes: 5 additions & 0 deletions pkg/cloud/aws/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ func (a *Actuator) isMachineOutdated(machineSpec *v1alpha1.AWSMachineProviderSpe
errs = append(errs, errors.Errorf("SSH key name cannot be mutated from %q to %q", aws.StringValue(instance.KeyName), machineSpec.KeyName))
}

// Root Device Size
if machineSpec.RootDeviceSize != instance.RootDeviceSize {
errs = append(errs, errors.Errorf("Root volume size cannot be mutated from %v to %v", instance.RootDeviceSize, machineSpec.RootDeviceSize))
}

// Subnet ID
// machineSpec.Subnet is a *AWSResourceReference and could technically be
// a *string, ARN or Filter. However, elsewhere in the code it is only used
Expand Down
20 changes: 20 additions & 0 deletions pkg/cloud/aws/actuators/machine/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,26 @@ func TestImmutableStateChange(t *testing.T) {
},
expected: 1,
},
{
name: "root device size is unchanged",
machineSpec: v1alpha1.AWSMachineProviderSpec{
RootDeviceSize: 12,
},
instance: v1alpha1.Instance{
RootDeviceSize: 12,
},
expected: 0,
},
{
name: "root device size is changed",
machineSpec: v1alpha1.AWSMachineProviderSpec{
RootDeviceSize: 12,
},
instance: v1alpha1.Instance{
RootDeviceSize: 16,
},
expected: 1,
},
{
name: "multiple immutable changes",
machineSpec: v1alpha1.AWSMachineProviderSpec{
Expand Down
4 changes: 4 additions & 0 deletions pkg/cloud/aws/converters/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import (
"sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1alpha1"
)

// SDKToInstance converts an EC2 instance type to the CAPA
// instance type.
// Note: This does not return a complete instance, as rootVolumeSize
// can not be determined via the output of EC2.DescribeInstances.
func SDKToInstance(v *ec2.Instance) *v1alpha1.Instance {
i := &v1alpha1.Instance{
ID: aws.StringValue(v.InstanceId),
Expand Down
117 changes: 111 additions & 6 deletions pkg/cloud/aws/services/ec2/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"compress/gzip"
"encoding/base64"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
Expand Down Expand Up @@ -62,7 +63,7 @@ func (s *Service) InstanceByTags(machine *actuators.MachineScope) (*v1alpha1.Ins
// match
for _, res := range out.Reservations {
for _, inst := range res.Instances {
return converters.SDKToInstance(inst), nil
return s.SDKToInstance(inst)
}
}

Expand Down Expand Up @@ -95,7 +96,7 @@ func (s *Service) InstanceIfExists(id *string) (*v1alpha1.Instance, error) {
}

if len(out.Reservations) > 0 && len(out.Reservations[0].Instances) > 0 {
return converters.SDKToInstance(out.Reservations[0].Instances[0]), nil
return s.SDKToInstance(out.Reservations[0].Instances[0])
}

return nil, nil
Expand All @@ -106,8 +107,9 @@ func (s *Service) createInstance(machine *actuators.MachineScope, bootstrapToken
s.scope.V(2).Info("Creating an instance for a machine")

input := &v1alpha1.Instance{
Type: machine.MachineConfig.InstanceType,
IAMProfile: machine.MachineConfig.IAMInstanceProfile,
Type: machine.MachineConfig.InstanceType,
IAMProfile: machine.MachineConfig.IAMInstanceProfile,
RootDeviceSize: machine.MachineConfig.RootDeviceSize,
}

input.Tags = tags.Build(tags.BuildParams{
Expand Down Expand Up @@ -395,6 +397,23 @@ func (s *Service) runInstance(role string, i *v1alpha1.Instance) (*v1alpha1.Inst
}
}

if i.RootDeviceSize != 0 {
rootDeviceName, err := s.getImageRootDevice(i.ImageID)
if err != nil {
return nil, errors.Wrapf(err, "failed to get root volume from image %q", i.ImageID)
}

input.BlockDeviceMappings = []*ec2.BlockDeviceMapping{
{
DeviceName: rootDeviceName,
Ebs: &ec2.EbsBlockDevice{
DeleteOnTermination: aws.Bool(true),
VolumeSize: aws.Int64(i.RootDeviceSize),
},
},
}
}

if len(i.Tags) > 0 {
spec := &ec2.TagSpecification{ResourceType: aws.String(ec2.ResourceTypeInstance)}
for key, value := range i.Tags {
Expand All @@ -416,8 +435,9 @@ func (s *Service) runInstance(role string, i *v1alpha1.Instance) (*v1alpha1.Inst
return nil, errors.Errorf("no instance returned for reservation %v", out.GoString())
}

err = s.scope.EC2.WaitUntilInstanceRunning(&ec2.DescribeInstancesInput{InstanceIds: []*string{out.Instances[0].InstanceId}})
return converters.SDKToInstance(out.Instances[0]), err
s.scope.EC2.WaitUntilInstanceRunning(&ec2.DescribeInstancesInput{InstanceIds: []*string{out.Instances[0].InstanceId}})

return s.SDKToInstance(out.Instances[0])
}

// UpdateInstanceSecurityGroups modifies the security groups of the given
Expand Down Expand Up @@ -511,3 +531,88 @@ func (s *Service) getInstanceENIs(instanceID string) ([]*ec2.NetworkInterface, e

return output.NetworkInterfaces, nil
}

func (s *Service) getImageRootDevice(imageID string) (*string, error) {
input := &ec2.DescribeImagesInput{
ImageIds: []*string{aws.String(imageID)},
}

output, err := s.scope.EC2.DescribeImages(input)
if err != nil {
return nil, err
}

if len(output.Images) == 0 {
return nil, errors.Errorf("no images returned when looking up ID %q", imageID)
}

return output.Images[0].RootDeviceName, nil
}

func (s *Service) getInstanceRootDeviceSize(instance *ec2.Instance) (*int64, error) {

for _, bdm := range instance.BlockDeviceMappings {
if aws.StringValue(bdm.DeviceName) == aws.StringValue(instance.RootDeviceName) {
input := &ec2.DescribeVolumesInput{
VolumeIds: []*string{bdm.Ebs.VolumeId},
}

out, err := s.scope.EC2.DescribeVolumes(input)
if err != nil {
return nil, err
}

if len(out.Volumes) == 0 {
return nil, errors.Errorf("no volumes found for id %q", aws.StringValue(bdm.Ebs.VolumeId))
}

return out.Volumes[0].Size, nil
}
}
return nil, nil
}

// SDKToInstance converts an AWS EC2 SDK instance to the CAPA instance type.
// converters.SDKToInstance populates all instance fields except for rootVolumeSize,
// because EC2.DescribeInstances does not return the size of storage devices. An
// additional call to EC2 is required to get this value.
func (s *Service) SDKToInstance(v *ec2.Instance) (*v1alpha1.Instance, error) {
i := &v1alpha1.Instance{
ID: aws.StringValue(v.InstanceId),
State: v1alpha1.InstanceState(*v.State.Name),
Type: aws.StringValue(v.InstanceType),
SubnetID: aws.StringValue(v.SubnetId),
ImageID: aws.StringValue(v.ImageId),
KeyName: v.KeyName,
PrivateIP: v.PrivateIpAddress,
PublicIP: v.PublicIpAddress,
ENASupport: v.EnaSupport,
EBSOptimized: v.EbsOptimized,
}

// Extract IAM Instance Profile name from ARN
// TODO: Handle this comparison more safely, perhaps by querying IAM for the
// instance profile ARN and comparing to the ARN returned by EC2
if v.IamInstanceProfile != nil && v.IamInstanceProfile.Arn != nil {
split := strings.Split(aws.StringValue(v.IamInstanceProfile.Arn), "instance-profile/")
if len(split) > 1 && split[1] != "" {
i.IAMProfile = split[1]
}
}

for _, sg := range v.SecurityGroups {
i.SecurityGroupIDs = append(i.SecurityGroupIDs, *sg.GroupId)
}

if len(v.Tags) > 0 {
i.Tags = converters.TagsToMap(v.Tags)
}

rootSize, err := s.getInstanceRootDeviceSize(v)
if err != nil {
return nil, errors.Wrapf(err, "unable to get root volume size for instance: %q", aws.StringValue(v.InstanceId))
}

i.RootDeviceSize = aws.Int64Value(rootSize)
return i, nil
}