volume.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. Copyright 2014 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 volume
  14. import (
  15. "time"
  16. v1 "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/api/resource"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/types"
  20. )
  21. // Volume represents a directory used by pods or hosts on a node. All method
  22. // implementations of methods in the volume interface must be idempotent.
  23. type Volume interface {
  24. // GetPath returns the path to which the volume should be mounted for the
  25. // pod.
  26. GetPath() string
  27. // MetricsProvider embeds methods for exposing metrics (e.g.
  28. // used, available space).
  29. MetricsProvider
  30. }
  31. // BlockVolume interface provides methods to generate global map path
  32. // and pod device map path.
  33. type BlockVolume interface {
  34. // GetGlobalMapPath returns a global map path which contains
  35. // bind mount associated to a block device.
  36. // ex. plugins/kubernetes.io/{PluginName}/{DefaultKubeletVolumeDevicesDirName}/{volumePluginDependentPath}/{pod uuid}
  37. GetGlobalMapPath(spec *Spec) (string, error)
  38. // GetPodDeviceMapPath returns a pod device map path
  39. // and name of a symbolic link associated to a block device.
  40. // ex. pods/{podUid}/{DefaultKubeletVolumeDevicesDirName}/{escapeQualifiedPluginName}/, {volumeName}
  41. GetPodDeviceMapPath() (string, string)
  42. }
  43. // MetricsProvider exposes metrics (e.g. used,available space) related to a
  44. // Volume.
  45. type MetricsProvider interface {
  46. // GetMetrics returns the Metrics for the Volume. Maybe expensive for
  47. // some implementations.
  48. GetMetrics() (*Metrics, error)
  49. }
  50. // Metrics represents the used and available bytes of the Volume.
  51. type Metrics struct {
  52. // The time at which these stats were updated.
  53. Time metav1.Time
  54. // Used represents the total bytes used by the Volume.
  55. // Note: For block devices this maybe more than the total size of the files.
  56. Used *resource.Quantity
  57. // Capacity represents the total capacity (bytes) of the volume's
  58. // underlying storage. For Volumes that share a filesystem with the host
  59. // (e.g. emptydir, hostpath) this is the size of the underlying storage,
  60. // and will not equal Used + Available as the fs is shared.
  61. Capacity *resource.Quantity
  62. // Available represents the storage space available (bytes) for the
  63. // Volume. For Volumes that share a filesystem with the host (e.g.
  64. // emptydir, hostpath), this is the available space on the underlying
  65. // storage, and is shared with host processes and other Volumes.
  66. Available *resource.Quantity
  67. // InodesUsed represents the total inodes used by the Volume.
  68. InodesUsed *resource.Quantity
  69. // Inodes represents the total number of inodes available in the volume.
  70. // For volumes that share a filesystem with the host (e.g. emptydir, hostpath),
  71. // this is the inodes available in the underlying storage,
  72. // and will not equal InodesUsed + InodesFree as the fs is shared.
  73. Inodes *resource.Quantity
  74. // InodesFree represent the inodes available for the volume. For Volumes that share
  75. // a filesystem with the host (e.g. emptydir, hostpath), this is the free inodes
  76. // on the underlying storage, and is shared with host processes and other volumes
  77. InodesFree *resource.Quantity
  78. }
  79. // Attributes represents the attributes of this mounter.
  80. type Attributes struct {
  81. ReadOnly bool
  82. Managed bool
  83. SupportsSELinux bool
  84. }
  85. // MounterArgs provides more easily extensible arguments to Mounter
  86. type MounterArgs struct {
  87. FsGroup *int64
  88. DesiredSize *resource.Quantity
  89. }
  90. // Mounter interface provides methods to set up/mount the volume.
  91. type Mounter interface {
  92. // Uses Interface to provide the path for Docker binds.
  93. Volume
  94. // CanMount is called immediately prior to Setup to check if
  95. // the required components (binaries, etc.) are available on
  96. // the underlying node to complete the subsequent SetUp (mount)
  97. // operation. If CanMount returns error, the mount operation is
  98. // aborted and an event is generated indicating that the node
  99. // does not have the required binaries to complete the mount.
  100. // If CanMount succeeds, the mount operation continues
  101. // normally. The CanMount check can be enabled or disabled
  102. // using the experimental-check-mount-binaries binary flag
  103. CanMount() error
  104. // SetUp prepares and mounts/unpacks the volume to a
  105. // self-determined directory path. The mount point and its
  106. // content should be owned by 'fsGroup' so that it can be
  107. // accessed by the pod. This may be called more than once, so
  108. // implementations must be idempotent.
  109. // It could return following types of errors:
  110. // - TransientOperationFailure
  111. // - UncertainProgressError
  112. // - Error of any other type should be considered a final error
  113. SetUp(mounterArgs MounterArgs) error
  114. // SetUpAt prepares and mounts/unpacks the volume to the
  115. // specified directory path, which may or may not exist yet.
  116. // The mount point and its content should be owned by
  117. // 'fsGroup' so that it can be accessed by the pod. This may
  118. // be called more than once, so implementations must be
  119. // idempotent.
  120. SetUpAt(dir string, mounterArgs MounterArgs) error
  121. // GetAttributes returns the attributes of the mounter.
  122. // This function is called after SetUp()/SetUpAt().
  123. GetAttributes() Attributes
  124. }
  125. // Unmounter interface provides methods to cleanup/unmount the volumes.
  126. type Unmounter interface {
  127. Volume
  128. // TearDown unmounts the volume from a self-determined directory and
  129. // removes traces of the SetUp procedure.
  130. TearDown() error
  131. // TearDown unmounts the volume from the specified directory and
  132. // removes traces of the SetUp procedure.
  133. TearDownAt(dir string) error
  134. }
  135. // BlockVolumeMapper interface is a mapper interface for block volume.
  136. type BlockVolumeMapper interface {
  137. BlockVolume
  138. }
  139. // CustomBlockVolumeMapper interface provides custom methods to set up/map the volume.
  140. type CustomBlockVolumeMapper interface {
  141. BlockVolumeMapper
  142. // SetUpDevice prepares the volume to the node by the plugin specific way.
  143. // For most in-tree plugins, attacher.Attach() and attacher.WaitForAttach()
  144. // will do necessary works.
  145. // This may be called more than once, so implementations must be idempotent.
  146. SetUpDevice() error
  147. // MapPodDevice maps the block device to a path and return the path.
  148. // Unique device path across kubelet node reboot is required to avoid
  149. // unexpected block volume destruction.
  150. // If empty string is returned, the path retuned by attacher.Attach() and
  151. // attacher.WaitForAttach() will be used.
  152. MapPodDevice() (string, error)
  153. }
  154. // BlockVolumeUnmapper interface is an unmapper interface for block volume.
  155. type BlockVolumeUnmapper interface {
  156. BlockVolume
  157. }
  158. // CustomBlockVolumeUnmapper interface provides custom methods to cleanup/unmap the volumes.
  159. type CustomBlockVolumeUnmapper interface {
  160. BlockVolumeUnmapper
  161. // TearDownDevice removes traces of the SetUpDevice procedure.
  162. // If the plugin is non-attachable, this method detaches the volume
  163. // from a node.
  164. TearDownDevice(mapPath string, devicePath string) error
  165. // UnmapPodDevice removes traces of the MapPodDevice procedure.
  166. UnmapPodDevice() error
  167. }
  168. // Provisioner is an interface that creates templates for PersistentVolumes
  169. // and can create the volume as a new resource in the infrastructure provider.
  170. type Provisioner interface {
  171. // Provision creates the resource by allocating the underlying volume in a
  172. // storage system. This method should block until completion and returns
  173. // PersistentVolume representing the created storage resource.
  174. Provision(selectedNode *v1.Node, allowedTopologies []v1.TopologySelectorTerm) (*v1.PersistentVolume, error)
  175. }
  176. // Deleter removes the resource from the underlying storage provider. Calls
  177. // to this method should block until the deletion is complete. Any error
  178. // returned indicates the volume has failed to be reclaimed. A nil return
  179. // indicates success.
  180. type Deleter interface {
  181. Volume
  182. // This method should block until completion.
  183. // deletedVolumeInUseError returned from this function will not be reported
  184. // as error and it will be sent as "Info" event to the PV being deleted. The
  185. // volume controller will retry deleting the volume in the next periodic
  186. // sync. This can be used to postpone deletion of a volume that is being
  187. // detached from a node. Deletion of such volume would fail anyway and such
  188. // error would confuse users.
  189. Delete() error
  190. }
  191. // Attacher can attach a volume to a node.
  192. type Attacher interface {
  193. DeviceMounter
  194. // Attaches the volume specified by the given spec to the node with the given Name.
  195. // On success, returns the device path where the device was attached on the
  196. // node.
  197. Attach(spec *Spec, nodeName types.NodeName) (string, error)
  198. // VolumesAreAttached checks whether the list of volumes still attached to the specified
  199. // node. It returns a map which maps from the volume spec to the checking result.
  200. // If an error is occurred during checking, the error will be returned
  201. VolumesAreAttached(specs []*Spec, nodeName types.NodeName) (map[*Spec]bool, error)
  202. // WaitForAttach blocks until the device is attached to this
  203. // node. If it successfully attaches, the path to the device
  204. // is returned. Otherwise, if the device does not attach after
  205. // the given timeout period, an error will be returned.
  206. WaitForAttach(spec *Spec, devicePath string, pod *v1.Pod, timeout time.Duration) (string, error)
  207. }
  208. // DeviceMounter can mount a block volume to a global path.
  209. type DeviceMounter interface {
  210. // GetDeviceMountPath returns a path where the device should
  211. // be mounted after it is attached. This is a global mount
  212. // point which should be bind mounted for individual volumes.
  213. GetDeviceMountPath(spec *Spec) (string, error)
  214. // MountDevice mounts the disk to a global path which
  215. // individual pods can then bind mount
  216. // Note that devicePath can be empty if the volume plugin does not implement any of Attach and WaitForAttach methods.
  217. // It could return following types of errors:
  218. // - TransientOperationFailure
  219. // - UncertainProgressError
  220. // - Error of any other type should be considered a final error
  221. MountDevice(spec *Spec, devicePath string, deviceMountPath string) error
  222. }
  223. type BulkVolumeVerifier interface {
  224. // BulkVerifyVolumes checks whether the list of volumes still attached to the
  225. // the clusters in the node. It returns a map which maps from the volume spec to the checking result.
  226. // If an error occurs during check - error should be returned and volume on nodes
  227. // should be assumed as still attached.
  228. BulkVerifyVolumes(volumesByNode map[types.NodeName][]*Spec) (map[types.NodeName]map[*Spec]bool, error)
  229. }
  230. // Detacher can detach a volume from a node.
  231. type Detacher interface {
  232. DeviceUnmounter
  233. // Detach the given volume from the node with the given Name.
  234. // volumeName is name of the volume as returned from plugin's
  235. // GetVolumeName().
  236. Detach(volumeName string, nodeName types.NodeName) error
  237. }
  238. // DeviceUnmounter can unmount a block volume from the global path.
  239. type DeviceUnmounter interface {
  240. // UnmountDevice unmounts the global mount of the disk. This
  241. // should only be called once all bind mounts have been
  242. // unmounted.
  243. UnmountDevice(deviceMountPath string) error
  244. }