Skip to content
Snippets Groups Projects
Commit af85381f authored by Alban Bedel's avatar Alban Bedel Committed by Derek Su
Browse files

Add support for custom path patterns


Add support for the `pathPattern` parameter, if set it is expanded as
a go template with the PV name and PVC metadata as input. This allow
configuring the provisioner to use predictable paths so volumes can be
prefilled externally or re-used.

Signed-off-by: default avatarAlban Bedel <alban.bedel@aerq.com>
parent 2d1a7094
Branches
Tags
No related merge requests found
...@@ -275,8 +275,9 @@ A few things to note; the annotation for the `StorageClass` will apply to all vo ...@@ -275,8 +275,9 @@ A few things to note; the annotation for the `StorageClass` will apply to all vo
### Storage classes ### Storage classes
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` 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.
``` ```
apiVersion: storage.k8s.io/v1 apiVersion: storage.k8s.io/v1
kind: StorageClass kind: StorageClass
...@@ -285,11 +286,12 @@ metadata: ...@@ -285,11 +286,12 @@ metadata:
provisioner: rancher.io/local-path provisioner: rancher.io/local-path
parameters: parameters:
nodePath: /data/ssd nodePath: /data/ssd
pathPattern: "{{ .PVC.Namespace }}/{{ .PVC.Name }}"
volumeBindingMode: WaitForFirstConsumer volumeBindingMode: WaitForFirstConsumer
reclaimPolicy: Delete reclaimPolicy: Delete
``` ```
Here the provisioner will use the path `/data/ssd` when storage class `ssd-local-path` is used. Here the provisioner will use the path `/data/ssd` with a subdirectory per namespace and PVC when storage class `ssd-local-path` is used.
## Uninstall ## Uninstall
......
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"text/template"
"time" "time"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
...@@ -291,6 +292,31 @@ func (p *LocalPathProvisioner) pickConfig(storageClassName string) (*StorageClas ...@@ -291,6 +292,31 @@ func (p *LocalPathProvisioner) pickConfig(storageClassName string) (*StorageClas
return &cfg, nil return &cfg, nil
} }
type pvMetadata struct {
PVName string
PVC metav1.ObjectMeta
}
func pathFromPattern(pattern string, opts pvController.ProvisionOptions) (string, error) {
metadata := pvMetadata{
PVName: opts.PVName,
PVC: opts.PVC.ObjectMeta,
}
tpl, err := template.New("pathPattern").Parse(pattern)
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
err = tpl.Execute(buf, metadata)
if err != nil {
return "", err
}
return buf.String(), nil
}
func (p *LocalPathProvisioner) Provision(ctx context.Context, opts pvController.ProvisionOptions) (*v1.PersistentVolume, pvController.ProvisioningState, error) { func (p *LocalPathProvisioner) Provision(ctx context.Context, opts pvController.ProvisionOptions) (*v1.PersistentVolume, pvController.ProvisioningState, error) {
cfg, err := p.pickConfig(opts.StorageClass.Name) cfg, err := p.pickConfig(opts.StorageClass.Name)
if err != nil { if err != nil {
...@@ -340,6 +366,15 @@ func (p *LocalPathProvisioner) provisionFor(opts pvController.ProvisionOptions, ...@@ -340,6 +366,15 @@ func (p *LocalPathProvisioner) provisionFor(opts pvController.ProvisionOptions,
name := opts.PVName name := opts.PVName
folderName := strings.Join([]string{name, opts.PVC.Namespace, opts.PVC.Name}, "_") folderName := strings.Join([]string{name, opts.PVC.Namespace, opts.PVC.Name}, "_")
pathPattern, exists := opts.StorageClass.Parameters["pathPattern"]
if exists {
folderName, err = pathFromPattern(pathPattern, opts)
if err != nil {
err = errors.Wrapf(err, "failed to create path from pattern %v", pathPattern)
return nil, pvController.ProvisioningFinished, err
}
}
path := filepath.Join(basePath, folderName) path := filepath.Join(basePath, folderName)
if nodeName == "" { if nodeName == "" {
logrus.Infof("Creating volume %v at %v", name, path) logrus.Infof("Creating volume %v at %v", name, path)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment