diff --git a/README.md b/README.md index 5b2f816a..2161c1ad 100644 --- a/README.md +++ b/README.md @@ -210,7 +210,7 @@ The `setupCommand` and `teardownCommand` allow you to specify the path to binary | -s | Requested volume size in bytes. | -s | Requested volume size in bytes. | | -a | Action type. Can be `create` or `delete` | -a | -a | Action type. -The `setupCommand` and `teardownCommand` have higher priority than the `setup` and `teardown` scripts from the ConfigMap. +The `setupCommand` and `teardownCommand` have higher priority than the `setup` and `teardown` scripts from the ConfigMap. ##### Rules The configuration must obey following rules: @@ -277,7 +277,12 @@ A few things to note; the annotation for the `StorageClass` will apply to all vo If more than one `paths` are specified in the `nodePathMap` the path is chosen randomly. To make the provisioner choose a specific path, use a `storageClass` defined with a parameter called `nodePath`. Note that this path should be defined in the `nodePathMap`. -By default the volume subdirectory is named using the template `{{ .PVName }}_{{ .PVC.Namespace }}_{{ .PVC.Name }}` which make the directory specific to the PV instance. The template can be changed using the `pathPattern` parameter which is interpreted as a go template. The template has access to the PV name using the `PVName` variable and the PVC metadata object, including labels and annotations, with the `PVC` variable. +By default the volume subdirectory is named using the template `{{ .PVName }}_{{ .PVC.Namespace }}_{{ .PVC.Name }}` which make the directory specific to the PV instance. The template can be changed using the `pathPattern` parameter which is interpreted as a go template. + +The volume name can also be changed in the same way by using the `volumeNamePattern` parameter. Although not generated by using templates under the hood, default value for volume name matches the template `pvc-{{ .PVC.UID }}`. + +The templates have access to the PV name using the `PVName` variable and the PVC metadata object, including labels and annotations, with the `PVC` variable. + ``` apiVersion: storage.k8s.io/v1 kind: StorageClass @@ -287,6 +292,7 @@ provisioner: rancher.io/local-path parameters: nodePath: /data/ssd pathPattern: "{{ .PVC.Namespace }}/{{ .PVC.Name }}" + volumeNamePattern: "{{ .PVC.Annotations.volumeName }}" volumeBindingMode: WaitForFirstConsumer reclaimPolicy: Delete ``` diff --git a/examples/pvc-with-patterns/kustomization.yaml b/examples/pvc-with-patterns/kustomization.yaml new file mode 100644 index 00000000..7bfd4518 --- /dev/null +++ b/examples/pvc-with-patterns/kustomization.yaml @@ -0,0 +1,4 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: +- pvc.yaml diff --git a/examples/pvc-with-patterns/pvc.yaml b/examples/pvc-with-patterns/pvc.yaml new file mode 100644 index 00000000..f5204162 --- /dev/null +++ b/examples/pvc-with-patterns/pvc.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: local-path-pvc-2 + annotations: + volumeName: local-path-pv + volumeDirName: local-path-pv-dir +spec: + accessModes: + - ReadWriteOnce + storageClassName: local-path + resources: + requests: + storage: 128Mi diff --git a/provisioner.go b/provisioner.go index ba22720e..402de669 100644 --- a/provisioner.go +++ b/provisioner.go @@ -297,13 +297,13 @@ type pvMetadata struct { PVC metav1.ObjectMeta } -func pathFromPattern(pattern string, opts pvController.ProvisionOptions) (string, error) { +func parseFromPattern(patternName string, pattern string, opts pvController.ProvisionOptions) (string, error) { metadata := pvMetadata{ PVName: opts.PVName, PVC: opts.PVC.ObjectMeta, } - tpl, err := template.New("pathPattern").Parse(pattern) + tpl, err := template.New(patternName).Parse(pattern) if err != nil { return "", err } @@ -366,9 +366,20 @@ func (p *LocalPathProvisioner) provisionFor(opts pvController.ProvisionOptions, name := opts.PVName folderName := strings.Join([]string{name, opts.PVC.Namespace, opts.PVC.Name}, "_") - pathPattern, exists := opts.StorageClass.Parameters["pathPattern"] + volumeNamePatternKey := "volumeNamePattern" + volumeNamePattern, exists := opts.StorageClass.Parameters[volumeNamePatternKey] if exists { - folderName, err = pathFromPattern(pathPattern, opts) + name, err = parseFromPattern(volumeNamePatternKey, volumeNamePattern, opts) + if err != nil { + err = errors.Wrapf(err, "failed to create volume name from pattern %v", volumeNamePattern) + return nil, pvController.ProvisioningFinished, err + } + } + + pathPatternKey := "pathPattern" + pathPattern, exists := opts.StorageClass.Parameters[pathPatternKey] + if exists { + folderName, err = parseFromPattern(pathPatternKey, pathPattern, opts) if err != nil { err = errors.Wrapf(err, "failed to create path from pattern %v", pathPattern) return nil, pvController.ProvisioningFinished, err