azure_mounter.go 6.9 KB

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