Skip to content

Commit

Permalink
Supporting String Interpolation for Modifying Tags On Existing Volume…
Browse files Browse the repository at this point in the history
…s Through VAC
  • Loading branch information
mdzraf committed Aug 14, 2024
1 parent b8e3563 commit b967901
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
22 changes: 16 additions & 6 deletions pkg/driver/controller_modify_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/awslabs/volume-modifier-for-k8s/pkg/rpc"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/cloud"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/coalescer"
"github.com/kubernetes-sigs/aws-ebs-csi-driver/pkg/util/template"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -168,6 +169,8 @@ func parseModifyVolumeParameters(params map[string]string) (*modifyVolumeRequest
TagsToDelete: make([]string, 0),
},
}
var rawTagsToAdd []string
tProps := new(template.PVProps)
for key, value := range params {
switch key {
case ModificationKeyIOPS:
Expand All @@ -191,22 +194,29 @@ func parseModifyVolumeParameters(params map[string]string) (*modifyVolumeRequest
options.modifyDiskOptions.VolumeType = value
case ModificationKeyVolumeType:
options.modifyDiskOptions.VolumeType = value
case PVCNameKey:
tProps.PVCName = value
case PVCNamespaceKey:
tProps.PVCNamespace = value
case PVNameKey:
tProps.PVName = value
default:
if strings.HasPrefix(key, ModificationAddTag) {
st := strings.SplitN(value, "=", 2)
if len(st) < 2 {
return nil, status.Errorf(codes.InvalidArgument, "Invalid tag specification: %v", st)
}
options.modifyTagsOptions.TagsToAdd[st[0]] = st[1]
rawTagsToAdd = append(rawTagsToAdd, value)
} else if strings.HasPrefix(key, ModificationDeleteTag) {
options.modifyTagsOptions.TagsToDelete = append(options.modifyTagsOptions.TagsToDelete, value)
} else {
return nil, status.Errorf(codes.InvalidArgument, "Invalid mutable parameter key: %s", key)
}
}
}
if err := validateExtraTags(options.modifyTagsOptions.TagsToAdd, false); err != nil {
addTags, err := template.Evaluate(rawTagsToAdd, tProps, false)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Error interpolating the tag value: %v", err)
}
if err := validateExtraTags(addTags, false); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Invalid tag value: %v", err)
}
options.modifyTagsOptions.TagsToAdd = addTags
return &options, nil
}
11 changes: 10 additions & 1 deletion pkg/driver/controller_modify_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,14 @@ func TestParseModifyVolumeParameters(t *testing.T) {
ModificationKeyVolumeType: validType,
ModificationKeyIOPS: validIops,
ModificationKeyThroughput: validThroughput,
ModificationAddTag: validTagSpecificationInput,
ModificationAddTag + "_1": validTagSpecificationInput,
ModificationAddTag + "_2": "key2={{ .PVCName }}",
ModificationAddTag + "_3": "key3={{ .PVCNamespace }}",
ModificationAddTag + "_4": "key4={{ .PVName }}",
ModificationDeleteTag: validTagDeletion,
PVCNameKey: "ebs-claim",
PVCNamespaceKey: "test-namespace",
PVNameKey: "testPV-Name",
},
expectedOptions: &modifyVolumeRequest{
modifyDiskOptions: cloud.ModifyDiskOptions{
Expand All @@ -166,6 +172,9 @@ func TestParseModifyVolumeParameters(t *testing.T) {
modifyTagsOptions: cloud.ModifyTagsOptions{
TagsToAdd: map[string]string{
"key1": "tag1",
"key2": "ebs-claim",
"key3": "test-namespace",
"key4": "testPV-Name",
},
TagsToDelete: []string{
"key2",
Expand Down

0 comments on commit b967901

Please sign in to comment.