Skip to content

Replace templates in SC parameter with PVC annotations #808

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 4 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
24 changes: 20 additions & 4 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ const (
snapshotAPIGroup = snapapi.GroupName // "snapshot.storage.k8s.io"
pvcKind = "PersistentVolumeClaim" // Native types don't require an API group

tokenPVNameKey = "pv.name"
tokenPVCNameKey = "pvc.name"
tokenPVCNameSpaceKey = "pvc.namespace"
tokenPVNameKey = "pv.name"
tokenPVCNameKey = "pvc.name"
tokenPVCNameSpaceKey = "pvc.namespace"
tokenPVCAnnoationTemplatePrefix = "pvc.annotations"

ResyncPeriodOfCsiNodeInformer = 1 * time.Hour

Expand Down Expand Up @@ -595,9 +596,24 @@ func (p *csiProvisioner) prepareProvision(ctx context.Context, claim *v1.Persist
return nil, controller.ProvisioningFinished, err
}

pvcAnnotationsForSCParam := map[string]string{}
for k, v := range claim.GetAnnotations() {
pvcAnnotationsForSCParam[tokenPVCAnnoationTemplatePrefix+"['"+k+"']"] = v
}

fsTypesFound := 0
fsType := ""
for k, v := range sc.Parameters {
// Replace SC parameters with PVC annoations
if strings.Contains(v, tokenPVCAnnoationTemplatePrefix) {
resolvedName, err := resolveTemplate(v, pvcAnnotationsForSCParam)

if err != nil {
return nil, controller.ProvisioningFinished, fmt.Errorf("error resolving value %q: %v", v, err)
}

sc.Parameters[k] = resolvedName
}
if strings.ToLower(k) == "fstype" || k == prefixedFsTypeKey {
fsType = v
fsTypesFound++
Expand Down Expand Up @@ -1749,7 +1765,7 @@ func getSecretReference(secretParams secretParamsMap, storageClassParams map[str
nameParams[tokenPVCNameKey] = pvc.Name
nameParams[tokenPVCNameSpaceKey] = pvc.Namespace
for k, v := range pvc.Annotations {
nameParams["pvc.annotations['"+k+"']"] = v
nameParams[tokenPVCAnnoationTemplatePrefix+"['"+k+"']"] = v
}
}
resolvedName, err := resolveTemplate(nameTemplate, nameParams)
Expand Down
68 changes: 65 additions & 3 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,10 +800,9 @@ func TestGetSecretReference(t *testing.T) {
t.Run(k, func(t *testing.T) {
ref, err := getSecretReference(tc.secretParams, tc.params, tc.pvName, tc.pvc)
if err != nil {
if tc.expectErr {
return
if !tc.expectErr {
t.Fatalf("Did not expect error but got: %v", err)
}
t.Fatalf("Did not expect error but got: %v", err)
} else {
if tc.expectErr {
t.Fatalf("Expected error but got none")
Expand All @@ -816,6 +815,69 @@ func TestGetSecretReference(t *testing.T) {
}
}

func TestResolveTemplate(t *testing.T) {
testcases := map[string]struct {
template string
params map[string]string

expectedResolved string
expectErr bool
}{
"no params": {
expectedResolved: "",
expectErr: false,
},
"nil template": {
template: "",
params: map[string]string{tokenPVCAnnoationTemplatePrefix + "['subDir']": "dir1", nodePublishSecretNamespaceKey: "value1"},
expectedResolved: "",
expectErr: false,
},
"nil params": {
template: tokenPVCAnnoationTemplatePrefix + "['subDir']",
params: nil,
expectedResolved: tokenPVCAnnoationTemplatePrefix + "['subDir']",
expectErr: false,
},
"not a template": {
template: nodePublishSecretNamespaceKey,
params: map[string]string{tokenPVCAnnoationTemplatePrefix + "['subDir']": "dir1", nodePublishSecretNamespaceKey: "value1"},
expectedResolved: nodePublishSecretNamespaceKey,
expectErr: false,
},
"template not exist": {
template: "${" + tokenPVCAnnoationTemplatePrefix + "['KeyNotExists']}",
params: map[string]string{tokenPVCAnnoationTemplatePrefix + "['subDir']": "dir1", nodePublishSecretNamespaceKey: "value1"},
expectedResolved: "",
expectErr: true,
},
"matched case": {
template: "${" + tokenPVCAnnoationTemplatePrefix + "['subDir']}",
params: map[string]string{tokenPVCAnnoationTemplatePrefix + "['subDir']": "dir1", tokenPVCAnnoationTemplatePrefix + "['subDir2']": "dir2", nodePublishSecretNamespaceKey: "value1"},
expectedResolved: "dir1",
expectErr: false,
},
}

for k, tc := range testcases {
t.Run(k, func(t *testing.T) {
actualResolved, err := resolveTemplate(tc.template, tc.params)
if err != nil {
if !tc.expectErr {
t.Fatalf("Did not expect error but got: %v", err)
}
} else {
if tc.expectErr {
t.Fatalf("Expected error but got none")
}
}
if actualResolved != tc.expectedResolved {
t.Errorf("Expected %s, got %s", tc.expectedResolved, actualResolved)
}
})
}
}

type provisioningTestcase struct {
capacity int64 // if zero, default capacity, otherwise available bytes
volOpts controller.ProvisionOptions
Expand Down