@@ -24,6 +24,7 @@ import (
24
24
25
25
"k8s.io/klog/v2"
26
26
"sigs.k8s.io/azurefile-csi-driver/pkg/util"
27
+ azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache"
27
28
)
28
29
29
30
func IsSmbMapped (remotePath string ) (bool , error ) {
@@ -91,17 +92,29 @@ func RemoveSmbGlobalMapping(remotePath string) error {
91
92
}
92
93
93
94
// GetRemoteServerFromTarget- gets the remote server path given a mount point, the function is recursive until it find the remote server or errors out
94
- func GetRemoteServerFromTarget (mount string ) (string , error ) {
95
+ func GetRemoteServerFromTarget (mount string , volStatsCache azcache.Resource ) (string , error ) {
96
+ cache , err := volStatsCache .Get (mount , azcache .CacheReadTypeDefault )
97
+ if err != nil {
98
+ return "" , err
99
+ }
100
+ if cache != nil {
101
+ remoteServer := cache .(string )
102
+ klog .V (6 ).Infof ("GetRemoteServerFromTarget(%s) cache hit: %s" , mount , remoteServer )
103
+ return remoteServer , nil
104
+ }
95
105
cmd := "(Get-Item -Path $Env:mount).Target"
96
106
out , err := util .RunPowershellCmd (cmd , fmt .Sprintf ("mount=%s" , mount ))
97
107
if err != nil || len (out ) == 0 {
98
108
return "" , fmt .Errorf ("error getting volume from mount. cmd: %s, output: %s, error: %v" , cmd , string (out ), err )
99
109
}
100
- return strings .TrimSpace (string (out )), nil
110
+ remoteServer := strings .TrimSpace (string (out ))
111
+ // cache the remote server path
112
+ volStatsCache .Set (mount , remoteServer )
113
+ return remoteServer , nil
101
114
}
102
115
103
116
// CheckForDuplicateSMBMounts checks if there is any other SMB mount exists on the same remote server
104
- func CheckForDuplicateSMBMounts (dir , mount , remoteServer string ) (bool , error ) {
117
+ func CheckForDuplicateSMBMounts (dir , mount , remoteServer string , volStatsCache azcache. Resource ) (bool , error ) {
105
118
files , err := os .ReadDir (dir )
106
119
if err != nil {
107
120
return false , err
@@ -117,7 +130,7 @@ func CheckForDuplicateSMBMounts(dir, mount, remoteServer string) (bool, error) {
117
130
fileInfo , err := os .Lstat (globalMountPath )
118
131
// check if the file is a symlink, if yes, check if it is pointing to the same remote server
119
132
if err == nil && fileInfo .Mode ()& os .ModeSymlink != 0 {
120
- remoteServerPath , err := GetRemoteServerFromTarget (globalMountPath )
133
+ remoteServerPath , err := GetRemoteServerFromTarget (globalMountPath , volStatsCache )
121
134
klog .V (2 ).Infof ("checking remote server path %s on local path %s" , remoteServerPath , globalMountPath )
122
135
if err == nil {
123
136
if remoteServerPath == remoteServer {
0 commit comments