azure_mounter.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // +build !providerless
  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 azure_dd
  15. import (
  16. "fmt"
  17. "os"
  18. "runtime"
  19. "k8s.io/klog"
  20. "k8s.io/utils/mount"
  21. v1 "k8s.io/api/core/v1"
  22. "k8s.io/kubernetes/pkg/volume"
  23. "k8s.io/kubernetes/pkg/volume/util"
  24. )
  25. type azureDiskMounter struct {
  26. *dataDisk
  27. spec *volume.Spec
  28. plugin *azureDataDiskPlugin
  29. options volume.VolumeOptions
  30. }
  31. type azureDiskUnmounter struct {
  32. *dataDisk
  33. plugin *azureDataDiskPlugin
  34. }
  35. var _ volume.Unmounter = &azureDiskUnmounter{}
  36. var _ volume.Mounter = &azureDiskMounter{}
  37. func (m *azureDiskMounter) GetAttributes() volume.Attributes {
  38. readOnly := false
  39. volumeSource, _, err := getVolumeSource(m.spec)
  40. if err != nil {
  41. klog.Infof("azureDisk - mounter failed to get volume source for spec %s %v", m.spec.Name(), err)
  42. } else if volumeSource.ReadOnly != nil {
  43. readOnly = *volumeSource.ReadOnly
  44. }
  45. return volume.Attributes{
  46. ReadOnly: readOnly,
  47. Managed: !readOnly,
  48. SupportsSELinux: true,
  49. }
  50. }
  51. func (m *azureDiskMounter) CanMount() error {
  52. return nil
  53. }
  54. func (m *azureDiskMounter) SetUp(mounterArgs volume.MounterArgs) error {
  55. return m.SetUpAt(m.GetPath(), mounterArgs)
  56. }
  57. func (m *azureDiskMounter) GetPath() string {
  58. return getPath(m.dataDisk.podUID, m.dataDisk.volumeName, m.plugin.host)
  59. }
  60. func (m *azureDiskMounter) SetUpAt(dir string, mounterArgs volume.MounterArgs) error {
  61. mounter := m.plugin.host.GetMounter(m.plugin.GetPluginName())
  62. volumeSource, _, err := getVolumeSource(m.spec)
  63. if err != nil {
  64. klog.Infof("azureDisk - mounter failed to get volume source for spec %s", m.spec.Name())
  65. return err
  66. }
  67. diskName := volumeSource.DiskName
  68. mountPoint, err := mounter.IsLikelyNotMountPoint(dir)
  69. if err != nil && !os.IsNotExist(err) {
  70. klog.Infof("azureDisk - cannot validate mount point for disk %s on %s %v", diskName, dir, err)
  71. return err
  72. }
  73. if !mountPoint {
  74. // testing original mount point, make sure the mount link is valid
  75. _, err := (&osIOHandler{}).ReadDir(dir)
  76. if err == nil {
  77. klog.V(4).Infof("azureDisk - already mounted to target %s", dir)
  78. return nil
  79. }
  80. // mount link is invalid, now unmount and remount later
  81. klog.Warningf("azureDisk - ReadDir %s failed with %v, unmount this directory", dir, err)
  82. if err := mounter.Unmount(dir); err != nil {
  83. klog.Errorf("azureDisk - Unmount directory %s failed with %v", dir, err)
  84. return err
  85. }
  86. mountPoint = true
  87. }
  88. if runtime.GOOS != "windows" {
  89. // in windows, we will use mklink to mount, will MkdirAll in Mount func
  90. if err := os.MkdirAll(dir, 0750); err != nil {
  91. klog.Errorf("azureDisk - mkdir failed on disk %s on dir: %s (%v)", diskName, dir, err)
  92. return err
  93. }
  94. }
  95. options := []string{"bind"}
  96. if volumeSource.ReadOnly != nil && *volumeSource.ReadOnly {
  97. options = append(options, "ro")
  98. }
  99. if m.options.MountOptions != nil {
  100. options = util.JoinMountOptions(m.options.MountOptions, options)
  101. }
  102. klog.V(4).Infof("azureDisk - Attempting to mount %s on %s", diskName, dir)
  103. isManagedDisk := (*volumeSource.Kind == v1.AzureManagedDisk)
  104. globalPDPath, err := makeGlobalPDPath(m.plugin.host, volumeSource.DataDiskURI, isManagedDisk)
  105. if err != nil {
  106. return err
  107. }
  108. mountErr := mounter.Mount(globalPDPath, dir, *volumeSource.FSType, options)
  109. // Everything in the following control flow is meant as an
  110. // attempt cleanup a failed setupAt (bind mount)
  111. if mountErr != nil {
  112. klog.Infof("azureDisk - SetupAt:Mount disk:%s at dir:%s failed during mounting with error:%v, will attempt to clean up", diskName, dir, mountErr)
  113. mountPoint, err := mounter.IsLikelyNotMountPoint(dir)
  114. if err != nil {
  115. return fmt.Errorf("azureDisk - SetupAt:Mount:Failure:cleanup IsLikelyNotMountPoint check failed for disk:%s on dir:%s with error %v original-mountErr:%v", diskName, dir, err, mountErr)
  116. }
  117. if !mountPoint {
  118. if err = mounter.Unmount(dir); err != nil {
  119. return fmt.Errorf("azureDisk - SetupAt:Mount:Failure:cleanup failed to unmount disk:%s on dir:%s with error:%v original-mountErr:%v", diskName, dir, err, mountErr)
  120. }
  121. mountPoint, err := mounter.IsLikelyNotMountPoint(dir)
  122. if err != nil {
  123. return fmt.Errorf("azureDisk - SetupAt:Mount:Failure:cleanup IsLikelyNotMountPoint for disk:%s on dir:%s check failed with error:%v original-mountErr:%v", diskName, dir, err, mountErr)
  124. }
  125. if !mountPoint {
  126. // not cool. leave for next sync loop.
  127. return fmt.Errorf("azureDisk - SetupAt:Mount:Failure:cleanup disk %s is still mounted on %s during cleanup original-mountErr:%v, despite call to unmount(). Will try again next sync loop.", diskName, dir, mountErr)
  128. }
  129. }
  130. if err = os.Remove(dir); err != nil {
  131. return fmt.Errorf("azureDisk - SetupAt:Mount:Failure error cleaning up (removing dir:%s) with error:%v original-mountErr:%v", dir, err, mountErr)
  132. }
  133. klog.V(2).Infof("azureDisk - Mount of disk:%s on dir:%s failed with mount error:%v post failure clean up was completed", diskName, dir, mountErr)
  134. return mountErr
  135. }
  136. if volumeSource.ReadOnly == nil || !*volumeSource.ReadOnly {
  137. volume.SetVolumeOwnership(m, mounterArgs.FsGroup)
  138. }
  139. klog.V(2).Infof("azureDisk - successfully mounted disk %s on %s", diskName, dir)
  140. return nil
  141. }
  142. func (u *azureDiskUnmounter) TearDown() error {
  143. return u.TearDownAt(u.GetPath())
  144. }
  145. func (u *azureDiskUnmounter) TearDownAt(dir string) error {
  146. if pathExists, pathErr := mount.PathExists(dir); pathErr != nil {
  147. return fmt.Errorf("Error checking if path exists: %v", pathErr)
  148. } else if !pathExists {
  149. klog.Warningf("Warning: Unmount skipped because path does not exist: %v", dir)
  150. return nil
  151. }
  152. klog.V(4).Infof("azureDisk - TearDownAt: %s", dir)
  153. mounter := u.plugin.host.GetMounter(u.plugin.GetPluginName())
  154. mountPoint, err := mounter.IsLikelyNotMountPoint(dir)
  155. if err != nil {
  156. return fmt.Errorf("azureDisk - TearDownAt: %s failed to do IsLikelyNotMountPoint %s", dir, err)
  157. }
  158. if mountPoint {
  159. if err := os.Remove(dir); err != nil {
  160. return fmt.Errorf("azureDisk - TearDownAt: %s failed to do os.Remove %s", dir, err)
  161. }
  162. }
  163. if err := mounter.Unmount(dir); err != nil {
  164. return fmt.Errorf("azureDisk - TearDownAt: %s failed to do mounter.Unmount %s", dir, err)
  165. }
  166. mountPoint, err = mounter.IsLikelyNotMountPoint(dir)
  167. if err != nil {
  168. return fmt.Errorf("azureDisk - TearTownAt:IsLikelyNotMountPoint check failed: %v", err)
  169. }
  170. if mountPoint {
  171. return os.Remove(dir)
  172. }
  173. return fmt.Errorf("azureDisk - failed to un-bind-mount volume dir")
  174. }
  175. func (u *azureDiskUnmounter) GetPath() string {
  176. return getPath(u.dataDisk.podUID, u.dataDisk.volumeName, u.plugin.host)
  177. }