Skip to content

Commit

Permalink
disk: support specify disk tags with diskTags/key: value
Browse files Browse the repository at this point in the history
To be consistent with snapshot tags,
and allow arbitrary characters in key or value.
  • Loading branch information
huww98 committed May 28, 2024
1 parent c223758 commit 2d34a3d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
45 changes: 23 additions & 22 deletions pkg/disk/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ var (
CustomDiskPerfermance = sets.NewString(DISK_PERFORMANCE_LEVEL0, DISK_PERFORMANCE_LEVEL1, DISK_PERFORMANCE_LEVEL2, DISK_PERFORMANCE_LEVEL3)
)

const DISK_TAG_PREFIX = "diskTags/"

// DefaultOptions is the struct for access key
type DefaultOptions struct {
Global struct {
Expand Down Expand Up @@ -663,29 +665,28 @@ func getDiskVolumeOptions(req *csi.CreateVolumeRequest) (*diskVolumeArgs, error)
}

// DiskTags
diskTags, ok := volOptions["diskTags"]
if ok {
for _, tag := range strings.Split(diskTags, ",") {
k, v, found := strings.Cut(tag, ":")
if !found {
return nil, fmt.Errorf("invalid diskTags format name: %s tags: %s", req.GetName(), diskTags)
for k, v := range volOptions {
switch k {
case "diskTags":
for _, tag := range strings.Split(v, ",") {
k, v, found := strings.Cut(tag, ":")
if !found {
return nil, fmt.Errorf("invalid diskTags format name: %s tag: %s", req.GetName(), tag)
}
diskVolArgs.DiskTags[k] = v
}
// k8s PV info as disk tags
case common.PVCNameKey:
diskVolArgs.DiskTags[common.PVCNameTag] = v
case common.PVNameKey:
diskVolArgs.DiskTags[common.PVNameTag] = v
case common.PVCNamespaceKey:
diskVolArgs.DiskTags[common.PVCNamespaceTag] = v
default:
// new custom tags
if strings.HasPrefix(k, DISK_TAG_PREFIX) {
diskVolArgs.DiskTags[k[len(DISK_TAG_PREFIX):]] = v
}
diskVolArgs.DiskTags[k] = v
}
}
// k8s PV info as disk tags
{
pvcName, ok := volOptions[common.PVCNameKey]
if ok {
diskVolArgs.DiskTags[common.PVCNameTag] = pvcName
}
pvName, ok := volOptions[common.PVNameKey]
if ok {
diskVolArgs.DiskTags[common.PVNameTag] = pvName
}
ns, ok := volOptions[common.PVCNamespaceKey]
if ok {
diskVolArgs.DiskTags[common.PVCNamespaceTag] = ns
}
}

Expand Down
28 changes: 28 additions & 0 deletions pkg/disk/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,31 @@ func TestValidateCapabilities(t *testing.T) {
})
}
}

func TestGetDiskVolumeOptions(t *testing.T) {
req := &csi.CreateVolumeRequest{
Parameters: map[string]string{
"zoneId": "cn-beijing-i",
"diskTags": "key1:value1,key2:v:value2",
"diskTags/key3": "value3",
"diskTags/key/abc=4": "v4:asdf,qwer",
"csi.storage.k8s.io/pvc/name": "pvc-123",
"csi.storage.k8s.io/pvc/namespace": "default",
"csi.storage.k8s.io/pv/name": "pv-123",
},
}
args, err := getDiskVolumeOptions(req)
assert.NoError(t, err)

assert.Equal(t, "cn-beijing-i", args.ZoneID)
assert.Equal(t, map[string]string{
"key1": "value1",
"key2": "v:value2",
"key3": "value3",
"key/abc=4": "v4:asdf,qwer",

"kubernetes.io/created-for/pvc/name": "pvc-123",
"kubernetes.io/created-for/pvc/namespace": "default",
"kubernetes.io/created-for/pv/name": "pv-123",
}, args.DiskTags)
}

0 comments on commit 2d34a3d

Please sign in to comment.