Skip to content
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

disk: support specify disk tags with diskTags/key: value #1000

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
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
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)
}