diff --git a/pkg/volume/metrics_du.go b/pkg/volume/metrics_du.go index 9defd0143be..5fa2cf58fe1 100644 --- a/pkg/volume/metrics_du.go +++ b/pkg/volume/metrics_du.go @@ -19,8 +19,6 @@ package volume import ( "errors" "fmt" - "os/exec" - "strings" "k8s.io/kubernetes/pkg/api/resource" "k8s.io/kubernetes/pkg/volume/util" @@ -64,17 +62,10 @@ func (md *metricsDu) GetMetrics() (*Metrics, error) { // runDu executes the "du" command and writes the results to metrics.Used func (md *metricsDu) runDu(metrics *Metrics) error { - // Uses the same niceness level as cadvisor.fs does when running du - // Uses -B 1 to always scale to a blocksize of 1 byte - out, err := exec.Command("nice", "-n", "19", "du", "-s", "-B", "1", md.path).CombinedOutput() + used, err := util.Du(md.path) if err != nil { - return fmt.Errorf("failed command 'du' on %s with error %v", md.path, err) + return err } - used, err := resource.ParseQuantity(strings.Fields(string(out))[0]) - if err != nil { - return fmt.Errorf("failed to parse 'du' output %s due to error %v", out, err) - } - used.Format = resource.BinarySI metrics.Used = used return nil } diff --git a/pkg/volume/secret/secret_test.go b/pkg/volume/secret/secret_test.go index a7c03269bf5..5515a13bf5a 100644 --- a/pkg/volume/secret/secret_test.go +++ b/pkg/volume/secret/secret_test.go @@ -21,10 +21,10 @@ import ( "io/ioutil" "os" "path" + "runtime" "strings" "testing" - "github.com/stretchr/testify/assert" "k8s.io/kubernetes/pkg/api" clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake" @@ -33,6 +33,8 @@ import ( "k8s.io/kubernetes/pkg/volume" "k8s.io/kubernetes/pkg/volume/empty_dir" "k8s.io/kubernetes/pkg/volume/util" + + "github.com/stretchr/testify/assert" ) func newTestHost(t *testing.T, clientset clientset.Interface) (string, volume.VolumeHost) { @@ -122,12 +124,16 @@ func TestPlugin(t *testing.T) { } } doTestSecretDataInVolume(volumePath, secret, t) + defer doTestCleanAndTeardown(plugin, testPodUID, testVolumeName, volumePath, t) + // Metrics only supported on linux metrics, err := builder.GetMetrics() - assert.NotEmpty(t, metrics) - assert.NoError(t, err) - - doTestCleanAndTeardown(plugin, testPodUID, testVolumeName, volumePath, t) + if runtime.GOOS == "linux" { + assert.NotEmpty(t, metrics) + assert.NoError(t, err) + } else { + t.Skipf("Volume metrics not supported on %s", runtime.GOOS) + } } // Test the case where the 'ready' file has been created and the pod volume dir diff --git a/pkg/volume/util/fs_darwin.go b/pkg/volume/util/fs_darwin.go new file mode 100644 index 00000000000..91d5ef97077 --- /dev/null +++ b/pkg/volume/util/fs_darwin.go @@ -0,0 +1,47 @@ +// +build darwin + +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package util + +import ( + "errors" + "fmt" + "os/exec" + "strings" + + "k8s.io/kubernetes/pkg/api/resource" +) + +// FSInfo linux returns (available bytes, byte capacity, error) for the filesystem that +// path resides upon. +func FsInfo(path string) (int64, int64, error) { + return 0, 0, errors.New("FsInfo not supported for this build.") +} + +func Du(path string) (*resource.Quantity, error) { + out, err := exec.Command("nice", "-n", "19", "du", "-s", path).CombinedOutput() + if err != nil { + return nil, fmt.Errorf("failed command 'du' ($ nice -n 19 du -s) on path %s with error %v", path, err) + } + used, err := resource.ParseQuantity(strings.Fields(string(out))[0]) + if err != nil { + return nil, fmt.Errorf("failed to parse 'du' output %s due to error %v", out, err) + } + used.Format = resource.BinarySI + return used, nil +} diff --git a/pkg/volume/util/fs_linux.go b/pkg/volume/util/fs_linux.go index 3713eaa6869..39d1b0399a8 100644 --- a/pkg/volume/util/fs_linux.go +++ b/pkg/volume/util/fs_linux.go @@ -19,7 +19,12 @@ limitations under the License. package util import ( + "fmt" + "os/exec" + "strings" "syscall" + + "k8s.io/kubernetes/pkg/api/resource" ) // FSInfo linux returns (available bytes, byte capacity, error) for the filesystem that @@ -39,3 +44,18 @@ func FsInfo(path string) (int64, int64, error) { return available, capacity, nil } + +func Du(path string) (*resource.Quantity, error) { + // Uses the same niceness level as cadvisor.fs does when running du + // Uses -B 1 to always scale to a blocksize of 1 byte + out, err := exec.Command("nice", "-n", "19", "du", "-s", "-B", "1", path).CombinedOutput() + if err != nil { + return nil, fmt.Errorf("failed command 'du' ($ nice -n 19 du -s -B 1) on path %s with error %v", path, err) + } + used, err := resource.ParseQuantity(strings.Fields(string(out))[0]) + if err != nil { + return nil, fmt.Errorf("failed to parse 'du' output %s due to error %v", out, err) + } + used.Format = resource.BinarySI + return used, nil +} diff --git a/pkg/volume/util/fs_unsupported.go b/pkg/volume/util/fs_unsupported.go index 3b63bc71342..0f9c5b9b928 100644 --- a/pkg/volume/util/fs_unsupported.go +++ b/pkg/volume/util/fs_unsupported.go @@ -1,4 +1,4 @@ -// +build !linux +// +build !linux,!darwin /* Copyright 2014 The Kubernetes Authors All rights reserved. @@ -20,9 +20,16 @@ package util import ( "errors" + "fmt" + + "k8s.io/kubernetes/pkg/api/resource" ) // FSInfo unsupported returns 0 values for available and capacity and an error. func FsInfo(path string) (int64, int64, error) { return 0, 0, errors.New("FsInfo not supported for this build.") } + +func Du(path string) (*resource.Quantity, error) { + return nil, fmt.Errorf("Du not support for this build.") +}