volume.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. "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. // symbolic links 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. PodUID string
  90. }
  91. // Mounter interface provides methods to set up/mount the volume.
  92. type Mounter interface {
  93. // Uses Interface to provide the path for Docker binds.
  94. Volume
  95. // CanMount is called immediately prior to Setup to check if
  96. // the required components (binaries, etc.) are available on
  97. // the underlying node to complete the subsequent SetUp (mount)
  98. // operation. If CanMount returns error, the mount operation is
  99. // aborted and an event is generated indicating that the node
  100. // does not have the required binaries to complete the mount.
  101. // If CanMount succeeds, the mount operation continues
  102. // normally. The CanMount check can be enabled or disabled
  103. // using the experimental-check-mount-binaries binary flag
  104. CanMount() error
  105. // SetUp prepares and mounts/unpacks the volume to a
  106. // self-determined directory path. The mount point and its
  107. // content should be owned by 'fsGroup' so that it can be
  108. // accessed by the pod. This may be called more than once, so
  109. // implementations must be idempotent.
  110. SetUp(mounterArgs MounterArgs) error
  111. // SetUpAt prepares and mounts/unpacks the volume to the
  112. // specified directory path, which may or may not exist yet.
  113. // The mount point and its content should be owned by
  114. // 'fsGroup' so that it can be accessed by the pod. This may
  115. // be called more than once, so implementations must be
  116. // idempotent.
  117. SetUpAt(dir string, mounterArgs MounterArgs) error
  118. // GetAttributes returns the attributes of the mounter.
  119. // This function is called after SetUp()/SetUpAt().
  120. GetAttributes() Attributes
  121. }
  122. // Unmounter interface provides methods to cleanup/unmount the volumes.
  123. type Unmounter interface {
  124. Volume
  125. // TearDown unmounts the volume from a self-determined directory and
  126. // removes traces of the SetUp procedure.
  127. TearDown() error
  128. // TearDown unmounts the volume from the specified directory and
  129. // removes traces of the SetUp procedure.
  130. TearDownAt(dir string) error
  131. }
  132. // BlockVolumeMapper interface provides methods to set up/map the volume.
  133. type BlockVolumeMapper interface {
  134. BlockVolume
  135. // SetUpDevice prepares the volume to a self-determined directory path,
  136. // which may or may not exist yet and returns combination of physical
  137. // device path of a block volume and error.
  138. // If the plugin is non-attachable, it should prepare the device
  139. // in /dev/ (or where appropriate) and return unique device path.
  140. // Unique device path across kubelet node reboot is required to avoid
  141. // unexpected block volume destruction.
  142. // If the plugin is attachable, it should not do anything here,
  143. // just return empty string for device path.
  144. // Instead, attachable plugin have to return unique device path
  145. // at attacher.Attach() and attacher.WaitForAttach().
  146. // This may be called more than once, so implementations must be idempotent.
  147. SetUpDevice() (string, error)
  148. // Map maps the block device path for the specified spec and pod.
  149. MapDevice(devicePath, globalMapPath, volumeMapPath, volumeMapName string, podUID types.UID) error
  150. }
  151. // BlockVolumeUnmapper interface provides methods to cleanup/unmap the volumes.
  152. type BlockVolumeUnmapper interface {
  153. BlockVolume
  154. // TearDownDevice removes traces of the SetUpDevice procedure under
  155. // a self-determined directory.
  156. // If the plugin is non-attachable, this method detaches the volume
  157. // from a node.
  158. TearDownDevice(mapPath string, devicePath string) error
  159. }
  160. // Provisioner is an interface that creates templates for PersistentVolumes
  161. // and can create the volume as a new resource in the infrastructure provider.
  162. type Provisioner interface {
  163. // Provision creates the resource by allocating the underlying volume in a
  164. // storage system. This method should block until completion and returns
  165. // PersistentVolume representing the created storage resource.
  166. Provision(selectedNode *v1.Node, allowedTopologies []v1.TopologySelectorTerm) (*v1.PersistentVolume, error)
  167. }
  168. // Deleter removes the resource from the underlying storage provider. Calls
  169. // to this method should block until the deletion is complete. Any error
  170. // returned indicates the volume has failed to be reclaimed. A nil return
  171. // indicates success.
  172. type Deleter interface {
  173. Volume
  174. // This method should block until completion.
  175. // deletedVolumeInUseError returned from this function will not be reported
  176. // as error and it will be sent as "Info" event to the PV being deleted. The
  177. // volume controller will retry deleting the volume in the next periodic
  178. // sync. This can be used to postpone deletion of a volume that is being
  179. // detached from a node. Deletion of such volume would fail anyway and such
  180. // error would confuse users.
  181. Delete() error
  182. }
  183. // Attacher can attach a volume to a node.
  184. type Attacher interface {
  185. DeviceMounter
  186. // Attaches the volume specified by the given spec to the node with the given Name.
  187. // On success, returns the device path where the device was attached on the
  188. // node.
  189. Attach(spec *Spec, nodeName types.NodeName) (string, error)
  190. // VolumesAreAttached checks whether the list of volumes still attached to the specified
  191. // node. It returns a map which maps from the volume spec to the checking result.
  192. // If an error is occurred during checking, the error will be returned
  193. VolumesAreAttached(specs []*Spec, nodeName types.NodeName) (map[*Spec]bool, error)
  194. // WaitForAttach blocks until the device is attached to this
  195. // node. If it successfully attaches, the path to the device
  196. // is returned. Otherwise, if the device does not attach after
  197. // the given timeout period, an error will be returned.
  198. WaitForAttach(spec *Spec, devicePath string, pod *v1.Pod, timeout time.Duration) (string, error)
  199. }
  200. // DeviceMounter can mount a block volume to a global path.
  201. type DeviceMounter interface {
  202. // GetDeviceMountPath returns a path where the device should
  203. // be mounted after it is attached. This is a global mount
  204. // point which should be bind mounted for individual volumes.
  205. GetDeviceMountPath(spec *Spec) (string, error)
  206. // MountDevice mounts the disk to a global path which
  207. // individual pods can then bind mount
  208. // Note that devicePath can be empty if the volume plugin does not implement any of Attach and WaitForAttach methods.
  209. MountDevice(spec *Spec, devicePath string, deviceMountPath string) error
  210. }
  211. type BulkVolumeVerifier interface {
  212. // BulkVerifyVolumes checks whether the list of volumes still attached to the
  213. // the clusters in the node. It returns a map which maps from the volume spec to the checking result.
  214. // If an error occurs during check - error should be returned and volume on nodes
  215. // should be assumed as still attached.
  216. BulkVerifyVolumes(volumesByNode map[types.NodeName][]*Spec) (map[types.NodeName]map[*Spec]bool, error)
  217. }
  218. // Detacher can detach a volume from a node.
  219. type Detacher interface {
  220. DeviceUnmounter
  221. // Detach the given volume from the node with the given Name.
  222. // volumeName is name of the volume as returned from plugin's
  223. // GetVolumeName().
  224. Detach(volumeName string, nodeName types.NodeName) error
  225. }
  226. // DeviceUnmounter can unmount a block volume from the global path.
  227. type DeviceUnmounter interface {
  228. // UnmountDevice unmounts the global mount of the disk. This
  229. // should only be called once all bind mounts have been
  230. // unmounted.
  231. UnmountDevice(deviceMountPath string) error
  232. }