Skip to content

Commit 3b85b32

Browse files
authored
Merge pull request #262 from andyzhangx/deletevolume-mountoptions
feat: support mountOptions in DeleteVolume
2 parents fbd2a68 + 498e837 commit 3b85b32

File tree

10 files changed

+76
-6
lines changed

10 files changed

+76
-6
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ endif
151151
.PHONY: install-nfs-server
152152
install-nfs-server:
153153
kubectl apply -f ./deploy/example/nfs-provisioner/nfs-server.yaml
154+
kubectl delete secret mount-options --ignore-not-found
155+
kubectl create secret generic mount-options --from-literal mountOptions="nfsvers=4.1"
154156

155157
.PHONY: install-helm
156158
install-helm:
9 Bytes
Binary file not shown.

charts/latest/csi-driver-nfs/templates/rbac-csi-nfs-controller.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ rules:
3737
- apiGroups: ["coordination.k8s.io"]
3838
resources: ["leases"]
3939
verbs: ["get", "list", "watch", "create", "update", "patch"]
40+
- apiGroups: [""]
41+
resources: ["secrets"]
42+
verbs: ["get"]
4043
---
4144
kind: ClusterRoleBinding
4245
apiVersion: rbac.authorization.k8s.io/v1

deploy/rbac-csi-nfs-controller.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ rules:
3333
- apiGroups: ["coordination.k8s.io"]
3434
resources: ["leases"]
3535
verbs: ["get", "list", "watch", "create", "update", "patch"]
36+
- apiGroups: [""]
37+
resources: ["secrets"]
38+
verbs: ["get"]
3639
---
3740

3841
kind: ClusterRoleBinding

docs/driver-parameters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Driver Parameters
2-
> This plugin driver itself only provides a communication layer between resources in the cluser and the NFS server, you need to bring your own NFS server before using this driver.
2+
> This driver requires existing and already configured NFSv3 or NFSv4 server, it supports dynamic provisioning of Persistent Volumes via Persistent Volume Claims by creating a new sub directory under NFS server.
33
44
### Storage Class Usage (Dynamic Provisioning)
55
> [`StorageClass` example](../deploy/example/storageclass-nfs.yaml)

pkg/nfs/controllerserver.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,21 @@ func (cs *ControllerServer) DeleteVolume(ctx context.Context, req *csi.DeleteVol
127127
return &csi.DeleteVolumeResponse{}, nil
128128
}
129129

130+
var volCap *csi.VolumeCapability
131+
mountOptions := getMountOptions(req.GetSecrets())
132+
if mountOptions != "" {
133+
klog.V(2).Infof("DeleteVolume: found mountOptions(%s) for volume(%s)", mountOptions, volumeID)
134+
volCap = &csi.VolumeCapability{
135+
AccessType: &csi.VolumeCapability_Mount{
136+
Mount: &csi.VolumeCapability_MountVolume{
137+
MountFlags: []string{mountOptions},
138+
},
139+
},
140+
}
141+
}
142+
130143
// Mount nfs base share so we can delete the subdirectory
131-
if err = cs.internalMount(ctx, nfsVol, nil); err != nil {
144+
if err = cs.internalMount(ctx, nfsVol, volCap); err != nil {
132145
return nil, status.Errorf(codes.Internal, "failed to mount nfs server: %v", err.Error())
133146
}
134147
defer func() {
@@ -285,8 +298,7 @@ func (cs *ControllerServer) newNFSVolume(name string, size int64, params map[str
285298
baseDir string
286299
)
287300

288-
// Validate parameters (case-insensitive).
289-
// TODO do more strict validation.
301+
// validate parameters (case-insensitive)
290302
for k, v := range params {
291303
switch strings.ToLower(k) {
292304
case paramServer:
@@ -298,7 +310,7 @@ func (cs *ControllerServer) newNFSVolume(name string, size int64, params map[str
298310
}
299311
}
300312

301-
// Validate required parameters
313+
// validate required parameters
302314
if server == "" {
303315
return nil, fmt.Errorf("%v is a required parameter", paramServer)
304316
}

pkg/nfs/nfs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ const (
4949
// The base directory must be a direct child of the root directory.
5050
// The root directory is omitted from the string, for example:
5151
// "base" instead of "/base"
52-
paramShare = "share"
52+
paramShare = "share"
53+
mountOptionsField = "mountoptions"
5354
)
5455

5556
func NewDriver(nodeID, driverName, endpoint string, perm *uint32) *Driver {

pkg/nfs/utils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,14 @@ func (vl *VolumeLocks) Release(volumeID string) {
121121
defer vl.mux.Unlock()
122122
vl.locks.Delete(volumeID)
123123
}
124+
125+
// getMountOptions get mountOptions value from a map
126+
func getMountOptions(context map[string]string) string {
127+
for k, v := range context {
128+
switch strings.ToLower(k) {
129+
case mountOptionsField:
130+
return v
131+
}
132+
}
133+
return ""
134+
}

pkg/nfs/utils_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,39 @@ func TestGetLogLevel(t *testing.T) {
118118
}
119119
}
120120
}
121+
122+
func TestGetMountOptions(t *testing.T) {
123+
tests := []struct {
124+
desc string
125+
context map[string]string
126+
result string
127+
}{
128+
{
129+
desc: "nil context",
130+
context: nil,
131+
result: "",
132+
},
133+
{
134+
desc: "empty context",
135+
context: map[string]string{},
136+
result: "",
137+
},
138+
{
139+
desc: "valid mountOptions",
140+
context: map[string]string{"mountOptions": "nfsvers=3"},
141+
result: "nfsvers=3",
142+
},
143+
{
144+
desc: "valid mountOptions(lowercase)",
145+
context: map[string]string{"mountoptions": "nfsvers=4"},
146+
result: "nfsvers=4",
147+
},
148+
}
149+
150+
for _, test := range tests {
151+
result := getMountOptions(test.context)
152+
if result != test.result {
153+
t.Errorf("Unexpected result: %s, expected: %s", result, test.result)
154+
}
155+
}
156+
}

test/e2e/e2e_suite_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ var (
4747
defaultStorageClassParameters = map[string]string{
4848
"server": "nfs-server.default.svc.cluster.local",
4949
"share": "/",
50+
"csi.storage.k8s.io/provisioner-secret-name": "mount-options",
51+
"csi.storage.k8s.io/provisioner-secret-namespace": "default",
5052
}
5153
controllerServer *nfs.ControllerServer
5254
)

0 commit comments

Comments
 (0)