-
Notifications
You must be signed in to change notification settings - Fork 104
/
cgroups.go
743 lines (669 loc) · 30.5 KB
/
cgroups.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
package __performance_kubelet_node_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"path/filepath"
"strconv"
"strings"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/utils/cpuset"
"sigs.k8s.io/controller-runtime/pkg/client"
machineconfigv1 "github.com/openshift/api/machineconfiguration/v1"
performancev2 "github.com/openshift/cluster-node-tuning-operator/pkg/apis/performanceprofile/v2"
testutils "github.com/openshift/cluster-node-tuning-operator/test/e2e/performanceprofile/functests/utils"
"github.com/openshift/cluster-node-tuning-operator/test/e2e/performanceprofile/functests/utils/cgroup"
testclient "github.com/openshift/cluster-node-tuning-operator/test/e2e/performanceprofile/functests/utils/client"
"github.com/openshift/cluster-node-tuning-operator/test/e2e/performanceprofile/functests/utils/discovery"
"github.com/openshift/cluster-node-tuning-operator/test/e2e/performanceprofile/functests/utils/images"
testlog "github.com/openshift/cluster-node-tuning-operator/test/e2e/performanceprofile/functests/utils/log"
"github.com/openshift/cluster-node-tuning-operator/test/e2e/performanceprofile/functests/utils/mcps"
"github.com/openshift/cluster-node-tuning-operator/test/e2e/performanceprofile/functests/utils/nodes"
"github.com/openshift/cluster-node-tuning-operator/test/e2e/performanceprofile/functests/utils/pods"
"github.com/openshift/cluster-node-tuning-operator/test/e2e/performanceprofile/functests/utils/profiles"
"github.com/openshift/cluster-node-tuning-operator/test/e2e/performanceprofile/functests/utils/systemd"
)
const minRequiredCPUs = 8
var _ = Describe("[performance] Cgroups and affinity", Ordered, func() {
const (
activation_file string = "/rootfs/var/lib/ovn-ic/etc/enable_dynamic_cpu_affinity"
cgroupRoot string = "/rootfs/sys/fs/cgroup"
)
var (
onlineCPUSet cpuset.CPUSet
workerRTNode *corev1.Node
workerRTNodes []corev1.Node
profile, initialProfile *performancev2.PerformanceProfile
performanceMCP string
ovsSliceCgroup string
ctx context.Context = context.Background()
ovsSystemdServices []string
isCgroupV2 bool
)
BeforeAll(func() {
if discovery.Enabled() && testutils.ProfileNotFound {
Skip("Discovery mode enabled, performance profile not found")
}
workerRTNodes, err := nodes.GetByLabels(testutils.NodeSelectorLabels)
Expect(err).ToNot(HaveOccurred())
workerRTNodes, err = nodes.MatchingOptionalSelector(workerRTNodes)
Expect(err).ToNot(HaveOccurred(), fmt.Sprintf("error looking for the optional selector: %v", err))
workerRTNode = &workerRTNodes[0]
profile, err = profiles.GetByNodeLabels(testutils.NodeSelectorLabels)
Expect(err).ToNot(HaveOccurred())
performanceMCP, err = mcps.GetByProfile(profile)
Expect(err).ToNot(HaveOccurred())
isCgroupV2, err = cgroup.IsVersion2(ctx, testclient.Client)
Expect(err).ToNot(HaveOccurred())
ovsSystemdServices = ovsSystemdServicesOnOvsSlice(ctx, workerRTNode)
})
BeforeEach(func() {
var err error
By(fmt.Sprintf("Checking the profile %s with cpus %s", profile.Name, cpuSpecToString(profile.Spec.CPU)))
Expect(profile.Spec.CPU.Isolated).NotTo(BeNil())
Expect(profile.Spec.CPU.Reserved).NotTo(BeNil())
onlineCPUSet, err = nodes.GetOnlineCPUsSet(context.TODO(), workerRTNode)
Expect(err).ToNot(HaveOccurred())
})
Describe("[rfe_id: 64006][Dynamic OVS Pinning]", Ordered, func() {
Context("[Performance Profile applied]", func() {
It("[test_id:64097] Activation file is created", func() {
cmd := []string{"ls", activation_file}
for _, node := range workerRTNodes {
out, err := nodes.ExecCommandOnNode(context.TODO(), cmd, &node)
Expect(err).ToNot(HaveOccurred(), "file %s doesn't exist ", activation_file)
Expect(out).To(Equal(activation_file))
}
})
It("[test_id:73046] Verify ovn kube node pod have their cpuset.cpus set to all available cpus", func() {
ovnKubenodepod, err := ovnCnfNodePod(ctx, workerRTNode)
Expect(err).ToNot(HaveOccurred())
containerIds, err := ovnPodContainers(&ovnKubenodepod)
for _, ctn := range containerIds {
var containerCgroupPath string
pid, err := nodes.ContainerPid(ctx, workerRTNode, ctn)
Expect(err).ToNot(HaveOccurred())
cmd := []string{"cat", fmt.Sprintf("/rootfs/proc/%s/cgroup", pid)}
out, err := nodes.ExecCommandOnMachineConfigDaemon(context.TODO(), workerRTNode, cmd)
Expect(err).ToNot(HaveOccurred())
cgroupPathOfPid, err := cgroup.PidParser(out)
if isCgroupV2 {
containerCgroupPath = filepath.Join(cgroupRoot, cgroupPathOfPid)
} else {
controller := filepath.Join(cgroupRoot, "/cpuset")
containerCgroupPath = filepath.Join(controller, cgroupPathOfPid)
}
cmd = []string{"cat", fmt.Sprintf("%s", filepath.Join(containerCgroupPath, "/cpuset.cpus"))}
cpus, err := nodes.ExecCommandOnNode(ctx, cmd, workerRTNode)
containerCpuset, err := cpuset.Parse(cpus)
Expect(containerCpuset).To(Equal(onlineCPUSet), "Burstable pod containers cpuset.cpus do not match total online cpus")
}
})
})
Context("[Performance Profile Modified]", func() {
BeforeEach(func() {
initialProfile = profile.DeepCopy()
})
It("[test_id:64099] Activation file doesn't get deleted", func() {
performanceMCP, err := mcps.GetByProfile(profile)
Expect(err).ToNot(HaveOccurred())
policy := "best-effort"
// Need to make some changes to pp , causing system reboot
// and check if activation files is modified or deleted
profile, err = profiles.GetByNodeLabels(testutils.NodeSelectorLabels)
Expect(err).ToNot(HaveOccurred(), "Unable to fetch latest performance profile")
currentPolicy := profile.Spec.NUMA.TopologyPolicy
if *currentPolicy == "best-effort" {
policy = "restricted"
}
profile.Spec.NUMA = &performancev2.NUMA{
TopologyPolicy: &policy,
}
By("Updating the performance profile")
profiles.UpdateWithRetry(profile)
By("Applying changes in performance profile and waiting until mcp will start updating")
mcps.WaitForCondition(performanceMCP, machineconfigv1.MachineConfigPoolUpdating, corev1.ConditionTrue)
By("Waiting for MCP being updated")
mcps.WaitForCondition(performanceMCP, machineconfigv1.MachineConfigPoolUpdated, corev1.ConditionTrue)
By("Checking Activation file")
cmd := []string{"ls", activation_file}
for _, node := range workerRTNodes {
out, err := nodes.ExecCommandOnNode(context.TODO(), cmd, &node)
Expect(err).ToNot(HaveOccurred(), "file %s doesn't exist ", activation_file)
Expect(out).To(Equal(activation_file))
}
})
AfterEach(func() {
By("Reverting the Profile")
profile, err := profiles.GetByNodeLabels(testutils.NodeSelectorLabels)
Expect(err).ToNot(HaveOccurred())
currentSpec, _ := json.Marshal(profile.Spec)
spec, _ := json.Marshal(initialProfile.Spec)
performanceMCP, err := mcps.GetByProfile(profile)
Expect(err).ToNot(HaveOccurred())
// revert only if the profile changes.
if !bytes.Equal(currentSpec, spec) {
Expect(testclient.Client.Patch(context.TODO(), profile,
client.RawPatch(
types.JSONPatchType,
[]byte(fmt.Sprintf(`[{ "op": "replace", "path": "/spec", "value": %s }]`, spec)),
),
)).ToNot(HaveOccurred())
By("Applying changes in performance profile and waiting until mcp will start updating")
mcps.WaitForCondition(performanceMCP, machineconfigv1.MachineConfigPoolUpdating, corev1.ConditionTrue)
By("Waiting when mcp finishes updates")
mcps.WaitForCondition(performanceMCP, machineconfigv1.MachineConfigPoolUpdated, corev1.ConditionTrue)
}
})
})
})
Context("Verification of cgroup layout on the worker node", func() {
var ctx context.Context = context.TODO()
var cgroupProcs, cgroupCpusetCpus, cgroupLoadBalance string
BeforeAll(func() {
pids, err := ovsPids(ctx, ovsSystemdServices, workerRTNode)
Expect(err).ToNot(HaveOccurred(), "unable to fetch pid of ovs services")
cmd := []string{"cat", fmt.Sprintf("/rootfs/proc/%s/cgroup", pids[0])}
out, err := nodes.ExecCommandOnMachineConfigDaemon(context.TODO(), workerRTNode, cmd)
Expect(err).ToNot(HaveOccurred())
ovsSliceCgroup, err = cgroup.PidParser(out)
Expect(err).ToNot(HaveOccurred())
cgroupSlices := strings.Split(ovsSliceCgroup, "/")
parentCgroup := strings.Join(cgroupSlices[:len(cgroupSlices)-1], "/")
if isCgroupV2 {
cgroupProcs = fmt.Sprintf("/rootfs/sys/fs/cgroup/%s/cgroup.procs", ovsSliceCgroup)
cgroupCpusetCpus = fmt.Sprintf("/rootfs/sys/fs/cgroup/%s/cpuset.cpus.effective", parentCgroup)
} else {
cgroupProcs = fmt.Sprintf("%s/cpuset/%s/cgroup.procs", cgroupRoot, parentCgroup)
cgroupCpusetCpus = fmt.Sprintf("%s/cpuset/%s/cpuset.cpus", cgroupRoot, parentCgroup)
cgroupLoadBalance = fmt.Sprintf("%s/cpuset/%s/cpuset.sched_load_balance", cgroupRoot, parentCgroup)
}
})
chkOvsCgrpProcs := func(node *corev1.Node) (string, error) {
testlog.Info("Verify cgroup.procs is not empty")
cmd := []string{"cat", cgroupProcs}
return nodes.ExecCommandOnNode(context.TODO(), cmd, node)
}
chkOvsCgrpCpuset := func(node *corev1.Node) (string, error) {
cmd := []string{"cat", cgroupCpusetCpus}
return nodes.ExecCommandOnNode(context.TODO(), cmd, node)
}
chkOvsCgroupLoadBalance := func(node *corev1.Node) (string, error) {
cmd := []string{"cat", cgroupLoadBalance}
return nodes.ExecCommandOnNode(context.TODO(), cmd, node)
}
It("[test_id:64098] Verify cgroup layout on worker node", func() {
// check cgroups.procs
result, err := chkOvsCgrpProcs(workerRTNode)
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Not(BeEmpty()))
result, err = chkOvsCgrpCpuset(workerRTNode)
Expect(err).ToNot(HaveOccurred(), "Unable to read cpuset.cpus")
ovsCPUSet, err := cpuset.Parse(result)
Expect(err).ToNot(HaveOccurred())
Expect(ovsCPUSet).To(Equal(onlineCPUSet))
if isCgroupV2 {
Skip("CPU load balance can be checked only functionally on cgroupv2")
}
result, err = chkOvsCgroupLoadBalance(workerRTNode)
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal("0"))
})
})
Describe("Affinity", func() {
var ctx context.Context = context.TODO()
Context("ovn-kubenode Pods affinity ", func() {
testutils.CustomBeforeAll(func() {
profile, err := profiles.GetByNodeLabels(testutils.NodeSelectorLabels)
Expect(err).ToNot(HaveOccurred())
initialProfile = profile.DeepCopy()
})
It("[test_id:64100] matches with ovs process affinity", func() {
ovnPod, err := ovnCnfNodePod(ctx, workerRTNode)
Expect(err).ToNot(HaveOccurred(), "Unable to get ovnPod")
ovnContainerids, err := ovnPodContainers(&ovnPod)
Expect(err).ToNot(HaveOccurred())
// Generally there are many containers inside a kubenode pods
// we don't need to check cpus used by all the containers
// we take first container
containerPid, err := nodes.ContainerPid(ctx, workerRTNode, ovnContainerids[0])
// we need to wait as process affinity can change
time.Sleep(30 * time.Second)
ctnCpuset := taskSet(ctx, containerPid, workerRTNode)
testlog.Infof("Cpus used by ovn Containers are %s", ctnCpuset.String())
pidList, err := ovsPids(ctx, ovsSystemdServices, workerRTNode)
Expect(err).ToNot(HaveOccurred())
cpumaskList, err := getCPUMaskForPids(ctx, pidList, workerRTNode)
Expect(err).ToNot(HaveOccurred())
for _, cpumask := range cpumaskList {
Expect(ctnCpuset).To(Equal(cpumask), "affinity of ovn kube node pods(%s) do not match with ovservices(%s)", ctnCpuset.String(), cpumask.String())
}
})
It("[test_id:64101] Creating gu pods modifies affinity of ovs", func() {
var testpod *corev1.Pod
var err error
testpod = pods.GetTestPod()
testpod.Namespace = testutils.NamespaceTesting
testpod.Spec.Containers[0].Resources = corev1.ResourceRequirements{Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("2"),
corev1.ResourceMemory: resource.MustParse("200Mi"),
},
}
testpod.Spec.NodeSelector = map[string]string{testutils.LabelHostname: workerRTNode.Name}
err = testclient.Client.Create(ctx, testpod)
Expect(err).ToNot(HaveOccurred())
testpod, err = pods.WaitForCondition(ctx, client.ObjectKeyFromObject(testpod), corev1.PodReady, corev1.ConditionTrue, 5*time.Minute)
Expect(err).ToNot(HaveOccurred())
Expect(testpod.Status.QOSClass).To(Equal(corev1.PodQOSGuaranteed))
cmd := []string{"taskset", "-pc", "1"}
outputb, err := pods.ExecCommandOnPod(testclient.K8sClient, testpod, "", cmd)
testpodCpus := bytes.Split(outputb, []byte(":"))
testlog.Infof("%v pod is using cpus %v", testpod.Name, string(testpodCpus[1]))
By("Get ovnpods running on the worker cnf node")
ovnPod, err := ovnCnfNodePod(ctx, workerRTNode)
Expect(err).ToNot(HaveOccurred(), "Unable to get ovnPod")
By("Get cpu used by ovn pod containers")
// We are fetching the container Process pid and
// using taskset we are fetching cpus used by the container process
// instead of using containers cpuset.cpus
ovnContainers, err := ovnPodContainers(&ovnPod)
Expect(err).ToNot(HaveOccurred())
containerPid, err := nodes.ContainerPid(ctx, workerRTNode, ovnContainers[0])
// we need to wait as process affinity can change
time.Sleep(30 * time.Second)
ctnCpuset := taskSet(ctx, containerPid, workerRTNode)
testlog.Infof("Container of ovn pod %s is using cpus %s", ovnPod.Name, ctnCpuset.String())
pidList, err := ovsPids(ctx, ovsSystemdServices, workerRTNode)
Expect(err).ToNot(HaveOccurred())
cpumaskList, err := getCPUMaskForPids(ctx, pidList, workerRTNode)
Expect(err).ToNot(HaveOccurred())
for _, cpumask := range cpumaskList {
Expect(ctnCpuset).To(Equal(cpumask), "affinity of ovn kube node pods(%s) do not match with ovservices(%s)", ctnCpuset.String(), cpumask.String())
}
deleteTestPod(ctx, testpod)
})
It("[test_id:64102] Create and remove gu pods to verify affinity of ovs are changed appropriately", func() {
var testpod1, testpod2 *corev1.Pod
var err error
ovnPod, err := ovnCnfNodePod(ctx, workerRTNode)
Expect(err).ToNot(HaveOccurred())
checkCpuCount(ctx, workerRTNode)
// Create testpod1
testpod1 = pods.GetTestPod()
testpod1.Namespace = testutils.NamespaceTesting
testpod1.Spec.Containers[0].Resources = corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("2"),
corev1.ResourceMemory: resource.MustParse("200Mi"),
},
}
testpod1.Spec.NodeSelector = map[string]string{testutils.LabelHostname: workerRTNode.Name}
err = testclient.Client.Create(ctx, testpod1)
Expect(err).ToNot(HaveOccurred())
testpod1, err = pods.WaitForCondition(ctx, client.ObjectKeyFromObject(testpod1), corev1.PodReady, corev1.ConditionTrue, 5*time.Minute)
Expect(err).ToNot(HaveOccurred())
Expect(testpod1.Status.QOSClass).To(Equal(corev1.PodQOSGuaranteed))
tasksetcmd := []string{"taskset", "-pc", "1"}
testpod1Cpus, err := pods.ExecCommandOnPod(testclient.K8sClient, testpod1, "", tasksetcmd)
testlog.Infof("%v pod is using %v cpus", testpod1.Name, string(testpod1Cpus))
// Create testpod2
testpod2 = pods.GetTestPod()
testpod2.Namespace = testutils.NamespaceTesting
testpod2.Spec.Containers[0].Resources = corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("2"),
corev1.ResourceMemory: resource.MustParse("200Mi"),
},
}
testpod2.Spec.NodeSelector = map[string]string{testutils.LabelHostname: workerRTNode.Name}
err = testclient.Client.Create(ctx, testpod2)
Expect(err).ToNot(HaveOccurred())
testpod2, err = pods.WaitForCondition(ctx, client.ObjectKeyFromObject(testpod2), corev1.PodReady, corev1.ConditionTrue, 5*time.Minute)
Expect(err).ToNot(HaveOccurred())
Expect(testpod1.Status.QOSClass).To(Equal(corev1.PodQOSGuaranteed))
By("fetch cpus used by container process using taskset")
testpod2Cpus, err := pods.ExecCommandOnPod(testclient.K8sClient, testpod2, "", tasksetcmd)
testlog.Infof("%v pod is using %v cpus", testpod2.Name, string(testpod2Cpus))
// Get cpus used by the ovnkubenode-pods containers
// Each kubenode pods have many containers, we check cpus of only 1 container
ovnContainers, err := ovnPodContainers(&ovnPod)
Expect(err).ToNot(HaveOccurred())
containerPid, err := nodes.ContainerPid(context.TODO(), workerRTNode, ovnContainers[0])
// we need to wait as process affinity can change
time.Sleep(30 * time.Second)
ovnContainerCpuset1 := taskSet(ctx, containerPid, workerRTNode)
testlog.Infof("Container of ovn pod %s is using cpus %s", ovnPod.Name, ovnContainerCpuset1.String())
pidList, err := ovsPids(ctx, ovsSystemdServices, workerRTNode)
Expect(err).ToNot(HaveOccurred())
// We wait for 30 seconds for ovs process cpu affinity to be updated
time.Sleep(30 * time.Second)
// Verify ovs-vswitchd and ovsdb-server process affinity is updated
cpumaskList1, err := getCPUMaskForPids(ctx, pidList, workerRTNode)
Expect(err).ToNot(HaveOccurred())
for _, cpumask := range cpumaskList1 {
Expect(ovnContainerCpuset1).To(Equal(cpumask), "affinity of ovn kube node pods(%s) do not match with ovservices(%s)", ovnContainerCpuset1.String(), cpumask.String())
}
// Delete testpod1
testlog.Infof("Deleting pod %v", testpod1.Name)
deleteTestPod(context.TODO(), testpod1)
time.Sleep(30 * time.Second)
// Check the cpus of ovnkubenode pods
ovnContainerCpuset2 := taskSet(ctx, containerPid, workerRTNode)
testlog.Infof("cpus used by ovn kube node pods after deleting pod %v is %v", testpod1.Name, ovnContainerCpuset2.String())
// we wait some time for ovs process affinity to change
time.Sleep(30 * time.Second)
// Verify ovs-vswitchd and ovsdb-server process affinity is updated
cpumaskList2, err := getCPUMaskForPids(ctx, pidList, workerRTNode)
Expect(err).ToNot(HaveOccurred())
for _, cpumask := range cpumaskList2 {
Expect(ovnContainerCpuset2).To(Equal(cpumask), "affinity of ovn kube node pods(%s) do not match with ovservices(%s)", ovnContainerCpuset2.String(), cpumask.String())
}
// Delete testpod2
deleteTestPod(context.TODO(), testpod2)
})
It("[test_id:64103] ovs process affinity still excludes guaranteed pods after reboot", func() {
checkCpuCount(context.TODO(), workerRTNode)
var dp *appsv1.Deployment
// create a deployment to deploy gu pods
dp = newDeployment()
testNode := make(map[string]string)
testNode["kubernetes.io/hostname"] = workerRTNode.Name
dp.Spec.Template.Spec.NodeSelector = testNode
err := testclient.Client.Create(ctx, dp)
Expect(err).ToNot(HaveOccurred(), "Unable to create Deployment")
defer func() {
// delete deployment
testlog.Infof("Deleting Deployment %v", dp.Name)
err := testclient.Client.Delete(ctx, dp)
Expect(err).ToNot(HaveOccurred())
}()
ovnPod, err := ovnCnfNodePod(ctx, workerRTNode)
Expect(err).ToNot(HaveOccurred(), "Unable to get ovnPod")
ovnContainerids, err := ovnPodContainers(&ovnPod)
Expect(err).ToNot(HaveOccurred())
containerPid, err := nodes.ContainerPid(ctx, workerRTNode, ovnContainerids[0])
// we need to wait as process affinity can change
time.Sleep(30 * time.Second)
ovnContainerCpuset := taskSet(ctx, containerPid, workerRTNode)
testlog.Infof("Container of ovn pod %s is using cpus %s", ovnPod.Name, ovnContainerCpuset.String())
pidList, err := ovsPids(ctx, ovsSystemdServices, workerRTNode)
Expect(err).ToNot(HaveOccurred())
//wait for 30 seconds for ovs process to have its cpu affinity updated
time.Sleep(30 * time.Second)
// Verify ovs-vswitchd and ovsdb-server process affinity is updated
cpumaskList1, err := getCPUMaskForPids(ctx, pidList, workerRTNode)
Expect(err).ToNot(HaveOccurred())
for _, cpumask := range cpumaskList1 {
Expect(ovnContainerCpuset).To(Equal(cpumask), "affinity of ovn kube node pods(%s) do not match with ovservices(%s)", ovnContainerCpuset.String(), cpumask.String())
}
testlog.Info("Rebooting the node")
// reboot the node, for that we change the numa policy to best-effort
// Note: this is used only to trigger reboot
policy := "best-effort"
// Need to make some changes to pp , causing system reboot
// and check if activation files is modified or deleted
profile, err = profiles.GetByNodeLabels(testutils.NodeSelectorLabels)
Expect(err).ToNot(HaveOccurred(), "Unable to fetch latest performance profile")
currentPolicy := profile.Spec.NUMA.TopologyPolicy
if *currentPolicy == "best-effort" {
policy = "restricted"
}
profile.Spec.NUMA = &performancev2.NUMA{
TopologyPolicy: &policy,
}
By("Updating the performance profile")
profiles.UpdateWithRetry(profile)
By("Applying changes in performance profile and waiting until mcp will start updating")
mcps.WaitForCondition(performanceMCP, machineconfigv1.MachineConfigPoolUpdating, corev1.ConditionTrue)
By("Waiting for MCP being updated")
mcps.WaitForCondition(performanceMCP, machineconfigv1.MachineConfigPoolUpdated, corev1.ConditionTrue)
// After reboot we want the deployment to be ready before moving forward
desiredStatus := appsv1.DeploymentStatus{
Replicas: 3,
AvailableReplicas: 3,
}
err = waitForCondition(dp, desiredStatus)
ovnPodAfterReboot, err := ovnCnfNodePod(ctx, workerRTNode)
Expect(err).ToNot(HaveOccurred(), "Unable to get ovnPod")
ovnContainerIdsAfterReboot, err := ovnPodContainers(&ovnPodAfterReboot)
Expect(err).ToNot(HaveOccurred())
containerPid, err = nodes.ContainerPid(ctx, workerRTNode, ovnContainerIdsAfterReboot[0])
// we need to wait as process affinity can change
time.Sleep(30 * time.Second)
ovnContainerCpusetAfterReboot := taskSet(ctx, containerPid, workerRTNode)
testlog.Infof("cpus used by ovn kube node pods %v", ovnContainerCpusetAfterReboot.String())
pidListAfterReboot, err := ovsPids(ctx, ovsSystemdServices, workerRTNode)
Expect(err).ToNot(HaveOccurred())
// Verify ovs-vswitchd and ovsdb-server process affinity is updated
cpumaskList2, err := getCPUMaskForPids(ctx, pidListAfterReboot, workerRTNode)
Expect(err).ToNot(HaveOccurred())
for _, cpumask := range cpumaskList2 {
Expect(ovnContainerCpusetAfterReboot).To(Equal(cpumask), "affinity of ovn kube node pods(%s) do not match with ovservices(%s)", ovnContainerCpusetAfterReboot.String(), cpumask.String())
}
})
AfterAll(func() {
By("Reverting the Profile")
profile, err := profiles.GetByNodeLabels(testutils.NodeSelectorLabels)
Expect(err).ToNot(HaveOccurred())
currentSpec, _ := json.Marshal(profile.Spec)
spec, _ := json.Marshal(initialProfile.Spec)
performanceMCP, err := mcps.GetByProfile(profile)
Expect(err).ToNot(HaveOccurred())
// revert only if the profile changes.
if !bytes.Equal(currentSpec, spec) {
Expect(testclient.Client.Patch(context.TODO(), profile,
client.RawPatch(
types.JSONPatchType,
[]byte(fmt.Sprintf(`[{ "op": "replace", "path": "/spec", "value": %s }]`, spec)),
),
)).ToNot(HaveOccurred())
By("Applying changes in performance profile and waiting until mcp will start updating")
mcps.WaitForCondition(performanceMCP, machineconfigv1.MachineConfigPoolUpdating, corev1.ConditionTrue)
By("Waiting when mcp finishes updates")
mcps.WaitForCondition(performanceMCP, machineconfigv1.MachineConfigPoolUpdated, corev1.ConditionTrue)
}
})
})
})
})
func cpuSpecToString(cpus *performancev2.CPU) string {
if cpus == nil {
return "<nil>"
}
sb := strings.Builder{}
if cpus.Reserved != nil {
fmt.Fprintf(&sb, "reserved=[%s]", *cpus.Reserved)
}
if cpus.Isolated != nil {
fmt.Fprintf(&sb, " isolated=[%s]", *cpus.Isolated)
}
if cpus.BalanceIsolated != nil {
fmt.Fprintf(&sb, " balanceIsolated=%t", *cpus.BalanceIsolated)
}
return sb.String()
}
// checkCpuCount check if the node has sufficient cpus
func checkCpuCount(ctx context.Context, workerNode *corev1.Node) {
onlineCPUCount, err := nodes.ExecCommandOnNode(ctx, []string{"nproc", "--all"}, workerNode)
if err != nil {
Fail(fmt.Sprintf("Failed to fetch online CPUs: %v", err))
}
onlineCPUInt, err := strconv.Atoi(onlineCPUCount)
if err != nil {
Fail(fmt.Sprintf("failed to convert online CPU count to integer: %v", err))
}
if onlineCPUInt <= minRequiredCPUs {
Skip(fmt.Sprintf("This test requires more than %d isolated CPUs, current available CPUs: %s", minRequiredCPUs, onlineCPUCount))
}
}
// deleteTestPod removes guaranteed pod
func deleteTestPod(ctx context.Context, testpod *corev1.Pod) {
// it possible that the pod already was deleted as part of the test, in this case we want to skip teardown
err := testclient.Client.Get(ctx, client.ObjectKeyFromObject(testpod), testpod)
if errors.IsNotFound(err) {
return
}
err = testclient.Client.Delete(ctx, testpod)
Expect(err).ToNot(HaveOccurred())
err = pods.WaitForDeletion(ctx, testpod, pods.DefaultDeletionTimeout*time.Second)
Expect(err).ToNot(HaveOccurred())
}
// ovnCnfNodePod returns OVN Kubenode pods running on the worker cnf node
func ovnCnfNodePod(ctx context.Context, workerNode *corev1.Node) (corev1.Pod, error) {
var ovnKubeNodePod corev1.Pod
ovnpods := &corev1.PodList{}
options := &client.ListOptions{
Namespace: "openshift-ovn-kubernetes",
}
err := testclient.Client.List(ctx, ovnpods, options)
if err != nil {
return ovnKubeNodePod, err
}
for _, pod := range ovnpods.Items {
if strings.Contains(pod.Name, "node") && pod.Spec.NodeName == workerNode.Name {
ovnKubeNodePod = pod
break
}
}
testlog.Infof("ovn kube node pod running on %s is %s", workerNode.Name, ovnKubeNodePod.Name)
return ovnKubeNodePod, err
}
// ovnPodContainers returns containerids of all containers running inside ovn kube node pod
func ovnPodContainers(ovnKubeNodePod *corev1.Pod) ([]string, error) {
var ovnKubeNodePodContainerids []string
var err error
for _, ovnctn := range ovnKubeNodePod.Spec.Containers {
ctnName, err := pods.GetContainerIDByName(ovnKubeNodePod, ovnctn.Name)
if err != nil {
err = fmt.Errorf("unable to fetch container id of %v", ovnctn)
}
ovnKubeNodePodContainerids = append(ovnKubeNodePodContainerids, ctnName)
}
return ovnKubeNodePodContainerids, err
}
// getCPUMaskForPids returns cpu mask of ovs process pids
func getCPUMaskForPids(ctx context.Context, pidList []string, targetNode *corev1.Node) ([]cpuset.CPUSet, error) {
var cpumaskList []cpuset.CPUSet
for _, pid := range pidList {
cmd := []string{"taskset", "-pc", pid}
cpumask, err := nodes.ExecCommandOnNode(ctx, cmd, targetNode)
if err != nil {
return nil, fmt.Errorf("failed to fetch cpus of %s: %s", pid, err)
}
mask := strings.SplitAfter(cpumask, " ")
maskSet, err := cpuset.Parse(mask[len(mask)-1])
if err != nil {
return nil, fmt.Errorf("failed to parse cpuset: %s", err)
}
cpumaskList = append(cpumaskList, maskSet)
}
return cpumaskList, nil
}
func newDeployment() *appsv1.Deployment {
var replicas int32 = 2
dp := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
Labels: map[string]string{
"testDeployment": "",
},
Namespace: testutils.NamespaceTesting,
},
Spec: appsv1.DeploymentSpec{
Replicas: &replicas,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"type": "telco",
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"type": "telco",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "test",
Image: images.Test(),
Command: []string{"sleep", "inf"},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("200Mi"),
corev1.ResourceCPU: resource.MustParse("2"),
},
},
},
},
},
},
},
}
return dp
}
// waitForCondition wait for deployment to be ready
func waitForCondition(deployment *appsv1.Deployment, status appsv1.DeploymentStatus) error {
var err error
var val bool
err = wait.PollUntilContextTimeout(context.TODO(), 5*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
if err != nil {
if errors.IsNotFound(err) {
return false, fmt.Errorf("deployment not found")
}
return false, err
}
val = deployment.Status.Replicas == status.Replicas && deployment.Status.AvailableReplicas == status.AvailableReplicas
return val, err
})
return err
}
// ovsSystemdServicesOnOvsSlice returns the systemd services dependent on ovs.slice cgroup
func ovsSystemdServicesOnOvsSlice(ctx context.Context, workerRTNode *corev1.Node) []string {
ovsServices, err := systemd.ShowProperty(context.TODO(), "ovs.slice", "RequiredBy", workerRTNode)
Expect(err).ToNot(HaveOccurred())
serviceList := strings.Split(strings.TrimSpace(ovsServices), "=")
ovsSystemdServices := strings.Split(serviceList[1], " ")
Expect(len(ovsSystemdServices)).ToNot(Equal(0), "OVS Services not moved under ovs.slice cgroup")
return ovsSystemdServices
}
// ovsPids Returns Pid of ovs services running inside the ovs.slice cgroup
func ovsPids(ctx context.Context, ovsSystemdServices []string, workerRTNode *corev1.Node) ([]string, error) {
var pidList []string
var err error
for _, service := range ovsSystemdServices {
//we need to ignore oneshot services which are part of ovs.slices
serviceType, err := systemd.ShowProperty(ctx, service, "Type", workerRTNode)
if err != nil {
return nil, err
}
if strings.Contains(serviceType, "oneshot") {
continue
}
pid, err := systemd.ShowProperty(context.TODO(), service, "ExecMainPID", workerRTNode)
ovsPid := strings.Split(strings.TrimSpace(pid), "=")
pidList = append(pidList, ovsPid[1])
}
return pidList, err
}
// taskSet returns cpus used by the pid
func taskSet(ctx context.Context, pid string, workerRTNode *corev1.Node) cpuset.CPUSet {
cmd := []string{"taskset", "-pc", pid}
output, err := nodes.ExecCommandOnNode(ctx, cmd, workerRTNode)
Expect(err).ToNot(HaveOccurred(), "unable to fetch cpus using taskset")
tasksetOutput := strings.Split(strings.TrimSpace(output), ":")
cpus := strings.TrimSpace(tasksetOutput[1])
ctnCpuset, err := cpuset.Parse(cpus)
Expect(err).ToNot(HaveOccurred(), "Unable to parse %s", cpus)
return ctnCpuset
}