2015-02-10 19:00:11 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors.
|
2015-02-10 19:00:11 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package nfs
|
|
|
|
|
|
|
|
import (
|
2015-04-03 01:08:04 +00:00
|
|
|
"fmt"
|
2015-02-10 19:00:11 +00:00
|
|
|
"os"
|
2016-11-03 19:15:52 +00:00
|
|
|
"runtime"
|
2015-02-10 19:00:11 +00:00
|
|
|
|
2016-11-03 19:15:52 +00:00
|
|
|
"github.com/golang/glog"
|
2017-01-17 03:38:19 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/types"
|
2016-11-18 20:58:56 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/v1"
|
2016-11-03 19:15:52 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/exec"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/mount"
|
2016-01-11 07:55:51 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/strings"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/volume"
|
2016-12-20 00:40:55 +00:00
|
|
|
"k8s.io/kubernetes/pkg/volume/util"
|
2015-02-10 19:00:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// This is the primary entrypoint for volume plugins.
|
2015-09-03 03:14:26 +00:00
|
|
|
// The volumeConfig arg provides the ability to configure recycler behavior. It is implemented as a pointer to allow nils.
|
|
|
|
// The nfsPlugin is used to store the volumeConfig and give it, when needed, to the func that creates NFS Recyclers.
|
|
|
|
// Tests that exercise recycling should not use this func but instead use ProbeRecyclablePlugins() to override default behavior.
|
|
|
|
func ProbeVolumePlugins(volumeConfig volume.VolumeConfig) []volume.VolumePlugin {
|
|
|
|
return []volume.VolumePlugin{
|
|
|
|
&nfsPlugin{
|
Simplifies NFS and Host Path Plugin - Removed newRecyclerFunc, newDeleterFunc and newProvisionerFunc
struct hostPathPlugin contains newRecyclerFunc, newDeleterFunc and newProvisionerFunc items that have only one instance, i.e. newRecycler, newDeleter or newProvisioner function.
That's why the newRecyclerFunc, newDeleterFunc and newProvisionerFunc items are removed and the newRecycler, newDeleter or newProvisioner functions are called directly.
In addition, the TestRecycler tests whether NewFakeRecycler function is called and returns nil. This is no longer needed so this particular part of the test is removed. In addition, the no longer used NewFakeRecycler function is removed also.
Similarly for the NFS plugin, struct nfsPlugin contains newRecyclerFunc item that has only one instance, i.e. newRecycler function. That's why the newRecyclerFunc item is removed and the newRecycler function is called directly. In addition, the TestRecycler tests whether newMockRecycler function is called and returns nil. This is no longer needed so this particular part of the test is removed. In addition, the no longer used newMockRecycler function is removed also.
2016-10-25 10:36:49 +00:00
|
|
|
host: nil,
|
|
|
|
config: volumeConfig,
|
2015-09-03 03:14:26 +00:00
|
|
|
},
|
|
|
|
}
|
2015-02-10 19:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type nfsPlugin struct {
|
Simplifies NFS and Host Path Plugin - Removed newRecyclerFunc, newDeleterFunc and newProvisionerFunc
struct hostPathPlugin contains newRecyclerFunc, newDeleterFunc and newProvisionerFunc items that have only one instance, i.e. newRecycler, newDeleter or newProvisioner function.
That's why the newRecyclerFunc, newDeleterFunc and newProvisionerFunc items are removed and the newRecycler, newDeleter or newProvisioner functions are called directly.
In addition, the TestRecycler tests whether NewFakeRecycler function is called and returns nil. This is no longer needed so this particular part of the test is removed. In addition, the no longer used NewFakeRecycler function is removed also.
Similarly for the NFS plugin, struct nfsPlugin contains newRecyclerFunc item that has only one instance, i.e. newRecycler function. That's why the newRecyclerFunc item is removed and the newRecycler function is called directly. In addition, the TestRecycler tests whether newMockRecycler function is called and returns nil. This is no longer needed so this particular part of the test is removed. In addition, the no longer used newMockRecycler function is removed also.
2016-10-25 10:36:49 +00:00
|
|
|
host volume.VolumeHost
|
|
|
|
config volume.VolumeConfig
|
2015-02-10 19:00:11 +00:00
|
|
|
}
|
|
|
|
|
2015-03-19 05:18:31 +00:00
|
|
|
var _ volume.VolumePlugin = &nfsPlugin{}
|
2015-05-29 20:34:02 +00:00
|
|
|
var _ volume.PersistentVolumePlugin = &nfsPlugin{}
|
|
|
|
var _ volume.RecyclableVolumePlugin = &nfsPlugin{}
|
2015-02-10 19:00:11 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
nfsPluginName = "kubernetes.io/nfs"
|
|
|
|
)
|
|
|
|
|
2015-09-30 18:31:53 +00:00
|
|
|
func (plugin *nfsPlugin) Init(host volume.VolumeHost) error {
|
2015-02-10 19:00:11 +00:00
|
|
|
plugin.host = host
|
2015-09-30 18:31:53 +00:00
|
|
|
return nil
|
2015-02-10 19:00:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 22:48:21 +00:00
|
|
|
func (plugin *nfsPlugin) GetPluginName() string {
|
2015-02-10 19:00:11 +00:00
|
|
|
return nfsPluginName
|
|
|
|
}
|
|
|
|
|
2016-05-30 22:48:21 +00:00
|
|
|
func (plugin *nfsPlugin) GetVolumeName(spec *volume.Spec) (string, error) {
|
2016-05-30 02:22:22 +00:00
|
|
|
volumeSource, _, err := getVolumeSource(spec)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-05-30 22:48:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"%v/%v",
|
|
|
|
volumeSource.Server,
|
|
|
|
volumeSource.Path), nil
|
|
|
|
}
|
|
|
|
|
2015-04-14 16:29:33 +00:00
|
|
|
func (plugin *nfsPlugin) CanSupport(spec *volume.Spec) bool {
|
2015-11-03 21:09:57 +00:00
|
|
|
return (spec.PersistentVolume != nil && spec.PersistentVolume.Spec.NFS != nil) ||
|
|
|
|
(spec.Volume != nil && spec.Volume.NFS != nil)
|
2015-02-10 19:00:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 02:22:22 +00:00
|
|
|
func (plugin *nfsPlugin) RequiresRemount() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-02-21 18:19:48 +00:00
|
|
|
func (plugin *nfsPlugin) SupportsMountOption() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-02-13 04:40:30 +00:00
|
|
|
func (plugin *nfsPlugin) SupportsBulkVolumeVerification() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:58:56 +00:00
|
|
|
func (plugin *nfsPlugin) GetAccessModes() []v1.PersistentVolumeAccessMode {
|
|
|
|
return []v1.PersistentVolumeAccessMode{
|
|
|
|
v1.ReadWriteOnce,
|
|
|
|
v1.ReadOnlyMany,
|
|
|
|
v1.ReadWriteMany,
|
2015-03-12 19:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:58:56 +00:00
|
|
|
func (plugin *nfsPlugin) NewMounter(spec *volume.Spec, pod *v1.Pod, _ volume.VolumeOptions) (volume.Mounter, error) {
|
2016-03-23 05:12:21 +00:00
|
|
|
return plugin.newMounterInternal(spec, pod, plugin.host.GetMounter())
|
2015-02-10 19:00:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 20:58:56 +00:00
|
|
|
func (plugin *nfsPlugin) newMounterInternal(spec *volume.Spec, pod *v1.Pod, mounter mount.Interface) (volume.Mounter, error) {
|
2016-05-30 02:22:22 +00:00
|
|
|
source, readOnly, err := getVolumeSource(spec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-06-01 14:34:40 +00:00
|
|
|
}
|
2016-05-30 02:22:22 +00:00
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
return &nfsMounter{
|
2015-07-20 06:00:07 +00:00
|
|
|
nfs: &nfs{
|
2015-08-12 19:11:03 +00:00
|
|
|
volName: spec.Name(),
|
2015-07-20 06:00:07 +00:00
|
|
|
mounter: mounter,
|
|
|
|
pod: pod,
|
|
|
|
plugin: plugin,
|
|
|
|
},
|
2017-02-21 18:19:48 +00:00
|
|
|
server: source.Server,
|
|
|
|
exportPath: source.Path,
|
|
|
|
readOnly: readOnly,
|
|
|
|
mountOptions: volume.MountOptionFromSpec(spec),
|
2015-07-01 14:50:39 +00:00
|
|
|
}, nil
|
2015-02-10 19:00:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
func (plugin *nfsPlugin) NewUnmounter(volName string, podUID types.UID) (volume.Unmounter, error) {
|
|
|
|
return plugin.newUnmounterInternal(volName, podUID, plugin.host.GetMounter())
|
2015-02-10 19:00:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
func (plugin *nfsPlugin) newUnmounterInternal(volName string, podUID types.UID, mounter mount.Interface) (volume.Unmounter, error) {
|
|
|
|
return &nfsUnmounter{&nfs{
|
2015-07-20 06:00:07 +00:00
|
|
|
volName: volName,
|
|
|
|
mounter: mounter,
|
2017-01-17 03:38:19 +00:00
|
|
|
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{UID: podUID}},
|
2015-07-20 06:00:07 +00:00
|
|
|
plugin: plugin,
|
|
|
|
}}, nil
|
2015-02-10 19:00:11 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 19:49:00 +00:00
|
|
|
// Recycle recycles/scrubs clean an NFS volume.
|
|
|
|
// Recycle blocks until the pod has completed or any error occurs.
|
|
|
|
func (plugin *nfsPlugin) Recycle(pvName string, spec *volume.Spec, eventRecorder volume.RecycleEventRecorder) error {
|
|
|
|
if spec.PersistentVolume == nil || spec.PersistentVolume.Spec.NFS == nil {
|
|
|
|
return fmt.Errorf("spec.PersistentVolumeSource.NFS is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
pod := plugin.config.RecyclerPodTemplate
|
|
|
|
timeout := volume.CalculateTimeoutForVolume(plugin.config.RecyclerMinimumTimeout, plugin.config.RecyclerTimeoutIncrement, spec.PersistentVolume)
|
|
|
|
// overrides
|
|
|
|
pod.Spec.ActiveDeadlineSeconds = &timeout
|
|
|
|
pod.GenerateName = "pv-recycler-nfs-"
|
|
|
|
pod.Spec.Volumes[0].VolumeSource = v1.VolumeSource{
|
|
|
|
NFS: &v1.NFSVolumeSource{
|
|
|
|
Server: spec.PersistentVolume.Spec.NFS.Server,
|
|
|
|
Path: spec.PersistentVolume.Spec.NFS.Path,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return volume.RecycleVolumeByWatchingPodUntilCompletion(pvName, pod, plugin.host.GetKubeClient(), eventRecorder)
|
2015-05-29 20:34:02 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 19:46:21 +00:00
|
|
|
func (plugin *nfsPlugin) ConstructVolumeSpec(volumeName, mountPath string) (*volume.Spec, error) {
|
2016-11-18 20:58:56 +00:00
|
|
|
nfsVolume := &v1.Volume{
|
2016-06-23 19:46:21 +00:00
|
|
|
Name: volumeName,
|
2016-11-18 20:58:56 +00:00
|
|
|
VolumeSource: v1.VolumeSource{
|
|
|
|
NFS: &v1.NFSVolumeSource{
|
2016-06-23 19:46:21 +00:00
|
|
|
Path: volumeName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return volume.NewSpecFromVolume(nfsVolume), nil
|
|
|
|
}
|
|
|
|
|
2015-02-10 19:00:11 +00:00
|
|
|
// NFS volumes represent a bare host file or directory mount of an NFS export.
|
|
|
|
type nfs struct {
|
2015-07-20 06:00:07 +00:00
|
|
|
volName string
|
2016-11-18 20:58:56 +00:00
|
|
|
pod *v1.Pod
|
2015-07-20 06:00:07 +00:00
|
|
|
mounter mount.Interface
|
|
|
|
plugin *nfsPlugin
|
2015-12-04 20:40:01 +00:00
|
|
|
volume.MetricsNil
|
2015-07-20 06:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (nfsVolume *nfs) GetPath() string {
|
|
|
|
name := nfsPluginName
|
2016-01-11 07:55:51 +00:00
|
|
|
return nfsVolume.plugin.host.GetPodVolumeDir(nfsVolume.pod.UID, strings.EscapeQualifiedNameForDisk(name), nfsVolume.volName)
|
2015-07-20 06:00:07 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 19:15:52 +00:00
|
|
|
// Checks prior to mount operations to verify that the required components (binaries, etc.)
|
|
|
|
// to mount the volume are available on the underlying node.
|
|
|
|
// If not, it returns an error
|
|
|
|
func (nfsMounter *nfsMounter) CanMount() error {
|
|
|
|
exe := exec.New()
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux":
|
2017-05-06 11:06:27 +00:00
|
|
|
if _, err := exe.Command("/bin/ls", "/sbin/mount.nfs").CombinedOutput(); err != nil {
|
2016-11-03 19:15:52 +00:00
|
|
|
return fmt.Errorf("Required binary /sbin/mount.nfs is missing")
|
|
|
|
}
|
2017-05-06 11:06:27 +00:00
|
|
|
if _, err := exe.Command("/bin/ls", "/sbin/mount.nfs4").CombinedOutput(); err != nil {
|
2016-11-03 19:15:52 +00:00
|
|
|
return fmt.Errorf("Required binary /sbin/mount.nfs4 is missing")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case "darwin":
|
2017-05-06 11:06:27 +00:00
|
|
|
if _, err := exe.Command("/bin/ls", "/sbin/mount_nfs").CombinedOutput(); err != nil {
|
2016-11-03 19:15:52 +00:00
|
|
|
return fmt.Errorf("Required binary /sbin/mount_nfs is missing")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
type nfsMounter struct {
|
2015-07-20 06:00:07 +00:00
|
|
|
*nfs
|
2017-02-21 18:19:48 +00:00
|
|
|
server string
|
|
|
|
exportPath string
|
|
|
|
readOnly bool
|
|
|
|
mountOptions []string
|
2015-02-10 19:00:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
var _ volume.Mounter = &nfsMounter{}
|
2015-07-20 06:00:07 +00:00
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
func (b *nfsMounter) GetAttributes() volume.Attributes {
|
2015-10-30 20:25:36 +00:00
|
|
|
return volume.Attributes{
|
2016-01-11 16:10:55 +00:00
|
|
|
ReadOnly: b.readOnly,
|
|
|
|
Managed: false,
|
|
|
|
SupportsSELinux: false,
|
2015-10-30 20:25:36 +00:00
|
|
|
}
|
2015-10-20 18:49:39 +00:00
|
|
|
}
|
|
|
|
|
2015-02-10 19:00:11 +00:00
|
|
|
// SetUp attaches the disk and bind mounts to the volume path.
|
2017-04-20 10:57:07 +00:00
|
|
|
func (b *nfsMounter) SetUp(fsGroup *types.UnixGroupID) error {
|
2015-12-18 15:55:11 +00:00
|
|
|
return b.SetUpAt(b.GetPath(), fsGroup)
|
2015-02-10 19:00:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 10:57:07 +00:00
|
|
|
func (b *nfsMounter) SetUpAt(dir string, fsGroup *types.UnixGroupID) error {
|
2015-04-16 23:49:53 +00:00
|
|
|
notMnt, err := b.mounter.IsLikelyNotMountPoint(dir)
|
|
|
|
glog.V(4).Infof("NFS mount set up: %s %v %v", dir, !notMnt, err)
|
2015-02-10 19:00:11 +00:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-16 23:49:53 +00:00
|
|
|
if !notMnt {
|
2015-02-10 19:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-12-15 07:53:44 +00:00
|
|
|
if err := os.MkdirAll(dir, 0750); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-20 06:00:07 +00:00
|
|
|
source := fmt.Sprintf("%s:%s", b.server, b.exportPath)
|
2015-04-03 01:08:04 +00:00
|
|
|
options := []string{}
|
2015-07-20 06:00:07 +00:00
|
|
|
if b.readOnly {
|
2015-04-03 01:08:04 +00:00
|
|
|
options = append(options, "ro")
|
|
|
|
}
|
2017-02-21 18:19:48 +00:00
|
|
|
mountOptions := volume.JoinMountOptions(b.mountOptions, options)
|
|
|
|
err = b.mounter.Mount(source, dir, "nfs", mountOptions)
|
2015-02-10 19:00:11 +00:00
|
|
|
if err != nil {
|
2015-04-16 23:49:53 +00:00
|
|
|
notMnt, mntErr := b.mounter.IsLikelyNotMountPoint(dir)
|
2015-02-10 19:00:11 +00:00
|
|
|
if mntErr != nil {
|
2015-04-16 23:49:53 +00:00
|
|
|
glog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
|
2015-02-10 19:00:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-04-16 23:49:53 +00:00
|
|
|
if !notMnt {
|
2015-07-20 06:00:07 +00:00
|
|
|
if mntErr = b.mounter.Unmount(dir); mntErr != nil {
|
2015-02-10 19:00:11 +00:00
|
|
|
glog.Errorf("Failed to unmount: %v", mntErr)
|
|
|
|
return err
|
|
|
|
}
|
2015-04-16 23:49:53 +00:00
|
|
|
notMnt, mntErr := b.mounter.IsLikelyNotMountPoint(dir)
|
2015-02-10 19:00:11 +00:00
|
|
|
if mntErr != nil {
|
2015-04-16 23:49:53 +00:00
|
|
|
glog.Errorf("IsLikelyNotMountPoint check failed: %v", mntErr)
|
2015-02-10 19:00:11 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-04-16 23:49:53 +00:00
|
|
|
if !notMnt {
|
2015-02-10 19:00:11 +00:00
|
|
|
// This is very odd, we don't expect it. We'll try again next sync loop.
|
|
|
|
glog.Errorf("%s is still mounted, despite call to unmount(). Will try again next sync loop.", dir)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
os.Remove(dir)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
var _ volume.Unmounter = &nfsUnmounter{}
|
2015-07-20 06:00:07 +00:00
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
type nfsUnmounter struct {
|
2015-07-24 19:04:03 +00:00
|
|
|
*nfs
|
|
|
|
}
|
2015-07-20 06:00:07 +00:00
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
func (c *nfsUnmounter) TearDown() error {
|
2015-07-20 06:00:07 +00:00
|
|
|
return c.TearDownAt(c.GetPath())
|
2015-02-10 19:00:11 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 05:12:21 +00:00
|
|
|
func (c *nfsUnmounter) TearDownAt(dir string) error {
|
2016-12-20 00:40:55 +00:00
|
|
|
return util.UnmountPath(dir, c.mounter)
|
2015-02-10 19:00:11 +00:00
|
|
|
}
|
2015-05-29 20:34:02 +00:00
|
|
|
|
2016-11-18 20:58:56 +00:00
|
|
|
func getVolumeSource(spec *volume.Spec) (*v1.NFSVolumeSource, bool, error) {
|
2016-05-30 22:48:21 +00:00
|
|
|
if spec.Volume != nil && spec.Volume.NFS != nil {
|
2016-05-30 02:22:22 +00:00
|
|
|
return spec.Volume.NFS, spec.Volume.NFS.ReadOnly, nil
|
|
|
|
} else if spec.PersistentVolume != nil &&
|
|
|
|
spec.PersistentVolume.Spec.NFS != nil {
|
|
|
|
return spec.PersistentVolume.Spec.NFS, spec.ReadOnly, nil
|
2016-05-30 22:48:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-30 02:22:22 +00:00
|
|
|
return nil, false, fmt.Errorf("Spec does not reference a NFS volume type")
|
2016-05-30 22:48:21 +00:00
|
|
|
}
|