metrics_statfs.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package volume
  14. import (
  15. "errors"
  16. "fmt"
  17. "k8s.io/kubernetes/pkg/api/resource"
  18. "k8s.io/kubernetes/pkg/volume/util"
  19. )
  20. var _ MetricsProvider = &metricsStatFS{}
  21. // metricsStatFS represents a MetricsProvider that calculates the used and available
  22. // Volume space by stat'ing and gathering filesystem info for the Volume path.
  23. type metricsStatFS struct {
  24. // the directory path the volume is mounted to.
  25. path string
  26. }
  27. // NewMetricsStatfs creates a new metricsStatFS with the Volume path.
  28. func NewMetricsStatFS(path string) MetricsProvider {
  29. return &metricsStatFS{path}
  30. }
  31. // See MetricsProvider.GetMetrics
  32. // GetMetrics calculates the volume usage and device free space by executing "du"
  33. // and gathering filesystem info for the Volume path.
  34. func (md *metricsStatFS) GetMetrics() (*Metrics, error) {
  35. metrics := &Metrics{}
  36. if md.path == "" {
  37. return metrics, errors.New("no path defined for disk usage metrics.")
  38. }
  39. err := md.getFsInfo(metrics)
  40. if err != nil {
  41. return metrics, err
  42. }
  43. return metrics, nil
  44. }
  45. // getFsInfo writes metrics.Capacity, metrics.Used and metrics.Available from the filesystem info
  46. func (md *metricsStatFS) getFsInfo(metrics *Metrics) error {
  47. available, capacity, usage, err := util.FsInfo(md.path)
  48. if err != nil {
  49. return fmt.Errorf("Failed to get FsInfo due to error %v", err)
  50. }
  51. metrics.Available = resource.NewQuantity(available, resource.BinarySI)
  52. metrics.Capacity = resource.NewQuantity(capacity, resource.BinarySI)
  53. metrics.Used = resource.NewQuantity(usage, resource.BinarySI)
  54. return nil
  55. }