diff --git a/examples/kubernetes/modify-volume/manifests/volumeattributesclass.yaml b/examples/kubernetes/modify-volume/manifests/volumeattributesclass.yaml index 5274403fa..f73bfca36 100644 --- a/examples/kubernetes/modify-volume/manifests/volumeattributesclass.yaml +++ b/examples/kubernetes/modify-volume/manifests/volumeattributesclass.yaml @@ -22,4 +22,4 @@ parameters: type: io2 iops: "10000" tagSpecification_1: "location=Seattle" - tagSpecification_2: "cost-center=" + tagSpecification_2: "cost-center=" \ No newline at end of file diff --git a/pkg/driver/controller_modify_volume.go b/pkg/driver/controller_modify_volume.go index cbfceaa7c..5b8f8c786 100644 --- a/pkg/driver/controller_modify_volume.go +++ b/pkg/driver/controller_modify_volume.go @@ -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" @@ -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: @@ -191,20 +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 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 } diff --git a/pkg/driver/controller_modify_volume_test.go b/pkg/driver/controller_modify_volume_test.go index 5e5eea68d..b128e981c 100644 --- a/pkg/driver/controller_modify_volume_test.go +++ b/pkg/driver/controller_modify_volume_test.go @@ -62,8 +62,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{ @@ -74,6 +80,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",