-
Notifications
You must be signed in to change notification settings - Fork 314
/
cpunormalization.go
346 lines (305 loc) · 13.6 KB
/
cpunormalization.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
Copyright 2022 The Koordinator 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 slocontroller
import (
"context"
"encoding/json"
"math"
"time"
topov1alpha1 "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/apis/topology/v1alpha1"
nrtclientset "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/generated/clientset/versioned"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/utils/pointer"
"github.com/koordinator-sh/koordinator/apis/configuration"
"github.com/koordinator-sh/koordinator/apis/extension"
koordinatorclientset "github.com/koordinator-sh/koordinator/pkg/client/clientset/versioned"
"github.com/koordinator-sh/koordinator/test/e2e/framework"
"github.com/koordinator-sh/koordinator/test/e2e/framework/manifest"
e2enode "github.com/koordinator-sh/koordinator/test/e2e/framework/node"
)
var (
defaultCPUModelRatioCfg = configuration.ModelRatioCfg{
BaseRatio: pointer.Float64(1.5),
TurboEnabledRatio: pointer.Float64(1.65),
HyperThreadEnabledRatio: pointer.Float64(1.0),
HyperThreadTurboEnabledRatio: pointer.Float64(1.1),
}
ratioDiffEpsilon = 0.01
minNodesCPUBasicInfoReadyRatio = 0.7
minNodesCPUNormalizationCorrectRatio = 0.7
)
var _ = SIGDescribe("CPUNormalization", func() {
var nodeList *corev1.NodeList
var c clientset.Interface
var koordClient koordinatorclientset.Interface
var nrtClient nrtclientset.Interface
var koordNamespace, sloConfigName string
var err error
f := framework.NewDefaultFramework("cpunormalization")
f.SkipNamespaceCreation = true
ginkgo.BeforeEach(func() {
c = f.ClientSet
koordClient = f.KoordinatorClientSet
koordNamespace = framework.TestContext.KoordinatorComponentNamespace
sloConfigName = framework.TestContext.SLOCtrlConfigMap
nrtClient, err = nrtclientset.NewForConfig(f.ClientConfig())
framework.ExpectNoError(err, "unable to create NRT ClientSet")
framework.Logf("get some nodes which are ready and schedulable")
nodeList, err = e2enode.GetReadySchedulableNodes(c)
framework.ExpectNoError(err)
// fail the test when no node available
gomega.Expect(len(nodeList.Items)).NotTo(gomega.BeZero())
})
framework.KoordinatorDescribe("CPUNormalization RatioUpdate", func() {
framework.ConformanceIt("update cpu normalization ratios in the node annotations", func() {
ginkgo.By("Check node cpu basic infos on NodeResourceTopology")
// assert totalCount > 0
// FIXME: the failures of NUMA topology reporting are ignored
totalCount, validNodeCount, skippedCount := len(nodeList.Items), 0, 0
var cpuModels []*extension.CPUBasicInfo
gomega.Eventually(func() bool {
for i := range nodeList.Items {
node := &nodeList.Items[i]
nodeMetric, err := koordClient.SloV1alpha1().NodeMetrics().Get(context.TODO(), node.Name, metav1.GetOptions{})
if err != nil {
framework.Logf("failed to get node metric for node %s, err: %v", node.Name, err)
continue
}
// validate NodeMetric
isMetricValid, msg := isNodeMetricValid(nodeMetric)
if !isMetricValid {
framework.Logf("node %s has no valid node metric, msg: %s", node.Name, msg)
continue
}
nrt, err := nrtClient.TopologyV1alpha1().NodeResourceTopologies().Get(context.TODO(), node.Name, metav1.GetOptions{})
if err != nil {
framework.Logf("failed to get node resource topology for node %s, err: %v", node.Name, err)
continue
}
// validate NodeResourceTopology
isNRTValid, msg := isNRTValid(nrt)
if !isNRTValid {
skippedCount++
framework.Logf("node %s has no valid node resource topology, msg: %s", node.Name, msg)
continue
}
cpuBasicInfo, err := extension.GetCPUBasicInfo(nrt.Annotations)
if err != nil {
framework.Logf("nrt %s has no valid cpu basic info, err: %s", nrt.Name, err)
continue
}
if cpuBasicInfo == nil {
skippedCount++
framework.Logf("nrt %s has no cpu basic info", nrt.Name)
continue
}
cpuModels = append(cpuModels, cpuBasicInfo)
validNodeCount++
}
// expect enough valid NRTs have cpu basic info
if float64(validNodeCount+skippedCount) <= float64(totalCount)*minNodesCPUBasicInfoReadyRatio {
framework.Logf("there should be enough nodes that have cpu basic info on NRT, but got:"+
" total[%v], valid[%v], skipped[%v]", totalCount, validNodeCount, skippedCount)
// reset nodes and counters
nodeList, err = e2enode.GetReadySchedulableNodes(c)
framework.ExpectNoError(err)
gomega.Expect(len(nodeList.Items)).NotTo(gomega.BeZero())
cpuModels = make([]*extension.CPUBasicInfo, 0)
totalCount, validNodeCount, skippedCount = len(nodeList.Items), 0, 0
return false
}
framework.Logf("finish checking node cpu basic info, total[%v], valid[%v], skipped[%v]",
totalCount, validNodeCount, skippedCount)
return true
}, 180*time.Second, 5*time.Second).Should(gomega.Equal(true))
cpuNormalizationStrategy := makeCPUNormalizationStrategyForModels(cpuModels)
framework.Logf("prepare cpu ratio model: [%+v]", cpuNormalizationStrategy)
cpuNormalizationConfigBytes, err := json.Marshal(cpuNormalizationStrategy)
framework.ExpectNoError(err)
cpuNormalizationData := string(cpuNormalizationConfigBytes)
ginkgo.By("Loading slo-controller-config in the cluster")
isConfigCreated := false
configMap, err := c.CoreV1().ConfigMaps(koordNamespace).Get(context.TODO(), sloConfigName, metav1.GetOptions{})
if err == nil {
isConfigCreated = true
framework.Logf("successfully get slo-controller-config %s/%s", koordNamespace, sloConfigName)
} else if errors.IsNotFound(err) {
framework.Logf("cannot get slo-controller-config %s/%s, try to create a new one",
koordNamespace, sloConfigName)
} else {
framework.Failf("failed to get slo-controller-config %s/%s, got unexpected error: %v",
koordNamespace, sloConfigName, err)
}
// If configmap is created, try to patch it with colocation enabled.
// If not exist, create the slo-controller-config.
// NOTE: slo-controller-config should not be modified by the others during the e2e test.
ginkgo.By("Prepare slo-controller-config to enable cpu normalization")
if isConfigCreated {
needUpdate := false
rollbackData := map[string]string{}
if configMap.Data == nil {
needUpdate = true
} else if configMap.Data[configuration.ColocationConfigKey] != colocationEnabledConfigData ||
configMap.Data[configuration.CPUNormalizationConfigKey] != cpuNormalizationData {
rollbackData[configuration.ColocationConfigKey] = configMap.Data[configuration.ColocationConfigKey]
rollbackData[configuration.CPUNormalizationConfigKey] = configMap.Data[configuration.CPUNormalizationConfigKey]
needUpdate = true
}
if needUpdate {
framework.Logf("cpu normalization is not enabled in slo-controller-config, need update")
defer rollbackSLOConfigData(f, koordNamespace, sloConfigName, rollbackData)
newConfigMap := configMap.DeepCopy()
newConfigMap.Data[configuration.ColocationConfigKey] = colocationEnabledConfigData
newConfigMap.Data[configuration.CPUNormalizationConfigKey] = cpuNormalizationData
newConfigMapUpdated, err := c.CoreV1().ConfigMaps(koordNamespace).Update(context.TODO(), newConfigMap, metav1.UpdateOptions{})
framework.ExpectNoError(err)
framework.Logf("update slo-controller-config successfully, data: %+v", newConfigMapUpdated.Data)
configMap = newConfigMapUpdated
} else {
framework.Logf("cpu normalization is already enabled in slo-controller-config, keep the same")
}
} else {
framework.Logf("slo-controller-config does not exist, need create")
newConfigMap, err := manifest.ConfigMapFromManifest("slocontroller/slo-controller-config.yaml")
framework.ExpectNoError(err)
newConfigMap.SetNamespace(koordNamespace)
newConfigMap.SetName(sloConfigName)
newConfigMap.Data[configuration.ColocationConfigKey] = colocationEnabledConfigData
newConfigMap.Data[configuration.CPUNormalizationConfigKey] = cpuNormalizationData
newConfigMapCreated, err := c.CoreV1().ConfigMaps(koordNamespace).Create(context.TODO(), newConfigMap, metav1.CreateOptions{})
framework.ExpectNoError(err)
framework.Logf("create slo-controller-config successfully, data: %+v", newConfigMapCreated.Data)
configMap = newConfigMapCreated
defer rollbackSLOConfigObject(f, koordNamespace, sloConfigName)
}
ginkgo.By("Check node cpu normalization ratios")
totalCount, validNodeCount, skippedCount = len(nodeList.Items), 0, 0
gomega.Eventually(func() bool {
framework.Logf("relist some nodes which are ready and schedulable")
nodeList, err = e2enode.GetReadySchedulableNodes(c)
framework.ExpectNoError(err)
for i := range nodeList.Items {
node := &nodeList.Items[i]
ratio, err := extension.GetCPUNormalizationRatio(node)
if err != nil {
framework.Logf("failed to get cpu normalization ratio for node %s, err: %v", node.Name, err)
continue
}
if ratio < 1.0 {
skippedCount++
framework.Logf("failed to get valid normalization ratio for node %s, skipped, ratio: %v",
node.Name, ratio)
continue
}
nrt, err := nrtClient.TopologyV1alpha1().NodeResourceTopologies().Get(context.TODO(), node.Name, metav1.GetOptions{})
if err != nil {
framework.Logf("failed to get node resource topology for node %s, err: %v", node.Name, err)
continue
}
// validate NodeResourceTopology
isNRTValid, msg := isNRTValid(nrt)
if !isNRTValid {
skippedCount++
framework.Logf("node %s has no valid node resource topology, msg: %s", node.Name, msg)
continue
}
cpuBasicInfo, err := extension.GetCPUBasicInfo(nrt.Annotations)
if err != nil {
framework.Logf("nrt %s has no valid cpu basic info, err: %s", nrt.Name, err)
continue
}
if cpuBasicInfo == nil {
skippedCount++
framework.Logf("nrt %s has no cpu basic info", nrt.Name)
continue
}
if expectedRatio := getCPUNormalizationRatioInDefaultModel(cpuBasicInfo); math.Abs(ratio-expectedRatio) > ratioDiffEpsilon {
framework.Logf("node cpu normalization ratio is different from expected, node %s, "+
"cpu model [%+v], expected %v, current %v", node.Name, cpuBasicInfo, expectedRatio, ratio)
continue
}
framework.Logf("check node %s cpu normalization ratio successfully, ratio %v",
node.Name, ratio)
validNodeCount++
}
// expect enough correct node ratios
if float64(validNodeCount+skippedCount) <= float64(totalCount)*minNodesCPUNormalizationCorrectRatio {
framework.Logf("there should be enough nodes that have correct cpu normalization ratio, "+
"but got: total[%v], valid[%v], skipped[%v]", totalCount, validNodeCount, skippedCount)
// reset nodes and counters
nodeList, err = e2enode.GetReadySchedulableNodes(c)
framework.ExpectNoError(err)
gomega.Expect(len(nodeList.Items)).NotTo(gomega.BeZero())
totalCount, validNodeCount, skippedCount = len(nodeList.Items), 0, 0
return false
}
framework.Logf("finish checking node cpu normalization ratio, total[%v], valid[%v], skipped[%v]",
totalCount, validNodeCount, skippedCount)
return true
}, 120*time.Second, 5*time.Second).Should(gomega.Equal(true))
})
// TODO: submit a LS pod allocating cpu-normalized cpu resource
})
})
func isNRTValid(nrt *topov1alpha1.NodeResourceTopology) (bool, string) {
if nrt == nil || nrt.Annotations == nil {
return false, "nrt is incomplete"
}
if len(nrt.Zones) <= 0 {
return false, "nrt has no zone"
}
if len(nrt.TopologyPolicies) <= 0 {
return false, "nrt has no topology policy"
}
return true, ""
}
func makeCPUNormalizationStrategyForModels(cpuModels []*extension.CPUBasicInfo) *configuration.CPUNormalizationStrategy {
ratioModel := map[string]configuration.ModelRatioCfg{}
for _, cpuModel := range cpuModels {
ratioCfg, ok := ratioModel[cpuModel.CPUModel]
if !ok {
ratioCfg = configuration.ModelRatioCfg{}
}
if cpuModel.HyperThreadEnabled && cpuModel.TurboEnabled {
ratioCfg.HyperThreadTurboEnabledRatio = defaultCPUModelRatioCfg.HyperThreadTurboEnabledRatio
} else if cpuModel.HyperThreadEnabled {
ratioCfg.HyperThreadEnabledRatio = defaultCPUModelRatioCfg.HyperThreadEnabledRatio
} else if cpuModel.TurboEnabled {
ratioCfg.TurboEnabledRatio = defaultCPUModelRatioCfg.TurboEnabledRatio
} else {
ratioCfg.BaseRatio = defaultCPUModelRatioCfg.BaseRatio
}
ratioModel[cpuModel.CPUModel] = ratioCfg
}
return &configuration.CPUNormalizationStrategy{
Enable: pointer.Bool(true),
RatioModel: ratioModel,
}
}
func getCPUNormalizationRatioInDefaultModel(info *extension.CPUBasicInfo) float64 {
if info.HyperThreadEnabled && info.TurboEnabled {
return *defaultCPUModelRatioCfg.HyperThreadTurboEnabledRatio
}
if info.HyperThreadEnabled {
return *defaultCPUModelRatioCfg.HyperThreadEnabledRatio
}
if info.TurboEnabled {
return *defaultCPUModelRatioCfg.TurboEnabledRatio
}
return *defaultCPUModelRatioCfg.BaseRatio
}