vsphere_volume.go 15 KB

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