Recycling HostPath and NFS impls
This commit is contained in:
@@ -19,25 +19,33 @@ package nfs
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/types"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/mount"
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/volume"
|
||||
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
// This is the primary entrypoint for volume plugins.
|
||||
// Tests covering recycling should not use this func but instead
|
||||
// use their own array of plugins w/ a custom recyclerFunc as appropriate
|
||||
func ProbeVolumePlugins() []volume.VolumePlugin {
|
||||
return []volume.VolumePlugin{&nfsPlugin{nil}}
|
||||
return []volume.VolumePlugin{&nfsPlugin{nil, newRecycler}}
|
||||
}
|
||||
|
||||
type nfsPlugin struct {
|
||||
host volume.VolumeHost
|
||||
// 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.VolumePlugin = &nfsPlugin{}
|
||||
var _ volume.PersistentVolumePlugin = &nfsPlugin{}
|
||||
var _ volume.RecyclableVolumePlugin = &nfsPlugin{}
|
||||
|
||||
const (
|
||||
nfsPluginName = "kubernetes.io/nfs"
|
||||
@@ -103,6 +111,28 @@ func (plugin *nfsPlugin) newCleanerInternal(volName string, podUID types.UID, mo
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (plugin *nfsPlugin) NewRecycler(spec *volume.Spec) (volume.Recycler, error) {
|
||||
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.
|
||||
type nfs struct {
|
||||
volName string
|
||||
@@ -112,6 +142,8 @@ type nfs struct {
|
||||
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)
|
||||
}
|
||||
|
||||
// SetUp attaches the disk and bind mounts to the volume path.
|
||||
@@ -199,3 +231,66 @@ func (nfsVolume *nfs) TearDownAt(dir string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// nfsRecycler scrubs an NFS volume by running "rm -rf" on the volume in a pod.
|
||||
type nfsRecycler struct {
|
||||
name string
|
||||
server string
|
||||
path string
|
||||
host volume.VolumeHost
|
||||
}
|
||||
|
||||
func (r *nfsRecycler) GetPath() string {
|
||||
return r.path
|
||||
}
|
||||
|
||||
// Recycler provides methods to reclaim the volume resource.
|
||||
// A NFS volume is recycled by scheduling a pod to run "rm -rf" on the contents of the volume.
|
||||
// Recycle blocks until the pod has completed or any error occurs.
|
||||
// The scrubber pod's is expected to succeed within 5 minutes else an error will be returned
|
||||
func (r *nfsRecycler) Recycle() error {
|
||||
timeout := int64(300 * time.Second) // 5 minutes
|
||||
pod := &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
GenerateName: "pv-scrubber-" + util.ShortenString(r.name, 44) + "-",
|
||||
Namespace: api.NamespaceDefault,
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
ActiveDeadlineSeconds: &timeout,
|
||||
RestartPolicy: api.RestartPolicyNever,
|
||||
Volumes: []api.Volume{
|
||||
{
|
||||
Name: "vol",
|
||||
VolumeSource: api.VolumeSource{
|
||||
NFS: &api.NFSVolumeSource{
|
||||
Server: r.server,
|
||||
Path: r.path,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Name: "scrubber",
|
||||
Image: "busybox",
|
||||
// delete the contents of the volume, but not the directory itself
|
||||
Command: []string{"/bin/sh"},
|
||||
// the scrubber:
|
||||
// 1. validates the /scrub directory exists
|
||||
// 2. creates a text file to be scrubbed
|
||||
// 3. performs rm -rf on the directory
|
||||
// 4. tests to see if the directory is empty
|
||||
// the pod fails if the error code is returned
|
||||
Args: []string{"-c", "test -e /scrub && echo $(date) > /scrub/trash.txt && rm -rf /scrub/* && test -z \"$(ls -A /scrub)\" || exit 1"},
|
||||
VolumeMounts: []api.VolumeMount{
|
||||
{
|
||||
Name: "vol",
|
||||
MountPath: "/scrub",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
return volume.ScrubPodVolumeAndWatchUntilCompletion(pod, r.host.GetKubeClient())
|
||||
}
|
||||
|
@@ -60,6 +60,47 @@ func TestGetAccessModes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecycler(t *testing.T) {
|
||||
plugMgr := volume.VolumePluginMgr{}
|
||||
plugMgr.InitPlugins([]volume.VolumePlugin{&nfsPlugin{newRecyclerFunc: newMockRecycler}}, volume.NewFakeVolumeHost("/tmp/fake", nil, nil))
|
||||
|
||||
spec := &volume.Spec{PersistentVolumeSource: api.PersistentVolumeSource{NFS: &api.NFSVolumeSource{Path: "/foo"}}}
|
||||
plug, err := plugMgr.FindRecyclablePluginBySpec(spec)
|
||||
if err != nil {
|
||||
t.Errorf("Can't find the plugin by name")
|
||||
}
|
||||
recycler, err := plug.NewRecycler(spec)
|
||||
if err != nil {
|
||||
t.Error("Failed to make a new Recyler: %v", err)
|
||||
}
|
||||
if recycler.GetPath() != spec.PersistentVolumeSource.NFS.Path {
|
||||
t.Errorf("Expected %s but got %s", spec.PersistentVolumeSource.NFS.Path, recycler.GetPath())
|
||||
}
|
||||
if err := recycler.Recycle(); err != nil {
|
||||
t.Errorf("Mock Recycler expected to return nil but got %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func newMockRecycler(spec *volume.Spec, host volume.VolumeHost) (volume.Recycler, error) {
|
||||
return &mockRecycler{
|
||||
path: spec.PersistentVolumeSource.NFS.Path,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type mockRecycler struct {
|
||||
path string
|
||||
host volume.VolumeHost
|
||||
}
|
||||
|
||||
func (r *mockRecycler) GetPath() string {
|
||||
return r.path
|
||||
}
|
||||
|
||||
func (r *mockRecycler) Recycle() error {
|
||||
// return nil means recycle passed
|
||||
return nil
|
||||
}
|
||||
|
||||
func contains(modes []api.PersistentVolumeAccessMode, mode api.PersistentVolumeAccessMode) bool {
|
||||
for _, m := range modes {
|
||||
if m == mode {
|
||||
|
Reference in New Issue
Block a user