storageos.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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 storageos
  14. import (
  15. "fmt"
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "strings"
  20. "k8s.io/api/core/v1"
  21. "k8s.io/apimachinery/pkg/api/resource"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/types"
  24. clientset "k8s.io/client-go/kubernetes"
  25. volumehelpers "k8s.io/cloud-provider/volume/helpers"
  26. "k8s.io/klog"
  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. // ProbeVolumePlugins is the primary entrypoint for volume plugins.
  33. func ProbeVolumePlugins() []volume.VolumePlugin {
  34. return []volume.VolumePlugin{&storageosPlugin{nil}}
  35. }
  36. type storageosPlugin struct {
  37. host volume.VolumeHost
  38. }
  39. var _ volume.VolumePlugin = &storageosPlugin{}
  40. var _ volume.PersistentVolumePlugin = &storageosPlugin{}
  41. var _ volume.DeletableVolumePlugin = &storageosPlugin{}
  42. var _ volume.ProvisionableVolumePlugin = &storageosPlugin{}
  43. const (
  44. storageosPluginName = "kubernetes.io/storageos"
  45. defaultDeviceDir = "/var/lib/storageos/volumes"
  46. defaultAPIAddress = "tcp://localhost:5705"
  47. defaultAPIUser = "storageos"
  48. defaultAPIPassword = "storageos"
  49. defaultAPIVersion = "1"
  50. defaultFSType = "ext4"
  51. defaultNamespace = "default"
  52. )
  53. func getPath(uid types.UID, volNamespace string, volName string, pvName string, host volume.VolumeHost) string {
  54. if len(volNamespace) != 0 && len(volName) != 0 && strings.Count(volName, ".") == 0 {
  55. return host.GetPodVolumeDir(uid, utilstrings.EscapeQualifiedName(storageosPluginName), pvName+"."+volNamespace+"."+volName)
  56. }
  57. return host.GetPodVolumeDir(uid, utilstrings.EscapeQualifiedName(storageosPluginName), pvName)
  58. }
  59. func (plugin *storageosPlugin) Init(host volume.VolumeHost) error {
  60. plugin.host = host
  61. return nil
  62. }
  63. func (plugin *storageosPlugin) GetPluginName() string {
  64. return storageosPluginName
  65. }
  66. func (plugin *storageosPlugin) GetVolumeName(spec *volume.Spec) (string, error) {
  67. volumeSource, _, err := getVolumeSource(spec)
  68. if err != nil {
  69. return "", err
  70. }
  71. return fmt.Sprintf("%s/%s", volumeSource.VolumeNamespace, volumeSource.VolumeName), nil
  72. }
  73. func (plugin *storageosPlugin) CanSupport(spec *volume.Spec) bool {
  74. return (spec.PersistentVolume != nil && spec.PersistentVolume.Spec.StorageOS != nil) ||
  75. (spec.Volume != nil && spec.Volume.StorageOS != nil)
  76. }
  77. func (plugin *storageosPlugin) IsMigratedToCSI() bool {
  78. return false
  79. }
  80. func (plugin *storageosPlugin) RequiresRemount() bool {
  81. return false
  82. }
  83. func (plugin *storageosPlugin) GetAccessModes() []v1.PersistentVolumeAccessMode {
  84. return []v1.PersistentVolumeAccessMode{
  85. v1.ReadWriteOnce,
  86. v1.ReadOnlyMany,
  87. }
  88. }
  89. func (plugin *storageosPlugin) NewMounter(spec *volume.Spec, pod *v1.Pod, _ volume.VolumeOptions) (volume.Mounter, error) {
  90. apiCfg, err := getAPICfg(spec, pod, plugin.host.GetKubeClient())
  91. if err != nil {
  92. return nil, err
  93. }
  94. return plugin.newMounterInternal(spec, pod, apiCfg, &storageosUtil{}, plugin.host.GetMounter(plugin.GetPluginName()), plugin.host.GetExec(plugin.GetPluginName()))
  95. }
  96. func (plugin *storageosPlugin) newMounterInternal(spec *volume.Spec, pod *v1.Pod, apiCfg *storageosAPIConfig, manager storageosManager, mounter mount.Interface, exec mount.Exec) (volume.Mounter, error) {
  97. volName, volNamespace, fsType, readOnly, err := getVolumeInfoFromSpec(spec)
  98. if err != nil {
  99. return nil, err
  100. }
  101. return &storageosMounter{
  102. storageos: &storageos{
  103. podUID: pod.UID,
  104. podNamespace: pod.GetNamespace(),
  105. pvName: spec.Name(),
  106. volName: volName,
  107. volNamespace: volNamespace,
  108. fsType: fsType,
  109. readOnly: readOnly,
  110. apiCfg: apiCfg,
  111. manager: manager,
  112. mounter: mounter,
  113. exec: exec,
  114. plugin: plugin,
  115. MetricsProvider: volume.NewMetricsStatFS(getPath(pod.UID, volNamespace, volName, spec.Name(), plugin.host)),
  116. },
  117. diskMounter: &mount.SafeFormatAndMount{Interface: mounter, Exec: exec},
  118. mountOptions: util.MountOptionFromSpec(spec),
  119. }, nil
  120. }
  121. func (plugin *storageosPlugin) NewUnmounter(pvName string, podUID types.UID) (volume.Unmounter, error) {
  122. return plugin.newUnmounterInternal(pvName, podUID, &storageosUtil{}, plugin.host.GetMounter(plugin.GetPluginName()), plugin.host.GetExec(plugin.GetPluginName()))
  123. }
  124. func (plugin *storageosPlugin) newUnmounterInternal(pvName string, podUID types.UID, manager storageosManager, mounter mount.Interface, exec mount.Exec) (volume.Unmounter, error) {
  125. // Parse volume namespace & name from mountpoint if mounted
  126. volNamespace, volName, err := getVolumeInfo(pvName, podUID, plugin.host)
  127. if err != nil {
  128. return nil, err
  129. }
  130. return &storageosUnmounter{
  131. storageos: &storageos{
  132. podUID: podUID,
  133. pvName: pvName,
  134. volName: volName,
  135. volNamespace: volNamespace,
  136. manager: manager,
  137. mounter: mounter,
  138. exec: exec,
  139. plugin: plugin,
  140. MetricsProvider: volume.NewMetricsStatFS(getPath(podUID, volNamespace, volName, pvName, plugin.host)),
  141. },
  142. }, nil
  143. }
  144. func (plugin *storageosPlugin) NewDeleter(spec *volume.Spec) (volume.Deleter, error) {
  145. if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.StorageOS == nil {
  146. return nil, fmt.Errorf("spec.PersistentVolumeSource.StorageOS is nil")
  147. }
  148. class, err := util.GetClassForVolume(plugin.host.GetKubeClient(), spec.PersistentVolume)
  149. if err != nil {
  150. return nil, err
  151. }
  152. var adminSecretName, adminSecretNamespace string
  153. for k, v := range class.Parameters {
  154. switch strings.ToLower(k) {
  155. case "adminsecretname":
  156. adminSecretName = v
  157. case "adminsecretnamespace":
  158. adminSecretNamespace = v
  159. }
  160. }
  161. apiCfg, err := parsePVSecret(adminSecretNamespace, adminSecretName, plugin.host.GetKubeClient())
  162. if err != nil {
  163. return nil, fmt.Errorf("failed to get admin secret from [%q/%q]: %v", adminSecretNamespace, adminSecretName, err)
  164. }
  165. return plugin.newDeleterInternal(spec, apiCfg, &storageosUtil{})
  166. }
  167. func (plugin *storageosPlugin) newDeleterInternal(spec *volume.Spec, apiCfg *storageosAPIConfig, manager storageosManager) (volume.Deleter, error) {
  168. return &storageosDeleter{
  169. storageosMounter: &storageosMounter{
  170. storageos: &storageos{
  171. pvName: spec.Name(),
  172. volName: spec.PersistentVolume.Spec.StorageOS.VolumeName,
  173. volNamespace: spec.PersistentVolume.Spec.StorageOS.VolumeNamespace,
  174. apiCfg: apiCfg,
  175. manager: manager,
  176. plugin: plugin,
  177. },
  178. },
  179. pvUID: spec.PersistentVolume.UID,
  180. }, nil
  181. }
  182. func (plugin *storageosPlugin) NewProvisioner(options volume.VolumeOptions) (volume.Provisioner, error) {
  183. return plugin.newProvisionerInternal(options, &storageosUtil{})
  184. }
  185. func (plugin *storageosPlugin) newProvisionerInternal(options volume.VolumeOptions, manager storageosManager) (volume.Provisioner, error) {
  186. return &storageosProvisioner{
  187. storageosMounter: &storageosMounter{
  188. storageos: &storageos{
  189. manager: manager,
  190. plugin: plugin,
  191. },
  192. },
  193. options: options,
  194. }, nil
  195. }
  196. func (plugin *storageosPlugin) ConstructVolumeSpec(volumeName, mountPath string) (*volume.Spec, error) {
  197. volNamespace, volName, err := getVolumeFromRef(volumeName)
  198. if err != nil {
  199. volNamespace = defaultNamespace
  200. volName = volumeName
  201. }
  202. storageosVolume := &v1.Volume{
  203. Name: volumeName,
  204. VolumeSource: v1.VolumeSource{
  205. StorageOS: &v1.StorageOSVolumeSource{
  206. VolumeName: volName,
  207. VolumeNamespace: volNamespace,
  208. },
  209. },
  210. }
  211. return volume.NewSpecFromVolume(storageosVolume), nil
  212. }
  213. func (plugin *storageosPlugin) SupportsMountOption() bool {
  214. return true
  215. }
  216. func (plugin *storageosPlugin) SupportsBulkVolumeVerification() bool {
  217. return false
  218. }
  219. func getVolumeSource(spec *volume.Spec) (*v1.StorageOSVolumeSource, bool, error) {
  220. if spec.Volume != nil && spec.Volume.StorageOS != nil {
  221. return spec.Volume.StorageOS, spec.Volume.StorageOS.ReadOnly, nil
  222. }
  223. return nil, false, fmt.Errorf("Spec does not reference a StorageOS volume type")
  224. }
  225. func getPersistentVolumeSource(spec *volume.Spec) (*v1.StorageOSPersistentVolumeSource, bool, error) {
  226. if spec.PersistentVolume != nil && spec.PersistentVolume.Spec.StorageOS != nil {
  227. return spec.PersistentVolume.Spec.StorageOS, spec.ReadOnly, nil
  228. }
  229. return nil, false, fmt.Errorf("Spec does not reference a StorageOS persistent volume type")
  230. }
  231. // storageosManager is the abstract interface to StorageOS volume ops.
  232. type storageosManager interface {
  233. // Connects to the StorageOS API using the supplied configuration.
  234. NewAPI(apiCfg *storageosAPIConfig) error
  235. // Creates a StorageOS volume.
  236. CreateVolume(provisioner *storageosProvisioner) (*storageosVolume, error)
  237. // Attaches the disk to the kubelet's host machine.
  238. AttachVolume(mounter *storageosMounter) (string, error)
  239. // Attaches the device to the host at a mount path.
  240. AttachDevice(mounter *storageosMounter, deviceMountPath string) error
  241. // Detaches the disk from the kubelet's host machine.
  242. DetachVolume(unmounter *storageosUnmounter, dir string) error
  243. // Mounts the disk on the Kubelet's host machine.
  244. MountVolume(mounter *storageosMounter, mnt, dir string) error
  245. // Unmounts the disk from the Kubelet's host machine.
  246. UnmountVolume(unounter *storageosUnmounter) error
  247. // Deletes the storageos volume. All data will be lost.
  248. DeleteVolume(deleter *storageosDeleter) error
  249. // Gets the node's device path.
  250. DeviceDir(mounter *storageosMounter) string
  251. }
  252. // storageos volumes represent a bare host directory mount of an StorageOS export.
  253. type storageos struct {
  254. podUID types.UID
  255. podNamespace string
  256. pvName string
  257. volName string
  258. volNamespace string
  259. secretName string
  260. readOnly bool
  261. description string
  262. pool string
  263. fsType string
  264. sizeGB int
  265. labels map[string]string
  266. apiCfg *storageosAPIConfig
  267. manager storageosManager
  268. mounter mount.Interface
  269. exec mount.Exec
  270. plugin *storageosPlugin
  271. volume.MetricsProvider
  272. }
  273. type storageosMounter struct {
  274. *storageos
  275. // The directory containing the StorageOS devices
  276. deviceDir string
  277. // Interface used to mount the file or block device
  278. diskMounter *mount.SafeFormatAndMount
  279. mountOptions []string
  280. }
  281. var _ volume.Mounter = &storageosMounter{}
  282. func (b *storageosMounter) GetAttributes() volume.Attributes {
  283. return volume.Attributes{
  284. ReadOnly: b.readOnly,
  285. Managed: !b.readOnly,
  286. SupportsSELinux: true,
  287. }
  288. }
  289. // Checks prior to mount operations to verify that the required components (binaries, etc.)
  290. // to mount the volume are available on the underlying node.
  291. // If not, it returns an error
  292. func (b *storageosMounter) CanMount() error {
  293. return nil
  294. }
  295. // SetUp attaches the disk and bind mounts to the volume path.
  296. func (b *storageosMounter) SetUp(mounterArgs volume.MounterArgs) error {
  297. // Need a namespace to find the volume, try pod's namespace if not set.
  298. if b.volNamespace == "" {
  299. klog.V(2).Infof("Setting StorageOS volume namespace to pod namespace: %s", b.podNamespace)
  300. b.volNamespace = b.podNamespace
  301. }
  302. targetPath := makeGlobalPDName(b.plugin.host, b.pvName, b.volNamespace, b.volName)
  303. // Attach the device to the host.
  304. if err := b.manager.AttachDevice(b, targetPath); err != nil {
  305. klog.Errorf("Failed to attach device at %s: %s", targetPath, err.Error())
  306. return err
  307. }
  308. // Attach the StorageOS volume as a block device
  309. devicePath, err := b.manager.AttachVolume(b)
  310. if err != nil {
  311. klog.Errorf("Failed to attach StorageOS volume %s: %s", b.volName, err.Error())
  312. return err
  313. }
  314. // Mount the loop device into the plugin's disk global mount dir.
  315. err = b.manager.MountVolume(b, devicePath, targetPath)
  316. if err != nil {
  317. return err
  318. }
  319. klog.V(4).Infof("Successfully mounted StorageOS volume %s into global mount directory", b.volName)
  320. // Bind mount the volume into the pod
  321. return b.SetUpAt(b.GetPath(), mounterArgs)
  322. }
  323. // SetUp bind mounts the disk global mount to the give volume path.
  324. func (b *storageosMounter) SetUpAt(dir string, mounterArgs volume.MounterArgs) error {
  325. notMnt, err := b.mounter.IsLikelyNotMountPoint(dir)
  326. klog.V(4).Infof("StorageOS volume set up: %s %v %v", dir, !notMnt, err)
  327. if err != nil && !os.IsNotExist(err) {
  328. klog.Errorf("Cannot validate mount point: %s %v", dir, err)
  329. return err
  330. }
  331. if !notMnt {
  332. return nil
  333. }
  334. if err = os.MkdirAll(dir, 0750); err != nil {
  335. klog.Errorf("mkdir failed on disk %s (%v)", dir, err)
  336. return err
  337. }
  338. // Perform a bind mount to the full path to allow duplicate mounts of the same PD.
  339. options := []string{"bind"}
  340. if b.readOnly {
  341. options = append(options, "ro")
  342. }
  343. mountOptions := util.JoinMountOptions(b.mountOptions, options)
  344. globalPDPath := makeGlobalPDName(b.plugin.host, b.pvName, b.volNamespace, b.volName)
  345. klog.V(4).Infof("Attempting to bind mount to pod volume at %s", dir)
  346. err = b.mounter.Mount(globalPDPath, dir, "", mountOptions)
  347. if err != nil {
  348. notMnt, mntErr := b.mounter.IsLikelyNotMountPoint(dir)
  349. if mntErr != nil {
  350. klog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
  351. return err
  352. }
  353. if !notMnt {
  354. if mntErr = b.mounter.Unmount(dir); mntErr != nil {
  355. klog.Errorf("Failed to unmount: %v", mntErr)
  356. return err
  357. }
  358. notMnt, mntErr := b.mounter.IsLikelyNotMountPoint(dir)
  359. if mntErr != nil {
  360. klog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
  361. return err
  362. }
  363. if !notMnt {
  364. klog.Errorf("%s is still mounted, despite call to unmount(). Will try again next sync loop.", dir)
  365. return err
  366. }
  367. }
  368. os.Remove(dir)
  369. klog.Errorf("Mount of disk %s failed: %v", dir, err)
  370. return err
  371. }
  372. if !b.readOnly {
  373. volume.SetVolumeOwnership(b, mounterArgs.FsGroup)
  374. }
  375. klog.V(4).Infof("StorageOS volume setup complete on %s", dir)
  376. return nil
  377. }
  378. func makeGlobalPDName(host volume.VolumeHost, pvName, volNamespace, volName string) string {
  379. return filepath.Join(host.GetPluginDir(utilstrings.EscapeQualifiedName(storageosPluginName)), util.MountsInGlobalPDPath, pvName+"."+volNamespace+"."+volName)
  380. }
  381. // Given the pod id and PV name, finds the volume's namespace and name from the
  382. // name or volume mount. We mount as volNamespace.pvName, but k8s will specify
  383. // only the pvName to unmount.
  384. // Will return empty volNamespace/pvName if the volume is not mounted.
  385. func getVolumeInfo(pvName string, podUID types.UID, host volume.VolumeHost) (string, string, error) {
  386. if volNamespace, volName, err := getVolumeFromRef(pvName); err == nil {
  387. return volNamespace, volName, nil
  388. }
  389. volumeDir := filepath.Dir(host.GetPodVolumeDir(podUID, utilstrings.EscapeQualifiedName(storageosPluginName), pvName))
  390. files, err := ioutil.ReadDir(volumeDir)
  391. if err != nil {
  392. return "", "", fmt.Errorf("Could not read mounts from pod volume dir: %s", err)
  393. }
  394. for _, f := range files {
  395. if f.Mode().IsDir() && strings.HasPrefix(f.Name(), pvName+".") {
  396. if volNamespace, volName, err := getVolumeFromRef(f.Name()); err == nil {
  397. return volNamespace, volName, nil
  398. }
  399. }
  400. }
  401. return "", "", fmt.Errorf("Could not get info from unmounted pv %q at %q", pvName, volumeDir)
  402. }
  403. // Splits the volume ref on "." to return the volNamespace and pvName. Neither
  404. // namespaces nor service names allow "." in their names.
  405. func getVolumeFromRef(ref string) (volNamespace string, volName string, err error) {
  406. refParts := strings.Split(ref, ".")
  407. switch len(refParts) {
  408. case 2:
  409. return refParts[0], refParts[1], nil
  410. case 3:
  411. return refParts[1], refParts[2], nil
  412. }
  413. return "", "", fmt.Errorf("ref not in format volNamespace.volName or pvName.volNamespace.volName")
  414. }
  415. // GetPath returns the path to the user specific mount of a StorageOS volume
  416. func (storageosVolume *storageos) GetPath() string {
  417. return getPath(storageosVolume.podUID, storageosVolume.volNamespace, storageosVolume.volName, storageosVolume.pvName, storageosVolume.plugin.host)
  418. }
  419. type storageosUnmounter struct {
  420. *storageos
  421. }
  422. var _ volume.Unmounter = &storageosUnmounter{}
  423. func (b *storageosUnmounter) GetPath() string {
  424. return getPath(b.podUID, b.volNamespace, b.volName, b.pvName, b.plugin.host)
  425. }
  426. // Unmounts the bind mount, and detaches the disk only if the PD
  427. // resource was the last reference to that disk on the kubelet.
  428. func (b *storageosUnmounter) TearDown() error {
  429. if len(b.volNamespace) == 0 || len(b.volName) == 0 {
  430. klog.Warningf("volNamespace: %q, volName: %q not set, skipping TearDown", b.volNamespace, b.volName)
  431. return fmt.Errorf("pvName not specified for TearDown, waiting for next sync loop")
  432. }
  433. // Unmount from pod
  434. mountPath := b.GetPath()
  435. err := b.TearDownAt(mountPath)
  436. if err != nil {
  437. klog.Errorf("Unmount from pod failed: %v", err)
  438. return err
  439. }
  440. // Find device name from global mount
  441. globalPDPath := makeGlobalPDName(b.plugin.host, b.pvName, b.volNamespace, b.volName)
  442. devicePath, _, err := mount.GetDeviceNameFromMount(b.mounter, globalPDPath)
  443. if err != nil {
  444. klog.Errorf("Detach failed when getting device from global mount: %v", err)
  445. return err
  446. }
  447. // Unmount from plugin's disk global mount dir.
  448. err = b.TearDownAt(globalPDPath)
  449. if err != nil {
  450. klog.Errorf("Detach failed during unmount: %v", err)
  451. return err
  452. }
  453. // Detach loop device
  454. err = b.manager.DetachVolume(b, devicePath)
  455. if err != nil {
  456. klog.Errorf("Detach device %s failed for volume %s: %v", devicePath, b.pvName, err)
  457. return err
  458. }
  459. klog.V(4).Infof("Successfully unmounted StorageOS volume %s and detached devices", b.pvName)
  460. return nil
  461. }
  462. // Unmounts the bind mount, and detaches the disk only if the PD
  463. // resource was the last reference to that disk on the kubelet.
  464. func (b *storageosUnmounter) TearDownAt(dir string) error {
  465. if err := mount.CleanupMountPoint(dir, b.mounter, false); err != nil {
  466. klog.V(4).Infof("Unmounted StorageOS volume %s failed with: %v", b.pvName, err)
  467. }
  468. if err := b.manager.UnmountVolume(b); err != nil {
  469. klog.V(4).Infof("Mount reference for volume %s could not be removed from StorageOS: %v", b.pvName, err)
  470. }
  471. return nil
  472. }
  473. type storageosDeleter struct {
  474. *storageosMounter
  475. pvUID types.UID
  476. }
  477. var _ volume.Deleter = &storageosDeleter{}
  478. func (d *storageosDeleter) GetPath() string {
  479. return getPath(d.podUID, d.volNamespace, d.volName, d.pvName, d.plugin.host)
  480. }
  481. func (d *storageosDeleter) Delete() error {
  482. return d.manager.DeleteVolume(d)
  483. }
  484. type storageosProvisioner struct {
  485. *storageosMounter
  486. options volume.VolumeOptions
  487. }
  488. var _ volume.Provisioner = &storageosProvisioner{}
  489. func (c *storageosProvisioner) Provision(selectedNode *v1.Node, allowedTopologies []v1.TopologySelectorTerm) (*v1.PersistentVolume, error) {
  490. if !util.AccessModesContainedInAll(c.plugin.GetAccessModes(), c.options.PVC.Spec.AccessModes) {
  491. return nil, fmt.Errorf("invalid AccessModes %v: only AccessModes %v are supported", c.options.PVC.Spec.AccessModes, c.plugin.GetAccessModes())
  492. }
  493. if util.CheckPersistentVolumeClaimModeBlock(c.options.PVC) {
  494. return nil, fmt.Errorf("%s does not support block volume provisioning", c.plugin.GetPluginName())
  495. }
  496. var adminSecretName, adminSecretNamespace string
  497. // Apply ProvisionerParameters (case-insensitive). We leave validation of
  498. // the values to the cloud provider.
  499. for k, v := range c.options.Parameters {
  500. switch strings.ToLower(k) {
  501. case "adminsecretname":
  502. adminSecretName = v
  503. case "adminsecretnamespace":
  504. adminSecretNamespace = v
  505. case "volumenamespace":
  506. c.volNamespace = v
  507. case "description":
  508. c.description = v
  509. case "pool":
  510. c.pool = v
  511. case "fstype":
  512. c.fsType = v
  513. default:
  514. return nil, fmt.Errorf("invalid option %q for volume plugin %s", k, c.plugin.GetPluginName())
  515. }
  516. }
  517. // Set from PVC
  518. c.podNamespace = c.options.PVC.Namespace
  519. c.volName = c.options.PVName
  520. if c.volNamespace == "" {
  521. c.volNamespace = c.options.PVC.Namespace
  522. }
  523. c.labels = make(map[string]string)
  524. for k, v := range c.options.PVC.Labels {
  525. c.labels[k] = v
  526. }
  527. capacity := c.options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
  528. var err error
  529. c.sizeGB, err = volumehelpers.RoundUpToGiBInt(capacity)
  530. if err != nil {
  531. return nil, err
  532. }
  533. apiCfg, err := parsePVSecret(adminSecretNamespace, adminSecretName, c.plugin.host.GetKubeClient())
  534. if err != nil {
  535. return nil, err
  536. }
  537. c.apiCfg = apiCfg
  538. vol, err := c.manager.CreateVolume(c)
  539. if err != nil {
  540. klog.Errorf("failed to create volume: %v", err)
  541. return nil, err
  542. }
  543. if vol.FSType == "" {
  544. vol.FSType = defaultFSType
  545. }
  546. pv := &v1.PersistentVolume{
  547. ObjectMeta: metav1.ObjectMeta{
  548. Name: vol.Name,
  549. Labels: map[string]string{},
  550. Annotations: map[string]string{
  551. util.VolumeDynamicallyCreatedByKey: "storageos-dynamic-provisioner",
  552. },
  553. },
  554. Spec: v1.PersistentVolumeSpec{
  555. PersistentVolumeReclaimPolicy: c.options.PersistentVolumeReclaimPolicy,
  556. AccessModes: c.options.PVC.Spec.AccessModes,
  557. Capacity: v1.ResourceList{
  558. v1.ResourceName(v1.ResourceStorage): resource.MustParse(fmt.Sprintf("%dGi", vol.SizeGB)),
  559. },
  560. PersistentVolumeSource: v1.PersistentVolumeSource{
  561. StorageOS: &v1.StorageOSPersistentVolumeSource{
  562. VolumeName: vol.Name,
  563. VolumeNamespace: vol.Namespace,
  564. FSType: vol.FSType,
  565. ReadOnly: false,
  566. SecretRef: &v1.ObjectReference{
  567. Name: adminSecretName,
  568. Namespace: adminSecretNamespace,
  569. },
  570. },
  571. },
  572. MountOptions: c.options.MountOptions,
  573. },
  574. }
  575. if len(c.options.PVC.Spec.AccessModes) == 0 {
  576. pv.Spec.AccessModes = c.plugin.GetAccessModes()
  577. }
  578. if len(vol.Labels) != 0 {
  579. if pv.Labels == nil {
  580. pv.Labels = make(map[string]string)
  581. }
  582. for k, v := range vol.Labels {
  583. pv.Labels[k] = v
  584. }
  585. }
  586. return pv, nil
  587. }
  588. // Returns StorageOS volume name, namespace, fstype and readonly from spec
  589. func getVolumeInfoFromSpec(spec *volume.Spec) (string, string, string, bool, error) {
  590. if spec.PersistentVolume != nil {
  591. source, readOnly, err := getPersistentVolumeSource(spec)
  592. if err != nil {
  593. return "", "", "", false, err
  594. }
  595. return source.VolumeName, source.VolumeNamespace, source.FSType, readOnly, nil
  596. }
  597. if spec.Volume != nil {
  598. source, readOnly, err := getVolumeSource(spec)
  599. if err != nil {
  600. return "", "", "", false, err
  601. }
  602. return source.VolumeName, source.VolumeNamespace, source.FSType, readOnly, nil
  603. }
  604. return "", "", "", false, fmt.Errorf("spec not Volume or PersistentVolume")
  605. }
  606. // Returns API config if secret set, otherwise empty struct so defaults can be
  607. // attempted.
  608. func getAPICfg(spec *volume.Spec, pod *v1.Pod, kubeClient clientset.Interface) (*storageosAPIConfig, error) {
  609. if spec.PersistentVolume != nil {
  610. source, _, err := getPersistentVolumeSource(spec)
  611. if err != nil {
  612. return nil, err
  613. }
  614. if source.SecretRef == nil {
  615. return nil, nil
  616. }
  617. return parsePVSecret(source.SecretRef.Namespace, source.SecretRef.Name, kubeClient)
  618. }
  619. if spec.Volume != nil {
  620. source, _, err := getVolumeSource(spec)
  621. if err != nil {
  622. return nil, err
  623. }
  624. if source.SecretRef == nil {
  625. return nil, nil
  626. }
  627. return parsePodSecret(pod, source.SecretRef.Name, kubeClient)
  628. }
  629. return nil, fmt.Errorf("spec not Volume or PersistentVolume")
  630. }
  631. func parsePodSecret(pod *v1.Pod, secretName string, kubeClient clientset.Interface) (*storageosAPIConfig, error) {
  632. secret, err := util.GetSecretForPod(pod, secretName, kubeClient)
  633. if err != nil {
  634. klog.Errorf("failed to get secret from [%q/%q]", pod.Namespace, secretName)
  635. return nil, fmt.Errorf("failed to get secret from [%q/%q]", pod.Namespace, secretName)
  636. }
  637. return parseAPIConfig(secret)
  638. }
  639. // Important: Only to be called with data from a PV to avoid secrets being
  640. // loaded from a user-suppler namespace.
  641. func parsePVSecret(namespace, secretName string, kubeClient clientset.Interface) (*storageosAPIConfig, error) {
  642. secret, err := util.GetSecretForPV(namespace, secretName, storageosPluginName, kubeClient)
  643. if err != nil {
  644. klog.Errorf("failed to get secret from [%q/%q]", namespace, secretName)
  645. return nil, fmt.Errorf("failed to get secret from [%q/%q]", namespace, secretName)
  646. }
  647. return parseAPIConfig(secret)
  648. }
  649. // Parse API configuration from parameters or secret
  650. func parseAPIConfig(params map[string]string) (*storageosAPIConfig, error) {
  651. if len(params) == 0 {
  652. return nil, fmt.Errorf("empty API config")
  653. }
  654. c := &storageosAPIConfig{}
  655. for name, data := range params {
  656. switch strings.ToLower(name) {
  657. case "apiaddress":
  658. c.apiAddr = string(data)
  659. case "apiusername":
  660. c.apiUser = string(data)
  661. case "apipassword":
  662. c.apiPass = string(data)
  663. case "apiversion":
  664. c.apiVersion = string(data)
  665. }
  666. }
  667. return c, nil
  668. }