summary_sys_containers.go 3.5 KB

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