Merge pull request #95583 from andyzhangx/fix-valid-path

fix: smb valid path error
This commit is contained in:
Kubernetes Prow Robot 2020-10-16 09:01:54 -07:00 committed by GitHub
commit 9af86e8db8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 18 deletions

View File

@ -29,6 +29,10 @@ import (
"k8s.io/utils/keymutex"
)
const (
accessDenied string = "access is denied"
)
// Mounter provides the default implementation of mount.Interface
// for the windows platform. This implementation assumes that the
// kubelet is running in the host's root mount namespace.
@ -103,32 +107,29 @@ func (mounter *Mounter) MountSensitive(source string, target string, fstype stri
getSMBMountMutex.LockKey(source)
defer getSMBMountMutex.UnlockKey(source)
var output string
var err error
username := allOptions[0]
password := allOptions[1]
if output, err = newSMBMapping(username, password, source); err != nil {
if output, err := newSMBMapping(username, password, source); err != nil {
klog.Warningf("SMB Mapping(%s) returned with error(%v), output(%s)", source, err, string(output))
if isSMBMappingExist(source) {
valid, errPath := isValidPath(source)
if errPath != nil {
return errPath
}
if valid {
err = nil
klog.V(2).Infof("SMB Mapping(%s) already exists and is still valid, skip error", source)
} else {
klog.V(2).Infof("SMB Mapping(%s) already exists while it's not valid, now begin to remove and remount", source)
if output, err = removeSMBMapping(source); err != nil {
return fmt.Errorf("Remove-SmbGlobalMapping failed: %v, output: %q", err, output)
valid, err := isValidPath(source)
if !valid {
if err == nil || isAccessDeniedError(err) {
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)
if output, err = removeSMBMapping(source); err != nil {
return fmt.Errorf("Remove-SmbGlobalMapping failed: %v, output: %q", err, output)
}
if output, err := newSMBMapping(username, password, source); err != nil {
return fmt.Errorf("New-SmbGlobalMapping(%s) failed: %v, output: %q", source, err, output)
}
}
output, err = newSMBMapping(username, password, source)
} else {
klog.V(2).Infof("SMB Mapping(%s) already exists and is still valid, skip error(%v)", source, err)
}
} else {
return fmt.Errorf("New-SmbGlobalMapping(%s) failed: %v, output: %q", source, err, output)
}
}
if err != nil {
return fmt.Errorf("New-SmbGlobalMapping(%s) failed: %v, output: %q", source, err, output)
}
}
output, err := exec.Command("cmd", "/c", "mklink", "/D", target, bindSource).CombinedOutput()
@ -184,6 +185,10 @@ func isValidPath(remotepath string) (bool, error) {
return strings.HasPrefix(strings.ToLower(string(output)), "true"), nil
}
func isAccessDeniedError(err error) bool {
return err != nil && strings.Contains(strings.ToLower(err.Error()), accessDenied)
}
// remove SMB mapping
func removeSMBMapping(remotepath string) (string, error) {
cmd := exec.Command("powershell", "/c", `Remove-SmbGlobalMapping -RemotePath $Env:smbremotepath -Force`)

View File

@ -361,3 +361,29 @@ func TestIsValidPath(t *testing.T) {
}
}
}
func TestIsAccessDeniedError(t *testing.T) {
tests := []struct {
err error
expectedResult bool
}{
{
nil,
false,
},
{
fmt.Errorf("other error message"),
false,
},
{
fmt.Errorf(`PathValid(\\xxx\share) failed with returned output: Test-Path : Access is denied`),
true,
},
}
for _, test := range tests {
result := isAccessDeniedError(test.err)
assert.Equal(t, result, test.expectedResult, "Expect result not equal with isAccessDeniedError(%v) return: %q, expected: %q",
test.err, result, test.expectedResult)
}
}