...

Source file src/k8s.io/kubernetes/pkg/kubelet/cadvisor/util.go

Documentation: k8s.io/kubernetes/pkg/kubelet/cadvisor

     1  /*
     2  Copyright 2015 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 cadvisor
    18  
    19  import (
    20  	"strings"
    21  
    22  	cadvisorapi "github.com/google/cadvisor/info/v1"
    23  	cadvisorapi2 "github.com/google/cadvisor/info/v2"
    24  	"k8s.io/api/core/v1"
    25  	"k8s.io/apimachinery/pkg/api/resource"
    26  	v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
    27  )
    28  
    29  const (
    30  	// CrioSocketSuffix is the path to the CRI-O socket.
    31  	// Please keep this in sync with the one in:
    32  	// github.com/google/cadvisor/tree/master/container/crio/client.go
    33  	// Note that however we only match on the suffix, as /var/run is often a
    34  	// symlink to /run, so the user can specify either path.
    35  	CrioSocketSuffix = "run/crio/crio.sock"
    36  )
    37  
    38  // CapacityFromMachineInfo returns the capacity of the resources from the machine info.
    39  func CapacityFromMachineInfo(info *cadvisorapi.MachineInfo) v1.ResourceList {
    40  	c := v1.ResourceList{
    41  		v1.ResourceCPU: *resource.NewMilliQuantity(
    42  			int64(info.NumCores*1000),
    43  			resource.DecimalSI),
    44  		v1.ResourceMemory: *resource.NewQuantity(
    45  			int64(info.MemoryCapacity),
    46  			resource.BinarySI),
    47  	}
    48  
    49  	// if huge pages are enabled, we report them as a schedulable resource on the node
    50  	for _, hugepagesInfo := range info.HugePages {
    51  		pageSizeBytes := int64(hugepagesInfo.PageSize * 1024)
    52  		hugePagesBytes := pageSizeBytes * int64(hugepagesInfo.NumPages)
    53  		pageSizeQuantity := resource.NewQuantity(pageSizeBytes, resource.BinarySI)
    54  		c[v1helper.HugePageResourceName(*pageSizeQuantity)] = *resource.NewQuantity(hugePagesBytes, resource.BinarySI)
    55  	}
    56  
    57  	return c
    58  }
    59  
    60  // EphemeralStorageCapacityFromFsInfo returns the capacity of the ephemeral storage from the FsInfo.
    61  func EphemeralStorageCapacityFromFsInfo(info cadvisorapi2.FsInfo) v1.ResourceList {
    62  	c := v1.ResourceList{
    63  		v1.ResourceEphemeralStorage: *resource.NewQuantity(
    64  			int64(info.Capacity),
    65  			resource.BinarySI),
    66  	}
    67  	return c
    68  }
    69  
    70  // UsingLegacyCadvisorStats returns true if container stats are provided by cadvisor instead of through the CRI.
    71  // CRI integrations should get container metrics via CRI.
    72  // TODO: cri-o relies on cadvisor as a temporary workaround. The code should
    73  // be removed. Related issue:
    74  // https://github.com/kubernetes/kubernetes/issues/51798
    75  func UsingLegacyCadvisorStats(runtimeEndpoint string) bool {
    76  	return strings.HasSuffix(runtimeEndpoint, CrioSocketSuffix)
    77  }
    78  

View as plain text