vsphere_volume.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. Copyright 2016 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 vsphere_volume
  14. import (
  15. "fmt"
  16. "os"
  17. "path/filepath"
  18. "strings"
  19. "k8s.io/api/core/v1"
  20. "k8s.io/apimachinery/pkg/api/resource"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/types"
  23. utilfeature "k8s.io/apiserver/pkg/util/feature"
  24. volumehelpers "k8s.io/cloud-provider/volume/helpers"
  25. "k8s.io/klog"
  26. "k8s.io/kubernetes/pkg/features"
  27. "k8s.io/kubernetes/pkg/util/mount"
  28. "k8s.io/kubernetes/pkg/volume"
  29. "k8s.io/kubernetes/pkg/volume/util"
  30. utilstrings "k8s.io/utils/strings"
  31. )
  32. // This is the primary entrypoint for volume plugins.
  33. func ProbeVolumePlugins() []volume.VolumePlugin {
  34. return []volume.VolumePlugin{&vsphereVolumePlugin{}}
  35. }
  36. type vsphereVolumePlugin struct {
  37. host volume.VolumeHost
  38. }
  39. var _ volume.VolumePlugin = &vsphereVolumePlugin{}
  40. var _ volume.PersistentVolumePlugin = &vsphereVolumePlugin{}
  41. var _ volume.DeletableVolumePlugin = &vsphereVolumePlugin{}
  42. var _ volume.ProvisionableVolumePlugin = &vsphereVolumePlugin{}
  43. const (
  44. vsphereVolumePluginName = "kubernetes.io/vsphere-volume"
  45. )
  46. func getPath(uid types.UID, volName string, host volume.VolumeHost) string {
  47. return host.GetPodVolumeDir(uid, utilstrings.EscapeQualifiedName(vsphereVolumePluginName), volName)
  48. }
  49. // vSphere Volume Plugin
  50. func (plugin *vsphereVolumePlugin) Init(host volume.VolumeHost) error {
  51. plugin.host = host
  52. return nil
  53. }
  54. func (plugin *vsphereVolumePlugin) GetPluginName() string {
  55. return vsphereVolumePluginName
  56. }
  57. func (plugin *vsphereVolumePlugin) GetVolumeName(spec *volume.Spec) (string, error) {
  58. volumeSource, _, err := getVolumeSource(spec)
  59. if err != nil {
  60. return "", err
  61. }
  62. return volumeSource.VolumePath, nil
  63. }
  64. func (plugin *vsphereVolumePlugin) CanSupport(spec *volume.Spec) bool {
  65. return (spec.PersistentVolume != nil && spec.PersistentVolume.Spec.VsphereVolume != nil) ||
  66. (spec.Volume != nil && spec.Volume.VsphereVolume != nil)
  67. }
  68. func (plugin *vsphereVolumePlugin) IsMigratedToCSI() bool {
  69. return false
  70. }
  71. func (plugin *vsphereVolumePlugin) RequiresRemount() bool {
  72. return false
  73. }
  74. func (plugin *vsphereVolumePlugin) SupportsMountOption() bool {
  75. return true
  76. }
  77. func (plugin *vsphereVolumePlugin) SupportsBulkVolumeVerification() bool {
  78. return true
  79. }
  80. func (plugin *vsphereVolumePlugin) NewMounter(spec *volume.Spec, pod *v1.Pod, _ volume.VolumeOptions) (volume.Mounter, error) {
  81. return plugin.newMounterInternal(spec, pod.UID, &VsphereDiskUtil{}, plugin.host.GetMounter(plugin.GetPluginName()))
  82. }
  83. func (plugin *vsphereVolumePlugin) NewUnmounter(volName string, podUID types.UID) (volume.Unmounter, error) {
  84. return plugin.newUnmounterInternal(volName, podUID, &VsphereDiskUtil{}, plugin.host.GetMounter(plugin.GetPluginName()))
  85. }
  86. func (plugin *vsphereVolumePlugin) newMounterInternal(spec *volume.Spec, podUID types.UID, manager vdManager, mounter mount.Interface) (volume.Mounter, error) {
  87. vvol, _, err := getVolumeSource(spec)
  88. if err != nil {
  89. return nil, err
  90. }
  91. volPath := vvol.VolumePath
  92. fsType := vvol.FSType
  93. return &vsphereVolumeMounter{
  94. vsphereVolume: &vsphereVolume{
  95. podUID: podUID,
  96. volName: spec.Name(),
  97. volPath: volPath,
  98. manager: manager,
  99. mounter: mounter,
  100. plugin: plugin,
  101. MetricsProvider: volume.NewMetricsStatFS(getPath(podUID, spec.Name(), plugin.host)),
  102. },
  103. fsType: fsType,
  104. diskMounter: util.NewSafeFormatAndMountFromHost(plugin.GetPluginName(), plugin.host),
  105. mountOptions: util.MountOptionFromSpec(spec),
  106. }, nil
  107. }
  108. func (plugin *vsphereVolumePlugin) newUnmounterInternal(volName string, podUID types.UID, manager vdManager, mounter mount.Interface) (volume.Unmounter, error) {
  109. return &vsphereVolumeUnmounter{
  110. &vsphereVolume{
  111. podUID: podUID,
  112. volName: volName,
  113. manager: manager,
  114. mounter: mounter,
  115. plugin: plugin,
  116. MetricsProvider: volume.NewMetricsStatFS(getPath(podUID, volName, plugin.host)),
  117. }}, nil
  118. }
  119. func (plugin *vsphereVolumePlugin) ConstructVolumeSpec(volumeName, mountPath string) (*volume.Spec, error) {
  120. mounter := plugin.host.GetMounter(plugin.GetPluginName())
  121. pluginMntDir := util.GetPluginMountDir(plugin.host, plugin.GetPluginName())
  122. volumePath, err := mounter.GetDeviceNameFromMount(mountPath, pluginMntDir)
  123. if err != nil {
  124. return nil, err
  125. }
  126. volumePath = strings.Replace(volumePath, "\\040", " ", -1)
  127. klog.V(5).Infof("vSphere volume path is %q", volumePath)
  128. vsphereVolume := &v1.Volume{
  129. Name: volumeName,
  130. VolumeSource: v1.VolumeSource{
  131. VsphereVolume: &v1.VsphereVirtualDiskVolumeSource{
  132. VolumePath: volumePath,
  133. },
  134. },
  135. }
  136. return volume.NewSpecFromVolume(vsphereVolume), nil
  137. }
  138. // Abstract interface to disk operations.
  139. type vdManager interface {
  140. // Creates a volume
  141. CreateVolume(provisioner *vsphereVolumeProvisioner, selectedZone []string) (volSpec *VolumeSpec, err error)
  142. // Deletes a volume
  143. DeleteVolume(deleter *vsphereVolumeDeleter) error
  144. }
  145. // vspherePersistentDisk volumes are disk resources are attached to the kubelet's host machine and exposed to the pod.
  146. type vsphereVolume struct {
  147. volName string
  148. podUID types.UID
  149. // Unique identifier of the volume, used to find the disk resource in the provider.
  150. volPath string
  151. // Filesystem type, optional.
  152. fsType string
  153. //diskID for detach disk
  154. diskID string
  155. // Utility interface that provides API calls to the provider to attach/detach disks.
  156. manager vdManager
  157. // Mounter interface that provides system calls to mount the global path to the pod local path.
  158. mounter mount.Interface
  159. // diskMounter provides the interface that is used to mount the actual block device.
  160. diskMounter mount.Interface
  161. plugin *vsphereVolumePlugin
  162. volume.MetricsProvider
  163. }
  164. var _ volume.Mounter = &vsphereVolumeMounter{}
  165. type vsphereVolumeMounter struct {
  166. *vsphereVolume
  167. fsType string
  168. diskMounter *mount.SafeFormatAndMount
  169. mountOptions []string
  170. }
  171. func (b *vsphereVolumeMounter) GetAttributes() volume.Attributes {
  172. return volume.Attributes{
  173. SupportsSELinux: true,
  174. Managed: true,
  175. }
  176. }
  177. // SetUp attaches the disk and bind mounts to the volume path.
  178. func (b *vsphereVolumeMounter) SetUp(mounterArgs volume.MounterArgs) error {
  179. return b.SetUpAt(b.GetPath(), mounterArgs)
  180. }
  181. // Checks prior to mount operations to verify that the required components (binaries, etc.)
  182. // to mount the volume are available on the underlying node.
  183. // If not, it returns an error
  184. func (b *vsphereVolumeMounter) CanMount() error {
  185. return nil
  186. }
  187. // SetUp attaches the disk and bind mounts to the volume path.
  188. func (b *vsphereVolumeMounter) SetUpAt(dir string, mounterArgs volume.MounterArgs) error {
  189. klog.V(5).Infof("vSphere volume setup %s to %s", b.volPath, dir)
  190. // TODO: handle failed mounts here.
  191. notmnt, err := b.mounter.IsLikelyNotMountPoint(dir)
  192. if err != nil && !os.IsNotExist(err) {
  193. klog.V(4).Infof("IsLikelyNotMountPoint failed: %v", err)
  194. return err
  195. }
  196. if !notmnt {
  197. klog.V(4).Infof("Something is already mounted to target %s", dir)
  198. return nil
  199. }
  200. if err := os.MkdirAll(dir, 0750); err != nil {
  201. klog.V(4).Infof("Could not create directory %s: %v", dir, err)
  202. return err
  203. }
  204. options := []string{"bind"}
  205. // Perform a bind mount to the full path to allow duplicate mounts of the same PD.
  206. globalPDPath := makeGlobalPDPath(b.plugin.host, b.volPath)
  207. mountOptions := util.JoinMountOptions(options, b.mountOptions)
  208. err = b.mounter.Mount(globalPDPath, dir, "", mountOptions)
  209. if err != nil {
  210. notmnt, mntErr := b.mounter.IsLikelyNotMountPoint(dir)
  211. if mntErr != nil {
  212. klog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
  213. return err
  214. }
  215. if !notmnt {
  216. if mntErr = b.mounter.Unmount(dir); mntErr != nil {
  217. klog.Errorf("Failed to unmount: %v", mntErr)
  218. return err
  219. }
  220. notmnt, mntErr := b.mounter.IsLikelyNotMountPoint(dir)
  221. if mntErr != nil {
  222. klog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
  223. return err
  224. }
  225. if !notmnt {
  226. klog.Errorf("%s is still mounted, despite call to unmount(). Will try again next sync loop.", b.GetPath())
  227. return err
  228. }
  229. }
  230. os.Remove(dir)
  231. return err
  232. }
  233. volume.SetVolumeOwnership(b, mounterArgs.FsGroup)
  234. klog.V(3).Infof("vSphere volume %s mounted to %s", b.volPath, dir)
  235. return nil
  236. }
  237. var _ volume.Unmounter = &vsphereVolumeUnmounter{}
  238. type vsphereVolumeUnmounter struct {
  239. *vsphereVolume
  240. }
  241. // Unmounts the bind mount, and detaches the disk only if the PD
  242. // resource was the last reference to that disk on the kubelet.
  243. func (v *vsphereVolumeUnmounter) TearDown() error {
  244. return v.TearDownAt(v.GetPath())
  245. }
  246. // Unmounts the bind mount, and detaches the disk only if the PD
  247. // resource was the last reference to that disk on the kubelet.
  248. func (v *vsphereVolumeUnmounter) TearDownAt(dir string) error {
  249. return mount.CleanupMountPoint(dir, v.mounter, false)
  250. }
  251. func makeGlobalPDPath(host volume.VolumeHost, devName string) string {
  252. return filepath.Join(host.GetPluginDir(vsphereVolumePluginName), util.MountsInGlobalPDPath, devName)
  253. }
  254. func (vv *vsphereVolume) GetPath() string {
  255. name := vsphereVolumePluginName
  256. return vv.plugin.host.GetPodVolumeDir(vv.podUID, utilstrings.EscapeQualifiedName(name), vv.volName)
  257. }
  258. // vSphere Persistent Volume Plugin
  259. func (plugin *vsphereVolumePlugin) GetAccessModes() []v1.PersistentVolumeAccessMode {
  260. return []v1.PersistentVolumeAccessMode{
  261. v1.ReadWriteOnce,
  262. }
  263. }
  264. // vSphere Deletable Volume Plugin
  265. type vsphereVolumeDeleter struct {
  266. *vsphereVolume
  267. }
  268. var _ volume.Deleter = &vsphereVolumeDeleter{}
  269. func (plugin *vsphereVolumePlugin) NewDeleter(spec *volume.Spec) (volume.Deleter, error) {
  270. return plugin.newDeleterInternal(spec, &VsphereDiskUtil{})
  271. }
  272. func (plugin *vsphereVolumePlugin) newDeleterInternal(spec *volume.Spec, manager vdManager) (volume.Deleter, error) {
  273. if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.VsphereVolume == nil {
  274. return nil, fmt.Errorf("spec.PersistentVolumeSource.VsphereVolume is nil")
  275. }
  276. return &vsphereVolumeDeleter{
  277. &vsphereVolume{
  278. volName: spec.Name(),
  279. volPath: spec.PersistentVolume.Spec.VsphereVolume.VolumePath,
  280. manager: manager,
  281. plugin: plugin,
  282. }}, nil
  283. }
  284. func (r *vsphereVolumeDeleter) Delete() error {
  285. return r.manager.DeleteVolume(r)
  286. }
  287. // vSphere Provisionable Volume Plugin
  288. type vsphereVolumeProvisioner struct {
  289. *vsphereVolume
  290. options volume.VolumeOptions
  291. }
  292. var _ volume.Provisioner = &vsphereVolumeProvisioner{}
  293. func (plugin *vsphereVolumePlugin) NewProvisioner(options volume.VolumeOptions) (volume.Provisioner, error) {
  294. return plugin.newProvisionerInternal(options, &VsphereDiskUtil{})
  295. }
  296. func (plugin *vsphereVolumePlugin) newProvisionerInternal(options volume.VolumeOptions, manager vdManager) (volume.Provisioner, error) {
  297. return &vsphereVolumeProvisioner{
  298. vsphereVolume: &vsphereVolume{
  299. manager: manager,
  300. plugin: plugin,
  301. },
  302. options: options,
  303. }, nil
  304. }
  305. func (v *vsphereVolumeProvisioner) Provision(selectedNode *v1.Node, allowedTopologies []v1.TopologySelectorTerm) (*v1.PersistentVolume, error) {
  306. if !util.AccessModesContainedInAll(v.plugin.GetAccessModes(), v.options.PVC.Spec.AccessModes) {
  307. return nil, fmt.Errorf("invalid AccessModes %v: only AccessModes %v are supported", v.options.PVC.Spec.AccessModes, v.plugin.GetAccessModes())
  308. }
  309. klog.V(1).Infof("Provision with allowedTopologies : %s", allowedTopologies)
  310. selectedZones, err := volumehelpers.ZonesFromAllowedTopologies(allowedTopologies)
  311. if err != nil {
  312. return nil, err
  313. }
  314. klog.V(4).Infof("Selected zones for volume : %s", selectedZones)
  315. volSpec, err := v.manager.CreateVolume(v, selectedZones.List())
  316. if err != nil {
  317. return nil, err
  318. }
  319. if volSpec.Fstype == "" {
  320. volSpec.Fstype = "ext4"
  321. }
  322. var volumeMode *v1.PersistentVolumeMode
  323. if utilfeature.DefaultFeatureGate.Enabled(features.BlockVolume) {
  324. volumeMode = v.options.PVC.Spec.VolumeMode
  325. if volumeMode != nil && *volumeMode == v1.PersistentVolumeBlock {
  326. klog.V(5).Infof("vSphere block volume should not have any FSType")
  327. volSpec.Fstype = ""
  328. }
  329. }
  330. pv := &v1.PersistentVolume{
  331. ObjectMeta: metav1.ObjectMeta{
  332. Name: v.options.PVName,
  333. Labels: map[string]string{},
  334. Annotations: map[string]string{
  335. util.VolumeDynamicallyCreatedByKey: "vsphere-volume-dynamic-provisioner",
  336. },
  337. },
  338. Spec: v1.PersistentVolumeSpec{
  339. PersistentVolumeReclaimPolicy: v.options.PersistentVolumeReclaimPolicy,
  340. AccessModes: v.options.PVC.Spec.AccessModes,
  341. Capacity: v1.ResourceList{
  342. v1.ResourceName(v1.ResourceStorage): resource.MustParse(fmt.Sprintf("%dKi", volSpec.Size)),
  343. },
  344. VolumeMode: volumeMode,
  345. PersistentVolumeSource: v1.PersistentVolumeSource{
  346. VsphereVolume: &v1.VsphereVirtualDiskVolumeSource{
  347. VolumePath: volSpec.Path,
  348. FSType: volSpec.Fstype,
  349. StoragePolicyName: volSpec.StoragePolicyName,
  350. StoragePolicyID: volSpec.StoragePolicyID,
  351. },
  352. },
  353. MountOptions: v.options.MountOptions,
  354. },
  355. }
  356. if len(v.options.PVC.Spec.AccessModes) == 0 {
  357. pv.Spec.AccessModes = v.plugin.GetAccessModes()
  358. }
  359. labels := volSpec.Labels
  360. requirements := make([]v1.NodeSelectorRequirement, 0)
  361. if len(labels) != 0 {
  362. if pv.Labels == nil {
  363. pv.Labels = make(map[string]string)
  364. }
  365. for k, v := range labels {
  366. pv.Labels[k] = v
  367. var values []string
  368. if k == v1.LabelZoneFailureDomain {
  369. values, err = volumehelpers.LabelZonesToList(v)
  370. if err != nil {
  371. return nil, fmt.Errorf("failed to convert label string for Zone: %s to a List: %v", v, err)
  372. }
  373. } else {
  374. values = []string{v}
  375. }
  376. requirements = append(requirements, v1.NodeSelectorRequirement{Key: k, Operator: v1.NodeSelectorOpIn, Values: values})
  377. }
  378. }
  379. if len(requirements) > 0 {
  380. pv.Spec.NodeAffinity = new(v1.VolumeNodeAffinity)
  381. pv.Spec.NodeAffinity.Required = new(v1.NodeSelector)
  382. pv.Spec.NodeAffinity.Required.NodeSelectorTerms = make([]v1.NodeSelectorTerm, 1)
  383. pv.Spec.NodeAffinity.Required.NodeSelectorTerms[0].MatchExpressions = requirements
  384. }
  385. return pv, nil
  386. }
  387. func getVolumeSource(
  388. spec *volume.Spec) (*v1.VsphereVirtualDiskVolumeSource, bool, error) {
  389. if spec.Volume != nil && spec.Volume.VsphereVolume != nil {
  390. return spec.Volume.VsphereVolume, spec.ReadOnly, nil
  391. } else if spec.PersistentVolume != nil &&
  392. spec.PersistentVolume.Spec.VsphereVolume != nil {
  393. return spec.PersistentVolume.Spec.VsphereVolume, spec.ReadOnly, nil
  394. }
  395. return nil, false, fmt.Errorf("Spec does not reference a VSphere volume type")
  396. }