mounter.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 flexvolume
  14. import (
  15. "os"
  16. "strconv"
  17. "k8s.io/kubernetes/pkg/volume"
  18. "k8s.io/utils/exec"
  19. )
  20. // FlexVolumeMounter is the disk that will be exposed by this plugin.
  21. type flexVolumeMounter struct {
  22. *flexVolume
  23. // Runner used to setup the volume.
  24. runner exec.Interface
  25. // the considered volume spec
  26. spec *volume.Spec
  27. readOnly bool
  28. }
  29. var _ volume.Mounter = &flexVolumeMounter{}
  30. // Mounter interface
  31. // SetUp creates new directory.
  32. func (f *flexVolumeMounter) SetUp(mounterArgs volume.MounterArgs) error {
  33. return f.SetUpAt(f.GetPath(), mounterArgs)
  34. }
  35. // SetUpAt creates new directory.
  36. func (f *flexVolumeMounter) SetUpAt(dir string, mounterArgs volume.MounterArgs) error {
  37. // Mount only once.
  38. alreadyMounted, err := prepareForMount(f.mounter, dir)
  39. if err != nil {
  40. return err
  41. }
  42. if alreadyMounted {
  43. return nil
  44. }
  45. call := f.plugin.NewDriverCall(mountCmd)
  46. // Interface parameters
  47. call.Append(dir)
  48. extraOptions := make(map[string]string)
  49. // pod metadata
  50. extraOptions[optionKeyPodName] = f.podName
  51. extraOptions[optionKeyPodNamespace] = f.podNamespace
  52. extraOptions[optionKeyPodUID] = string(f.podUID)
  53. // service account metadata
  54. extraOptions[optionKeyServiceAccountName] = f.podServiceAccountName
  55. // Extract secret and pass it as options.
  56. if err := addSecretsToOptions(extraOptions, f.spec, f.podNamespace, f.driverName, f.plugin.host); err != nil {
  57. os.Remove(dir)
  58. return err
  59. }
  60. // Implicit parameters
  61. if mounterArgs.FsGroup != nil {
  62. extraOptions[optionFSGroup] = strconv.FormatInt(int64(*mounterArgs.FsGroup), 10)
  63. }
  64. call.AppendSpec(f.spec, f.plugin.host, extraOptions)
  65. _, err = call.Run()
  66. if isCmdNotSupportedErr(err) {
  67. err = (*mounterDefaults)(f).SetUpAt(dir, mounterArgs)
  68. }
  69. if err != nil {
  70. os.Remove(dir)
  71. return err
  72. }
  73. if !f.readOnly {
  74. if f.plugin.capabilities.FSGroup {
  75. volume.SetVolumeOwnership(f, mounterArgs.FsGroup)
  76. }
  77. }
  78. return nil
  79. }
  80. // GetAttributes get the flex volume attributes. The attributes will be queried
  81. // using plugin callout after we finalize the callout syntax.
  82. func (f *flexVolumeMounter) GetAttributes() volume.Attributes {
  83. return (*mounterDefaults)(f).GetAttributes()
  84. }
  85. func (f *flexVolumeMounter) CanMount() error {
  86. return nil
  87. }