Skip to content

Add pv labels #1185

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

Closed
wants to merge 5 commits into from
Closed
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
39 changes: 39 additions & 0 deletions pkg/common/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package common
import (
"fmt"
"strings"

"k8s.io/klog/v2"
)

const (
Expand Down Expand Up @@ -58,6 +60,11 @@ const (
tagKeyCreatedForSnapshotName = "kubernetes.io/created-for/volumesnapshot/name"
tagKeyCreatedForSnapshotNamespace = "kubernetes.io/created-for/volumesnapshot/namespace"
tagKeyCreatedForSnapshotContentName = "kubernetes.io/created-for/volumesnapshotcontent/name"

// Keys for labels to tag to PV
labelKeyCreatedForClaimNamespace = "kubernetes_io_created_for_pvc_namespace"
labelKeyCreatedForVolumeName = "kubernetes_io_created_for_pv_name"
labelKeyCreatedForClaimName = "kubernetes_io_created_for_pvc_name"
)

// DiskParameters contains normalized and defaulted disk parameters
Expand Down Expand Up @@ -130,10 +137,42 @@ func ExtractAndDefaultParameters(parameters map[string]string, driverName string
p.DiskEncryptionKMSKey = v
case ParameterKeyPVCName:
p.Tags[tagKeyCreatedForClaimName] = v
// Verify that the parameters satisfy label restrictions before adding them as label
var pvc_name = strings.ToLower(v)
if len(pvc_name) > 63 {
pvc_name = pvc_name[:63]
klog.V(4).Info("The disk label for PVC-name is truncated to match label restriction (<64 characters allowed)")
}
_, err := ConvertLabelsStringToMap(labelKeyCreatedForClaimNamespace + "=" + pvc_name)
if err != nil {
klog.V(4).Info("Unable to add PVC name as a label", err)
} else {
p.Labels[labelKeyCreatedForClaimName] = pvc_name
}
case ParameterKeyPVCNamespace:
p.Tags[tagKeyCreatedForClaimNamespace] = v
// Verify that the parameters satisfy label restrictions before adding them as label
var namespace_name = strings.ToLower(v)
_, err := ConvertLabelsStringToMap(labelKeyCreatedForClaimNamespace + "=" + namespace_name)
if err != nil {
klog.V(4).Info("Unable to add Namespace name as a label", err)
} else {
p.Labels[labelKeyCreatedForClaimNamespace] = namespace_name
}
case ParameterKeyPVName:
p.Tags[tagKeyCreatedForVolumeName] = v
// Verify that the parameters satisfy label restrictions before adding them as label
var pv_name = strings.ToLower(v)
if len(pv_name) > 63 {
pv_name = pv_name[:63]
klog.V(4).Info("The disk label for PV-name is truncated to match label restriction (<64 characters allowed)")
}
_, err := ConvertLabelsStringToMap(labelKeyCreatedForVolumeName + "=" + pv_name)
if err != nil {
klog.V(4).Info("Unable to add PV name as a label", err)
} else {
p.Labels[labelKeyCreatedForVolumeName] = pv_name
}
case ParameterKeyLabels:
paramLabels, err := ConvertLabelsStringToMap(v)
if err != nil {
Expand Down
26 changes: 25 additions & 1 deletion pkg/common/parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestExtractAndDefaultParameters(t *testing.T) {
ReplicationType: "none",
DiskEncryptionKMSKey: "",
Tags: map[string]string{tagKeyCreatedForClaimName: "testPVCName", tagKeyCreatedForClaimNamespace: "testPVCNamespace", tagKeyCreatedForVolumeName: "testPVName", tagKeyCreatedBy: "testDriver"},
Labels: map[string]string{},
Labels: map[string]string{labelKeyCreatedForClaimNamespace: "testpvcnamespace", labelKeyCreatedForVolumeName: "testpvname", labelKeyCreatedForClaimName: "testpvcname"},
},
},
{
Expand Down Expand Up @@ -178,6 +178,30 @@ func TestExtractAndDefaultParameters(t *testing.T) {
Labels: map[string]string{"key1": "value1", "label-1": "value-a", "label-2": "label-value-2"},
},
},
{
name: "pv labels invalid",
parameters: map[string]string{ParameterKeyPVCNamespace: "test-namespace-name-as-labels-with-label-restrictions-does-not-allow*_--and-63+char", ParameterKeyPVName: "test-pv-name-as-label-with-label-restriction-truncate-char-at63-these-are-additional_with*+", ParameterKeyPVCName: "test-pvc-name-as-labels-with-label-restrictions-does-not-allow*_--and-63+char"},
labels: map[string]string{},
expectParams: DiskParameters{
DiskType: "pd-standard",
ReplicationType: "none",
DiskEncryptionKMSKey: "",
Tags: map[string]string{tagKeyCreatedForClaimNamespace: "test-namespace-name-as-labels-with-label-restrictions-does-not-allow*_--and-63+char", tagKeyCreatedForVolumeName: "test-pv-name-as-label-with-label-restriction-truncate-char-at63-these-are-additional_with*+", tagKeyCreatedForClaimName: "test-pvc-name-as-labels-with-label-restrictions-does-not-allow*_--and-63+char", tagKeyCreatedBy: "testDriver"},
Labels: map[string]string{labelKeyCreatedForVolumeName: "test-pv-name-as-label-with-label-restriction-truncate-char-at63"},
},
},
{
name: "pv labels valid",
parameters: map[string]string{ParameterKeyPVCNamespace: "test-namespace-name-with-label-restrictions-allow_-and-63char", ParameterKeyPVName: "test-pv-name-with-label-restrictions-allow_-and-63char", ParameterKeyPVCName: "test-pvc-name-as-label-with-label-restriction-allow_-and-63char"},
labels: map[string]string{},
expectParams: DiskParameters{
DiskType: "pd-standard",
ReplicationType: "none",
DiskEncryptionKMSKey: "",
Tags: map[string]string{tagKeyCreatedForClaimNamespace: "test-namespace-name-with-label-restrictions-allow_-and-63char", tagKeyCreatedForVolumeName: "test-pv-name-with-label-restrictions-allow_-and-63char", tagKeyCreatedForClaimName: "test-pvc-name-as-label-with-label-restriction-allow_-and-63char", tagKeyCreatedBy: "testDriver"},
Labels: map[string]string{labelKeyCreatedForClaimNamespace: "test-namespace-name-with-label-restrictions-allow_-and-63char", labelKeyCreatedForVolumeName: "test-pv-name-with-label-restrictions-allow_-and-63char", labelKeyCreatedForClaimName: "test-pvc-name-as-label-with-label-restriction-allow_-and-63char"},
},
},
}

for _, tc := range tests {
Expand Down