forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirt_control_plane_test.go
284 lines (241 loc) · 9.53 KB
/
virt_control_plane_test.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
/*
* This file is part of the KubeVirt project
*
* 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.
*
* Copyright 2019 Red Hat, Inc.
*
*/
package tests_test
import (
"context"
"fmt"
"strings"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/apps/v1"
k8sv1 "k8s.io/api/core/v1"
"k8s.io/api/policy/v1beta1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k6sv1 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/kubevirt/tests"
"kubevirt.io/kubevirt/tests/decorators"
"kubevirt.io/kubevirt/tests/flags"
"kubevirt.io/kubevirt/tests/framework/checks"
"kubevirt.io/kubevirt/tests/framework/kubevirt"
"kubevirt.io/kubevirt/tests/framework/matcher"
"kubevirt.io/kubevirt/tests/libnode"
"kubevirt.io/kubevirt/tests/libpod"
"kubevirt.io/kubevirt/tests/testsuite"
"kubevirt.io/kubevirt/tests/util"
)
const (
DefaultStabilizationTimeoutInSeconds = 300
DefaultPollIntervalInSeconds = 3
)
const (
multiReplica = true
singleReplica = false
)
var _ = Describe("[Serial][ref_id:2717][sig-compute]KubeVirt control plane resilience", Serial, decorators.SigCompute, func() {
var virtCli kubecli.KubevirtClient
RegisterFailHandler(Fail)
controlPlaneDeploymentNames := []string{"virt-api", "virt-controller"}
BeforeEach(func() {
virtCli = kubevirt.Client()
})
Context("pod eviction", func() {
var nodeList []k8sv1.Node
getRunningReadyPods := func(podList *k8sv1.PodList, podNames []string, nodeNames ...string) (pods []*k8sv1.Pod) {
pods = make([]*k8sv1.Pod, 0)
for _, pod := range podList.Items {
if pod.Status.Phase != k8sv1.PodRunning {
continue
}
if success, err := matcher.HaveConditionTrue(k8sv1.PodReady).Match(pod); !success {
Expect(err).ToNot(HaveOccurred())
continue
}
for _, podName := range podNames {
if strings.HasPrefix(pod.Name, podName) {
if len(nodeNames) > 0 {
for _, nodeName := range nodeNames {
if pod.Spec.NodeName == nodeName {
deepCopy := pod.DeepCopy()
pods = append(pods, deepCopy)
}
}
} else {
deepCopy := pod.DeepCopy()
pods = append(pods, deepCopy)
}
}
}
}
return
}
getPodList := func() (podList *k8sv1.PodList, err error) {
podList, err = virtCli.CoreV1().Pods(flags.KubeVirtInstallNamespace).List(context.Background(), metav1.ListOptions{})
return
}
waitForDeploymentsToStabilize := func() (bool, error) {
deploymentsClient := virtCli.AppsV1().Deployments(flags.KubeVirtInstallNamespace)
for _, deploymentName := range controlPlaneDeploymentNames {
deployment, err := deploymentsClient.Get(context.Background(), deploymentName, metav1.GetOptions{})
if err != nil {
return false, err
}
if !(deployment.Status.UpdatedReplicas == *(deployment.Spec.Replicas) &&
deployment.Status.Replicas == *(deployment.Spec.Replicas) &&
deployment.Status.AvailableReplicas == *(deployment.Spec.Replicas)) {
return false, err
}
}
return true, nil
}
eventuallyWithTimeout := func(f func() (bool, error)) {
Eventually(f,
DefaultStabilizationTimeoutInSeconds, DefaultPollIntervalInSeconds,
).Should(BeTrue())
}
BeforeEach(func() {
nodeList = libnode.GetAllSchedulableNodes(virtCli).Items
for _, node := range nodeList {
libnode.SetNodeUnschedulable(node.Name, virtCli)
}
eventuallyWithTimeout(waitForDeploymentsToStabilize)
})
AfterEach(func() {
for _, node := range nodeList {
libnode.SetNodeSchedulable(node.Name, virtCli)
}
eventuallyWithTimeout(waitForDeploymentsToStabilize)
})
DescribeTable("evicting pods of control plane", func(podName string, isMultiReplica bool, msg string) {
if isMultiReplica {
checks.SkipIfSingleReplica(virtCli)
} else {
checks.SkipIfMultiReplica(virtCli)
}
By(fmt.Sprintf("Try to evict all pods %s\n", podName))
podList, err := getPodList()
Expect(err).ToNot(HaveOccurred())
runningPods := getRunningReadyPods(podList, []string{podName})
Expect(runningPods).ToNot(BeEmpty())
for index, pod := range runningPods {
err = virtCli.CoreV1().Pods(flags.KubeVirtInstallNamespace).EvictV1beta1(context.Background(), &v1beta1.Eviction{ObjectMeta: metav1.ObjectMeta{Name: pod.Name}})
if index == len(runningPods)-1 {
if isMultiReplica {
Expect(err).To(HaveOccurred(), msg)
} else {
Expect(err).ToNot(HaveOccurred(), msg)
}
} else {
Expect(err).ToNot(HaveOccurred())
}
}
},
Entry("[test_id:2830]last eviction should fail for multi-replica virt-controller pods",
"virt-controller", multiReplica, "no error occurred on evict of last virt-controller pod"),
Entry("[test_id:2799]last eviction should fail for multi-replica virt-api pods",
"virt-api", multiReplica, "no error occurred on evict of last virt-api pod"),
Entry("eviction of single-replica virt-controller pod should succeed",
"virt-controller", singleReplica, "error occurred on eviction of single-replica virt-controller pod"),
Entry("eviction of multi-replica virt-api pod should succeed",
"virt-api", singleReplica, "error occurred on eviction of single-replica virt-api pod"),
)
})
Context("control plane components check", func() {
When("control plane pods are running", func() {
It("[test_id:2806]virt-controller and virt-api pods have a pod disruption budget", func() {
// Single replica deployments do not create PDBs
checks.SkipIfSingleReplica(virtCli)
deploymentsClient := virtCli.AppsV1().Deployments(flags.KubeVirtInstallNamespace)
By("check deployments")
deployments, err := deploymentsClient.List(context.Background(), metav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
for _, expectedDeployment := range controlPlaneDeploymentNames {
found := false
for _, deployment := range deployments.Items {
if deployment.Name != expectedDeployment {
continue
}
found = true
break
}
if !found {
Fail(fmt.Sprintf("deployment %s not found", expectedDeployment))
}
}
By("check pod disruption budgets exist")
podDisruptionBudgetList, err := virtCli.PolicyV1().PodDisruptionBudgets(flags.KubeVirtInstallNamespace).List(context.Background(), metav1.ListOptions{})
Expect(err).ToNot(HaveOccurred())
for _, controlPlaneDeploymentName := range controlPlaneDeploymentNames {
pdbName := controlPlaneDeploymentName + "-pdb"
found := false
for _, pdb := range podDisruptionBudgetList.Items {
if pdb.Name != pdbName {
continue
}
found = true
break
}
if !found {
Fail(fmt.Sprintf("pod disruption budget %s not found for control plane pod %s", pdbName, controlPlaneDeploymentName))
}
}
})
})
When("Control plane pods temporarily lose connection to Kubernetes API", func() {
// virt-handler is the only component that has the tools to add blackhole routes for testing healthz. Ideally we would test all component healthz endpoints.
componentName := "virt-handler"
getVirtHandler := func() *v1.DaemonSet {
daemonSet, err := virtCli.AppsV1().DaemonSets(flags.KubeVirtInstallNamespace).Get(context.Background(), componentName, metav1.GetOptions{})
ExpectWithOffset(1, err).NotTo(HaveOccurred())
return daemonSet
}
readyFunc := func() int32 {
return getVirtHandler().Status.NumberReady
}
getHandlerPods := func() *k8sv1.PodList {
pods, err := virtCli.CoreV1().Pods(flags.KubeVirtInstallNamespace).List(context.Background(), metav1.ListOptions{LabelSelector: fmt.Sprintf("kubevirt.io=%s", componentName)})
Expect(err).NotTo(HaveOccurred())
return pods
}
It("should fail health checks when connectivity is lost, and recover when connectivity is regained", func() {
desiredDeamonsSetCount := getVirtHandler().Status.DesiredNumberScheduled
By("ensuring we have ready pods")
Eventually(readyFunc, 30*time.Second, time.Second).Should(BeNumerically(">", 0))
By("blocking connection to API on pods")
libpod.AddKubernetesAPIBlackhole(getHandlerPods(), componentName)
By("ensuring we no longer have a ready pod")
Eventually(readyFunc, 120*time.Second, time.Second).Should(BeNumerically("==", 0))
By("removing blockage to API")
libpod.DeleteKubernetesAPIBlackhole(getHandlerPods(), componentName)
By("ensuring we now have a ready virt-handler daemonset")
Eventually(readyFunc, 30*time.Second, time.Second).Should(BeNumerically("==", desiredDeamonsSetCount))
By("changing a setting and ensuring that the config update watcher eventually resumes and picks it up")
migrationBandwidth := resource.MustParse("1Mi")
kv := util.GetCurrentKv(virtCli)
kv.Spec.Configuration.MigrationConfiguration = &k6sv1.MigrationConfiguration{
BandwidthPerMigration: &migrationBandwidth,
}
kv = testsuite.UpdateKubeVirtConfigValue(kv.Spec.Configuration)
tests.WaitForConfigToBePropagatedToComponent("kubevirt.io=virt-handler", kv.ResourceVersion, tests.ExpectResourceVersionToBeLessEqualThanConfigVersion, 60*time.Second)
})
})
})
})