Add missing pvname
This adds the ability to use the pvname as part of the pathpattern. The defaultpath is now a string that is parsed the same way as the user specified pathpattern.
This commit is contained in:
parent
c2a2d5d544
commit
b48ecac166
10
README.md
10
README.md
|
|
@ -234,11 +234,11 @@ To disable leader election, define an env variable named ENABLE_LEADER_ELECTION
|
|||
|
||||
**_Parameters:_**
|
||||
|
||||
| Name | Description | Default |
|
||||
| --------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------: |
|
||||
| onDelete | If it exists and has a delete value, delete the directory, if it exists and has a retain value, save the directory. | will be archived with name on the share: `archived-<volume.Name>` |
|
||||
| archiveOnDelete | If it exists and has a false value, delete the directory. if `onDelete` exists, `archiveOnDelete` will be ignored. | will be archived with name on the share: `archived-<volume.Name>` |
|
||||
| pathPattern | Specifies a template for creating a directory path via PVC metadata's such as labels, annotations, name or namespace. To specify metadata use `${.PVC.<metadata>}`. Example: If folder should be named like `<pvc-namespace>-<pvc-name>`, use `${.PVC.namespace}-${.PVC.name}` as pathPattern. | n/a |
|
||||
| Name | Description | Default |
|
||||
| --------------- | ------------------------------------------------------------ | :----------------------------------------------------------: |
|
||||
| onDelete | If it exists and has a delete value, delete the directory, if it exists and has a retain value, save the directory. | will be archived with name on the share: `archived-<volume.Name>` |
|
||||
| archiveOnDelete | If it exists and has a false value, delete the directory. if `onDelete` exists, `archiveOnDelete` will be ignored. | will be archived with name on the share: `archived-<volume.Name>` |
|
||||
| pathPattern | Specifies a template for creating a directory path via PVC metadata's such as labels, annotations, name or namespace. To specify metadata use `${.PVC.<metadata>}`. Example: If folder should be named like `<pvc-namespace>-<pvc-name>`, use `${.PVC.namespace}-${.PVC.name}` as pathPattern. The default pattern will add the pvname to folder, which will result in a unique folder name, combing namespace, name and an unique id. | will create a folder witht the pattern: `${.PVC.namespace}-${.PVC.name}-${.PVC.pvname}` |
|
||||
|
||||
This is `deploy/class.yaml` which defines the NFS subdir external provisioner's Kubernetes Storage Class:
|
||||
|
||||
|
|
|
|||
|
|
@ -41,6 +41,8 @@ import (
|
|||
|
||||
const (
|
||||
provisionerNameKey = "PROVISIONER_NAME"
|
||||
defaultPathPattern = "${.PVC.namespace}-${.PVC.name}-${.PVC.pvname}"
|
||||
mountPath = "/persistentvolumes"
|
||||
)
|
||||
|
||||
type nfsProvisioner struct {
|
||||
|
|
@ -73,10 +75,6 @@ func (meta *pvcMetadata) stringParser(str string) string {
|
|||
return str
|
||||
}
|
||||
|
||||
const (
|
||||
mountPath = "/persistentvolumes"
|
||||
)
|
||||
|
||||
var _ controller.Provisioner = &nfsProvisioner{}
|
||||
|
||||
func (p *nfsProvisioner) Provision(ctx context.Context, options controller.ProvisionOptions) (*v1.PersistentVolume, controller.ProvisioningState, error) {
|
||||
|
|
@ -85,32 +83,28 @@ func (p *nfsProvisioner) Provision(ctx context.Context, options controller.Provi
|
|||
}
|
||||
glog.V(4).Infof("nfs provisioner: VolumeOptions %v", options)
|
||||
|
||||
pvcNamespace := options.PVC.Namespace
|
||||
pvcName := options.PVC.Name
|
||||
|
||||
pvName := strings.Join([]string{pvcNamespace, pvcName, options.PVName}, "-")
|
||||
|
||||
metadata := &pvcMetadata{
|
||||
data: map[string]string{
|
||||
"name": pvcName,
|
||||
"namespace": pvcNamespace,
|
||||
"name": options.PVC.Name,
|
||||
"namespace": options.PVC.Namespace,
|
||||
"pvname": options.PVName,
|
||||
},
|
||||
labels: options.PVC.Labels,
|
||||
annotations: options.PVC.Annotations,
|
||||
}
|
||||
|
||||
fullPath := filepath.Join(mountPath, pvName)
|
||||
path := filepath.Join(p.path, pvName)
|
||||
|
||||
pathPattern, exists := options.StorageClass.Parameters["pathPattern"]
|
||||
if exists {
|
||||
customPath := metadata.stringParser(pathPattern)
|
||||
if customPath != "" {
|
||||
path = filepath.Join(p.path, customPath)
|
||||
fullPath = filepath.Join(mountPath, customPath)
|
||||
}
|
||||
if !exists {
|
||||
pathPattern = defaultPathPattern
|
||||
}
|
||||
|
||||
parsedPath := metadata.stringParser(pathPattern)
|
||||
if parsedPath == "" {
|
||||
return nil, controller.ProvisioningFinished, errors.New("unable to parse pathPattern: " + pathPattern)
|
||||
}
|
||||
path := filepath.Join(p.path, parsedPath)
|
||||
fullPath := filepath.Join(mountPath, parsedPath)
|
||||
|
||||
glog.V(4).Infof("creating path %s", fullPath)
|
||||
if err := os.MkdirAll(fullPath, 0o777); err != nil {
|
||||
return nil, controller.ProvisioningFinished, errors.New("unable to create directory to provision new pv: " + err.Error())
|
||||
|
|
|
|||
Loading…
Reference in New Issue