Volume Metrics Interface and base implementation.
- Add volume.MetricsProvider function to Volume interface. - Add volume.MetricsDu for providing metrics via executing "du". - Add volulme.MetricsNil for unsupported Volumes.
This commit is contained in:
@@ -79,13 +79,14 @@ func (plugin *emptyDirPlugin) newBuilderInternal(spec *volume.Spec, pod *api.Pod
|
||||
medium = spec.Volume.EmptyDir.Medium
|
||||
}
|
||||
return &emptyDir{
|
||||
pod: pod,
|
||||
volName: spec.Name(),
|
||||
medium: medium,
|
||||
mounter: mounter,
|
||||
mountDetector: mountDetector,
|
||||
plugin: plugin,
|
||||
rootContext: opts.RootContext,
|
||||
pod: pod,
|
||||
volName: spec.Name(),
|
||||
medium: medium,
|
||||
mounter: mounter,
|
||||
mountDetector: mountDetector,
|
||||
plugin: plugin,
|
||||
rootContext: opts.RootContext,
|
||||
MetricsProvider: volume.NewMetricsDu(GetPath(pod.UID, spec.Name(), plugin.host)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -96,12 +97,13 @@ func (plugin *emptyDirPlugin) NewCleaner(volName string, podUID types.UID) (volu
|
||||
|
||||
func (plugin *emptyDirPlugin) newCleanerInternal(volName string, podUID types.UID, mounter mount.Interface, mountDetector mountDetector) (volume.Cleaner, error) {
|
||||
ed := &emptyDir{
|
||||
pod: &api.Pod{ObjectMeta: api.ObjectMeta{UID: podUID}},
|
||||
volName: volName,
|
||||
medium: api.StorageMediumDefault, // might be changed later
|
||||
mounter: mounter,
|
||||
mountDetector: mountDetector,
|
||||
plugin: plugin,
|
||||
pod: &api.Pod{ObjectMeta: api.ObjectMeta{UID: podUID}},
|
||||
volName: volName,
|
||||
medium: api.StorageMediumDefault, // might be changed later
|
||||
mounter: mounter,
|
||||
mountDetector: mountDetector,
|
||||
plugin: plugin,
|
||||
MetricsProvider: volume.NewMetricsDu(GetPath(podUID, volName, plugin.host)),
|
||||
}
|
||||
return ed, nil
|
||||
}
|
||||
@@ -133,6 +135,7 @@ type emptyDir struct {
|
||||
mountDetector mountDetector
|
||||
plugin *emptyDirPlugin
|
||||
rootContext string
|
||||
volume.MetricsProvider
|
||||
}
|
||||
|
||||
func (ed *emptyDir) GetAttributes() volume.Attributes {
|
||||
@@ -265,8 +268,12 @@ func (ed *emptyDir) setupDir(dir string) error {
|
||||
}
|
||||
|
||||
func (ed *emptyDir) GetPath() string {
|
||||
return GetPath(ed.pod.UID, ed.volName, ed.plugin.host)
|
||||
}
|
||||
|
||||
func GetPath(uid types.UID, volName string, host volume.VolumeHost) string {
|
||||
name := emptyDirPluginName
|
||||
return ed.plugin.host.GetPodVolumeDir(ed.pod.UID, util.EscapeQualifiedNameForDisk(name), ed.volName)
|
||||
return host.GetPodVolumeDir(uid, util.EscapeQualifiedNameForDisk(name), volName)
|
||||
}
|
||||
|
||||
// TearDown simply discards everything in the directory.
|
||||
|
@@ -274,3 +274,42 @@ func TestPluginBackCompat(t *testing.T) {
|
||||
t.Errorf("Got unexpected path: %s", volPath)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMetrics tests that MetricProvider methods return sane values.
|
||||
func TestMetrics(t *testing.T) {
|
||||
// Create an empty temp directory for the volume
|
||||
tmpDir, err := ioutil.TempDir(os.TempDir(), "empty_dir_test")
|
||||
if err != nil {
|
||||
t.Fatalf("Can't make a tmp dir: %v", err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
plug := makePluginUnderTest(t, "kubernetes.io/empty-dir", tmpDir)
|
||||
|
||||
spec := &api.Volume{
|
||||
Name: "vol1",
|
||||
}
|
||||
pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: types.UID("poduid")}}
|
||||
builder, err := plug.NewBuilder(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{RootContext: ""})
|
||||
if err != nil {
|
||||
t.Errorf("Failed to make a new Builder: %v", err)
|
||||
}
|
||||
|
||||
// Need to create the subdirectory
|
||||
os.MkdirAll(builder.GetPath(), 0755)
|
||||
|
||||
// TODO(pwittroc): Move this into a reusable testing utility
|
||||
metrics, err := builder.GetMetrics()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error when calling GetMetrics %v", err)
|
||||
}
|
||||
if metrics.Used.Value() != 4096 {
|
||||
t.Errorf("Expected Used %d to be 4096", metrics.Used.Value())
|
||||
}
|
||||
if metrics.Capacity.Value() <= 0 {
|
||||
t.Errorf("Expected Capacity to be greater than 0")
|
||||
}
|
||||
if metrics.Available.Value() <= 0 {
|
||||
t.Errorf("Expected Available to be greater than 0")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user