...

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

Documentation: k8s.io/kubernetes/pkg/volume

     1  /*
     2  Copyright 2021 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  	"fmt"
    21  	"io"
    22  	"os"
    23  	"runtime"
    24  	"time"
    25  
    26  	"k8s.io/apimachinery/pkg/api/resource"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  	servermetrics "k8s.io/kubernetes/pkg/kubelet/server/metrics"
    29  )
    30  
    31  var _ MetricsProvider = &metricsBlock{}
    32  
    33  // metricsBlock represents a MetricsProvider that detects the size of the
    34  // BlockMode Volume.
    35  type metricsBlock struct {
    36  	// the device node where the volume is attached to.
    37  	device string
    38  }
    39  
    40  // NewMetricsStatfs creates a new metricsBlock with the device node of the
    41  // Volume.
    42  func NewMetricsBlock(device string) MetricsProvider {
    43  	return &metricsBlock{device}
    44  }
    45  
    46  // See MetricsProvider.GetMetrics
    47  // GetMetrics detects the size of the BlockMode volume for the device node
    48  // where the Volume is attached.
    49  //
    50  // Note that only the capacity of the device can be detected with standard
    51  // tools. Storage systems may have more information that they can provide by
    52  // going through specialized APIs.
    53  func (mb *metricsBlock) GetMetrics() (*Metrics, error) {
    54  	startTime := time.Now()
    55  	defer servermetrics.CollectVolumeStatCalDuration("block", startTime)
    56  
    57  	// TODO: Windows does not yet support VolumeMode=Block
    58  	if runtime.GOOS == "windows" {
    59  		return nil, NewNotImplementedError("Windows does not support Block volumes")
    60  	}
    61  
    62  	metrics := &Metrics{Time: metav1.Now()}
    63  	if mb.device == "" {
    64  		return metrics, NewNoPathDefinedError()
    65  	}
    66  
    67  	err := mb.getBlockInfo(metrics)
    68  	if err != nil {
    69  		return metrics, err
    70  	}
    71  
    72  	return metrics, nil
    73  }
    74  
    75  // getBlockInfo fetches metrics.Capacity by opening the device and seeking to
    76  // the end.
    77  func (mb *metricsBlock) getBlockInfo(metrics *Metrics) error {
    78  	dev, err := os.Open(mb.device)
    79  	if err != nil {
    80  		return fmt.Errorf("unable to open device %q: %w", mb.device, err)
    81  	}
    82  	defer dev.Close()
    83  
    84  	end, err := dev.Seek(0, io.SeekEnd)
    85  	if err != nil {
    86  		return fmt.Errorf("failed to detect size of %q: %w", mb.device, err)
    87  	}
    88  
    89  	metrics.Capacity = resource.NewQuantity(end, resource.BinarySI)
    90  
    91  	return nil
    92  }
    93  

View as plain text