forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
monitor_resources.go
129 lines (110 loc) · 4.33 KB
/
monitor_resources.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e
import (
"time"
client "k8s.io/kubernetes/pkg/client/unversioned"
"k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
const datapointAmount = 5
type resourceUsagePerContainer map[string]*containerResourceUsage
var systemContainers = []string{"/docker-daemon", "/kubelet", "/kube-proxy", "/system"}
//TODO tweak those values.
var allowedUsage = resourceUsagePerContainer{
"/docker-daemon": &containerResourceUsage{
CPUUsageInCores: 0.08,
MemoryUsageInBytes: 4500000000,
MemoryWorkingSetInBytes: 1500000000,
},
"/kubelet": &containerResourceUsage{
CPUUsageInCores: 0.1,
MemoryUsageInBytes: 150000000,
MemoryWorkingSetInBytes: 150000000,
},
"/kube-proxy": &containerResourceUsage{
CPUUsageInCores: 0.025,
MemoryUsageInBytes: 12000000,
MemoryWorkingSetInBytes: 12000000,
},
"/system": &containerResourceUsage{
CPUUsageInCores: 0.03,
MemoryUsageInBytes: 100000000,
MemoryWorkingSetInBytes: 100000000,
},
}
func computeAverage(sliceOfUsages []resourceUsagePerContainer) (result resourceUsagePerContainer) {
result = make(resourceUsagePerContainer)
for _, container := range systemContainers {
result[container] = &containerResourceUsage{}
}
for _, usage := range sliceOfUsages {
for _, container := range systemContainers {
singleResult := &containerResourceUsage{
CPUUsageInCores: result[container].CPUUsageInCores + usage[container].CPUUsageInCores,
MemoryUsageInBytes: result[container].MemoryUsageInBytes + usage[container].MemoryUsageInBytes,
MemoryWorkingSetInBytes: result[container].MemoryWorkingSetInBytes + usage[container].MemoryWorkingSetInBytes,
}
result[container] = singleResult
}
}
for _, container := range systemContainers {
singleResult := &containerResourceUsage{
CPUUsageInCores: result[container].CPUUsageInCores / float64(len(sliceOfUsages)),
MemoryUsageInBytes: result[container].MemoryUsageInBytes / int64(len(sliceOfUsages)),
MemoryWorkingSetInBytes: result[container].MemoryWorkingSetInBytes / int64(len(sliceOfUsages)),
}
result[container] = singleResult
}
return
}
// This tests does nothing except checking current resource usage of containers defined in kubelet_stats systemContainers variable.
// Test fails if an average container resource consumption over datapointAmount tries exceeds amount defined in allowedUsage.
var _ = Describe("Resource usage of system containers", func() {
var c *client.Client
BeforeEach(func() {
var err error
c, err = loadClient()
expectNoError(err)
})
It("should not exceed expected amount.", func() {
By("Getting ResourceConsumption on all nodes")
nodeList, err := c.Nodes().List(labels.Everything(), fields.Everything())
expectNoError(err)
resourceUsagePerNode := make(map[string][]resourceUsagePerContainer)
for i := 0; i < datapointAmount; i++ {
for _, node := range nodeList.Items {
resourceUsage, err := getOneTimeResourceUsageOnNode(c, node.Name, 5*time.Second)
expectNoError(err)
resourceUsagePerNode[node.Name] = append(resourceUsagePerNode[node.Name], resourceUsage)
}
time.Sleep(3 * time.Second)
}
averageResourceUsagePerNode := make(map[string]resourceUsagePerContainer)
for _, node := range nodeList.Items {
averageResourceUsagePerNode[node.Name] = computeAverage(resourceUsagePerNode[node.Name])
}
violating := make(map[string]resourceUsagePerContainer)
for node, usage := range averageResourceUsagePerNode {
for container, cUsage := range usage {
Logf("%v on %v usage: %#v", container, node, cUsage)
if !allowedUsage[container].isStrictlyGreaterThan(cUsage) {
violating[node] = usage
}
}
}
Expect(violating).To(BeEmpty())
})
})