From bbcac49c009eecda9ff393b730c090c6e4f602fd Mon Sep 17 00:00:00 2001 From: andyzhangx Date: Sat, 18 Sep 2021 02:26:26 +0000 Subject: [PATCH] chore: refine ProxyMounter interface --- pkg/azurefile/azure_common_windows.go | 36 ++---------- pkg/mounter/safe_mounter_v1beta_windows.go | 54 ++++++++--------- pkg/mounter/safe_mounter_windows.go | 68 +++++++++++++--------- 3 files changed, 73 insertions(+), 85 deletions(-) diff --git a/pkg/azurefile/azure_common_windows.go b/pkg/azurefile/azure_common_windows.go index caeac609ac..4d56c803a9 100644 --- a/pkg/azurefile/azure_common_windows.go +++ b/pkg/azurefile/azure_common_windows.go @@ -28,30 +28,21 @@ import ( ) func SMBMount(m *mount.SafeFormatAndMount, source, target, fsType string, mountOptions, sensitiveMountOptions []string) error { - if proxy, ok := m.Interface.(*mounter.CSIProxyMounter); ok { - return proxy.SMBMount(source, target, fsType, mountOptions, sensitiveMountOptions) - } - if proxy, ok := m.Interface.(*mounter.CSIProxyMounterV1Beta); ok { + if proxy, ok := m.Interface.(mounter.CSIProxyMounter); ok { return proxy.SMBMount(source, target, fsType, mountOptions, sensitiveMountOptions) } return fmt.Errorf("could not cast to csi proxy class") } func SMBUnmount(m *mount.SafeFormatAndMount, target string) error { - if proxy, ok := m.Interface.(*mounter.CSIProxyMounter); ok { - return proxy.SMBUnmount(target) - } - if proxy, ok := m.Interface.(*mounter.CSIProxyMounterV1Beta); ok { + if proxy, ok := m.Interface.(mounter.CSIProxyMounter); ok { return proxy.SMBUnmount(target) } return fmt.Errorf("could not cast to csi proxy class") } func RemoveStageTarget(m *mount.SafeFormatAndMount, target string) error { - if proxy, ok := m.Interface.(*mounter.CSIProxyMounter); ok { - return proxy.Rmdir(target) - } - if proxy, ok := m.Interface.(*mounter.CSIProxyMounterV1Beta); ok { + if proxy, ok := m.Interface.(mounter.CSIProxyMounter); ok { return proxy.Rmdir(target) } return fmt.Errorf("could not cast to csi proxy class") @@ -65,31 +56,14 @@ func CleanupSMBMountPoint(m *mount.SafeFormatAndMount, target string, extensiveM } func CleanupMountPoint(m *mount.SafeFormatAndMount, target string, extensiveMountCheck bool) error { - if proxy, ok := m.Interface.(*mounter.CSIProxyMounter); ok { - return proxy.Rmdir(target) - } - if proxy, ok := m.Interface.(*mounter.CSIProxyMounterV1Beta); ok { + if proxy, ok := m.Interface.(mounter.CSIProxyMounter); ok { return proxy.Rmdir(target) } return fmt.Errorf("could not cast to csi proxy class") } func removeDir(path string, m *mount.SafeFormatAndMount) error { - if proxy, ok := m.Interface.(*mounter.CSIProxyMounter); ok { - isExists, err := proxy.ExistsPath(path) - if err != nil { - return err - } - - if isExists { - klog.V(4).Infof("Removing path: %s", path) - if err = proxy.Rmdir(path); err != nil { - return err - } - } - return nil - } - if proxy, ok := m.Interface.(*mounter.CSIProxyMounterV1Beta); ok { + if proxy, ok := m.Interface.(mounter.CSIProxyMounter); ok { isExists, err := proxy.ExistsPath(path) if err != nil { return err diff --git a/pkg/mounter/safe_mounter_v1beta_windows.go b/pkg/mounter/safe_mounter_v1beta_windows.go index 1acf499f10..c1c232f360 100644 --- a/pkg/mounter/safe_mounter_v1beta_windows.go +++ b/pkg/mounter/safe_mounter_v1beta_windows.go @@ -36,14 +36,14 @@ import ( mount "k8s.io/mount-utils" ) -var _ mount.Interface = &CSIProxyMounterV1Beta{} +var _ CSIProxyMounter = &csiProxyMounterV1Beta{} -type CSIProxyMounterV1Beta struct { +type csiProxyMounterV1Beta struct { FsClient *fsclient.Client SMBClient *smbclient.Client } -func (mounter *CSIProxyMounterV1Beta) SMBMount(source, target, fsType string, mountOptions, sensitiveMountOptions []string) error { +func (mounter *csiProxyMounterV1Beta) SMBMount(source, target, fsType string, mountOptions, sensitiveMountOptions []string) error { klog.V(4).Infof("SMBMount: remote path: %s. local path: %s", source, target) if len(mountOptions) == 0 || len(sensitiveMountOptions) == 0 { @@ -76,7 +76,7 @@ func (mounter *CSIProxyMounterV1Beta) SMBMount(source, target, fsType string, mo return nil } -func (mounter *CSIProxyMounterV1Beta) SMBUnmount(target string) error { +func (mounter *csiProxyMounterV1Beta) SMBUnmount(target string) error { klog.V(4).Infof("SMBUnmount: local path: %s", target) // TODO: We need to remove the SMB mapping. The change to remove the // directory brings the CSI code in parity with the in-tree. @@ -84,7 +84,7 @@ func (mounter *CSIProxyMounterV1Beta) SMBUnmount(target string) error { } // Mount just creates a soft link at target pointing to source. -func (mounter *CSIProxyMounterV1Beta) Mount(source string, target string, fstype string, options []string) error { +func (mounter *csiProxyMounterV1Beta) Mount(source string, target string, fstype string, options []string) error { klog.V(4).Infof("Mount: old name: %s. new name: %s", source, target) // Mount is called after the format is done. // TODO: Confirm that fstype is empty. @@ -103,7 +103,7 @@ func (mounter *CSIProxyMounterV1Beta) Mount(source string, target string, fstype // TODO: Call separate rmdir for pod context and plugin context. v1alpha1 for CSI // proxy does a relaxed check for prefix as c:\var\lib\kubelet, so we can do // rmdir with either pod or plugin context. -func (mounter *CSIProxyMounterV1Beta) Rmdir(path string) error { +func (mounter *csiProxyMounterV1Beta) Rmdir(path string) error { klog.V(4).Infof("Remove directory: %s", path) rmdirRequest := &fs.RmdirRequest{ Path: normalizeWindowsPath(path), @@ -118,23 +118,23 @@ func (mounter *CSIProxyMounterV1Beta) Rmdir(path string) error { } // Unmount - Removes the directory - equivalent to unmount on Linux. -func (mounter *CSIProxyMounterV1Beta) Unmount(target string) error { +func (mounter *csiProxyMounterV1Beta) Unmount(target string) error { klog.V(4).Infof("Unmount: %s", target) return mounter.Rmdir(target) } -func (mounter *CSIProxyMounterV1Beta) List() ([]mount.MountPoint, error) { +func (mounter *csiProxyMounterV1Beta) List() ([]mount.MountPoint, error) { return []mount.MountPoint{}, fmt.Errorf("List not implemented for CSIProxyMounterV1Beta") } -func (mounter *CSIProxyMounterV1Beta) IsMountPointMatch(mp mount.MountPoint, dir string) bool { +func (mounter *csiProxyMounterV1Beta) IsMountPointMatch(mp mount.MountPoint, dir string) bool { return mp.Path == dir } // IsLikelyMountPoint - If the directory does not exists, the function will return os.ErrNotExist error. // If the path exists, call to CSI proxy will check if its a link, if its a link then existence of target // path is checked. -func (mounter *CSIProxyMounterV1Beta) IsLikelyNotMountPoint(path string) (bool, error) { +func (mounter *csiProxyMounterV1Beta) IsLikelyNotMountPoint(path string) (bool, error) { klog.V(4).Infof("IsLikelyNotMountPoint: %s", path) isExists, err := mounter.ExistsPath(path) if err != nil { @@ -154,30 +154,30 @@ func (mounter *CSIProxyMounterV1Beta) IsLikelyNotMountPoint(path string) (bool, return !response.IsMountPoint, nil } -func (mounter *CSIProxyMounterV1Beta) PathIsDevice(pathname string) (bool, error) { +func (mounter *csiProxyMounterV1Beta) PathIsDevice(pathname string) (bool, error) { return false, fmt.Errorf("PathIsDevice not implemented for CSIProxyMounterV1Beta") } -func (mounter *CSIProxyMounterV1Beta) DeviceOpened(pathname string) (bool, error) { +func (mounter *csiProxyMounterV1Beta) DeviceOpened(pathname string) (bool, error) { return false, fmt.Errorf("DeviceOpened not implemented for CSIProxyMounterV1Beta") } -func (mounter *CSIProxyMounterV1Beta) GetDeviceNameFromMount(mountPath, pluginMountDir string) (string, error) { +func (mounter *csiProxyMounterV1Beta) GetDeviceNameFromMount(mountPath, pluginMountDir string) (string, error) { return "", fmt.Errorf("GetDeviceNameFromMount not implemented for CSIProxyMounterV1Beta") } -func (mounter *CSIProxyMounterV1Beta) MakeRShared(path string) error { +func (mounter *csiProxyMounterV1Beta) MakeRShared(path string) error { return fmt.Errorf("MakeRShared not implemented for CSIProxyMounterV1Beta") } -func (mounter *CSIProxyMounterV1Beta) MakeFile(pathname string) error { +func (mounter *csiProxyMounterV1Beta) MakeFile(pathname string) error { return fmt.Errorf("MakeFile not implemented for CSIProxyMounterV1Beta") } // MakeDir - Creates a directory. The CSI proxy takes in context information. // Currently the make dir is only used from the staging code path, hence we call it // with Plugin context.. -func (mounter *CSIProxyMounterV1Beta) MakeDir(path string) error { +func (mounter *csiProxyMounterV1Beta) MakeDir(path string) error { klog.V(4).Infof("Make directory: %s", path) mkdirReq := &fs.MkdirRequest{ Path: normalizeWindowsPath(path), @@ -192,7 +192,7 @@ func (mounter *CSIProxyMounterV1Beta) MakeDir(path string) error { } // ExistsPath - Checks if a path exists. Unlike util ExistsPath, this call does not perform follow link. -func (mounter *CSIProxyMounterV1Beta) ExistsPath(path string) (bool, error) { +func (mounter *csiProxyMounterV1Beta) ExistsPath(path string) (bool, error) { klog.V(4).Infof("Exists path: %s", path) isExistsResponse, err := mounter.FsClient.PathExists(context.Background(), &fs.PathExistsRequest{ @@ -205,7 +205,7 @@ func (mounter *CSIProxyMounterV1Beta) ExistsPath(path string) (bool, error) { } // GetAPIVersions returns the versions of the client APIs this mounter is using. -func (mounter *CSIProxyMounterV1Beta) GetAPIVersions() string { +func (mounter *csiProxyMounterV1Beta) GetAPIVersions() string { return fmt.Sprintf( "API Versions filesystem: %s, SMB: %s", fsclient.Version, @@ -213,37 +213,37 @@ func (mounter *CSIProxyMounterV1Beta) GetAPIVersions() string { ) } -func (mounter *CSIProxyMounterV1Beta) EvalHostSymlinks(pathname string) (string, error) { +func (mounter *csiProxyMounterV1Beta) EvalHostSymlinks(pathname string) (string, error) { return "", fmt.Errorf("EvalHostSymlinks not implemented for CSIProxyMounterV1Beta") } -func (mounter *CSIProxyMounterV1Beta) GetMountRefs(pathname string) ([]string, error) { +func (mounter *csiProxyMounterV1Beta) GetMountRefs(pathname string) ([]string, error) { return []string{}, fmt.Errorf("GetMountRefs not implemented for CSIProxyMounterV1Beta") } -func (mounter *CSIProxyMounterV1Beta) GetFSGroup(pathname string) (int64, error) { +func (mounter *csiProxyMounterV1Beta) GetFSGroup(pathname string) (int64, error) { return -1, fmt.Errorf("GetFSGroup not implemented for CSIProxyMounterV1Beta") } -func (mounter *CSIProxyMounterV1Beta) GetSELinuxSupport(pathname string) (bool, error) { +func (mounter *csiProxyMounterV1Beta) GetSELinuxSupport(pathname string) (bool, error) { return false, fmt.Errorf("GetSELinuxSupport not implemented for CSIProxyMounterV1Beta") } -func (mounter *CSIProxyMounterV1Beta) GetMode(pathname string) (os.FileMode, error) { +func (mounter *csiProxyMounterV1Beta) GetMode(pathname string) (os.FileMode, error) { return 0, fmt.Errorf("GetMode not implemented for CSIProxyMounterV1Beta") } -func (mounter *CSIProxyMounterV1Beta) MountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error { +func (mounter *csiProxyMounterV1Beta) MountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error { return fmt.Errorf("MountSensitive not implemented for CSIProxyMounterV1Beta") } -func (mounter *CSIProxyMounterV1Beta) MountSensitiveWithoutSystemd(source string, target string, fstype string, options []string, sensitiveOptions []string) error { +func (mounter *csiProxyMounterV1Beta) MountSensitiveWithoutSystemd(source string, target string, fstype string, options []string, sensitiveOptions []string) error { return fmt.Errorf("MountSensitiveWithoutSystemd not implemented for CSIProxyMounterV1Beta") } // NewCSIProxyMounter - creates a new CSI Proxy mounter struct which encompassed all the // clients to the CSI proxy - filesystem, disk and volume clients. -func NewCSIProxyMounterV1Beta() (*CSIProxyMounterV1Beta, error) { +func NewCSIProxyMounterV1Beta() (*csiProxyMounterV1Beta, error) { fsClient, err := fsclient.NewClient() if err != nil { return nil, err @@ -253,7 +253,7 @@ func NewCSIProxyMounterV1Beta() (*CSIProxyMounterV1Beta, error) { return nil, err } - return &CSIProxyMounterV1Beta{ + return &csiProxyMounterV1Beta{ FsClient: fsClient, SMBClient: smbClient, }, nil diff --git a/pkg/mounter/safe_mounter_windows.go b/pkg/mounter/safe_mounter_windows.go index 3db46d0bfd..7acde398de 100644 --- a/pkg/mounter/safe_mounter_windows.go +++ b/pkg/mounter/safe_mounter_windows.go @@ -37,9 +37,23 @@ import ( utilexec "k8s.io/utils/exec" ) -var _ mount.Interface = &CSIProxyMounter{} +// CSIProxyMounter extends the mount.Interface interface with CSI Proxy methods. +type CSIProxyMounter interface { + mount.Interface + + SMBMount(source, target, fsType string, mountOptions, sensitiveMountOptions []string) error + SMBUnmount(target string) error + MakeDir(path string) error + Rmdir(path string) error + IsMountPointMatch(mp mount.MountPoint, dir string) bool + ExistsPath(path string) (bool, error) + GetAPIVersions() string + EvalHostSymlinks(pathname string) (string, error) +} + +var _ CSIProxyMounter = &csiProxyMounter{} -type CSIProxyMounter struct { +type csiProxyMounter struct { FsClient *fsclient.Client SMBClient *smbclient.Client } @@ -52,7 +66,7 @@ func normalizeWindowsPath(path string) string { return normalizedPath } -func (mounter *CSIProxyMounter) SMBMount(source, target, fsType string, mountOptions, sensitiveMountOptions []string) error { +func (mounter *csiProxyMounter) SMBMount(source, target, fsType string, mountOptions, sensitiveMountOptions []string) error { klog.V(4).Infof("SMBMount: remote path: %s. local path: %s", source, target) if len(mountOptions) == 0 || len(sensitiveMountOptions) == 0 { @@ -85,7 +99,7 @@ func (mounter *CSIProxyMounter) SMBMount(source, target, fsType string, mountOpt return nil } -func (mounter *CSIProxyMounter) SMBUnmount(target string) error { +func (mounter *csiProxyMounter) SMBUnmount(target string) error { klog.V(4).Infof("SMBUnmount: local path: %s", target) // TODO: We need to remove the SMB mapping. The change to remove the // directory brings the CSI code in parity with the in-tree. @@ -93,7 +107,7 @@ func (mounter *CSIProxyMounter) SMBUnmount(target string) error { } // Mount just creates a soft link at target pointing to source. -func (mounter *CSIProxyMounter) Mount(source string, target string, fstype string, options []string) error { +func (mounter *csiProxyMounter) Mount(source string, target string, fstype string, options []string) error { klog.V(4).Infof("Mount: old name: %s. new name: %s", source, target) // Mount is called after the format is done. // TODO: Confirm that fstype is empty. @@ -112,7 +126,7 @@ func (mounter *CSIProxyMounter) Mount(source string, target string, fstype strin // TODO: Call separate rmdir for pod context and plugin context. v1alpha1 for CSI // proxy does a relaxed check for prefix as c:\var\lib\kubelet, so we can do // rmdir with either pod or plugin context. -func (mounter *CSIProxyMounter) Rmdir(path string) error { +func (mounter *csiProxyMounter) Rmdir(path string) error { klog.V(4).Infof("Remove directory: %s", path) rmdirRequest := &fs.RmdirRequest{ Path: normalizeWindowsPath(path), @@ -126,23 +140,23 @@ func (mounter *CSIProxyMounter) Rmdir(path string) error { } // Unmount - Removes the directory - equivalent to unmount on Linux. -func (mounter *CSIProxyMounter) Unmount(target string) error { +func (mounter *csiProxyMounter) Unmount(target string) error { klog.V(4).Infof("Unmount: %s", target) return mounter.Rmdir(target) } -func (mounter *CSIProxyMounter) List() ([]mount.MountPoint, error) { +func (mounter *csiProxyMounter) List() ([]mount.MountPoint, error) { return []mount.MountPoint{}, fmt.Errorf("List not implemented for CSIProxyMounter") } -func (mounter *CSIProxyMounter) IsMountPointMatch(mp mount.MountPoint, dir string) bool { +func (mounter *csiProxyMounter) IsMountPointMatch(mp mount.MountPoint, dir string) bool { return mp.Path == dir } // IsLikelyMountPoint - If the directory does not exists, the function will return os.ErrNotExist error. // If the path exists, call to CSI proxy will check if its a link, if its a link then existence of target // path is checked. -func (mounter *CSIProxyMounter) IsLikelyNotMountPoint(path string) (bool, error) { +func (mounter *csiProxyMounter) IsLikelyNotMountPoint(path string) (bool, error) { klog.V(4).Infof("IsLikelyNotMountPoint: %s", path) isExists, err := mounter.ExistsPath(path) if err != nil { @@ -162,30 +176,30 @@ func (mounter *CSIProxyMounter) IsLikelyNotMountPoint(path string) (bool, error) return !response.IsSymlink, nil } -func (mounter *CSIProxyMounter) PathIsDevice(pathname string) (bool, error) { +func (mounter *csiProxyMounter) PathIsDevice(pathname string) (bool, error) { return false, fmt.Errorf("PathIsDevice not implemented for CSIProxyMounter") } -func (mounter *CSIProxyMounter) DeviceOpened(pathname string) (bool, error) { +func (mounter *csiProxyMounter) DeviceOpened(pathname string) (bool, error) { return false, fmt.Errorf("DeviceOpened not implemented for CSIProxyMounter") } -func (mounter *CSIProxyMounter) GetDeviceNameFromMount(mountPath, pluginMountDir string) (string, error) { +func (mounter *csiProxyMounter) GetDeviceNameFromMount(mountPath, pluginMountDir string) (string, error) { return "", fmt.Errorf("GetDeviceNameFromMount not implemented for CSIProxyMounter") } -func (mounter *CSIProxyMounter) MakeRShared(path string) error { +func (mounter *csiProxyMounter) MakeRShared(path string) error { return fmt.Errorf("MakeRShared not implemented for CSIProxyMounter") } -func (mounter *CSIProxyMounter) MakeFile(pathname string) error { +func (mounter *csiProxyMounter) MakeFile(pathname string) error { return fmt.Errorf("MakeFile not implemented for CSIProxyMounter") } // MakeDir - Creates a directory. The CSI proxy takes in context information. // Currently the make dir is only used from the staging code path, hence we call it // with Plugin context.. -func (mounter *CSIProxyMounter) MakeDir(path string) error { +func (mounter *csiProxyMounter) MakeDir(path string) error { klog.V(4).Infof("Make directory: %s", path) mkdirReq := &fs.MkdirRequest{ Path: normalizeWindowsPath(path), @@ -199,7 +213,7 @@ func (mounter *CSIProxyMounter) MakeDir(path string) error { } // ExistsPath - Checks if a path exists. Unlike util ExistsPath, this call does not perform follow link. -func (mounter *CSIProxyMounter) ExistsPath(path string) (bool, error) { +func (mounter *csiProxyMounter) ExistsPath(path string) (bool, error) { klog.V(4).Infof("Exists path: %s", path) isExistsResponse, err := mounter.FsClient.PathExists(context.Background(), &fs.PathExistsRequest{ @@ -212,7 +226,7 @@ func (mounter *CSIProxyMounter) ExistsPath(path string) (bool, error) { } // GetAPIVersions returns the versions of the client APIs this mounter is using. -func (mounter *CSIProxyMounter) GetAPIVersions() string { +func (mounter *csiProxyMounter) GetAPIVersions() string { return fmt.Sprintf( "API Versions filesystem: %s, SMB: %s", fsclient.Version, @@ -220,37 +234,37 @@ func (mounter *CSIProxyMounter) GetAPIVersions() string { ) } -func (mounter *CSIProxyMounter) EvalHostSymlinks(pathname string) (string, error) { +func (mounter *csiProxyMounter) EvalHostSymlinks(pathname string) (string, error) { return "", fmt.Errorf("EvalHostSymlinks not implemented for CSIProxyMounter") } -func (mounter *CSIProxyMounter) GetMountRefs(pathname string) ([]string, error) { +func (mounter *csiProxyMounter) GetMountRefs(pathname string) ([]string, error) { return []string{}, fmt.Errorf("GetMountRefs not implemented for CSIProxyMounter") } -func (mounter *CSIProxyMounter) GetFSGroup(pathname string) (int64, error) { +func (mounter *csiProxyMounter) GetFSGroup(pathname string) (int64, error) { return -1, fmt.Errorf("GetFSGroup not implemented for CSIProxyMounter") } -func (mounter *CSIProxyMounter) GetSELinuxSupport(pathname string) (bool, error) { +func (mounter *csiProxyMounter) GetSELinuxSupport(pathname string) (bool, error) { return false, fmt.Errorf("GetSELinuxSupport not implemented for CSIProxyMounter") } -func (mounter *CSIProxyMounter) GetMode(pathname string) (os.FileMode, error) { +func (mounter *csiProxyMounter) GetMode(pathname string) (os.FileMode, error) { return 0, fmt.Errorf("GetMode not implemented for CSIProxyMounter") } -func (mounter *CSIProxyMounter) MountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error { +func (mounter *csiProxyMounter) MountSensitive(source string, target string, fstype string, options []string, sensitiveOptions []string) error { return fmt.Errorf("MountSensitive not implemented for CSIProxyMounter") } -func (mounter *CSIProxyMounter) MountSensitiveWithoutSystemd(source string, target string, fstype string, options []string, sensitiveOptions []string) error { +func (mounter *csiProxyMounter) MountSensitiveWithoutSystemd(source string, target string, fstype string, options []string, sensitiveOptions []string) error { return fmt.Errorf("MountSensitiveWithoutSystemd not implemented for CSIProxyMounter") } // NewCSIProxyMounter - creates a new CSI Proxy mounter struct which encompassed all the // clients to the CSI proxy - filesystem, disk and volume clients. -func NewCSIProxyMounter() (*CSIProxyMounter, error) { +func NewCSIProxyMounter() (*csiProxyMounter, error) { fsClient, err := fsclient.NewClient() if err != nil { return nil, err @@ -260,7 +274,7 @@ func NewCSIProxyMounter() (*CSIProxyMounter, error) { return nil, err } - return &CSIProxyMounter{ + return &csiProxyMounter{ FsClient: fsClient, SMBClient: smbClient, }, nil