portworx.go 13 KB

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