Skip to content

Commit

Permalink
Round up when launching a node with block device mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis committed Jan 31, 2023
1 parent 287ab08 commit cba928b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Pallinder/go-randomdata v1.2.0
github.com/avast/retry-go v3.0.0+incompatible
github.com/aws/aws-sdk-go v1.44.189
github.com/aws/karpenter-core v0.23.1-0.20230131022723-53e783dcaf19
github.com/aws/karpenter-core v0.23.1-0.20230131200554-558e8c538723
github.com/go-playground/validator/v10 v10.11.2
github.com/imdario/mergo v0.3.13
github.com/mitchellh/hashstructure/v2 v2.0.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHS
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/aws/aws-sdk-go v1.44.189 h1:9PBrjndH1uL5AN8818qI3duhQ4hgkMuLvqkJlg9MRyk=
github.com/aws/aws-sdk-go v1.44.189/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/karpenter-core v0.23.1-0.20230131022723-53e783dcaf19 h1:XyKV4kfgGaEARqxS+/Gj32ipl/jTwX19VRTOTAGY5tU=
github.com/aws/karpenter-core v0.23.1-0.20230131022723-53e783dcaf19/go.mod h1:WJQJrJR0uxiCxZPD8ooSA8AvY/39pDotFaVFod0dV0g=
github.com/aws/karpenter-core v0.23.1-0.20230131200554-558e8c538723 h1:Za+AfCA1ioffVGubRscWCVhyof1VdQjF5/i9MZguKbs=
github.com/aws/karpenter-core v0.23.1-0.20230131200554-558e8c538723/go.mod h1:WJQJrJR0uxiCxZPD8ooSA8AvY/39pDotFaVFod0dV0g=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/v1alpha1/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ type BlockDevice struct {
// * st1 and sc1: 125-16,384
//
// * standard: 1-1,024
VolumeSize *resource.Quantity `json:"volumeSize,omitempty"`
VolumeSize *resource.Quantity `json:"volumeSize,omitempty" hash:"string"`

// VolumeType of the block device.
// For more information, see Amazon EBS volume types (https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html)
Expand Down
6 changes: 4 additions & 2 deletions pkg/cloudprovider/launchtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ func (p *LaunchTemplateProvider) Invalidate(ctx context.Context, ltName string,
}

func launchTemplateName(options *amifamily.LaunchTemplate) string {
// We use the stringer in this hash function, so we catch changes in the blockDeviceMapping sizing
hash, err := hashstructure.Hash(options, hashstructure.FormatV2, nil)
if err != nil {
panic(fmt.Sprintf("hashing launch template, %s", err))
Expand Down Expand Up @@ -239,7 +240,7 @@ func (p *LaunchTemplateProvider) blockDeviceMappings(blockDeviceMappings []*v1al
// The EC2 API fails with empty slices and expects nil.
return nil
}
blockDeviceMappingsRequest := []*ec2.LaunchTemplateBlockDeviceMappingRequest{}
var blockDeviceMappingsRequest []*ec2.LaunchTemplateBlockDeviceMappingRequest
for _, blockDeviceMapping := range blockDeviceMappings {
blockDeviceMappingsRequest = append(blockDeviceMappingsRequest, &ec2.LaunchTemplateBlockDeviceMappingRequest{
DeviceName: blockDeviceMapping.DeviceName,
Expand All @@ -263,7 +264,8 @@ func (p *LaunchTemplateProvider) volumeSize(quantity *resource.Quantity) *int64
if quantity == nil {
return nil
}
return aws.Int64(int64(quantity.AsApproximateFloat64() / math.Pow(2, 30)))
// Converts the value to Gi and rounds up the value to the nearest Gi
return aws.Int64(int64(math.Ceil(quantity.AsApproximateFloat64() / math.Pow(2, 30))))
}

// hydrateCache queries for existing Launch Templates created by Karpenter for the current cluster and adds to the LT cache.
Expand Down
38 changes: 37 additions & 1 deletion pkg/cloudprovider/launchtemplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ var _ = Describe("LaunchTemplates", func() {
Expect(fakeEC2API.CalledWithCreateLaunchTemplateInput.Len()).To(Equal(1))
input := fakeEC2API.CalledWithCreateLaunchTemplateInput.Pop()
Expect(input.LaunchTemplateData.BlockDeviceMappings[0].Ebs).To(Equal(&ec2.LaunchTemplateEbsBlockDeviceRequest{
VolumeSize: aws.Int64(186),
VolumeSize: aws.Int64(187),
VolumeType: aws.String("io2"),
Iops: aws.Int64(10_000),
DeleteOnTermination: aws.Bool(true),
Expand All @@ -423,6 +423,42 @@ var _ = Describe("LaunchTemplates", func() {
KmsKeyId: aws.String("arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"),
}))
})
It("should round up for custom block device mappings when specified in gigabytes", func() {
nodeTemplate.Spec.AMIFamily = &v1alpha1.AMIFamilyAL2
nodeTemplate.Spec.BlockDeviceMappings = []*v1alpha1.BlockDeviceMapping{
{
DeviceName: aws.String("/dev/xvda"),
EBS: &v1alpha1.BlockDevice{
DeleteOnTermination: aws.Bool(true),
Encrypted: aws.Bool(true),
VolumeType: aws.String("io2"),
VolumeSize: lo.ToPtr(resource.MustParse("4G")),
IOPS: aws.Int64(10_000),
KMSKeyID: aws.String("arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"),
},
},
{
DeviceName: aws.String("/dev/xvdb"),
EBS: &v1alpha1.BlockDevice{
DeleteOnTermination: aws.Bool(true),
Encrypted: aws.Bool(true),
VolumeType: aws.String("io2"),
VolumeSize: lo.ToPtr(resource.MustParse("2G")),
IOPS: aws.Int64(10_000),
KMSKeyID: aws.String("arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"),
},
},
}
ExpectApplied(ctx, env.Client, provisioner, nodeTemplate)
pod := ExpectProvisioned(ctx, env.Client, cluster, recorder, provisioningController, prov, coretest.UnschedulablePod())[0]
ExpectScheduled(ctx, env.Client, pod)
Expect(fakeEC2API.CalledWithCreateLaunchTemplateInput.Len()).To(Equal(1))
input := fakeEC2API.CalledWithCreateLaunchTemplateInput.Pop()

// Both of these values are rounded up when converting to Gibibytes
Expect(aws.Int64Value(input.LaunchTemplateData.BlockDeviceMappings[0].Ebs.VolumeSize)).To(BeNumerically("==", 4))
Expect(aws.Int64Value(input.LaunchTemplateData.BlockDeviceMappings[1].Ebs.VolumeSize)).To(BeNumerically("==", 2))
})
It("should default bottlerocket second volume with root volume size", func() {
nodeTemplate.Spec.AMIFamily = &v1alpha1.AMIFamilyBottlerocket
ExpectApplied(ctx, env.Client, provisioner, nodeTemplate)
Expand Down
2 changes: 1 addition & 1 deletion test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/aws/aws-sdk-go v1.44.189
github.com/aws/aws-sdk-go-v2/config v1.18.10
github.com/aws/karpenter v0.22.0
github.com/aws/karpenter-core v0.23.1-0.20230131022723-53e783dcaf19
github.com/aws/karpenter-core v0.23.1-0.20230131200554-558e8c538723
github.com/onsi/ginkgo/v2 v2.8.0
github.com/onsi/gomega v1.26.0
github.com/samber/lo v1.37.0
Expand Down
4 changes: 2 additions & 2 deletions test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.0 h1:Jfly6mRxk2ZOSlbCvZfKNS7T
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.0/go.mod h1:TZSH7xLO7+phDtViY/KUp9WGCJMQkLJ/VpgkTFd5gh8=
github.com/aws/aws-sdk-go-v2/service/sts v1.18.2 h1:J/4wIaGInCEYCGhTSruxCxeoA5cy91a+JT7cHFKFSHQ=
github.com/aws/aws-sdk-go-v2/service/sts v1.18.2/go.mod h1:+lGbb3+1ugwKrNTWcf2RT05Xmp543B06zDFTwiTLp7I=
github.com/aws/karpenter-core v0.23.1-0.20230131022723-53e783dcaf19 h1:XyKV4kfgGaEARqxS+/Gj32ipl/jTwX19VRTOTAGY5tU=
github.com/aws/karpenter-core v0.23.1-0.20230131022723-53e783dcaf19/go.mod h1:WJQJrJR0uxiCxZPD8ooSA8AvY/39pDotFaVFod0dV0g=
github.com/aws/karpenter-core v0.23.1-0.20230131200554-558e8c538723 h1:Za+AfCA1ioffVGubRscWCVhyof1VdQjF5/i9MZguKbs=
github.com/aws/karpenter-core v0.23.1-0.20230131200554-558e8c538723/go.mod h1:WJQJrJR0uxiCxZPD8ooSA8AvY/39pDotFaVFod0dV0g=
github.com/aws/smithy-go v1.11.2/go.mod h1:3xHYmszWVx2c0kIwQeEVf9uSm4fYZt67FBJnwub1bgM=
github.com/aws/smithy-go v1.13.5 h1:hgz0X/DX0dGqTYpGALqXJoRKRj5oQ7150i5FdTePzO8=
github.com/aws/smithy-go v1.13.5/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA=
Expand Down

0 comments on commit cba928b

Please sign in to comment.