nfs.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. Copyright 2014 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 nfs
  14. import (
  15. "fmt"
  16. "os"
  17. "runtime"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/types"
  21. "k8s.io/klog"
  22. "k8s.io/kubernetes/pkg/util/mount"
  23. "k8s.io/kubernetes/pkg/volume"
  24. "k8s.io/kubernetes/pkg/volume/util"
  25. "k8s.io/kubernetes/pkg/volume/util/recyclerclient"
  26. utilstrings "k8s.io/utils/strings"
  27. )
  28. // ProbeVolumePlugins is the primary entrypoint for volume plugins.
  29. // The volumeConfig arg provides the ability to configure recycler behavior. It is implemented as a pointer to allow nils.
  30. // The nfsPlugin is used to store the volumeConfig and give it, when needed, to the func that creates NFS Recyclers.
  31. // Tests that exercise recycling should not use this func but instead use ProbeRecyclablePlugins() to override default behavior.
  32. func ProbeVolumePlugins(volumeConfig volume.VolumeConfig) []volume.VolumePlugin {
  33. return []volume.VolumePlugin{
  34. &nfsPlugin{
  35. host: nil,
  36. config: volumeConfig,
  37. },
  38. }
  39. }
  40. type nfsPlugin struct {
  41. host volume.VolumeHost
  42. config volume.VolumeConfig
  43. }
  44. var _ volume.VolumePlugin = &nfsPlugin{}
  45. var _ volume.PersistentVolumePlugin = &nfsPlugin{}
  46. var _ volume.RecyclableVolumePlugin = &nfsPlugin{}
  47. const (
  48. nfsPluginName = "kubernetes.io/nfs"
  49. )
  50. func (plugin *nfsPlugin) Init(host volume.VolumeHost) error {
  51. plugin.host = host
  52. return nil
  53. }
  54. func (plugin *nfsPlugin) GetPluginName() string {
  55. return nfsPluginName
  56. }
  57. func (plugin *nfsPlugin) GetVolumeName(spec *volume.Spec) (string, error) {
  58. volumeSource, _, err := getVolumeSource(spec)
  59. if err != nil {
  60. return "", err
  61. }
  62. return fmt.Sprintf(
  63. "%v/%v",
  64. volumeSource.Server,
  65. volumeSource.Path), nil
  66. }
  67. func (plugin *nfsPlugin) CanSupport(spec *volume.Spec) bool {
  68. return (spec.PersistentVolume != nil && spec.PersistentVolume.Spec.NFS != nil) ||
  69. (spec.Volume != nil && spec.Volume.NFS != nil)
  70. }
  71. func (plugin *nfsPlugin) IsMigratedToCSI() bool {
  72. return false
  73. }
  74. func (plugin *nfsPlugin) RequiresRemount() bool {
  75. return false
  76. }
  77. func (plugin *nfsPlugin) SupportsMountOption() bool {
  78. return true
  79. }
  80. func (plugin *nfsPlugin) SupportsBulkVolumeVerification() bool {
  81. return false
  82. }
  83. func (plugin *nfsPlugin) GetAccessModes() []v1.PersistentVolumeAccessMode {
  84. return []v1.PersistentVolumeAccessMode{
  85. v1.ReadWriteOnce,
  86. v1.ReadOnlyMany,
  87. v1.ReadWriteMany,
  88. }
  89. }
  90. func (plugin *nfsPlugin) NewMounter(spec *volume.Spec, pod *v1.Pod, _ volume.VolumeOptions) (volume.Mounter, error) {
  91. return plugin.newMounterInternal(spec, pod, plugin.host.GetMounter(plugin.GetPluginName()))
  92. }
  93. func (plugin *nfsPlugin) newMounterInternal(spec *volume.Spec, pod *v1.Pod, mounter mount.Interface) (volume.Mounter, error) {
  94. source, readOnly, err := getVolumeSource(spec)
  95. if err != nil {
  96. return nil, err
  97. }
  98. return &nfsMounter{
  99. nfs: &nfs{
  100. volName: spec.Name(),
  101. mounter: mounter,
  102. pod: pod,
  103. plugin: plugin,
  104. },
  105. server: source.Server,
  106. exportPath: source.Path,
  107. readOnly: readOnly,
  108. mountOptions: util.MountOptionFromSpec(spec),
  109. }, nil
  110. }
  111. func (plugin *nfsPlugin) NewUnmounter(volName string, podUID types.UID) (volume.Unmounter, error) {
  112. return plugin.newUnmounterInternal(volName, podUID, plugin.host.GetMounter(plugin.GetPluginName()))
  113. }
  114. func (plugin *nfsPlugin) newUnmounterInternal(volName string, podUID types.UID, mounter mount.Interface) (volume.Unmounter, error) {
  115. return &nfsUnmounter{&nfs{
  116. volName: volName,
  117. mounter: mounter,
  118. pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: podUID}},
  119. plugin: plugin,
  120. }}, nil
  121. }
  122. // Recycle recycles/scrubs clean an NFS volume.
  123. // Recycle blocks until the pod has completed or any error occurs.
  124. func (plugin *nfsPlugin) Recycle(pvName string, spec *volume.Spec, eventRecorder recyclerclient.RecycleEventRecorder) error {
  125. if spec.PersistentVolume == nil || spec.PersistentVolume.Spec.NFS == nil {
  126. return fmt.Errorf("spec.PersistentVolumeSource.NFS is nil")
  127. }
  128. pod := plugin.config.RecyclerPodTemplate
  129. timeout := util.CalculateTimeoutForVolume(plugin.config.RecyclerMinimumTimeout, plugin.config.RecyclerTimeoutIncrement, spec.PersistentVolume)
  130. // overrides
  131. pod.Spec.ActiveDeadlineSeconds = &timeout
  132. pod.GenerateName = "pv-recycler-nfs-"
  133. pod.Spec.Volumes[0].VolumeSource = v1.VolumeSource{
  134. NFS: &v1.NFSVolumeSource{
  135. Server: spec.PersistentVolume.Spec.NFS.Server,
  136. Path: spec.PersistentVolume.Spec.NFS.Path,
  137. },
  138. }
  139. return recyclerclient.RecycleVolumeByWatchingPodUntilCompletion(pvName, pod, plugin.host.GetKubeClient(), eventRecorder)
  140. }
  141. func (plugin *nfsPlugin) ConstructVolumeSpec(volumeName, mountPath string) (*volume.Spec, error) {
  142. nfsVolume := &v1.Volume{
  143. Name: volumeName,
  144. VolumeSource: v1.VolumeSource{
  145. NFS: &v1.NFSVolumeSource{
  146. Path: volumeName,
  147. },
  148. },
  149. }
  150. return volume.NewSpecFromVolume(nfsVolume), nil
  151. }
  152. // NFS volumes represent a bare host file or directory mount of an NFS export.
  153. type nfs struct {
  154. volName string
  155. pod *v1.Pod
  156. mounter mount.Interface
  157. plugin *nfsPlugin
  158. volume.MetricsNil
  159. }
  160. func (nfsVolume *nfs) GetPath() string {
  161. name := nfsPluginName
  162. return nfsVolume.plugin.host.GetPodVolumeDir(nfsVolume.pod.UID, utilstrings.EscapeQualifiedName(name), nfsVolume.volName)
  163. }
  164. // Checks prior to mount operations to verify that the required components (binaries, etc.)
  165. // to mount the volume are available on the underlying node.
  166. // If not, it returns an error
  167. func (nfsMounter *nfsMounter) CanMount() error {
  168. exec := nfsMounter.plugin.host.GetExec(nfsMounter.plugin.GetPluginName())
  169. switch runtime.GOOS {
  170. case "linux":
  171. if _, err := exec.Run("test", "-x", "/sbin/mount.nfs"); err != nil {
  172. return fmt.Errorf("Required binary /sbin/mount.nfs is missing")
  173. }
  174. if _, err := exec.Run("test", "-x", "/sbin/mount.nfs4"); err != nil {
  175. return fmt.Errorf("Required binary /sbin/mount.nfs4 is missing")
  176. }
  177. return nil
  178. case "darwin":
  179. if _, err := exec.Run("test", "-x", "/sbin/mount_nfs"); err != nil {
  180. return fmt.Errorf("Required binary /sbin/mount_nfs is missing")
  181. }
  182. }
  183. return nil
  184. }
  185. type nfsMounter struct {
  186. *nfs
  187. server string
  188. exportPath string
  189. readOnly bool
  190. mountOptions []string
  191. }
  192. var _ volume.Mounter = &nfsMounter{}
  193. func (nfsMounter *nfsMounter) GetAttributes() volume.Attributes {
  194. return volume.Attributes{
  195. ReadOnly: nfsMounter.readOnly,
  196. Managed: false,
  197. SupportsSELinux: false,
  198. }
  199. }
  200. // SetUp attaches the disk and bind mounts to the volume path.
  201. func (nfsMounter *nfsMounter) SetUp(mounterArgs volume.MounterArgs) error {
  202. return nfsMounter.SetUpAt(nfsMounter.GetPath(), mounterArgs)
  203. }
  204. func (nfsMounter *nfsMounter) SetUpAt(dir string, mounterArgs volume.MounterArgs) error {
  205. notMnt, err := mount.IsNotMountPoint(nfsMounter.mounter, dir)
  206. klog.V(4).Infof("NFS mount set up: %s %v %v", dir, !notMnt, err)
  207. if err != nil && !os.IsNotExist(err) {
  208. return err
  209. }
  210. if !notMnt {
  211. return nil
  212. }
  213. if err := os.MkdirAll(dir, 0750); err != nil {
  214. return err
  215. }
  216. source := fmt.Sprintf("%s:%s", nfsMounter.server, nfsMounter.exportPath)
  217. options := []string{}
  218. if nfsMounter.readOnly {
  219. options = append(options, "ro")
  220. }
  221. mountOptions := util.JoinMountOptions(nfsMounter.mountOptions, options)
  222. err = nfsMounter.mounter.Mount(source, dir, "nfs", mountOptions)
  223. if err != nil {
  224. notMnt, mntErr := mount.IsNotMountPoint(nfsMounter.mounter, dir)
  225. if mntErr != nil {
  226. klog.Errorf("IsNotMountPoint check failed: %v", mntErr)
  227. return err
  228. }
  229. if !notMnt {
  230. if mntErr = nfsMounter.mounter.Unmount(dir); mntErr != nil {
  231. klog.Errorf("Failed to unmount: %v", mntErr)
  232. return err
  233. }
  234. notMnt, mntErr := mount.IsNotMountPoint(nfsMounter.mounter, dir)
  235. if mntErr != nil {
  236. klog.Errorf("IsNotMountPoint check failed: %v", mntErr)
  237. return err
  238. }
  239. if !notMnt {
  240. // This is very odd, we don't expect it. We'll try again next sync loop.
  241. klog.Errorf("%s is still mounted, despite call to unmount(). Will try again next sync loop.", dir)
  242. return err
  243. }
  244. }
  245. os.Remove(dir)
  246. return err
  247. }
  248. return nil
  249. }
  250. var _ volume.Unmounter = &nfsUnmounter{}
  251. type nfsUnmounter struct {
  252. *nfs
  253. }
  254. func (c *nfsUnmounter) TearDown() error {
  255. return c.TearDownAt(c.GetPath())
  256. }
  257. func (c *nfsUnmounter) TearDownAt(dir string) error {
  258. // Use extensiveMountPointCheck to consult /proc/mounts. We can't use faster
  259. // IsLikelyNotMountPoint (lstat()), since there may be root_squash on the
  260. // NFS server and kubelet may not be able to do lstat/stat() there.
  261. return mount.CleanupMountPoint(dir, c.mounter, true /* extensiveMountPointCheck */)
  262. }
  263. func getVolumeSource(spec *volume.Spec) (*v1.NFSVolumeSource, bool, error) {
  264. if spec.Volume != nil && spec.Volume.NFS != nil {
  265. return spec.Volume.NFS, spec.Volume.NFS.ReadOnly, nil
  266. } else if spec.PersistentVolume != nil &&
  267. spec.PersistentVolume.Spec.NFS != nil {
  268. return spec.PersistentVolume.Spec.NFS, spec.ReadOnly, nil
  269. }
  270. return nil, false, fmt.Errorf("Spec does not reference a NFS volume type")
  271. }