Skip to content

Commit

Permalink
add support for EBS snapshotID in block device mappings (#1554)
Browse files Browse the repository at this point in the history
* add support for EBS snapshotID in block device mappings

* add volumeSize func
  • Loading branch information
bwagner5 authored Mar 21, 2022
1 parent 6e14b61 commit f425d5c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pkg/cloudprovider/aws/apis/v1alpha1/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ type BlockDevice struct {
// KMSKeyID (ARN) of the symmetric Key Management Service (KMS) CMK used for encryption.
KMSKeyID *string `json:"kmsKeyID,omitempty"`

// SnapshotID is the ID of an EBS snapshot
SnapshotID *string `json:"snapshotID,omitempty"`

// Throughput to provision for a gp3 volume, with a maximum of 1,000 MiB/s.
// Valid Range: Minimum value of 125. Maximum value of 1000.
Throughput *int64 `json:"throughput,omitempty"`
Expand Down
5 changes: 4 additions & 1 deletion pkg/cloudprovider/aws/apis/v1alpha1/provider_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ func (a *AWS) validateVolumeType(blockDeviceMapping *BlockDeviceMapping) *apis.F
}

func (a *AWS) validateVolumeSize(blockDeviceMapping *BlockDeviceMapping) *apis.FieldError {
if blockDeviceMapping.EBS.VolumeSize == nil {
// if an EBS mapping is present, one of volumeSize or snapshotID must be present
if blockDeviceMapping.EBS.SnapshotID != nil && blockDeviceMapping.EBS.VolumeSize == nil {
return nil
} else if blockDeviceMapping.EBS.VolumeSize == nil {
return apis.ErrMissingField("volumeSize")
} else if blockDeviceMapping.EBS.VolumeSize.Cmp(minVolumeSize) == -1 || blockDeviceMapping.EBS.VolumeSize.Cmp(maxVolumeSize) == 1 {
return apis.ErrOutOfBoundsValue(blockDeviceMapping.EBS.VolumeSize.String(), minVolumeSize.String(), maxVolumeSize.String(), "volumeSize")
Expand Down
5 changes: 5 additions & 0 deletions pkg/cloudprovider/aws/apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion pkg/cloudprovider/aws/launchtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,22 @@ func (p *LaunchTemplateProvider) blockDeviceMappings(blockDeviceMappings []*v1al
Iops: blockDeviceMapping.EBS.IOPS,
Throughput: blockDeviceMapping.EBS.Throughput,
KmsKeyId: blockDeviceMapping.EBS.KMSKeyID,
VolumeSize: aws.Int64(blockDeviceMapping.EBS.VolumeSize.ScaledValue(resource.Giga)),
SnapshotId: blockDeviceMapping.EBS.SnapshotID,
VolumeSize: p.volumeSize(blockDeviceMapping.EBS.VolumeSize),
},
})
}
return blockDeviceMappingsRequest
}

// volumeSize returns a Giga scaled value from a resource quantity or nil if the resource quantity passed in is nil
func (p *LaunchTemplateProvider) volumeSize(quantity *resource.Quantity) *int64 {
if quantity == nil {
return nil
}
return aws.Int64(quantity.ScaledValue(resource.Giga))
}

// hydrateCache queries for existing Launch Templates created by Karpenter for the current cluster and adds to the LT cache.
// Any error during hydration will result in a panic
func (p *LaunchTemplateProvider) hydrateCache(ctx context.Context) {
Expand Down
12 changes: 12 additions & 0 deletions pkg/cloudprovider/aws/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,18 @@ var _ = Describe("Allocation", func() {
provisioner := ProvisionerWithProvider(provisioner, provider)
Expect(provisioner.Validate(ctx)).To(Succeed())
})
It("should validate ebs device mapping with snapshotID only", func() {
provider, err := ProviderFromProvisioner(provisioner)
Expect(err).ToNot(HaveOccurred())
provider.BlockDeviceMappings = []*v1alpha1.BlockDeviceMapping{{
DeviceName: aws.String("/dev/xvda"),
EBS: &v1alpha1.BlockDevice{
SnapshotID: aws.String("snap-0123456789"),
},
}}
provisioner := ProvisionerWithProvider(provisioner, provider)
Expect(provisioner.Validate(ctx)).To(Succeed())
})
It("should not allow volume size below minimum", func() {
provider, err := ProviderFromProvisioner(provisioner)
Expect(err).ToNot(HaveOccurred())
Expand Down
1 change: 1 addition & 0 deletions website/content/en/preview/AWS/provisioning.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ spec:
kmsKeyID: "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"
deleteOnTermination: true
throughput: 125
snapshotID: snap-0123456789
```

## Other Resources
Expand Down

0 comments on commit f425d5c

Please sign in to comment.