From 22117dcb5e5d7639f6d349709cb6fef412b8800c Mon Sep 17 00:00:00 2001 From: Jonathan Innis Date: Tue, 31 Jan 2023 11:31:53 -0800 Subject: [PATCH] Round up when launching a node with block device mappings --- go.mod | 2 +- go.sum | 4 +-- pkg/apis/v1alpha1/provider.go | 2 +- pkg/cloudprovider/launchtemplate.go | 5 ++-- pkg/cloudprovider/launchtemplate_test.go | 38 +++++++++++++++++++++++- test/go.mod | 2 +- test/go.sum | 4 +-- 7 files changed, 47 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 5bb7bd4ea0ae..29f23392c726 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index a1fc2847177d..8c8b706c3850 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/apis/v1alpha1/provider.go b/pkg/apis/v1alpha1/provider.go index e1d35537fa54..abc4c02e0a1b 100644 --- a/pkg/apis/v1alpha1/provider.go +++ b/pkg/apis/v1alpha1/provider.go @@ -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) diff --git a/pkg/cloudprovider/launchtemplate.go b/pkg/cloudprovider/launchtemplate.go index ce1bc94dc8d7..8ed10c93ad77 100644 --- a/pkg/cloudprovider/launchtemplate.go +++ b/pkg/cloudprovider/launchtemplate.go @@ -239,7 +239,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, @@ -263,7 +263,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. diff --git a/pkg/cloudprovider/launchtemplate_test.go b/pkg/cloudprovider/launchtemplate_test.go index a599c5bb2390..04b06aaa83a7 100644 --- a/pkg/cloudprovider/launchtemplate_test.go +++ b/pkg/cloudprovider/launchtemplate_test.go @@ -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), @@ -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) diff --git a/test/go.mod b/test/go.mod index b8dc4aa0f84c..f4a8c4a4dc14 100644 --- a/test/go.mod +++ b/test/go.mod @@ -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 diff --git a/test/go.sum b/test/go.sum index eb61f4607a7b..33b44ba10964 100644 --- a/test/go.sum +++ b/test/go.sum @@ -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=