hostutil_windows.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // +build windows
  2. /*
  3. Copyright 2017 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package hostutil
  15. import (
  16. "fmt"
  17. "os"
  18. "path"
  19. "path/filepath"
  20. "strings"
  21. "k8s.io/klog"
  22. "k8s.io/utils/mount"
  23. utilpath "k8s.io/utils/path"
  24. )
  25. // HostUtil implements HostUtils for Windows platforms.
  26. type HostUtil struct{}
  27. // NewHostUtil returns a struct that implements HostUtils on Windows platforms
  28. func NewHostUtil() *HostUtil {
  29. return &HostUtil{}
  30. }
  31. // GetDeviceNameFromMount given a mnt point, find the device
  32. func (hu *HostUtil) GetDeviceNameFromMount(mounter mount.Interface, mountPath, pluginMountDir string) (string, error) {
  33. return getDeviceNameFromMount(mounter, mountPath, pluginMountDir)
  34. }
  35. // getDeviceNameFromMount find the device(drive) name in which
  36. // the mount path reference should match the given plugin mount directory. In case no mount path reference
  37. // matches, returns the volume name taken from its given mountPath
  38. func getDeviceNameFromMount(mounter mount.Interface, mountPath, pluginMountDir string) (string, error) {
  39. refs, err := mounter.GetMountRefs(mountPath)
  40. if err != nil {
  41. klog.V(4).Infof("GetMountRefs failed for mount path %q: %v", mountPath, err)
  42. return "", err
  43. }
  44. if len(refs) == 0 {
  45. return "", fmt.Errorf("directory %s is not mounted", mountPath)
  46. }
  47. basemountPath := mount.NormalizeWindowsPath(pluginMountDir)
  48. for _, ref := range refs {
  49. if strings.Contains(ref, basemountPath) {
  50. volumeID, err := filepath.Rel(mount.NormalizeWindowsPath(basemountPath), ref)
  51. if err != nil {
  52. klog.Errorf("Failed to get volume id from mount %s - %v", mountPath, err)
  53. return "", err
  54. }
  55. return volumeID, nil
  56. }
  57. }
  58. return path.Base(mountPath), nil
  59. }
  60. // DeviceOpened determines if the device is in use elsewhere
  61. func (hu *HostUtil) DeviceOpened(pathname string) (bool, error) {
  62. return false, nil
  63. }
  64. // PathIsDevice determines if a path is a device.
  65. func (hu *HostUtil) PathIsDevice(pathname string) (bool, error) {
  66. return false, nil
  67. }
  68. // MakeRShared checks that given path is on a mount with 'rshared' mount
  69. // propagation. Empty implementation here.
  70. func (hu *HostUtil) MakeRShared(path string) error {
  71. return nil
  72. }
  73. // GetFileType checks for sockets/block/character devices
  74. func (hu *(HostUtil)) GetFileType(pathname string) (FileType, error) {
  75. return getFileType(pathname)
  76. }
  77. // PathExists checks whether the path exists
  78. func (hu *HostUtil) PathExists(pathname string) (bool, error) {
  79. return utilpath.Exists(utilpath.CheckFollowSymlink, pathname)
  80. }
  81. // EvalHostSymlinks returns the path name after evaluating symlinks
  82. func (hu *HostUtil) EvalHostSymlinks(pathname string) (string, error) {
  83. return filepath.EvalSymlinks(pathname)
  84. }
  85. // GetOwner returns the integer ID for the user and group of the given path
  86. // Note that on windows, it always returns 0. We actually don't set Group on
  87. // windows platform, see SetVolumeOwnership implementation.
  88. func (hu *HostUtil) GetOwner(pathname string) (int64, int64, error) {
  89. return -1, -1, nil
  90. }
  91. // GetSELinuxSupport returns a boolean indicating support for SELinux.
  92. // Windows does not support SELinux.
  93. func (hu *HostUtil) GetSELinuxSupport(pathname string) (bool, error) {
  94. return false, nil
  95. }
  96. // GetMode returns permissions of the path.
  97. func (hu *HostUtil) GetMode(pathname string) (os.FileMode, error) {
  98. info, err := os.Stat(pathname)
  99. if err != nil {
  100. return 0, err
  101. }
  102. return info.Mode(), nil
  103. }