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

feat: custom volume name template using parameter #479

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
```
Expand Down
4 changes: 4 additions & 0 deletions examples/pvc-with-patterns/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- pvc.yaml
14 changes: 14 additions & 0 deletions examples/pvc-with-patterns/pvc.yaml
Original file line number Diff line number Diff line change
@@ -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
19 changes: 15 additions & 4 deletions provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down