nfs.go 9.7 KB

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