portworx.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 portworx
  14. import (
  15. "fmt"
  16. "os"
  17. volumeclient "github.com/libopenstorage/openstorage/api/client/volume"
  18. "k8s.io/klog"
  19. "k8s.io/utils/mount"
  20. utilstrings "k8s.io/utils/strings"
  21. v1 "k8s.io/api/core/v1"
  22. "k8s.io/apimachinery/pkg/api/resource"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. "k8s.io/apimachinery/pkg/types"
  25. "k8s.io/kubernetes/pkg/volume"
  26. "k8s.io/kubernetes/pkg/volume/util"
  27. )
  28. const (
  29. attachContextKey = "context"
  30. attachHostKey = "host"
  31. )
  32. // ProbeVolumePlugins is the primary entrypoint for volume plugins.
  33. func ProbeVolumePlugins() []volume.VolumePlugin {
  34. return []volume.VolumePlugin{&portworxVolumePlugin{nil, nil}}
  35. }
  36. type portworxVolumePlugin struct {
  37. host volume.VolumeHost
  38. util *portworxVolumeUtil
  39. }
  40. var _ volume.VolumePlugin = &portworxVolumePlugin{}
  41. var _ volume.PersistentVolumePlugin = &portworxVolumePlugin{}
  42. var _ volume.DeletableVolumePlugin = &portworxVolumePlugin{}
  43. var _ volume.ProvisionableVolumePlugin = &portworxVolumePlugin{}
  44. var _ volume.ExpandableVolumePlugin = &portworxVolumePlugin{}
  45. const (
  46. portworxVolumePluginName = "kubernetes.io/portworx-volume"
  47. )
  48. func getPath(uid types.UID, volName string, host volume.VolumeHost) string {
  49. return host.GetPodVolumeDir(uid, utilstrings.EscapeQualifiedName(portworxVolumePluginName), volName)
  50. }
  51. func (plugin *portworxVolumePlugin) Init(host volume.VolumeHost) error {
  52. client, err := volumeclient.NewDriverClient(
  53. fmt.Sprintf("http://%s:%d", host.GetHostName(), osdMgmtDefaultPort),
  54. pxdDriverName, osdDriverVersion, pxDriverName)
  55. if err != nil {
  56. return err
  57. }
  58. plugin.host = host
  59. plugin.util = &portworxVolumeUtil{
  60. portworxClient: client,
  61. }
  62. return nil
  63. }
  64. func (plugin *portworxVolumePlugin) GetPluginName() string {
  65. return portworxVolumePluginName
  66. }
  67. func (plugin *portworxVolumePlugin) GetVolumeName(spec *volume.Spec) (string, error) {
  68. volumeSource, _, err := getVolumeSource(spec)
  69. if err != nil {
  70. return "", err
  71. }
  72. return volumeSource.VolumeID, nil
  73. }
  74. func (plugin *portworxVolumePlugin) CanSupport(spec *volume.Spec) bool {
  75. return (spec.PersistentVolume != nil && spec.PersistentVolume.Spec.PortworxVolume != nil) ||
  76. (spec.Volume != nil && spec.Volume.PortworxVolume != nil)
  77. }
  78. func (plugin *portworxVolumePlugin) RequiresRemount() bool {
  79. return false
  80. }
  81. func (plugin *portworxVolumePlugin) GetAccessModes() []v1.PersistentVolumeAccessMode {
  82. return []v1.PersistentVolumeAccessMode{
  83. v1.ReadWriteOnce,
  84. v1.ReadWriteMany,
  85. }
  86. }
  87. func (plugin *portworxVolumePlugin) NewMounter(spec *volume.Spec, pod *v1.Pod, _ volume.VolumeOptions) (volume.Mounter, error) {
  88. return plugin.newMounterInternal(spec, pod.UID, plugin.util, plugin.host.GetMounter(plugin.GetPluginName()))
  89. }
  90. func (plugin *portworxVolumePlugin) newMounterInternal(spec *volume.Spec, podUID types.UID, manager portworxManager, mounter mount.Interface) (volume.Mounter, error) {
  91. pwx, readOnly, err := getVolumeSource(spec)
  92. if err != nil {
  93. return nil, err
  94. }
  95. volumeID := pwx.VolumeID
  96. fsType := pwx.FSType
  97. return &portworxVolumeMounter{
  98. portworxVolume: &portworxVolume{
  99. podUID: podUID,
  100. volName: spec.Name(),
  101. volumeID: volumeID,
  102. manager: manager,
  103. mounter: mounter,
  104. plugin: plugin,
  105. MetricsProvider: volume.NewMetricsStatFS(getPath(podUID, spec.Name(), plugin.host)),
  106. },
  107. fsType: fsType,
  108. readOnly: readOnly,
  109. diskMounter: util.NewSafeFormatAndMountFromHost(plugin.GetPluginName(), plugin.host)}, nil
  110. }
  111. func (plugin *portworxVolumePlugin) NewUnmounter(volName string, podUID types.UID) (volume.Unmounter, error) {
  112. return plugin.newUnmounterInternal(volName, podUID, plugin.util, plugin.host.GetMounter(plugin.GetPluginName()))
  113. }
  114. func (plugin *portworxVolumePlugin) newUnmounterInternal(volName string, podUID types.UID, manager portworxManager,
  115. mounter mount.Interface) (volume.Unmounter, error) {
  116. return &portworxVolumeUnmounter{
  117. &portworxVolume{
  118. podUID: podUID,
  119. volName: volName,
  120. manager: manager,
  121. mounter: mounter,
  122. plugin: plugin,
  123. MetricsProvider: volume.NewMetricsStatFS(getPath(podUID, volName, plugin.host)),
  124. }}, nil
  125. }
  126. func (plugin *portworxVolumePlugin) NewDeleter(spec *volume.Spec) (volume.Deleter, error) {
  127. return plugin.newDeleterInternal(spec, plugin.util)
  128. }
  129. func (plugin *portworxVolumePlugin) newDeleterInternal(spec *volume.Spec, manager portworxManager) (volume.Deleter, error) {
  130. if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.PortworxVolume == nil {
  131. return nil, fmt.Errorf("spec.PersistentVolumeSource.PortworxVolume is nil")
  132. }
  133. return &portworxVolumeDeleter{
  134. portworxVolume: &portworxVolume{
  135. volName: spec.Name(),
  136. volumeID: spec.PersistentVolume.Spec.PortworxVolume.VolumeID,
  137. manager: manager,
  138. plugin: plugin,
  139. }}, nil
  140. }
  141. func (plugin *portworxVolumePlugin) NewProvisioner(options volume.VolumeOptions) (volume.Provisioner, error) {
  142. return plugin.newProvisionerInternal(options, plugin.util)
  143. }
  144. func (plugin *portworxVolumePlugin) newProvisionerInternal(options volume.VolumeOptions, manager portworxManager) (volume.Provisioner, error) {
  145. return &portworxVolumeProvisioner{
  146. portworxVolume: &portworxVolume{
  147. manager: manager,
  148. plugin: plugin,
  149. },
  150. options: options,
  151. }, nil
  152. }
  153. func (plugin *portworxVolumePlugin) RequiresFSResize() bool {
  154. return false
  155. }
  156. func (plugin *portworxVolumePlugin) ExpandVolumeDevice(
  157. spec *volume.Spec,
  158. newSize resource.Quantity,
  159. oldSize resource.Quantity) (resource.Quantity, error) {
  160. klog.V(4).Infof("Expanding: %s from %v to %v", spec.Name(), oldSize, newSize)
  161. err := plugin.util.ResizeVolume(spec, newSize, plugin.host)
  162. if err != nil {
  163. return oldSize, err
  164. }
  165. klog.V(4).Infof("Successfully resized %s to %v", spec.Name(), newSize)
  166. return newSize, nil
  167. }
  168. func (plugin *portworxVolumePlugin) ConstructVolumeSpec(volumeName, mountPath string) (*volume.Spec, error) {
  169. portworxVolume := &v1.Volume{
  170. Name: volumeName,
  171. VolumeSource: v1.VolumeSource{
  172. PortworxVolume: &v1.PortworxVolumeSource{
  173. VolumeID: volumeName,
  174. },
  175. },
  176. }
  177. return volume.NewSpecFromVolume(portworxVolume), nil
  178. }
  179. func (plugin *portworxVolumePlugin) SupportsMountOption() bool {
  180. return false
  181. }
  182. func (plugin *portworxVolumePlugin) SupportsBulkVolumeVerification() bool {
  183. return false
  184. }
  185. func getVolumeSource(
  186. spec *volume.Spec) (*v1.PortworxVolumeSource, bool, error) {
  187. if spec.Volume != nil && spec.Volume.PortworxVolume != nil {
  188. return spec.Volume.PortworxVolume, spec.Volume.PortworxVolume.ReadOnly, nil
  189. } else if spec.PersistentVolume != nil &&
  190. spec.PersistentVolume.Spec.PortworxVolume != nil {
  191. return spec.PersistentVolume.Spec.PortworxVolume, spec.ReadOnly, nil
  192. }
  193. return nil, false, fmt.Errorf("Spec does not reference a Portworx Volume type")
  194. }
  195. // Abstract interface to PD operations.
  196. type portworxManager interface {
  197. // Creates a volume
  198. CreateVolume(provisioner *portworxVolumeProvisioner) (volumeID string, volumeSizeGB int64, labels map[string]string, err error)
  199. // Deletes a volume
  200. DeleteVolume(deleter *portworxVolumeDeleter) error
  201. // Attach a volume
  202. AttachVolume(mounter *portworxVolumeMounter, attachOptions map[string]string) (string, error)
  203. // Detach a volume
  204. DetachVolume(unmounter *portworxVolumeUnmounter) error
  205. // Mount a volume
  206. MountVolume(mounter *portworxVolumeMounter, mountDir string) error
  207. // Unmount a volume
  208. UnmountVolume(unmounter *portworxVolumeUnmounter, mountDir string) error
  209. // Resize a volume
  210. ResizeVolume(spec *volume.Spec, newSize resource.Quantity, host volume.VolumeHost) error
  211. }
  212. // portworxVolume volumes are portworx block devices
  213. // that are attached to the kubelet's host machine and exposed to the pod.
  214. type portworxVolume struct {
  215. volName string
  216. podUID types.UID
  217. // Unique id of the PD, used to find the disk resource in the provider.
  218. volumeID string
  219. // Utility interface that provides API calls to the provider to attach/detach disks.
  220. manager portworxManager
  221. // Mounter interface that provides system calls to mount the global path to the pod local path.
  222. mounter mount.Interface
  223. plugin *portworxVolumePlugin
  224. volume.MetricsProvider
  225. }
  226. type portworxVolumeMounter struct {
  227. *portworxVolume
  228. // Filesystem type, optional.
  229. fsType string
  230. // Specifies whether the disk will be attached as read-only.
  231. readOnly bool
  232. // diskMounter provides the interface that is used to mount the actual block device.
  233. diskMounter *mount.SafeFormatAndMount
  234. }
  235. var _ volume.Mounter = &portworxVolumeMounter{}
  236. func (b *portworxVolumeMounter) GetAttributes() volume.Attributes {
  237. return volume.Attributes{
  238. ReadOnly: b.readOnly,
  239. Managed: !b.readOnly,
  240. SupportsSELinux: false,
  241. }
  242. }
  243. // Checks prior to mount operations to verify that the required components (binaries, etc.)
  244. // to mount the volume are available on the underlying node.
  245. // If not, it returns an error
  246. func (b *portworxVolumeMounter) CanMount() error {
  247. return nil
  248. }
  249. // SetUp attaches the disk and bind mounts to the volume path.
  250. func (b *portworxVolumeMounter) SetUp(mounterArgs volume.MounterArgs) error {
  251. return b.SetUpAt(b.GetPath(), mounterArgs)
  252. }
  253. // SetUpAt attaches the disk and bind mounts to the volume path.
  254. func (b *portworxVolumeMounter) SetUpAt(dir string, mounterArgs volume.MounterArgs) error {
  255. notMnt, err := b.mounter.IsLikelyNotMountPoint(dir)
  256. klog.Infof("Portworx Volume set up. Dir: %s %v %v", dir, !notMnt, err)
  257. if err != nil && !os.IsNotExist(err) {
  258. klog.Errorf("Cannot validate mountpoint: %s", dir)
  259. return err
  260. }
  261. if !notMnt {
  262. return nil
  263. }
  264. attachOptions := make(map[string]string)
  265. attachOptions[attachContextKey] = dir
  266. attachOptions[attachHostKey] = b.plugin.host.GetHostName()
  267. if _, err := b.manager.AttachVolume(b, attachOptions); err != nil {
  268. return err
  269. }
  270. klog.V(4).Infof("Portworx Volume %s attached", b.volumeID)
  271. if err := os.MkdirAll(dir, 0750); err != nil {
  272. return err
  273. }
  274. if err := b.manager.MountVolume(b, dir); err != nil {
  275. return err
  276. }
  277. if !b.readOnly {
  278. volume.SetVolumeOwnership(b, mounterArgs.FsGroup)
  279. }
  280. klog.Infof("Portworx Volume %s setup at %s", b.volumeID, dir)
  281. return nil
  282. }
  283. func (pwx *portworxVolume) GetPath() string {
  284. return getPath(pwx.podUID, pwx.volName, pwx.plugin.host)
  285. }
  286. type portworxVolumeUnmounter struct {
  287. *portworxVolume
  288. }
  289. var _ volume.Unmounter = &portworxVolumeUnmounter{}
  290. // Unmounts the bind mount, and detaches the disk only if the PD
  291. // resource was the last reference to that disk on the kubelet.
  292. func (c *portworxVolumeUnmounter) TearDown() error {
  293. return c.TearDownAt(c.GetPath())
  294. }
  295. // Unmounts the bind mount, and detaches the disk only if the PD
  296. // resource was the last reference to that disk on the kubelet.
  297. func (c *portworxVolumeUnmounter) TearDownAt(dir string) error {
  298. klog.Infof("Portworx Volume TearDown of %s", dir)
  299. if err := c.manager.UnmountVolume(c, dir); err != nil {
  300. return err
  301. }
  302. // Call Portworx Detach Volume.
  303. if err := c.manager.DetachVolume(c); err != nil {
  304. return err
  305. }
  306. return nil
  307. }
  308. type portworxVolumeDeleter struct {
  309. *portworxVolume
  310. }
  311. var _ volume.Deleter = &portworxVolumeDeleter{}
  312. func (d *portworxVolumeDeleter) GetPath() string {
  313. return getPath(d.podUID, d.volName, d.plugin.host)
  314. }
  315. func (d *portworxVolumeDeleter) Delete() error {
  316. return d.manager.DeleteVolume(d)
  317. }
  318. type portworxVolumeProvisioner struct {
  319. *portworxVolume
  320. options volume.VolumeOptions
  321. }
  322. var _ volume.Provisioner = &portworxVolumeProvisioner{}
  323. func (c *portworxVolumeProvisioner) Provision(selectedNode *v1.Node, allowedTopologies []v1.TopologySelectorTerm) (*v1.PersistentVolume, error) {
  324. if !util.AccessModesContainedInAll(c.plugin.GetAccessModes(), c.options.PVC.Spec.AccessModes) {
  325. return nil, fmt.Errorf("invalid AccessModes %v: only AccessModes %v are supported", c.options.PVC.Spec.AccessModes, c.plugin.GetAccessModes())
  326. }
  327. if util.CheckPersistentVolumeClaimModeBlock(c.options.PVC) {
  328. return nil, fmt.Errorf("%s does not support block volume provisioning", c.plugin.GetPluginName())
  329. }
  330. volumeID, sizeGiB, labels, err := c.manager.CreateVolume(c)
  331. if err != nil {
  332. return nil, err
  333. }
  334. pv := &v1.PersistentVolume{
  335. ObjectMeta: metav1.ObjectMeta{
  336. Name: c.options.PVName,
  337. Labels: map[string]string{},
  338. Annotations: map[string]string{
  339. util.VolumeDynamicallyCreatedByKey: "portworx-volume-dynamic-provisioner",
  340. },
  341. },
  342. Spec: v1.PersistentVolumeSpec{
  343. PersistentVolumeReclaimPolicy: c.options.PersistentVolumeReclaimPolicy,
  344. AccessModes: c.options.PVC.Spec.AccessModes,
  345. Capacity: v1.ResourceList{
  346. v1.ResourceName(v1.ResourceStorage): resource.MustParse(fmt.Sprintf("%dGi", sizeGiB)),
  347. },
  348. PersistentVolumeSource: v1.PersistentVolumeSource{
  349. PortworxVolume: &v1.PortworxVolumeSource{
  350. VolumeID: volumeID,
  351. },
  352. },
  353. },
  354. }
  355. if len(labels) != 0 {
  356. if pv.Labels == nil {
  357. pv.Labels = make(map[string]string)
  358. }
  359. for k, v := range labels {
  360. pv.Labels[k] = v
  361. }
  362. }
  363. if len(c.options.PVC.Spec.AccessModes) == 0 {
  364. pv.Spec.AccessModes = c.plugin.GetAccessModes()
  365. }
  366. return pv, nil
  367. }