diff --git a/README.md b/README.md index 5afde3aab1b0df561ab7acf0267f11898d172cfa..ae18dfc09c7e9fda15c90b756c6e6d59d0b7261c 100644 --- a/README.md +++ b/README.md @@ -251,7 +251,7 @@ A few things to note; the annotation for the `StorageClass` will apply to all vo ### 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 `path`. +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` ``` apiVersion: storage.k8s.io/v1 @@ -260,7 +260,7 @@ metadata: name: ssd-local-path provisioner: cluster.local/local-path-provisioner parameters: - path: /data/ssd + nodePath: /data/ssd volumeBindingMode: WaitForFirstConsumer reclaimPolicy: Delete ``` diff --git a/provisioner.go b/provisioner.go index c5812d6a7001e7e626fd14723e085d636d7861fe..59ca0e39888d67d300dc37f39e5b1ad6d27b9e2e 100644 --- a/provisioner.go +++ b/provisioner.go @@ -166,7 +166,7 @@ func (p *LocalPathProvisioner) watchAndRefreshConfig() { }() } -func (p *LocalPathProvisioner) getRandomPathOnNode(node string) (string, error) { +func (p *LocalPathProvisioner) getPathOnNode(node string, requestedPath string) (string, error) { p.configMutex.RLock() defer p.configMutex.RUnlock() @@ -196,6 +196,14 @@ func (p *LocalPathProvisioner) getRandomPathOnNode(node string) (string, error) if len(paths) == 0 { return "", fmt.Errorf("no local path available on node %v", node) } + // if a particular path was requested by storage class + if requestedPath != "" { + if _, ok := paths[requestedPath]; !ok { + return "", fmt.Errorf("config doesn't contain path %v on node %v", requestedPath, node) + } + return requestedPath, nil + } + // if no particular path was requested, choose a random one path := "" for path = range paths { break @@ -254,13 +262,13 @@ func (p *LocalPathProvisioner) Provision(ctx context.Context, opts pvController. // This clause works only with sharedFS nodeName = node.Name } - var basePath string - basePath, err = p.getRandomPathOnNode(nodeName) + var requestedPath string if storageClass.Parameters != nil { - if _, ok := storageClass.Parameters["path"]; ok { - basePath = storageClass.Parameters["path"] + if _, ok := storageClass.Parameters["nodePath"]; ok { + requestedPath = storageClass.Parameters["nodePath"] } } + basePath, err := p.getPathOnNode(nodeName, requestedPath) if err != nil { return nil, pvController.ProvisioningFinished, err }