...
1
2
3
4
19
20 package stats
21
22 import (
23 "time"
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 stats = append(stats, sp.getSystemPodsCPUAndMemoryStats(nodeConfig, podStats, updateStats))
32 return stats
33 }
34
35 func (sp *summaryProviderImpl) GetSystemContainersCPUAndMemoryStats(nodeConfig cm.NodeConfig, podStats []statsapi.PodStats, updateStats bool) (stats []statsapi.ContainerStats) {
36 stats = append(stats, sp.getSystemPodsCPUAndMemoryStats(nodeConfig, podStats, updateStats))
37 return stats
38 }
39
40 func (sp *summaryProviderImpl) getSystemPodsCPUAndMemoryStats(nodeConfig cm.NodeConfig, podStats []statsapi.PodStats, updateStats bool) statsapi.ContainerStats {
41 now := metav1.NewTime(time.Now())
42 podsSummary := statsapi.ContainerStats{
43 StartTime: now,
44 CPU: &statsapi.CPUStats{},
45 Memory: &statsapi.MemoryStats{},
46 Name: statsapi.SystemContainerPods,
47 }
48
49
50 var usageCoreNanoSeconds uint64
51 var usageNanoCores uint64
52 var availableBytes uint64
53 var usageBytes uint64
54 var workingSetBytes uint64
55 for _, pod := range podStats {
56 if pod.CPU != nil {
57 podsSummary.CPU.Time = now
58 if pod.CPU.UsageCoreNanoSeconds != nil {
59 usageCoreNanoSeconds = usageCoreNanoSeconds + *pod.CPU.UsageCoreNanoSeconds
60 }
61 if pod.CPU.UsageNanoCores != nil {
62 usageNanoCores = usageNanoCores + *pod.CPU.UsageNanoCores
63 }
64 }
65
66 if pod.Memory != nil {
67 podsSummary.Memory.Time = now
68 if pod.Memory.AvailableBytes != nil {
69 availableBytes = availableBytes + *pod.Memory.AvailableBytes
70 }
71 if pod.Memory.UsageBytes != nil {
72 usageBytes = usageBytes + *pod.Memory.UsageBytes
73 }
74 if pod.Memory.WorkingSetBytes != nil {
75 workingSetBytes = workingSetBytes + *pod.Memory.WorkingSetBytes
76 }
77 }
78 }
79
80
81 if usageCoreNanoSeconds != 0 {
82 podsSummary.CPU.UsageCoreNanoSeconds = &usageCoreNanoSeconds
83 }
84 if usageNanoCores != 0 {
85 podsSummary.CPU.UsageNanoCores = &usageNanoCores
86 }
87 if availableBytes != 0 {
88 podsSummary.Memory.AvailableBytes = &availableBytes
89 }
90 if usageBytes != 0 {
91 podsSummary.Memory.UsageBytes = &usageBytes
92 }
93 if workingSetBytes != 0 {
94 podsSummary.Memory.WorkingSetBytes = &workingSetBytes
95 }
96
97 return podsSummary
98 }
99
View as plain text