@@ -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 ) {
@@ -67,17 +68,29 @@ func RemoveSmbGlobalMapping(remotePath string) error {
67
68
}
68
69
69
70
// GetRemoteServerFromTarget- gets the remote server path given a mount point, the function is recursive until it find the remote server or errors out
70
- func GetRemoteServerFromTarget (mount string ) (string , error ) {
71
+ func GetRemoteServerFromTarget (mount string , volStatsCache azcache.Resource ) (string , error ) {
72
+ cache , err := volStatsCache .Get (mount , azcache .CacheReadTypeDefault )
73
+ if err != nil {
74
+ return "" , err
75
+ }
76
+ if cache != nil {
77
+ remoteServer := cache .(string )
78
+ klog .V (6 ).Infof ("GetRemoteServerFromTarget(%s) cache hit: %s" , mount , remoteServer )
79
+ return remoteServer , nil
80
+ }
71
81
cmd := "(Get-Item -Path $Env:mount).Target"
72
82
out , err := util .RunPowershellCmd (cmd , fmt .Sprintf ("mount=%s" , mount ))
73
83
if err != nil || len (out ) == 0 {
74
84
return "" , fmt .Errorf ("error getting volume from mount. cmd: %s, output: %s, error: %v" , cmd , string (out ), err )
75
85
}
76
- return strings .TrimSpace (string (out )), nil
86
+ remoteServer := strings .TrimSpace (string (out ))
87
+ // cache the remote server path
88
+ volStatsCache .Set (mount , remoteServer )
89
+ return remoteServer , nil
77
90
}
78
91
79
92
// CheckForDuplicateSMBMounts checks if there is any other SMB mount exists on the same remote server
80
- func CheckForDuplicateSMBMounts (dir , mount , remoteServer string ) (bool , error ) {
93
+ func CheckForDuplicateSMBMounts (dir , mount , remoteServer string , volStatsCache azcache. Resource ) (bool , error ) {
81
94
files , err := os .ReadDir (dir )
82
95
if err != nil {
83
96
return false , err
@@ -93,7 +106,7 @@ func CheckForDuplicateSMBMounts(dir, mount, remoteServer string) (bool, error) {
93
106
fileInfo , err := os .Lstat (globalMountPath )
94
107
// check if the file is a symlink, if yes, check if it is pointing to the same remote server
95
108
if err == nil && fileInfo .Mode ()& os .ModeSymlink != 0 {
96
- remoteServerPath , err := GetRemoteServerFromTarget (globalMountPath )
109
+ remoteServerPath , err := GetRemoteServerFromTarget (globalMountPath , volStatsCache )
97
110
klog .V (2 ).Infof ("checking remote server path %s on local path %s" , remoteServerPath , globalMountPath )
98
111
if err == nil {
99
112
if remoteServerPath == remoteServer {
0 commit comments