Skip to content

Commit f184d4d

Browse files
authored
Merge pull request #843 from andyzhangx/mount-with-0777
fix: mount with 0777 in volume creation and deletion
2 parents 3f54a44 + e2db88a commit f184d4d

File tree

4 files changed

+95
-19
lines changed

4 files changed

+95
-19
lines changed

.github/workflows/trivy.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212
- name: Set up Go 1.x
1313
uses: actions/setup-go@v5
1414
with:
15-
go-version: 1.22.5
15+
go-version: 1.23.1
1616
id: go
1717

1818
- name: Checkout code

pkg/smb/controllerserver.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package smb
1919
import (
2020
"context"
2121
"fmt"
22-
"io/fs"
2322
"os"
2423
"os/exec"
2524
"path/filepath"
@@ -92,12 +91,17 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
9291
}
9392

9493
volCap := volumeCapabilities[0]
95-
if volCap.GetMount() != nil && !createSubDir {
94+
if volCap.GetMount() != nil {
9695
options := volCap.GetMount().GetMountFlags()
97-
if hasGuestMountOptions(options) {
96+
if !createSubDir && hasGuestMountOptions(options) {
9897
klog.V(2).Infof("guest mount option(%v) is provided, create subdirectory", options)
9998
createSubDir = true
10099
}
100+
// set default file/dir mode
101+
volCap.GetMount().MountFlags = appendMountOptions(options, map[string]string{
102+
fileMode: defaultFileMode,
103+
dirMode: defaultDirMode,
104+
})
101105
}
102106

103107
if acquired := d.volumeLocks.TryAcquire(name); !acquired {
@@ -159,18 +163,22 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)
159163
}
160164
defer d.volumeLocks.Release(volumeID)
161165

162-
var volCap *csi.VolumeCapability
163166
secrets := req.GetSecrets()
164167
mountOptions := getMountOptions(secrets)
165168
if mountOptions != "" {
166169
klog.V(2).Infof("DeleteVolume: found mountOptions(%v) for volume(%s)", mountOptions, volumeID)
167-
volCap = &csi.VolumeCapability{
168-
AccessType: &csi.VolumeCapability_Mount{
169-
Mount: &csi.VolumeCapability_MountVolume{
170-
MountFlags: []string{mountOptions},
171-
},
170+
}
171+
// set default file/dir mode
172+
volCap := &csi.VolumeCapability{
173+
AccessType: &csi.VolumeCapability_Mount{
174+
Mount: &csi.VolumeCapability_MountVolume{
175+
MountFlags: appendMountOptions([]string{mountOptions},
176+
map[string]string{
177+
fileMode: defaultFileMode,
178+
dirMode: defaultDirMode,
179+
}),
172180
},
173-
}
181+
},
174182
}
175183

176184
if smbVol.onDelete == "" {
@@ -224,14 +232,6 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)
224232
return nil, status.Errorf(codes.Internal, "archive subdirectory(%s, %s) failed with %v", internalVolumePath, archivedInternalVolumePath, err)
225233
}
226234
} else {
227-
if _, err2 := os.Lstat(internalVolumePath); err2 == nil {
228-
err2 := filepath.WalkDir(internalVolumePath, func(path string, _ fs.DirEntry, _ error) error {
229-
return os.Chmod(path, 0777)
230-
})
231-
if err2 != nil {
232-
klog.Errorf("failed to chmod subdirectory: %v", err2)
233-
}
234-
}
235235
klog.V(2).Infof("Removing subdirectory at %v", internalVolumePath)
236236
if err = os.RemoveAll(internalVolumePath); err != nil {
237237
return nil, status.Errorf(codes.Internal, "failed to delete subdirectory: %v", err)

pkg/smb/smb.go

+29
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ const (
5252
DefaultKrb5CacheDirectory = "/var/lib/kubelet/kerberos/"
5353
retain = "retain"
5454
archive = "archive"
55+
fileMode = "file_mode"
56+
dirMode = "dir_mode"
57+
defaultFileMode = "0777"
58+
defaultDirMode = "0777"
5559
)
5660

5761
var supportedOnDeleteValues = []string{"", "delete", retain, archive}
@@ -234,3 +238,28 @@ func validateOnDeleteValue(onDelete string) error {
234238

235239
return fmt.Errorf("invalid value %s for OnDelete, supported values are %v", onDelete, supportedOnDeleteValues)
236240
}
241+
242+
// appendMountOptions appends extra mount options to the given mount options
243+
func appendMountOptions(mountOptions []string, extraMountOptions map[string]string) []string {
244+
// stores the mount options already included in mountOptions
245+
included := make(map[string]bool)
246+
for _, mountOption := range mountOptions {
247+
for k := range extraMountOptions {
248+
if strings.HasPrefix(mountOption, k) {
249+
included[k] = true
250+
}
251+
}
252+
}
253+
254+
allMountOptions := mountOptions
255+
for k, v := range extraMountOptions {
256+
if _, isIncluded := included[k]; !isIncluded {
257+
if v != "" {
258+
allMountOptions = append(allMountOptions, fmt.Sprintf("%s=%s", k, v))
259+
} else {
260+
allMountOptions = append(allMountOptions, k)
261+
}
262+
}
263+
}
264+
return allMountOptions
265+
}

pkg/smb/smb_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,50 @@ func TestValidateOnDeleteValue(t *testing.T) {
338338
}
339339
}
340340
}
341+
342+
func TestAppendMountOptions(t *testing.T) {
343+
tests := []struct {
344+
desc string
345+
options []string
346+
newOpts map[string]string
347+
expected []string
348+
}{
349+
{
350+
desc: "empty options",
351+
options: nil,
352+
newOpts: map[string]string{},
353+
expected: nil,
354+
},
355+
{
356+
desc: "empty newOpts",
357+
options: []string{"a", "b"},
358+
newOpts: map[string]string{},
359+
expected: []string{"a", "b"},
360+
},
361+
{
362+
desc: "empty newOpts",
363+
options: []string{"a", "b"},
364+
newOpts: map[string]string{"c": "d"},
365+
expected: []string{"a", "b", "c=d"},
366+
},
367+
{
368+
desc: "duplicate newOpts",
369+
options: []string{"a", "b", "c=d"},
370+
newOpts: map[string]string{"c": "d"},
371+
expected: []string{"a", "b", "c=d"},
372+
},
373+
{
374+
desc: "normal newOpts",
375+
options: []string{"a", "b"},
376+
newOpts: map[string]string{"c": "d", "e": "f"},
377+
expected: []string{"a", "b", "c=d", "e=f"},
378+
},
379+
}
380+
381+
for _, test := range tests {
382+
result := appendMountOptions(test.options, test.newOpts)
383+
if !reflect.DeepEqual(result, test.expected) {
384+
t.Errorf("test[%s]: unexpected output: %v, expected result: %v", test.desc, result, test.expected)
385+
}
386+
}
387+
}

0 commit comments

Comments
 (0)