Skip to content

Commit 90cc83f

Browse files
committed
feat: skip chmod if mountPermissions is 0
fix
1 parent c038402 commit 90cc83f

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

docs/driver-parameters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Name | Meaning | Example Value | Mandatory | Default value
88
--- | --- | --- | --- | ---
99
server | NFS Server address | domain name `nfs-server.default.svc.cluster.local` <br>or IP address `127.0.0.1` | Yes |
1010
share | NFS share path | `/` | Yes |
11-
mountPermissions | mounted folder permissions. The default is `0777` | | No |
11+
mountPermissions | mounted folder permissions. The default is `0777`, if set as `0`, driver will not perform `chmod` after mount | | No |
1212

1313
### PV/PVC usage (static provisioning)
1414
> [`PersistentVolume` example](../deploy/example/pv-nfs-csi.yaml)

pkg/nfs/nodeserver.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func (ns *NodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
5858

5959
var server, baseDir string
6060
mountPermissions := ns.Driver.mountPermissions
61+
performChmodOp := (mountPermissions > 0)
6162
for k, v := range req.GetVolumeContext() {
6263
switch strings.ToLower(k) {
6364
case paramServer:
@@ -71,9 +72,15 @@ func (ns *NodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
7172
case mountPermissionsField:
7273
if v != "" {
7374
var err error
74-
if mountPermissions, err = strconv.ParseUint(v, 8, 32); err != nil {
75+
var perm uint64
76+
if perm, err = strconv.ParseUint(v, 8, 32); err != nil {
7577
return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid mountPermissions %s", v))
7678
}
79+
if perm == 0 {
80+
performChmodOp = false
81+
} else {
82+
mountPermissions = perm
83+
}
7784
}
7885
}
7986
}
@@ -114,8 +121,12 @@ func (ns *NodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
114121
}
115122

116123
klog.V(2).Infof("volumeID(%v): mount targetPath(%s) with permissions(0%o)", volumeID, targetPath, mountPermissions)
117-
if err := os.Chmod(targetPath, os.FileMode(mountPermissions)); err != nil {
118-
return nil, status.Error(codes.Internal, err.Error())
124+
if performChmodOp {
125+
if err := os.Chmod(targetPath, os.FileMode(mountPermissions)); err != nil {
126+
return nil, status.Error(codes.Internal, err.Error())
127+
}
128+
} else {
129+
klog.V(2).Infof("skip chmod on targetPath(%s) since mountPermissions is set as 0", targetPath)
119130
}
120131
return &csi.NodePublishVolumeResponse{}, nil
121132
}

pkg/nfs/nodeserver_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ func TestNodePublishVolume(t *testing.T) {
4747
"share": "share",
4848
mountPermissionsField: "0755",
4949
}
50+
paramsWithZeroPermissions := map[string]string{
51+
"server": "server",
52+
"share": "share",
53+
mountPermissionsField: "0",
54+
}
5055

5156
invalidParams := map[string]string{
5257
"server": "server",
@@ -121,6 +126,16 @@ func TestNodePublishVolume(t *testing.T) {
121126
Readonly: true},
122127
expectedErr: nil,
123128
},
129+
{
130+
desc: "[Success] Valid request with 0 mountPermissions",
131+
req: csi.NodePublishVolumeRequest{
132+
VolumeContext: paramsWithZeroPermissions,
133+
VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap},
134+
VolumeId: "vol_1",
135+
TargetPath: targetTest,
136+
Readonly: true},
137+
expectedErr: nil,
138+
},
124139
{
125140
desc: "[Error] invalid mountPermissions",
126141
req: csi.NodePublishVolumeRequest{

test/e2e/dynamic_provisioning_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,29 @@ var _ = ginkgo.Describe("Dynamic Provisioning", func() {
7070
Pods: pods,
7171
StorageClassParameters: defaultStorageClassParameters,
7272
}
73+
test.Run(cs, ns)
74+
})
7375

76+
ginkgo.It("should create a volume on demand with zero mountPermissions [nfs.csi.k8s.io]", func() {
77+
pods := []testsuites.PodDetails{
78+
{
79+
Cmd: "echo 'hello world' > /mnt/test-1/data && grep 'hello world' /mnt/test-1/data",
80+
Volumes: []testsuites.VolumeDetails{
81+
{
82+
ClaimSize: "10Gi",
83+
VolumeMount: testsuites.VolumeMountDetails{
84+
NameGenerate: "test-volume-",
85+
MountPathGenerate: "/mnt/test-",
86+
},
87+
},
88+
},
89+
},
90+
}
91+
test := testsuites.DynamicallyProvisionedCmdVolumeTest{
92+
CSIDriver: testDriver,
93+
Pods: pods,
94+
StorageClassParameters: storageClassParametersWithZeroMountPermisssions,
95+
}
7496
test.Run(cs, ns)
7597
})
7698

test/e2e/e2e_suite_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ var (
5252
"csi.storage.k8s.io/provisioner-secret-namespace": "default",
5353
"mountPermissions": "0755",
5454
}
55+
storageClassParametersWithZeroMountPermisssions = map[string]string{
56+
"server": nfsServerAddress,
57+
"share": nfsShare,
58+
"csi.storage.k8s.io/provisioner-secret-name": "mount-options",
59+
"csi.storage.k8s.io/provisioner-secret-namespace": "default",
60+
"mountPermissions": "0",
61+
}
5562
controllerServer *nfs.ControllerServer
5663
)
5764

0 commit comments

Comments
 (0)