Skip to content

Commit 952003f

Browse files
authored
Merge pull request #402 from andyzhangx/mountoptions-deletevolume
feat: support mountOptions in DeleteVolume
2 parents 9b4f192 + b44c2b4 commit 952003f

File tree

5 files changed

+77
-7
lines changed

5 files changed

+77
-7
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ clean:
213213
.PHONY: install-smb-provisioner
214214
install-smb-provisioner:
215215
kubectl delete secret smbcreds --ignore-not-found
216-
kubectl create secret generic smbcreds --from-literal username=USERNAME --from-literal password="PASSWORD"
216+
kubectl create secret generic smbcreds --from-literal username=USERNAME --from-literal password="PASSWORD" --from-literal mountOptions="dir_mode=0777,file_mode=0777,uid=0,gid=0,mfsymlinks"
217217
ifdef TEST_WINDOWS
218218
kubectl apply -f deploy/example/smb-provisioner/smb-server-lb.yaml
219219
else

docs/driver-parameters.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,14 @@ nodeStageSecretRef.namespace | namespace where the secret is | k8s namespace |
2525
- Use `kubectl create secret` to create `smbcreds` secret to store Samba Server username, password
2626
```console
2727
kubectl create secret generic smbcreds --from-literal username=USERNAME --from-literal password="PASSWORD"
28-
```
28+
```
29+
30+
### Tips
31+
#### provide `mountOptions` for `DeleteVolume`
32+
> since `DeleteVolumeRequest` does not provide `mountOptions`, following is the workaround to provide `mountOptions` for `DeleteVolume`
33+
- create a secret `smbcreds` with `mountOptions`
34+
```console
35+
kubectl create secret generic smbcreds --from-literal username=USERNAME --from-literal password="PASSWORD" --from-literal mountOptions="dir_mode=0777,file_mode=0777,uid=0,gid=0,mfsymlinks"
36+
```
37+
38+
- set `csi.storage.k8s.io/provisioner-secret-name: "smbcreds"` in storage class

pkg/smb/controllerserver.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,26 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)
123123
return &csi.DeleteVolumeResponse{}, nil
124124
}
125125

126+
var volCap *csi.VolumeCapability
127+
mountOptions := getMountOptions(req.GetSecrets())
128+
if mountOptions != "" {
129+
klog.V(2).Infof("DeleteVolume: found mountOptions(%v) for volume(%s)", mountOptions, volumeID)
130+
volCap = &csi.VolumeCapability{
131+
AccessType: &csi.VolumeCapability_Mount{
132+
Mount: &csi.VolumeCapability_MountVolume{
133+
MountFlags: []string{mountOptions},
134+
},
135+
},
136+
}
137+
}
138+
126139
secrets := req.GetSecrets()
127140
if len(secrets) > 0 {
128141
if len(smbVol.uuid) > 0 {
129142
klog.V(2).Infof("existing subDir(%s) is provided, skip subdirectory creation", smbVol.subDir)
130143
} else {
131144
// Mount smb base share so we can delete the subdirectory
132-
if err = d.internalMount(ctx, smbVol, nil, secrets); err != nil {
145+
if err = d.internalMount(ctx, smbVol, volCap, secrets); err != nil {
133146
return nil, status.Errorf(codes.Internal, "failed to mount smb server: %v", err.Error())
134147
}
135148
defer func() {
@@ -146,7 +159,7 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)
146159
}
147160
}
148161
} else {
149-
klog.Infof("DeleteVolume(%s) does not provide secrets", volumeID)
162+
klog.V(2).Infof("DeleteVolume(%s) does not provide secrets", volumeID)
150163
}
151164

152165
return &csi.DeleteVolumeResponse{}, nil

pkg/smb/smb.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
package smb
1818

1919
import (
20-
"fmt"
20+
"strings"
2121

2222
"github.com/container-storage-interface/spec/lib/go/csi"
2323

@@ -35,6 +35,7 @@ const (
3535
sourceField = "source"
3636
subDirField = "subdir"
3737
domainField = "domain"
38+
mountOptionsField = "mountoptions"
3839
defaultDomainName = "AZURE"
3940
)
4041

@@ -67,7 +68,7 @@ func (d *Driver) Run(endpoint, kubeconfig string, testMode bool) {
6768
if err != nil {
6869
klog.Fatalf("%v", err)
6970
}
70-
klog.Infof("\nDRIVER INFORMATION:\n-------------------\n%s\n\nStreaming logs below:", versionMeta)
71+
klog.V(2).Infof("\nDRIVER INFORMATION:\n-------------------\n%s\n\nStreaming logs below:", versionMeta)
7172

7273
d.mounter, err = mounter.NewSafeMounter()
7374
if err != nil {
@@ -109,6 +110,16 @@ func (d *Driver) Run(endpoint, kubeconfig string, testMode bool) {
109110

110111
func IsCorruptedDir(dir string) bool {
111112
_, pathErr := mount.PathExists(dir)
112-
fmt.Printf("IsCorruptedDir(%s) returned with error: %v", dir, pathErr)
113113
return pathErr != nil && mount.IsCorruptedMnt(pathErr)
114114
}
115+
116+
// getMountOptions get mountOptions value from a map
117+
func getMountOptions(context map[string]string) string {
118+
for k, v := range context {
119+
switch strings.ToLower(k) {
120+
case mountOptionsField:
121+
return v
122+
}
123+
}
124+
return ""
125+
}

pkg/smb/smb_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,39 @@ func TestRun(t *testing.T) {
100100
t.Run(tc.name, tc.testFunc)
101101
}
102102
}
103+
104+
func TestGetMountOptions(t *testing.T) {
105+
tests := []struct {
106+
desc string
107+
context map[string]string
108+
result string
109+
}{
110+
{
111+
desc: "nil context",
112+
context: nil,
113+
result: "",
114+
},
115+
{
116+
desc: "empty context",
117+
context: map[string]string{},
118+
result: "",
119+
},
120+
{
121+
desc: "valid mountOptions",
122+
context: map[string]string{"mountOptions": "dir_mode=0777"},
123+
result: "dir_mode=0777",
124+
},
125+
{
126+
desc: "valid mountOptions(lowercase)",
127+
context: map[string]string{"mountoptions": "dir_mode=0777,file_mode=0777,uid=0,gid=0,mfsymlinks"},
128+
result: "dir_mode=0777,file_mode=0777,uid=0,gid=0,mfsymlinks",
129+
},
130+
}
131+
132+
for _, test := range tests {
133+
result := getMountOptions(test.context)
134+
if result != test.result {
135+
t.Errorf("Unexpected result: %s, expected: %s", result, test.result)
136+
}
137+
}
138+
}

0 commit comments

Comments
 (0)