From 9b6f21f6ac3524c22232bbd2ee7d846ec9d73e11 Mon Sep 17 00:00:00 2001 From: Sameer Mene <sameer.mene@infovista.com> Date: Wed, 22 Mar 2023 12:47:42 +0530 Subject: [PATCH] check the path is present in nodePathMap --- README.md | 4 ++-- provisioner.go | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5afde3aa..ae18dfc0 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 c5812d6a..59ca0e39 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 } -- GitLab