...

Source file src/k8s.io/kubernetes/pkg/kubelet/server/stats/summary_sys_containers.go

Documentation: k8s.io/kubernetes/pkg/kubelet/server/stats

     1  //go:build !windows
     2  // +build !windows
     3  
     4  /*
     5  Copyright 2018 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package stats
    21  
    22  import (
    23  	"k8s.io/klog/v2"
    24  
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
    27  	"k8s.io/kubernetes/pkg/kubelet/cm"
    28  )
    29  
    30  func (sp *summaryProviderImpl) GetSystemContainersStats(nodeConfig cm.NodeConfig, podStats []statsapi.PodStats, updateStats bool) (stats []statsapi.ContainerStats) {
    31  	systemContainers := map[string]struct {
    32  		name             string
    33  		forceStatsUpdate bool
    34  		startTime        metav1.Time
    35  	}{
    36  		statsapi.SystemContainerKubelet: {name: nodeConfig.KubeletCgroupsName, forceStatsUpdate: false, startTime: sp.kubeletCreationTime},
    37  		statsapi.SystemContainerRuntime: {name: nodeConfig.RuntimeCgroupsName, forceStatsUpdate: false},
    38  		statsapi.SystemContainerMisc:    {name: nodeConfig.SystemCgroupsName, forceStatsUpdate: false},
    39  		statsapi.SystemContainerPods:    {name: sp.provider.GetPodCgroupRoot(), forceStatsUpdate: updateStats},
    40  	}
    41  	for sys, cont := range systemContainers {
    42  		// skip if cgroup name is undefined (not all system containers are required)
    43  		if cont.name == "" {
    44  			continue
    45  		}
    46  		s, _, err := sp.provider.GetCgroupStats(cont.name, cont.forceStatsUpdate)
    47  		if err != nil {
    48  			klog.ErrorS(err, "Failed to get system container stats", "containerName", cont.name)
    49  			continue
    50  		}
    51  		// System containers don't have a filesystem associated with them.
    52  		s.Logs, s.Rootfs = nil, nil
    53  		s.Name = sys
    54  
    55  		// if we know the start time of a system container, use that instead of the start time provided by cAdvisor
    56  		if !cont.startTime.IsZero() {
    57  			s.StartTime = cont.startTime
    58  		}
    59  		stats = append(stats, *s)
    60  	}
    61  
    62  	return stats
    63  }
    64  
    65  func (sp *summaryProviderImpl) GetSystemContainersCPUAndMemoryStats(nodeConfig cm.NodeConfig, podStats []statsapi.PodStats, updateStats bool) (stats []statsapi.ContainerStats) {
    66  	systemContainers := map[string]struct {
    67  		name             string
    68  		forceStatsUpdate bool
    69  		startTime        metav1.Time
    70  	}{
    71  		statsapi.SystemContainerKubelet: {name: nodeConfig.KubeletCgroupsName, forceStatsUpdate: false, startTime: sp.kubeletCreationTime},
    72  		statsapi.SystemContainerRuntime: {name: nodeConfig.RuntimeCgroupsName, forceStatsUpdate: false},
    73  		statsapi.SystemContainerMisc:    {name: nodeConfig.SystemCgroupsName, forceStatsUpdate: false},
    74  		statsapi.SystemContainerPods:    {name: sp.provider.GetPodCgroupRoot(), forceStatsUpdate: updateStats},
    75  	}
    76  	for sys, cont := range systemContainers {
    77  		// skip if cgroup name is undefined (not all system containers are required)
    78  		if cont.name == "" {
    79  			continue
    80  		}
    81  		s, err := sp.provider.GetCgroupCPUAndMemoryStats(cont.name, cont.forceStatsUpdate)
    82  		if err != nil {
    83  			klog.ErrorS(err, "Failed to get system container stats", "containerName", cont.name)
    84  			continue
    85  		}
    86  		s.Name = sys
    87  
    88  		// if we know the start time of a system container, use that instead of the start time provided by cAdvisor
    89  		if !cont.startTime.IsZero() {
    90  			s.StartTime = cont.startTime
    91  		}
    92  		stats = append(stats, *s)
    93  	}
    94  
    95  	return stats
    96  }
    97  

View as plain text