...

Source file src/k8s.io/kubernetes/pkg/volume/metrics_statfs.go

Documentation: k8s.io/kubernetes/pkg/volume

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package volume
    18  
    19  import (
    20  	"time"
    21  
    22  	"k8s.io/apimachinery/pkg/api/resource"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	servermetrics "k8s.io/kubernetes/pkg/kubelet/server/metrics"
    25  	"k8s.io/kubernetes/pkg/volume/util/fs"
    26  )
    27  
    28  var _ MetricsProvider = &metricsStatFS{}
    29  
    30  // metricsStatFS represents a MetricsProvider that calculates the used and available
    31  // Volume space by stat'ing and gathering filesystem info for the Volume path.
    32  type metricsStatFS struct {
    33  	// the directory path the volume is mounted to.
    34  	path string
    35  }
    36  
    37  // NewMetricsStatfs creates a new metricsStatFS with the Volume path.
    38  func NewMetricsStatFS(path string) MetricsProvider {
    39  	return &metricsStatFS{path}
    40  }
    41  
    42  // See MetricsProvider.GetMetrics
    43  // GetMetrics calculates the volume usage and device free space by executing "du"
    44  // and gathering filesystem info for the Volume path.
    45  func (md *metricsStatFS) GetMetrics() (*Metrics, error) {
    46  	startTime := time.Now()
    47  	defer servermetrics.CollectVolumeStatCalDuration("statfs", startTime)
    48  
    49  	metrics := &Metrics{Time: metav1.Now()}
    50  	if md.path == "" {
    51  		return metrics, NewNoPathDefinedError()
    52  	}
    53  
    54  	err := md.getFsInfo(metrics)
    55  	if err != nil {
    56  		return metrics, err
    57  	}
    58  
    59  	return metrics, nil
    60  }
    61  
    62  // getFsInfo writes metrics.Capacity, metrics.Used and metrics.Available from the filesystem info
    63  func (md *metricsStatFS) getFsInfo(metrics *Metrics) error {
    64  	available, capacity, usage, inodes, inodesFree, inodesUsed, err := fs.Info(md.path)
    65  	if err != nil {
    66  		return NewFsInfoFailedError(err)
    67  	}
    68  	metrics.Available = resource.NewQuantity(available, resource.BinarySI)
    69  	metrics.Capacity = resource.NewQuantity(capacity, resource.BinarySI)
    70  	metrics.Used = resource.NewQuantity(usage, resource.BinarySI)
    71  	metrics.Inodes = resource.NewQuantity(inodes, resource.BinarySI)
    72  	metrics.InodesFree = resource.NewQuantity(inodesFree, resource.BinarySI)
    73  	metrics.InodesUsed = resource.NewQuantity(inodesUsed, resource.BinarySI)
    74  	return nil
    75  }
    76  

View as plain text