Skip to content

Adding Proper Handling of VAC and Controller Tags On Volume Creation #2470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 62 additions & 4 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (d *ControllerService) CreateVolume(ctx context.Context, req *csi.CreateVol
}
}

modifyOptions, err := parseModifyVolumeParameters(req.GetMutableParameters())
modifyOptions, err := parseCreateRequestMutableParameters(req.GetMutableParameters(), d.options.ExtraTags, *tProps)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Invalid mutable parameter: %v", err)
}
Expand Down Expand Up @@ -301,9 +301,6 @@ func (d *ControllerService) CreateVolume(ctx context.Context, req *csi.CreateVol
volumeTags[NameTag] = d.options.KubernetesClusterID + "-dynamic-" + volName
volumeTags[KubernetesClusterTag] = d.options.KubernetesClusterID
}
for k, v := range d.options.ExtraTags {
volumeTags[k] = v
}

addTags, err := template.Evaluate(scTags, tProps, d.options.WarnOnInvalidTag)
if err != nil {
Expand All @@ -318,6 +315,10 @@ func (d *ControllerService) CreateVolume(ctx context.Context, req *csi.CreateVol
volumeTags[k] = v
}

for k, v := range modifyOptions.modifyTagsOptions.TagsToAdd {
Copy link
Contributor

@AndrewSirenko AndrewSirenko May 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why VAC and 'extra' tags being evaluated and validated separately from SCTags? (But lines 402-409 are duplicate of 308-315?)

A smell here is that tags specified via extra-tags option would wind up in the unrelated struct modifyOptions.modifyTagOptions.TagsToAdd.


Have you considered instead:

  1. Collating all validated scTags, extraTags, and vacTags into an array called tagsToEvaluate
  2. Put them through template.Evaluate and validateExtraTags
  3. Append them to list of volumeTags that get passed in to cloud.DiskOptions

That way all tags, no matter their source, go through a clear set of steps? Bonus points if these steps are located near each other so it's easy to see how all tags are processed.

volumeTags[k] = v
}

opts := &cloud.DiskOptions{
CapacityBytes: volSizeBytes,
Tags: volumeTags,
Expand Down Expand Up @@ -352,6 +353,63 @@ func (d *ControllerService) CreateVolume(ctx context.Context, req *csi.CreateVol
return newCreateVolumeResponse(disk, responseCtx), nil
}

func parseCreateRequestMutableParameters(params map[string]string, extraTags map[string]string, tProps template.PVProps) (*modifyVolumeRequest, error) {
options := modifyVolumeRequest{
modifyTagsOptions: cloud.ModifyTagsOptions{
TagsToAdd: make(map[string]string),
TagsToDelete: make([]string, 0),
},
}

rawTagsToAdd := []string{}
for key, value := range params {
switch key {
case ModificationKeyIOPS:
Copy link
Contributor

@AndrewSirenko AndrewSirenko May 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit worried at the amount code duplication we have for validating parameters that can be specified in either the SC versus mutable_parameters from VAC. Can we consolidate the logic?

Before this PR there are 2 separate functions that validate parameters shared between SC and VAC like IOPS:

  • controller.go:
    case IopsKey:
    parseIopsKey, parseIopsKeyErr := strconv.ParseInt(value, 10, 32)
    if parseIopsKeyErr != nil {
    return nil, status.Errorf(codes.InvalidArgument, "Could not parse invalid iops: %v", err)
    }
    iops = int32(parseIopsKey)
  • controller_modify_volume.go:
    case ModificationKeyIOPS:
    iops, err := strconv.ParseInt(value, 10, 32)
    if err != nil {
    return nil, status.Errorf(codes.InvalidArgument, "Could not parse IOPS: %q", value)
    }
    options.modifyDiskOptions.IOPS = int32(iops)

This PR adds a third place we copy paste same exact code. (Which means there are three separate places to forget about when dealing with new parameters)


One possible solution for the create path would be to overwrite SC parameters with values from VAC mutable_parameters BEFORE validating them. They share the same parameter key anyway, and can be validated within the existing loop + switch statement.

This is in line with CSI Spec:

Values specified in mutable_parameters MUST take precedence over the values from parameters.

Alternatively we can refactor the switch statement + for loop into a shared validate function used in Create and Modify paths.

iops, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Could not parse IOPS: %q", value)
}
options.modifyDiskOptions.IOPS = int32(iops)
case ModificationKeyThroughput:
throughput, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "Could not parse throughput: %q", value)
}
options.modifyDiskOptions.Throughput = int32(throughput)
case DeprecatedModificationKeyVolumeType:
if _, ok := params[ModificationKeyVolumeType]; ok {
klog.Infof("Ignoring deprecated key `volumeType` because preferred key `type` is present")
continue
}
klog.InfoS("Key `volumeType` is deprecated, please use `type` instead")
options.modifyDiskOptions.VolumeType = value
case ModificationKeyVolumeType:
options.modifyDiskOptions.VolumeType = value
default:
switch {
case strings.HasPrefix(key, ModificationAddTag):
rawTagsToAdd = append(rawTagsToAdd, value)
default:
return nil, status.Errorf(codes.InvalidArgument, "Invalid mutable parameter key: %s", key)
}
}
}

for k, v := range extraTags {
rawTagsToAdd = append(rawTagsToAdd, k+"="+v)
}

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
}

Copy link
Contributor

@AndrewSirenko AndrewSirenko May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General comment: Should we add a unit test that ensures all tags get evaluated for string interpolation to prevent regressions? (SCTags, VACTags, and Extra tags can be tested in the same unit test)

func validateCreateVolumeRequest(req *csi.CreateVolumeRequest) error {
volName := req.GetName()
if len(volName) == 0 {
Expand Down