Merge pull request #11739 from jiangyaoguo/new-builder-cleaner-for-glusterfs

Refector glusterfs volume to seperate builder and cleaner
This commit is contained in:
Vish Kannan 2015-07-23 13:17:22 -07:00
commit 94bed5a426

View File

@ -87,16 +87,17 @@ func (plugin *glusterfsPlugin) getGlusterVolumeSource(spec *volume.Spec) *api.Gl
func (plugin *glusterfsPlugin) newBuilderInternal(spec *volume.Spec, ep *api.Endpoints, pod *api.Pod, mounter mount.Interface, exe exec.Interface) (volume.Builder, error) {
source := plugin.getGlusterVolumeSource(spec)
return &glusterfs{
volName: spec.Name,
return &glusterfsBuilder{
glusterfs: &glusterfs{
volName: spec.Name,
mounter: mounter,
pod: pod,
plugin: plugin,
},
hosts: ep,
path: source.Path,
readonly: source.ReadOnly,
mounter: mounter,
exe: exe,
pod: pod,
plugin: plugin,
}, nil
exe: exe}, nil
}
func (plugin *glusterfsPlugin) NewCleaner(volName string, podUID types.UID, mounter mount.Interface) (volume.Cleaner, error) {
@ -104,33 +105,39 @@ func (plugin *glusterfsPlugin) NewCleaner(volName string, podUID types.UID, moun
}
func (plugin *glusterfsPlugin) newCleanerInternal(volName string, podUID types.UID, mounter mount.Interface) (volume.Cleaner, error) {
return &glusterfs{
return &glusterfsCleaner{&glusterfs{
volName: volName,
mounter: mounter,
pod: &api.Pod{ObjectMeta: api.ObjectMeta{UID: podUID}},
plugin: plugin,
}, nil
}}, nil
}
// Glusterfs volumes represent a bare host file or directory mount of an Glusterfs export.
type glusterfs struct {
volName string
pod *api.Pod
volName string
pod *api.Pod
mounter mount.Interface
plugin *glusterfsPlugin
}
type glusterfsBuilder struct {
*glusterfs
hosts *api.Endpoints
path string
readonly bool
mounter mount.Interface
exe exec.Interface
plugin *glusterfsPlugin
}
var _ volume.Builder = &glusterfsBuilder{}
// SetUp attaches the disk and bind mounts to the volume path.
func (glusterfsVolume *glusterfs) SetUp() error {
return glusterfsVolume.SetUpAt(glusterfsVolume.GetPath())
func (b *glusterfsBuilder) SetUp() error {
return b.SetUpAt(b.GetPath())
}
func (glusterfsVolume *glusterfs) SetUpAt(dir string) error {
mountpoint, err := glusterfsVolume.mounter.IsMountPoint(dir)
func (b *glusterfsBuilder) SetUpAt(dir string) error {
mountpoint, err := b.mounter.IsMountPoint(dir)
glog.V(4).Infof("Glusterfs: mount set up: %s %v %v", dir, mountpoint, err)
if err != nil && !os.IsNotExist(err) {
return err
@ -140,13 +147,14 @@ func (glusterfsVolume *glusterfs) SetUpAt(dir string) error {
}
os.MkdirAll(dir, 0750)
err = glusterfsVolume.setUpAtInternal(dir)
err = b.setUpAtInternal(dir)
if err == nil {
return nil
}
// Cleanup upon failure.
glusterfsVolume.cleanup(dir)
c := &glusterfsCleaner{b.glusterfs}
c.cleanup(dir)
return err
}
@ -155,16 +163,22 @@ func (glusterfsVolume *glusterfs) GetPath() string {
return glusterfsVolume.plugin.host.GetPodVolumeDir(glusterfsVolume.pod.UID, util.EscapeQualifiedNameForDisk(name), glusterfsVolume.volName)
}
func (glusterfsVolume *glusterfs) TearDown() error {
return glusterfsVolume.TearDownAt(glusterfsVolume.GetPath())
type glusterfsCleaner struct {
*glusterfs
}
func (glusterfsVolume *glusterfs) TearDownAt(dir string) error {
return glusterfsVolume.cleanup(dir)
var _ volume.Cleaner = &glusterfsCleaner{}
func (c *glusterfsCleaner) TearDown() error {
return c.TearDownAt(c.GetPath())
}
func (glusterfsVolume *glusterfs) cleanup(dir string) error {
mountpoint, err := glusterfsVolume.mounter.IsMountPoint(dir)
func (c *glusterfsCleaner) TearDownAt(dir string) error {
return c.cleanup(dir)
}
func (c *glusterfsCleaner) cleanup(dir string) error {
mountpoint, err := c.mounter.IsMountPoint(dir)
if err != nil {
glog.Errorf("Glusterfs: Error checking IsMountPoint: %v", err)
return err
@ -173,11 +187,11 @@ func (glusterfsVolume *glusterfs) cleanup(dir string) error {
return os.RemoveAll(dir)
}
if err := glusterfsVolume.mounter.Unmount(dir); err != nil {
if err := c.mounter.Unmount(dir); err != nil {
glog.Errorf("Glusterfs: Unmounting failed: %v", err)
return err
}
mountpoint, mntErr := glusterfsVolume.mounter.IsMountPoint(dir)
mountpoint, mntErr := c.mounter.IsMountPoint(dir)
if mntErr != nil {
glog.Errorf("Glusterfs: IsMountpoint check failed: %v", mntErr)
return mntErr
@ -191,21 +205,21 @@ func (glusterfsVolume *glusterfs) cleanup(dir string) error {
return nil
}
func (glusterfsVolume *glusterfs) setUpAtInternal(dir string) error {
func (b *glusterfsBuilder) setUpAtInternal(dir string) error {
var errs error
options := []string{}
if glusterfsVolume.readonly {
if b.readonly {
options = append(options, "ro")
}
l := len(glusterfsVolume.hosts.Subsets)
l := len(b.hosts.Subsets)
// Avoid mount storm, pick a host randomly.
start := rand.Int() % l
// Iterate all hosts until mount succeeds.
for i := start; i < start+l; i++ {
hostIP := glusterfsVolume.hosts.Subsets[i%l].Addresses[0].IP
errs = glusterfsVolume.mounter.Mount(hostIP+":"+glusterfsVolume.path, dir, "glusterfs", options)
hostIP := b.hosts.Subsets[i%l].Addresses[0].IP
errs = b.mounter.Mount(hostIP+":"+b.path, dir, "glusterfs", options)
if errs == nil {
return nil
}