Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated cherry pick of #23446 #23457

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 12 additions & 14 deletions pkg/volume/host_path/host_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,21 @@ func (plugin *hostPathPlugin) NewBuilder(spec *volume.Spec, pod *api.Pod, _ volu
if spec.Volume != nil && spec.Volume.HostPath != nil {
path := spec.Volume.HostPath.Path
return &hostPathBuilder{
hostPath: &hostPath{path: path, MetricsProvider: volume.NewMetricsDu(path)},
hostPath: &hostPath{path: path},
readOnly: false,
}, nil
} else {
path := spec.PersistentVolume.Spec.HostPath.Path
return &hostPathBuilder{
hostPath: &hostPath{path: path, MetricsProvider: volume.NewMetricsDu(path)},
hostPath: &hostPath{path: path},
readOnly: spec.ReadOnly,
}, nil
}
}

func (plugin *hostPathPlugin) NewCleaner(volName string, podUID types.UID) (volume.Cleaner, error) {
return &hostPathCleaner{&hostPath{
path: "",
MetricsProvider: volume.NewMetricsDu(""),
path: "",
}}, nil
}

Expand All @@ -137,12 +136,11 @@ func newRecycler(spec *volume.Spec, host volume.VolumeHost, config volume.Volume
}
path := spec.PersistentVolume.Spec.HostPath.Path
return &hostPathRecycler{
name: spec.Name(),
path: path,
host: host,
config: config,
timeout: volume.CalculateTimeoutForVolume(config.RecyclerMinimumTimeout, config.RecyclerTimeoutIncrement, spec.PersistentVolume),
MetricsProvider: volume.NewMetricsDu(path),
name: spec.Name(),
path: path,
host: host,
config: config,
timeout: volume.CalculateTimeoutForVolume(config.RecyclerMinimumTimeout, config.RecyclerTimeoutIncrement, spec.PersistentVolume),
}, nil
}

Expand All @@ -151,7 +149,7 @@ func newDeleter(spec *volume.Spec, host volume.VolumeHost) (volume.Deleter, erro
return nil, fmt.Errorf("spec.PersistentVolumeSource.HostPath is nil")
}
path := spec.PersistentVolume.Spec.HostPath.Path
return &hostPathDeleter{spec.Name(), path, host, volume.NewMetricsDu(path)}, nil
return &hostPathDeleter{name: spec.Name(), path: path, host: host}, nil
}

func newProvisioner(options volume.VolumeOptions, host volume.VolumeHost) (volume.Provisioner, error) {
Expand All @@ -162,7 +160,7 @@ func newProvisioner(options volume.VolumeOptions, host volume.VolumeHost) (volum
// The direct at the specified path will be directly exposed to the container.
type hostPath struct {
path string
volume.MetricsProvider
volume.MetricsNil
}

func (hp *hostPath) GetPath() string {
Expand Down Expand Up @@ -222,7 +220,7 @@ type hostPathRecycler struct {
host volume.VolumeHost
config volume.VolumeConfig
timeout int64
volume.MetricsProvider
volume.MetricsNil
}

func (r *hostPathRecycler) GetPath() string {
Expand Down Expand Up @@ -291,7 +289,7 @@ type hostPathDeleter struct {
name string
path string
host volume.VolumeHost
volume.MetricsProvider
volume.MetricsNil
}

func (r *hostPathDeleter) GetPath() string {
Expand Down
47 changes: 0 additions & 47 deletions pkg/volume/host_path/host_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package host_path

import (
"fmt"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -272,49 +271,3 @@ func TestPersistentClaimReadOnlyFlag(t *testing.T) {
t.Errorf("Expected true for builder.IsReadOnly")
}
}

// 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(), "host_path_test")
if err != nil {
t.Fatalf("Can't make a tmp dir: %v", err)
}
defer os.RemoveAll(tmpDir)

plugMgr := volume.VolumePluginMgr{}
plugMgr.InitPlugins(ProbeVolumePlugins(volume.VolumeConfig{}), volumetest.NewFakeVolumeHost(tmpDir, nil, nil))

plug, err := plugMgr.FindPluginByName("kubernetes.io/host-path")
if err != nil {
t.Errorf("Can't find the plugin by name")
}
spec := &api.Volume{
Name: "vol1",
VolumeSource: api.VolumeSource{HostPath: &api.HostPathVolumeSource{Path: tmpDir}},
}
pod := &api.Pod{ObjectMeta: api.ObjectMeta{UID: types.UID("poduid")}}
builder, err := plug.NewBuilder(volume.NewSpecFromVolume(spec), pod, volume.VolumeOptions{})
if err != nil {
t.Errorf("Failed to make a new Builder: %v", err)
}

expectedEmptyDirUsage, err := volumetest.FindEmptyDirectoryUsageOnTmpfs()
if err != nil {
t.Errorf("Unexpected error finding expected empty directory usage on tmpfs: %v", err)
}

metrics, err := builder.GetMetrics()
if err != nil {
t.Errorf("Unexpected error when calling GetMetrics %v", err)
}
if e, a := expectedEmptyDirUsage.Value(), metrics.Used.Value(); e != a {
t.Errorf("Unexpected value for empty directory; expected %v, got %v", e, a)
}
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")
}
}