Skip to content

Commit 9af86e8

Browse files
authored
Merge pull request kubernetes#95583 from andyzhangx/fix-valid-path
fix: smb valid path error
2 parents 7b40b34 + 5656e34 commit 9af86e8

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed

staging/src/k8s.io/mount-utils/mount_windows.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ import (
2929
"k8s.io/utils/keymutex"
3030
)
3131

32+
const (
33+
accessDenied string = "access is denied"
34+
)
35+
3236
// Mounter provides the default implementation of mount.Interface
3337
// for the windows platform. This implementation assumes that the
3438
// kubelet is running in the host's root mount namespace.
@@ -103,32 +107,29 @@ func (mounter *Mounter) MountSensitive(source string, target string, fstype stri
103107
getSMBMountMutex.LockKey(source)
104108
defer getSMBMountMutex.UnlockKey(source)
105109

106-
var output string
107-
var err error
108110
username := allOptions[0]
109111
password := allOptions[1]
110-
if output, err = newSMBMapping(username, password, source); err != nil {
112+
if output, err := newSMBMapping(username, password, source); err != nil {
111113
klog.Warningf("SMB Mapping(%s) returned with error(%v), output(%s)", source, err, string(output))
112114
if isSMBMappingExist(source) {
113-
valid, errPath := isValidPath(source)
114-
if errPath != nil {
115-
return errPath
116-
}
117-
if valid {
118-
err = nil
119-
klog.V(2).Infof("SMB Mapping(%s) already exists and is still valid, skip error", source)
120-
} else {
121-
klog.V(2).Infof("SMB Mapping(%s) already exists while it's not valid, now begin to remove and remount", source)
122-
if output, err = removeSMBMapping(source); err != nil {
123-
return fmt.Errorf("Remove-SmbGlobalMapping failed: %v, output: %q", err, output)
115+
valid, err := isValidPath(source)
116+
if !valid {
117+
if err == nil || isAccessDeniedError(err) {
118+
klog.V(2).Infof("SMB Mapping(%s) already exists while it's not valid, return error: %v, now begin to remove and remount", source, err)
119+
if output, err = removeSMBMapping(source); err != nil {
120+
return fmt.Errorf("Remove-SmbGlobalMapping failed: %v, output: %q", err, output)
121+
}
122+
if output, err := newSMBMapping(username, password, source); err != nil {
123+
return fmt.Errorf("New-SmbGlobalMapping(%s) failed: %v, output: %q", source, err, output)
124+
}
124125
}
125-
output, err = newSMBMapping(username, password, source)
126+
} else {
127+
klog.V(2).Infof("SMB Mapping(%s) already exists and is still valid, skip error(%v)", source, err)
126128
}
129+
} else {
130+
return fmt.Errorf("New-SmbGlobalMapping(%s) failed: %v, output: %q", source, err, output)
127131
}
128132
}
129-
if err != nil {
130-
return fmt.Errorf("New-SmbGlobalMapping(%s) failed: %v, output: %q", source, err, output)
131-
}
132133
}
133134

134135
output, err := exec.Command("cmd", "/c", "mklink", "/D", target, bindSource).CombinedOutput()
@@ -184,6 +185,10 @@ func isValidPath(remotepath string) (bool, error) {
184185
return strings.HasPrefix(strings.ToLower(string(output)), "true"), nil
185186
}
186187

188+
func isAccessDeniedError(err error) bool {
189+
return err != nil && strings.Contains(strings.ToLower(err.Error()), accessDenied)
190+
}
191+
187192
// remove SMB mapping
188193
func removeSMBMapping(remotepath string) (string, error) {
189194
cmd := exec.Command("powershell", "/c", `Remove-SmbGlobalMapping -RemotePath $Env:smbremotepath -Force`)

staging/src/k8s.io/mount-utils/mount_windows_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,29 @@ func TestIsValidPath(t *testing.T) {
361361
}
362362
}
363363
}
364+
365+
func TestIsAccessDeniedError(t *testing.T) {
366+
tests := []struct {
367+
err error
368+
expectedResult bool
369+
}{
370+
{
371+
nil,
372+
false,
373+
},
374+
{
375+
fmt.Errorf("other error message"),
376+
false,
377+
},
378+
{
379+
fmt.Errorf(`PathValid(\\xxx\share) failed with returned output: Test-Path : Access is denied`),
380+
true,
381+
},
382+
}
383+
384+
for _, test := range tests {
385+
result := isAccessDeniedError(test.err)
386+
assert.Equal(t, result, test.expectedResult, "Expect result not equal with isAccessDeniedError(%v) return: %q, expected: %q",
387+
test.err, result, test.expectedResult)
388+
}
389+
}

0 commit comments

Comments
 (0)