/
cpu.go
360 lines (307 loc) · 14.3 KB
/
cpu.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
package hotplug
import (
"context"
"encoding/json"
"fmt"
"time"
"kubevirt.io/kubevirt/tests/libnet"
migrationsv1 "kubevirt.io/api/migrations/v1alpha1"
"kubevirt.io/kubevirt/pkg/libvmi"
"kubevirt.io/kubevirt/pkg/pointer"
"kubevirt.io/kubevirt/tests/framework/checks"
"kubevirt.io/kubevirt/tests/libmigration"
"kubevirt.io/kubevirt/tests/libvmifact"
"kubevirt.io/kubevirt/tests/flags"
util2 "kubevirt.io/kubevirt/tests/util"
"kubevirt.io/kubevirt/tests/testsuite"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/resource"
k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/kubevirt/pkg/apimachinery/patch"
"kubevirt.io/kubevirt/tests"
"kubevirt.io/kubevirt/tests/decorators"
"kubevirt.io/kubevirt/tests/framework/kubevirt"
. "kubevirt.io/kubevirt/tests/framework/matcher"
"kubevirt.io/kubevirt/tests/libpod"
"kubevirt.io/kubevirt/tests/libwait"
testsmig "kubevirt.io/kubevirt/tests/migration"
)
var _ = Describe("[sig-compute][Serial]CPU Hotplug", decorators.SigCompute, decorators.SigComputeMigrations, decorators.RequiresTwoSchedulableNodes, decorators.VMLiveUpdateFeaturesGate, Serial, func() {
var (
virtClient kubecli.KubevirtClient
)
BeforeEach(func() {
virtClient = kubevirt.Client()
originalKv := util2.GetCurrentKv(virtClient)
updateStrategy := &v1.KubeVirtWorkloadUpdateStrategy{
WorkloadUpdateMethods: []v1.WorkloadUpdateMethod{v1.WorkloadUpdateMethodLiveMigrate},
}
rolloutStrategy := pointer.P(v1.VMRolloutStrategyLiveUpdate)
patchWorkloadUpdateMethodAndRolloutStrategy(originalKv.Name, virtClient, updateStrategy, rolloutStrategy)
currentKv := util2.GetCurrentKv(virtClient)
tests.WaitForConfigToBePropagatedToComponent(
"kubevirt.io=virt-controller",
currentKv.ResourceVersion,
tests.ExpectResourceVersionToBeLessEqualThanConfigVersion,
time.Minute)
})
Context("A VM with cpu.maxSockets set higher than cpu.sockets", func() {
type cpuCount struct {
enabled int
disabled int
}
countDomCPUs := func(vmi *v1.VirtualMachineInstance) (count cpuCount) {
domSpec, err := tests.GetRunningVMIDomainSpec(vmi)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, domSpec.VCPUs).NotTo(BeNil())
for _, cpu := range domSpec.VCPUs.VCPU {
if cpu.Enabled == "yes" {
count.enabled++
} else {
ExpectWithOffset(1, cpu.Enabled).To(Equal("no"))
ExpectWithOffset(1, cpu.Hotpluggable).To(Equal("yes"))
count.disabled++
}
}
return
}
It("[test_id:10811]should successfully plug vCPUs", func() {
By("Creating a running VM with 1 socket and 2 max sockets")
const (
maxSockets uint32 = 2
)
vmi := libvmifact.NewAlpineWithTestTooling(
libnet.WithMasqueradeNetworking()...,
)
vmi.Namespace = testsuite.GetTestNamespace(vmi)
vmi.Spec.Domain.CPU = &v1.CPU{
Sockets: 1,
Cores: 2,
Threads: 1,
MaxSockets: maxSockets,
}
vm := libvmi.NewVirtualMachine(vmi, libvmi.WithRunning())
vm, err := virtClient.VirtualMachine(vm.Namespace).Create(context.Background(), vm, k8smetav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
Eventually(ThisVM(vm), 360*time.Second, 1*time.Second).Should(BeReady())
libwait.WaitForSuccessfulVMIStart(vmi)
By("Ensuring the compute container has 200m CPU")
compute := libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))
Expect(compute).NotTo(BeNil(), "failed to find compute container")
reqCpu := compute.Resources.Requests.Cpu().Value()
expCpu := resource.MustParse("200m")
Expect(reqCpu).To(Equal(expCpu.Value()))
By("Ensuring the libvirt domain has 2 enabled cores and 2 hotpluggable cores")
Expect(countDomCPUs(vmi)).To(Equal(cpuCount{
enabled: 2,
disabled: 2,
}))
By("Enabling the second socket")
patchData, err := patch.GenerateTestReplacePatch("/spec/template/spec/domain/cpu/sockets", 1, 2)
Expect(err).NotTo(HaveOccurred())
_, err = virtClient.VirtualMachine(vm.Namespace).Patch(context.Background(), vm.Name, types.JSONPatchType, patchData, k8smetav1.PatchOptions{})
Expect(err).ToNot(HaveOccurred())
By("Waiting for hot change CPU condition to appear")
// Need to wait for the hotplug to begin.
Eventually(ThisVMI(vmi), 1*time.Minute, 2*time.Second).Should(HaveConditionTrue(v1.VirtualMachineInstanceVCPUChange))
By("Ensuring live-migration started")
var migration *v1.VirtualMachineInstanceMigration
Eventually(func() bool {
migrations, err := virtClient.VirtualMachineInstanceMigration(vm.Namespace).List(&k8smetav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
for _, mig := range migrations.Items {
if mig.Spec.VMIName == vmi.Name {
migration = mig.DeepCopy()
return true
}
}
return false
}, 30*time.Second, time.Second).Should(BeTrue())
libmigration.ExpectMigrationToSucceedWithDefaultTimeout(virtClient, migration)
By("Ensuring the libvirt domain has 4 enabled cores")
Eventually(func() cpuCount {
vmi, err = virtClient.VirtualMachineInstance(vm.Namespace).Get(context.Background(), vm.Name, k8smetav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
return countDomCPUs(vmi)
}, 240*time.Second, time.Second).Should(Equal(cpuCount{
enabled: 4,
disabled: 0,
}))
By("Ensuring the virt-launcher pod now has 400m CPU")
compute = libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))
Expect(compute).NotTo(BeNil(), "failed to find compute container")
reqCpu = compute.Resources.Requests.Cpu().Value()
expCpu = resource.MustParse("400m")
Expect(reqCpu).To(Equal(expCpu.Value()))
By("Ensuring the vm doesn't have a RestartRequired condition")
vm, err = virtClient.VirtualMachine(vm.Namespace).Get(context.Background(), vm.Name, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
Eventually(ThisVM(vm), 4*time.Minute, 2*time.Second).Should(HaveConditionMissingOrFalse(v1.VirtualMachineRestartRequired))
By("Changing the number of CPU cores")
patchData, err = patch.GenerateTestReplacePatch("/spec/template/spec/domain/cpu/cores", 2, 4)
Expect(err).NotTo(HaveOccurred())
_, err = virtClient.VirtualMachine(vm.Namespace).Patch(context.Background(), vm.Name, types.JSONPatchType, patchData, k8smetav1.PatchOptions{})
Expect(err).ToNot(HaveOccurred())
By("Ensuring the vm has a RestartRequired condition")
Eventually(ThisVM(vm), 4*time.Minute, 2*time.Second).Should(HaveConditionTrue(v1.VirtualMachineRestartRequired))
By("Restarting the VM and expecting RestartRequired to be gone")
err = virtClient.VirtualMachine(vm.Namespace).Restart(context.Background(), vm.Name, &v1.RestartOptions{})
Expect(err).NotTo(HaveOccurred())
Eventually(ThisVM(vm), 4*time.Minute, 2*time.Second).Should(HaveConditionMissingOrFalse(v1.VirtualMachineRestartRequired))
})
It("[test_id:10822]should successfully plug guaranteed vCPUs", decorators.RequiresTwoWorkerNodesWithCPUManager, func() {
checks.ExpectAtLeastTwoWorkerNodesWithCPUManager(virtClient)
const maxSockets uint32 = 3
By("Creating a running VM with 1 socket and 2 max sockets")
vmi := libvmifact.NewAlpineWithTestTooling(
libnet.WithMasqueradeNetworking()...,
)
vmi.Namespace = testsuite.GetTestNamespace(vmi)
vmi.Spec.Domain.CPU = &v1.CPU{
Cores: 2,
Sockets: 1,
Threads: 1,
DedicatedCPUPlacement: true,
MaxSockets: maxSockets,
}
vm := libvmi.NewVirtualMachine(vmi, libvmi.WithRunning())
vm, err := virtClient.VirtualMachine(vm.Namespace).Create(context.Background(), vm, k8smetav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
Eventually(ThisVM(vm), 360*time.Second, 1*time.Second).Should(BeReady())
libwait.WaitForSuccessfulVMIStart(vmi)
By("Ensuring the compute container has 2 CPU")
compute := libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))
Expect(compute).NotTo(BeNil(), "failed to find compute container")
reqCpu := compute.Resources.Requests.Cpu().Value()
expCpu := resource.MustParse("2")
Expect(reqCpu).To(Equal(expCpu.Value()))
By("Ensuring the libvirt domain has 2 enabled cores and 4 disabled cores")
Expect(countDomCPUs(vmi)).To(Equal(cpuCount{
enabled: 2,
disabled: 4,
}))
By("starting the migration")
migration := libmigration.New(vm.Name, vm.Namespace)
migration, err = virtClient.VirtualMachineInstanceMigration(vm.Namespace).Create(migration, &metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
libmigration.ExpectMigrationToSucceedWithDefaultTimeout(virtClient, migration)
By("Enabling the second socket")
patchData, err := patch.GenerateTestReplacePatch("/spec/template/spec/domain/cpu/sockets", 1, 2)
Expect(err).NotTo(HaveOccurred())
_, err = virtClient.VirtualMachine(vm.Namespace).Patch(context.Background(), vm.Name, types.JSONPatchType, patchData, k8smetav1.PatchOptions{})
Expect(err).ToNot(HaveOccurred())
By("Waiting for hot change CPU condition to appear")
// Need to wait for the hotplug to begin.
Eventually(ThisVMI(vmi), 1*time.Minute, 2*time.Second).Should(HaveConditionTrue(v1.VirtualMachineInstanceVCPUChange))
By("Ensuring hotplug ended")
Eventually(ThisVMI(vmi), 1*time.Minute, 2*time.Second).ShouldNot(SatisfyAny(
HaveConditionTrue(v1.VirtualMachineInstanceVCPUChange),
HaveConditionFalse(v1.VirtualMachineInstanceVCPUChange),
))
By("Ensuring the libvirt domain has 4 enabled cores")
Eventually(func() cpuCount {
vmi, err = virtClient.VirtualMachineInstance(vm.Namespace).Get(context.Background(), vm.Name, k8smetav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())
return countDomCPUs(vmi)
}, 30*time.Second, time.Second).Should(Equal(cpuCount{
enabled: 4,
disabled: 2,
}))
By("Ensuring the virt-launcher pod now has 4 CPU")
compute = libpod.LookupComputeContainer(tests.GetVmiPod(virtClient, vmi))
Expect(compute).NotTo(BeNil(), "failed to find compute container")
reqCpu = compute.Resources.Requests.Cpu().Value()
expCpu = resource.MustParse("4")
Expect(reqCpu).To(Equal(expCpu.Value()))
})
})
Context("Abort CPU change", func() {
It("Test automated workload update", func() {
vmi := libvmifact.NewAlpineWithTestTooling(
libnet.WithMasqueradeNetworking()...,
)
vmi.Namespace = testsuite.GetTestNamespace(vmi)
vmi.Spec.Domain.CPU = &v1.CPU{
Sockets: 1,
Cores: 2,
Threads: 1,
MaxSockets: 2,
}
By("Limiting the bandwidth of migrations in the test namespace")
policy := testsmig.PreparePolicyAndVMIWithBandwidthLimitation(vmi, resource.MustParse("1Ki"))
testsmig.CreateMigrationPolicy(virtClient, policy)
Eventually(func() *migrationsv1.MigrationPolicy {
policy, err := virtClient.MigrationPolicy().Get(context.Background(), policy.Name, metav1.GetOptions{})
if err != nil {
return nil
}
return policy
}, 30*time.Second, time.Second).ShouldNot(BeNil())
vm := libvmi.NewVirtualMachine(vmi, libvmi.WithRunning())
vm, err := virtClient.VirtualMachine(vm.Namespace).Create(context.Background(), vm, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
Eventually(ThisVM(vm), 360*time.Second, 1*time.Second).Should(BeReady())
libwait.WaitForSuccessfulVMIStart(vmi)
// Update the CPU number and trigger the workload update
// and migration
By("Enabling the second socket")
p, err := patch.New(patch.WithAdd("/spec/template/spec/domain/cpu/sockets", 2)).GeneratePayload()
Expect(err).NotTo(HaveOccurred())
_, err = virtClient.VirtualMachine(vm.Namespace).Patch(context.Background(), vm.Name, types.JSONPatchType, p, k8smetav1.PatchOptions{})
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
migrations, err := virtClient.VirtualMachineInstanceMigration(vm.Namespace).List(&k8smetav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
for _, mig := range migrations.Items {
if mig.Spec.VMIName == vmi.Name {
return true
}
}
return false
}, 30*time.Second, time.Second).Should(BeTrue())
// Add annotation to cancel the workload update
vmi, err = virtClient.VirtualMachineInstance(vm.Namespace).Get(context.Background(), vm.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
vmi.ObjectMeta.Annotations[v1.WorkloadUpdateMigrationAbortionAnnotation] = ""
p, err = patch.New(patch.WithAdd("/metadata/annotations", vmi.ObjectMeta.Annotations)).GeneratePayload()
Expect(err).ToNot(HaveOccurred())
_, err = virtClient.VirtualMachineInstance(vmi.Namespace).Patch(context.Background(), vmi.Name, types.JSONPatchType, p, metav1.PatchOptions{})
Expect(err).ToNot(HaveOccurred())
Eventually(func() bool {
vmi, err = virtClient.VirtualMachineInstance(vm.Namespace).Get(context.Background(), vm.Name, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
return metav1.HasAnnotation(vmi.ObjectMeta, v1.WorkloadUpdateMigrationAbortionAnnotation)
}, 30*time.Second, time.Second).Should(BeTrue())
// Wait until the migration is cancelled by the workload
// updater
Eventually(func() bool {
migrations, err := virtClient.VirtualMachineInstanceMigration(vm.Namespace).List(&k8smetav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
for _, mig := range migrations.Items {
if mig.Spec.VMIName == vmi.Name {
return true
}
}
return false
}, 30*time.Second, time.Second).Should(BeFalse())
})
})
})
func patchWorkloadUpdateMethodAndRolloutStrategy(kvName string, virtClient kubecli.KubevirtClient, updateStrategy *v1.KubeVirtWorkloadUpdateStrategy, rolloutStrategy *v1.VMRolloutStrategy) {
methodData, err := json.Marshal(updateStrategy)
ExpectWithOffset(1, err).To(Not(HaveOccurred()))
rolloutData, err := json.Marshal(rolloutStrategy)
ExpectWithOffset(1, err).To(Not(HaveOccurred()))
data1 := fmt.Sprintf(`{"op": "replace", "path": "/spec/workloadUpdateStrategy", "value": %s}`, string(methodData))
data2 := fmt.Sprintf(`{"op": "replace", "path": "/spec/configuration/vmRolloutStrategy", "value": %s}`, string(rolloutData))
data := []byte(fmt.Sprintf(`[%s, %s]`, data1, data2))
EventuallyWithOffset(1, func() error {
_, err := virtClient.KubeVirt(flags.KubeVirtInstallNamespace).Patch(kvName, types.JSONPatchType, data, &k8smetav1.PatchOptions{})
return err
}, 10*time.Second, 1*time.Second).ShouldNot(HaveOccurred())
}