@@ -29,6 +29,10 @@ import (
29
29
"k8s.io/utils/keymutex"
30
30
)
31
31
32
+ const (
33
+ accessDenied string = "access is denied"
34
+ )
35
+
32
36
// Mounter provides the default implementation of mount.Interface
33
37
// for the windows platform. This implementation assumes that the
34
38
// kubelet is running in the host's root mount namespace.
@@ -103,32 +107,29 @@ func (mounter *Mounter) MountSensitive(source string, target string, fstype stri
103
107
getSMBMountMutex .LockKey (source )
104
108
defer getSMBMountMutex .UnlockKey (source )
105
109
106
- var output string
107
- var err error
108
110
username := allOptions [0 ]
109
111
password := allOptions [1 ]
110
- if output , err = newSMBMapping (username , password , source ); err != nil {
112
+ if output , err : = newSMBMapping (username , password , source ); err != nil {
111
113
klog .Warningf ("SMB Mapping(%s) returned with error(%v), output(%s)" , source , err , string (output ))
112
114
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
+ }
124
125
}
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 )
126
128
}
129
+ } else {
130
+ return fmt .Errorf ("New-SmbGlobalMapping(%s) failed: %v, output: %q" , source , err , output )
127
131
}
128
132
}
129
- if err != nil {
130
- return fmt .Errorf ("New-SmbGlobalMapping(%s) failed: %v, output: %q" , source , err , output )
131
- }
132
133
}
133
134
134
135
output , err := exec .Command ("cmd" , "/c" , "mklink" , "/D" , target , bindSource ).CombinedOutput ()
@@ -184,6 +185,10 @@ func isValidPath(remotepath string) (bool, error) {
184
185
return strings .HasPrefix (strings .ToLower (string (output )), "true" ), nil
185
186
}
186
187
188
+ func isAccessDeniedError (err error ) bool {
189
+ return err != nil && strings .Contains (strings .ToLower (err .Error ()), accessDenied )
190
+ }
191
+
187
192
// remove SMB mapping
188
193
func removeSMBMapping (remotepath string ) (string , error ) {
189
194
cmd := exec .Command ("powershell" , "/c" , `Remove-SmbGlobalMapping -RemotePath $Env:smbremotepath -Force` )
0 commit comments