/
metrics_client_test_util.go
137 lines (112 loc) · 4.82 KB
/
metrics_client_test_util.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
130
131
132
133
134
135
136
137
/*
Copyright 2017 The Kubernetes Authors.
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 metrics
import (
"math/big"
"time"
k8sapiv1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
core "k8s.io/client-go/testing"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/recommender/model"
metricsapi "k8s.io/metrics/pkg/apis/metrics/v1beta1"
"k8s.io/metrics/pkg/client/clientset/versioned/fake"
)
type metricsClientTestCase struct {
snapshotTimestamp time.Time
snapshotWindow time.Duration
namespace *v1.Namespace
pod1Snaps, pod2Snaps []*ContainerMetricsSnapshot
}
func newMetricsClientTestCase() *metricsClientTestCase {
namespaceName := "test-namespace"
testCase := &metricsClientTestCase{
snapshotTimestamp: time.Now(),
snapshotWindow: time.Duration(1234),
namespace: &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespaceName}},
}
id1 := model.ContainerID{PodID: model.PodID{Namespace: namespaceName, PodName: "Pod1"}, ContainerName: "Name1"}
id2 := model.ContainerID{PodID: model.PodID{Namespace: namespaceName, PodName: "Pod1"}, ContainerName: "Name2"}
id3 := model.ContainerID{PodID: model.PodID{Namespace: namespaceName, PodName: "Pod2"}, ContainerName: "Name1"}
id4 := model.ContainerID{PodID: model.PodID{Namespace: namespaceName, PodName: "Pod2"}, ContainerName: "Name2"}
testCase.pod1Snaps = append(testCase.pod1Snaps, testCase.newContainerMetricsSnapshot(id1, 400, 333))
testCase.pod1Snaps = append(testCase.pod1Snaps, testCase.newContainerMetricsSnapshot(id2, 800, 666))
testCase.pod2Snaps = append(testCase.pod2Snaps, testCase.newContainerMetricsSnapshot(id3, 401, 334))
testCase.pod2Snaps = append(testCase.pod2Snaps, testCase.newContainerMetricsSnapshot(id4, 801, 667))
return testCase
}
func newEmptyMetricsClientTestCase() *metricsClientTestCase {
return &metricsClientTestCase{}
}
func (tc *metricsClientTestCase) newContainerMetricsSnapshot(id model.ContainerID, cpuUsage int64, memUsage int64) *ContainerMetricsSnapshot {
return &ContainerMetricsSnapshot{
ID: id,
SnapshotTime: tc.snapshotTimestamp,
SnapshotWindow: tc.snapshotWindow,
Usage: model.Resources{
model.ResourceCPU: model.ResourceAmount(cpuUsage),
model.ResourceMemory: model.ResourceAmount(memUsage),
},
}
}
func (tc *metricsClientTestCase) createFakeMetricsClient() MetricsClient {
fakeMetricsGetter := &fake.Clientset{}
fakeMetricsGetter.AddReactor("list", "pods", func(action core.Action) (handled bool, ret runtime.Object, err error) {
return true, tc.getFakePodMetricsList(), nil
})
return NewMetricsClient(fakeMetricsGetter.MetricsV1beta1(), "", "fake")
}
func (tc *metricsClientTestCase) getFakePodMetricsList() *metricsapi.PodMetricsList {
metrics := &metricsapi.PodMetricsList{}
if tc.pod1Snaps != nil && tc.pod2Snaps != nil {
metrics.Items = append(metrics.Items, makePodMetrics(tc.pod1Snaps))
metrics.Items = append(metrics.Items, makePodMetrics(tc.pod2Snaps))
}
return metrics
}
func makePodMetrics(snaps []*ContainerMetricsSnapshot) metricsapi.PodMetrics {
firstSnap := snaps[0]
podMetrics := metricsapi.PodMetrics{
ObjectMeta: metav1.ObjectMeta{
Namespace: firstSnap.ID.Namespace,
Name: firstSnap.ID.PodName,
},
Timestamp: metav1.Time{Time: firstSnap.SnapshotTime},
Window: metav1.Duration{Duration: firstSnap.SnapshotWindow},
Containers: make([]metricsapi.ContainerMetrics, len(snaps)),
}
for i, snap := range snaps {
resourceList := calculateResourceList(snap.Usage)
podMetrics.Containers[i] = metricsapi.ContainerMetrics{
Name: snap.ID.ContainerName,
Usage: resourceList,
}
}
return podMetrics
}
func calculateResourceList(usage model.Resources) k8sapiv1.ResourceList {
cpuCores := big.NewRat(int64(usage[model.ResourceCPU]), 1000)
cpuQuantityString := cpuCores.FloatString(3)
memoryBytes := big.NewInt(int64(usage[model.ResourceMemory]))
memoryQuantityString := memoryBytes.String()
resourceMap := map[k8sapiv1.ResourceName]resource.Quantity{
k8sapiv1.ResourceCPU: resource.MustParse(cpuQuantityString),
k8sapiv1.ResourceMemory: resource.MustParse(memoryQuantityString),
}
return k8sapiv1.ResourceList(resourceMap)
}
func (tc *metricsClientTestCase) getAllSnaps() []*ContainerMetricsSnapshot {
return append(tc.pod1Snaps, tc.pod2Snaps...)
}