mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 02:09:56 +00:00
Merge pull request #11739 from jiangyaoguo/new-builder-cleaner-for-glusterfs
Refector glusterfs volume to seperate builder and cleaner
This commit is contained in:
commit
94bed5a426
@ -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) {
|
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)
|
source := plugin.getGlusterVolumeSource(spec)
|
||||||
return &glusterfs{
|
return &glusterfsBuilder{
|
||||||
|
glusterfs: &glusterfs{
|
||||||
volName: spec.Name,
|
volName: spec.Name,
|
||||||
|
mounter: mounter,
|
||||||
|
pod: pod,
|
||||||
|
plugin: plugin,
|
||||||
|
},
|
||||||
hosts: ep,
|
hosts: ep,
|
||||||
path: source.Path,
|
path: source.Path,
|
||||||
readonly: source.ReadOnly,
|
readonly: source.ReadOnly,
|
||||||
mounter: mounter,
|
exe: exe}, nil
|
||||||
exe: exe,
|
|
||||||
pod: pod,
|
|
||||||
plugin: plugin,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (plugin *glusterfsPlugin) NewCleaner(volName string, podUID types.UID, mounter mount.Interface) (volume.Cleaner, error) {
|
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) {
|
func (plugin *glusterfsPlugin) newCleanerInternal(volName string, podUID types.UID, mounter mount.Interface) (volume.Cleaner, error) {
|
||||||
return &glusterfs{
|
return &glusterfsCleaner{&glusterfs{
|
||||||
volName: volName,
|
volName: volName,
|
||||||
mounter: mounter,
|
mounter: mounter,
|
||||||
pod: &api.Pod{ObjectMeta: api.ObjectMeta{UID: podUID}},
|
pod: &api.Pod{ObjectMeta: api.ObjectMeta{UID: podUID}},
|
||||||
plugin: plugin,
|
plugin: plugin,
|
||||||
}, nil
|
}}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Glusterfs volumes represent a bare host file or directory mount of an Glusterfs export.
|
// Glusterfs volumes represent a bare host file or directory mount of an Glusterfs export.
|
||||||
type glusterfs struct {
|
type glusterfs struct {
|
||||||
volName string
|
volName string
|
||||||
pod *api.Pod
|
pod *api.Pod
|
||||||
hosts *api.Endpoints
|
|
||||||
path string
|
|
||||||
readonly bool
|
|
||||||
mounter mount.Interface
|
mounter mount.Interface
|
||||||
exe exec.Interface
|
|
||||||
plugin *glusterfsPlugin
|
plugin *glusterfsPlugin
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetUp attaches the disk and bind mounts to the volume path.
|
type glusterfsBuilder struct {
|
||||||
func (glusterfsVolume *glusterfs) SetUp() error {
|
*glusterfs
|
||||||
return glusterfsVolume.SetUpAt(glusterfsVolume.GetPath())
|
hosts *api.Endpoints
|
||||||
|
path string
|
||||||
|
readonly bool
|
||||||
|
exe exec.Interface
|
||||||
}
|
}
|
||||||
|
|
||||||
func (glusterfsVolume *glusterfs) SetUpAt(dir string) error {
|
var _ volume.Builder = &glusterfsBuilder{}
|
||||||
mountpoint, err := glusterfsVolume.mounter.IsMountPoint(dir)
|
|
||||||
|
// SetUp attaches the disk and bind mounts to the volume path.
|
||||||
|
func (b *glusterfsBuilder) SetUp() error {
|
||||||
|
return b.SetUpAt(b.GetPath())
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
glog.V(4).Infof("Glusterfs: 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
|
||||||
@ -140,13 +147,14 @@ func (glusterfsVolume *glusterfs) SetUpAt(dir string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
os.MkdirAll(dir, 0750)
|
os.MkdirAll(dir, 0750)
|
||||||
err = glusterfsVolume.setUpAtInternal(dir)
|
err = b.setUpAtInternal(dir)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup upon failure.
|
// Cleanup upon failure.
|
||||||
glusterfsVolume.cleanup(dir)
|
c := &glusterfsCleaner{b.glusterfs}
|
||||||
|
c.cleanup(dir)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,16 +163,22 @@ func (glusterfsVolume *glusterfs) GetPath() string {
|
|||||||
return glusterfsVolume.plugin.host.GetPodVolumeDir(glusterfsVolume.pod.UID, util.EscapeQualifiedNameForDisk(name), glusterfsVolume.volName)
|
return glusterfsVolume.plugin.host.GetPodVolumeDir(glusterfsVolume.pod.UID, util.EscapeQualifiedNameForDisk(name), glusterfsVolume.volName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (glusterfsVolume *glusterfs) TearDown() error {
|
type glusterfsCleaner struct {
|
||||||
return glusterfsVolume.TearDownAt(glusterfsVolume.GetPath())
|
*glusterfs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (glusterfsVolume *glusterfs) TearDownAt(dir string) error {
|
var _ volume.Cleaner = &glusterfsCleaner{}
|
||||||
return glusterfsVolume.cleanup(dir)
|
|
||||||
|
func (c *glusterfsCleaner) TearDown() error {
|
||||||
|
return c.TearDownAt(c.GetPath())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (glusterfsVolume *glusterfs) cleanup(dir string) error {
|
func (c *glusterfsCleaner) TearDownAt(dir string) error {
|
||||||
mountpoint, err := glusterfsVolume.mounter.IsMountPoint(dir)
|
return c.cleanup(dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *glusterfsCleaner) cleanup(dir string) error {
|
||||||
|
mountpoint, err := c.mounter.IsMountPoint(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorf("Glusterfs: Error checking IsMountPoint: %v", err)
|
glog.Errorf("Glusterfs: Error checking IsMountPoint: %v", err)
|
||||||
return err
|
return err
|
||||||
@ -173,11 +187,11 @@ func (glusterfsVolume *glusterfs) cleanup(dir string) error {
|
|||||||
return os.RemoveAll(dir)
|
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)
|
glog.Errorf("Glusterfs: Unmounting failed: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
mountpoint, mntErr := glusterfsVolume.mounter.IsMountPoint(dir)
|
mountpoint, mntErr := c.mounter.IsMountPoint(dir)
|
||||||
if mntErr != nil {
|
if mntErr != nil {
|
||||||
glog.Errorf("Glusterfs: IsMountpoint check failed: %v", mntErr)
|
glog.Errorf("Glusterfs: IsMountpoint check failed: %v", mntErr)
|
||||||
return mntErr
|
return mntErr
|
||||||
@ -191,21 +205,21 @@ func (glusterfsVolume *glusterfs) cleanup(dir string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (glusterfsVolume *glusterfs) setUpAtInternal(dir string) error {
|
func (b *glusterfsBuilder) setUpAtInternal(dir string) error {
|
||||||
var errs error
|
var errs error
|
||||||
|
|
||||||
options := []string{}
|
options := []string{}
|
||||||
if glusterfsVolume.readonly {
|
if b.readonly {
|
||||||
options = append(options, "ro")
|
options = append(options, "ro")
|
||||||
}
|
}
|
||||||
|
|
||||||
l := len(glusterfsVolume.hosts.Subsets)
|
l := len(b.hosts.Subsets)
|
||||||
// Avoid mount storm, pick a host randomly.
|
// Avoid mount storm, pick a host randomly.
|
||||||
start := rand.Int() % l
|
start := rand.Int() % l
|
||||||
// Iterate all hosts until mount succeeds.
|
// Iterate all hosts until mount succeeds.
|
||||||
for i := start; i < start+l; i++ {
|
for i := start; i < start+l; i++ {
|
||||||
hostIP := glusterfsVolume.hosts.Subsets[i%l].Addresses[0].IP
|
hostIP := b.hosts.Subsets[i%l].Addresses[0].IP
|
||||||
errs = glusterfsVolume.mounter.Mount(hostIP+":"+glusterfsVolume.path, dir, "glusterfs", options)
|
errs = b.mounter.Mount(hostIP+":"+b.path, dir, "glusterfs", options)
|
||||||
if errs == nil {
|
if errs == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user