Commit d09d121b authored by Vish Kannan's avatar Vish Kannan

Merge pull request #11572 from jiangyaoguo/new-builder-cleaner-for-nfs

Refactor nfs volume to seperate builder and cleaner
parents dc59c99d 612f68f8
...@@ -82,16 +82,16 @@ func (plugin *nfsPlugin) newBuilderInternal(spec *volume.Spec, pod *api.Pod, mou ...@@ -82,16 +82,16 @@ func (plugin *nfsPlugin) newBuilderInternal(spec *volume.Spec, pod *api.Pod, mou
} else { } else {
source = spec.PersistentVolumeSource.NFS source = spec.PersistentVolumeSource.NFS
} }
return &nfs{ return &nfsBuilder{
volName: spec.Name, nfs: &nfs{
volName: spec.Name,
mounter: mounter,
pod: pod,
plugin: plugin,
},
server: source.Server, server: source.Server,
exportPath: source.Path, exportPath: source.Path,
readOnly: source.ReadOnly, readOnly: source.ReadOnly}, nil
mounter: mounter,
pod: pod,
plugin: plugin,
}, nil
} }
func (plugin *nfsPlugin) NewCleaner(volName string, podUID types.UID, mounter mount.Interface) (volume.Cleaner, error) { func (plugin *nfsPlugin) NewCleaner(volName string, podUID types.UID, mounter mount.Interface) (volume.Cleaner, error) {
...@@ -99,59 +99,49 @@ func (plugin *nfsPlugin) NewCleaner(volName string, podUID types.UID, mounter mo ...@@ -99,59 +99,49 @@ func (plugin *nfsPlugin) NewCleaner(volName string, podUID types.UID, mounter mo
} }
func (plugin *nfsPlugin) newCleanerInternal(volName string, podUID types.UID, mounter mount.Interface) (volume.Cleaner, error) { func (plugin *nfsPlugin) newCleanerInternal(volName string, podUID types.UID, mounter mount.Interface) (volume.Cleaner, error) {
return &nfs{ return &nfsCleaner{&nfs{
volName: volName, volName: volName,
server: "", mounter: mounter,
exportPath: "", pod: &api.Pod{ObjectMeta: api.ObjectMeta{UID: podUID}},
readOnly: false, plugin: plugin,
mounter: mounter, }}, nil
pod: &api.Pod{ObjectMeta: api.ObjectMeta{UID: podUID}},
plugin: plugin,
}, nil
} }
func (plugin *nfsPlugin) NewRecycler(spec *volume.Spec) (volume.Recycler, error) { func (plugin *nfsPlugin) NewRecycler(spec *volume.Spec) (volume.Recycler, error) {
return plugin.newRecyclerFunc(spec, plugin.host) return plugin.newRecyclerFunc(spec, plugin.host)
} }
func newRecycler(spec *volume.Spec, host volume.VolumeHost) (volume.Recycler, error) {
if spec.VolumeSource.HostPath != nil {
return &nfsRecycler{
name: spec.Name,
server: spec.VolumeSource.NFS.Server,
path: spec.VolumeSource.NFS.Path,
host: host,
}, nil
} else {
return &nfsRecycler{
name: spec.Name,
server: spec.PersistentVolumeSource.NFS.Server,
path: spec.PersistentVolumeSource.NFS.Path,
host: host,
}, nil
}
}
// NFS volumes represent a bare host file or directory mount of an NFS export. // NFS volumes represent a bare host file or directory mount of an NFS export.
type nfs struct { type nfs struct {
volName string volName string
pod *api.Pod pod *api.Pod
mounter mount.Interface
plugin *nfsPlugin
// decouple creating recyclers by deferring to a function. Allows for easier testing.
newRecyclerFunc func(spec *volume.Spec, host volume.VolumeHost) (volume.Recycler, error)
}
func (nfsVolume *nfs) GetPath() string {
name := nfsPluginName
return nfsVolume.plugin.host.GetPodVolumeDir(nfsVolume.pod.UID, util.EscapeQualifiedNameForDisk(name), nfsVolume.volName)
}
type nfsBuilder struct {
*nfs
server string server string
exportPath string exportPath string
readOnly bool readOnly bool
mounter mount.Interface
plugin *nfsPlugin
// decouple creating recyclers by deferring to a function. Allows for easier testing.
newRecyclerFunc func(spec *volume.Spec, host volume.VolumeHost) (volume.Recycler, error)
} }
var _ volume.Builder = &nfsBuilder{}
// SetUp attaches the disk and bind mounts to the volume path. // SetUp attaches the disk and bind mounts to the volume path.
func (nfsVolume *nfs) SetUp() error { func (b *nfsBuilder) SetUp() error {
return nfsVolume.SetUpAt(nfsVolume.GetPath()) return b.SetUpAt(b.GetPath())
} }
func (nfsVolume *nfs) SetUpAt(dir string) error { func (b *nfsBuilder) SetUpAt(dir string) error {
mountpoint, err := nfsVolume.mounter.IsMountPoint(dir) mountpoint, err := b.mounter.IsMountPoint(dir)
glog.V(4).Infof("NFS mount set up: %s %v %v", dir, mountpoint, err) glog.V(4).Infof("NFS mount set up: %s %v %v", dir, mountpoint, err)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
return err return err
...@@ -160,24 +150,24 @@ func (nfsVolume *nfs) SetUpAt(dir string) error { ...@@ -160,24 +150,24 @@ func (nfsVolume *nfs) SetUpAt(dir string) error {
return nil return nil
} }
os.MkdirAll(dir, 0750) os.MkdirAll(dir, 0750)
source := fmt.Sprintf("%s:%s", nfsVolume.server, nfsVolume.exportPath) source := fmt.Sprintf("%s:%s", b.server, b.exportPath)
options := []string{} options := []string{}
if nfsVolume.readOnly { if b.readOnly {
options = append(options, "ro") options = append(options, "ro")
} }
err = nfsVolume.mounter.Mount(source, dir, "nfs", options) err = b.mounter.Mount(source, dir, "nfs", options)
if err != nil { if err != nil {
mountpoint, mntErr := nfsVolume.mounter.IsMountPoint(dir) mountpoint, mntErr := b.mounter.IsMountPoint(dir)
if mntErr != nil { if mntErr != nil {
glog.Errorf("IsMountpoint check failed: %v", mntErr) glog.Errorf("IsMountpoint check failed: %v", mntErr)
return err return err
} }
if mountpoint { if mountpoint {
if mntErr = nfsVolume.mounter.Unmount(dir); mntErr != nil { if mntErr = b.mounter.Unmount(dir); mntErr != nil {
glog.Errorf("Failed to unmount: %v", mntErr) glog.Errorf("Failed to unmount: %v", mntErr)
return err return err
} }
mountpoint, mntErr := nfsVolume.mounter.IsMountPoint(dir) mountpoint, mntErr := b.mounter.IsMountPoint(dir)
if mntErr != nil { if mntErr != nil {
glog.Errorf("IsMountpoint check failed: %v", mntErr) glog.Errorf("IsMountpoint check failed: %v", mntErr)
return err return err
...@@ -194,17 +184,18 @@ func (nfsVolume *nfs) SetUpAt(dir string) error { ...@@ -194,17 +184,18 @@ func (nfsVolume *nfs) SetUpAt(dir string) error {
return nil return nil
} }
func (nfsVolume *nfs) GetPath() string { type nfsCleaner struct {
name := nfsPluginName *nfs
return nfsVolume.plugin.host.GetPodVolumeDir(nfsVolume.pod.UID, util.EscapeQualifiedNameForDisk(name), nfsVolume.volName)
} }
func (nfsVolume *nfs) TearDown() error { var _ volume.Cleaner = &nfsCleaner{}
return nfsVolume.TearDownAt(nfsVolume.GetPath())
func (c *nfsCleaner) TearDown() error {
return c.TearDownAt(c.GetPath())
} }
func (nfsVolume *nfs) TearDownAt(dir string) error { func (c *nfsCleaner) TearDownAt(dir string) error {
mountpoint, err := nfsVolume.mounter.IsMountPoint(dir) mountpoint, err := c.mounter.IsMountPoint(dir)
if err != nil { if err != nil {
glog.Errorf("Error checking IsMountPoint: %v", err) glog.Errorf("Error checking IsMountPoint: %v", err)
return err return err
...@@ -213,11 +204,11 @@ func (nfsVolume *nfs) TearDownAt(dir string) error { ...@@ -213,11 +204,11 @@ func (nfsVolume *nfs) TearDownAt(dir string) error {
return os.Remove(dir) return os.Remove(dir)
} }
if err := nfsVolume.mounter.Unmount(dir); err != nil { if err := c.mounter.Unmount(dir); err != nil {
glog.Errorf("Unmounting failed: %v", err) glog.Errorf("Unmounting failed: %v", err)
return err return err
} }
mountpoint, mntErr := nfsVolume.mounter.IsMountPoint(dir) mountpoint, mntErr := c.mounter.IsMountPoint(dir)
if mntErr != nil { if mntErr != nil {
glog.Errorf("IsMountpoint check failed: %v", mntErr) glog.Errorf("IsMountpoint check failed: %v", mntErr)
return mntErr return mntErr
...@@ -231,6 +222,24 @@ func (nfsVolume *nfs) TearDownAt(dir string) error { ...@@ -231,6 +222,24 @@ func (nfsVolume *nfs) TearDownAt(dir string) error {
return nil return nil
} }
func newRecycler(spec *volume.Spec, host volume.VolumeHost) (volume.Recycler, error) {
if spec.VolumeSource.HostPath != nil {
return &nfsRecycler{
name: spec.Name,
server: spec.VolumeSource.NFS.Server,
path: spec.VolumeSource.NFS.Path,
host: host,
}, nil
} else {
return &nfsRecycler{
name: spec.Name,
server: spec.PersistentVolumeSource.NFS.Server,
path: spec.PersistentVolumeSource.NFS.Path,
host: host,
}, nil
}
}
// nfsRecycler scrubs an NFS volume by running "rm -rf" on the volume in a pod. // nfsRecycler scrubs an NFS volume by running "rm -rf" on the volume in a pod.
type nfsRecycler struct { type nfsRecycler struct {
name string name string
......
...@@ -141,7 +141,7 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) { ...@@ -141,7 +141,7 @@ func doTestPlugin(t *testing.T, spec *volume.Spec) {
t.Errorf("SetUp() failed: %v", err) t.Errorf("SetUp() failed: %v", err)
} }
} }
if builder.(*nfs).readOnly { if builder.(*nfsBuilder).readOnly {
t.Errorf("The volume source should not be read-only and it is.") t.Errorf("The volume source should not be read-only and it is.")
} }
if len(fake.Log) != 1 { if len(fake.Log) != 1 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment