-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
assertionHelpers.go
246 lines (207 loc) · 9.22 KB
/
assertionHelpers.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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2018-2021 Authors of Cilium
package k8sTest
import (
"fmt"
"time"
"github.com/cilium/cilium/test/config"
. "github.com/cilium/cilium/test/ginkgo-ext"
"github.com/cilium/cilium/test/helpers"
. "github.com/onsi/gomega"
)
var longTimeout = 10 * time.Minute
// ExpectKubeDNSReady is a wrapper around helpers/WaitKubeDNS. It asserts that
// the error returned by that function is nil.
func ExpectKubeDNSReady(vm *helpers.Kubectl) {
By("Waiting for kube-dns to be ready")
err := vm.WaitKubeDNS()
ExpectWithOffset(1, err).Should(BeNil(), "kube-dns was not able to get into ready state")
By("Running kube-dns preflight check")
err = vm.KubeDNSPreFlightCheck()
ExpectWithOffset(1, err).Should(BeNil(), "kube-dns service not ready")
}
// ExpectCiliumReady is a wrapper around helpers/WaitForPods. It asserts that
// the error returned by that function is nil.
func ExpectCiliumReady(vm *helpers.Kubectl) {
vm.WaitForCiliumReadiness(0, "Timeout while waiting for Cilium to become ready")
err := vm.CiliumPreFlightCheck()
ExpectWithOffset(1, err).Should(BeNil(), "cilium pre-flight checks failed")
}
func ExpectCiliumNotRunning(vm *helpers.Kubectl) {
err := vm.WaitTerminatingPodsInNsWithFilter(helpers.CiliumNamespace, "-l name=cilium-operator", helpers.HelperTimeout)
ExpectWithOffset(1, err).To(BeNil(), "terminating cilium-operator pod is not deleted after timeout")
err = vm.WaitTerminatingPodsInNsWithFilter(helpers.CiliumNamespace, "-l k8s-app=cilium", helpers.HelperTimeout)
ExpectWithOffset(1, err).To(BeNil(), "terminating cilium pods are not deleted after timeout")
}
// ExpectCiliumOperatorReady is a wrapper around helpers/WaitForPods. It asserts that
// the error returned by that function is nil.
func ExpectCiliumOperatorReady(vm *helpers.Kubectl) {
By("Waiting for cilium-operator to be ready")
var err error
if vm.NumNodes() < 2 {
err = vm.WaitforNPods(helpers.CiliumNamespace, "-l name=cilium-operator", 1, longTimeout)
} else {
err = vm.WaitforPods(helpers.CiliumNamespace, "-l name=cilium-operator", longTimeout)
}
ExpectWithOffset(1, err).Should(BeNil(), "Cilium operator was not able to get into ready state")
}
// ExpectHubbleCLIReady is a wrapper around helpers/WaitForPods. It asserts
// that the error returned by that function is nil.
func ExpectHubbleCLIReady(vm *helpers.Kubectl, ns string) {
By("Waiting for hubble-cli to be ready")
err := vm.WaitforPods(ns, "-l k8s-app=hubble-cli", longTimeout)
ExpectWithOffset(1, err).Should(BeNil(), "hubble-cli was not able to get into ready state")
}
// ExpectHubbleRelayReady is a wrapper around helpers/WaitForPods. It asserts
// that the error returned by that function is nil.
func ExpectHubbleRelayReady(vm *helpers.Kubectl, ns string) {
By("Waiting for hubble-relay to be ready")
err := vm.WaitforPods(ns, "-l k8s-app=hubble-relay", longTimeout)
ExpectWithOffset(1, err).Should(BeNil(), "hubble-relay was not able to get into ready state")
}
// ExpectAllPodsTerminated is a wrapper around helpers/WaitTerminatingPods.
// It asserts that the error returned by that function is nil.
func ExpectAllPodsTerminated(vm *helpers.Kubectl) {
err := vm.WaitTerminatingPods(helpers.HelperTimeout)
ExpectWithOffset(1, err).To(BeNil(), "terminating containers are not deleted after timeout")
}
// ExpectAllPodsInNsTerminated is a wrapper around helpers/WaitTerminatingPods.
// It asserts that the error returned by that function is nil.
func ExpectAllPodsInNsTerminated(vm *helpers.Kubectl, ns string) {
err := vm.WaitTerminatingPodsInNs(ns, helpers.HelperTimeout)
ExpectWithOffset(1, err).To(BeNil(), "terminating containers are not deleted after timeout")
}
// ExpectCiliumPreFlightInstallReady is a wrapper around helpers/WaitForNPods.
// It asserts the error returned by that function is nil.
func ExpectCiliumPreFlightInstallReady(vm *helpers.Kubectl) {
By("Waiting for all cilium pre-flight pods to be ready")
err := vm.WaitforPods(helpers.CiliumNamespace, "-l k8s-app=cilium-pre-flight-check", longTimeout)
warningMessage := ""
if err != nil {
res := vm.Exec(fmt.Sprintf(
"%s -n %s get pods -l k8s-app=cilium-pre-flight-check",
helpers.KubectlCmd, helpers.CiliumNamespace))
warningMessage = res.Stdout()
}
Expect(err).To(BeNil(), "cilium pre-flight check is not ready after timeout, pods status:\n %s", warningMessage)
}
// DeployCiliumAndDNS deploys DNS and cilium into the kubernetes cluster
func DeployCiliumAndDNS(vm *helpers.Kubectl, ciliumFilename string) {
DeployCiliumOptionsAndDNS(vm, ciliumFilename, map[string]string{})
}
func redeployCilium(vm *helpers.Kubectl, ciliumFilename string, options map[string]string) {
By("Installing Cilium")
err := vm.CiliumInstall(ciliumFilename, options)
Expect(err).To(BeNil(), "Cilium cannot be installed")
vm.WaitForCiliumReadiness(0, "Timeout while waiting for Cilium to become ready")
}
// RedeployCilium reinstantiates the Cilium DS and ensures it is running.
//
// This helper is only appropriate for reconfiguring Cilium in the middle of
// an existing testsuite that calls DeployCiliumAndDNS(...).
func RedeployCilium(vm *helpers.Kubectl, ciliumFilename string, options map[string]string) {
redeployCilium(vm, ciliumFilename, options)
err := vm.CiliumPreFlightCheck()
ExpectWithOffset(1, err).Should(BeNil(), "cilium pre-flight checks failed")
ExpectCiliumOperatorReady(vm)
}
// UninstallCiliumFromManifest uninstall a deployed Cilium configuration from the
// provided manifest file.
// Treat this as a cleanup function for RedeployCilium/Redeploy/DeployCiliumAndDNS/CiliumInstall.
func UninstallCiliumFromManifest(vm *helpers.Kubectl, ciliumFilename string) {
By("Removing Cilium installation using generated helm manifest")
Expect(vm.DeleteAndWait(ciliumFilename, true).GetError()).
To(BeNil(), "Error removing cilium from installed manifest")
}
// RedeployCiliumWithMerge merges the configuration passed as "from" into
// "options", allowing the caller to preserve the previous Cilium
// configuration, along with passing new configuration. This function behaves
// equivalently to RedeployCilium. Note that "options" is deep copied, meaning
// it will NOT be modified. Any modifications will be local to this function.
func RedeployCiliumWithMerge(vm *helpers.Kubectl,
ciliumFilename string,
from, options map[string]string) {
// Merge configuration
newOpts := make(map[string]string, len(options))
for k, v := range from {
newOpts[k] = v
}
for k, v := range options {
newOpts[k] = v
}
RedeployCilium(vm, ciliumFilename, newOpts)
}
// optionChangeRequiresPodRedeploy returns true if the difference between the
// specified options requires redeployment of all pods to ensure that the
// datapath is operating consistently.
func optionChangeRequiresPodRedeploy(prev, next map[string]string) bool {
// See GH-16717, as of v1.10.x Cilium does not support migrating
// between endpointRoutes modes without restarting pods.
// Also, the default setting for endpointRoutes is disabled.
// If either of these properties change, this logic needs updating!
a := "false"
if opt, ok := prev["endpointRoutes.enabled"]; ok {
a = opt
}
b := "false"
if opt, ok := next["endpointRoutes.enabled"]; ok {
b = opt
}
if a != b {
return true
}
// Switching on and off KPR affects who is handling service traffic.
// E.g., off => on some traffic might be handled by iptables. For existing
// connections it might not have enough of state information which could
// lead to connection interruptions. To avoid it, restart the pods to restart
// such connections.
a = "disabled"
if opt, ok := prev["kubeProxyReplacement"]; ok {
a = opt
}
b = "disabled"
if opt, ok := next["kubeProxyReplacement"]; ok {
b = opt
}
if a != b {
return true
}
return false
}
// DeployCiliumOptionsAndDNS deploys DNS and cilium with options into the kubernetes cluster
func DeployCiliumOptionsAndDNS(vm *helpers.Kubectl, ciliumFilename string, options map[string]string) {
prevOptions := vm.CiliumOptions()
redeployCilium(vm, ciliumFilename, options)
vm.RestartUnmanagedPodsInNamespace(helpers.LogGathererNamespace)
forceDNSRedeploy := optionChangeRequiresPodRedeploy(prevOptions, options)
vm.RedeployKubernetesDnsIfNecessary(forceDNSRedeploy)
switch helpers.GetCurrentIntegration() {
case helpers.CIIntegrationGKE:
if helpers.LogGathererNamespace != helpers.KubeSystemNamespace {
vm.RestartUnmanagedPodsInNamespace(helpers.KubeSystemNamespace)
}
}
err := vm.CiliumPreFlightCheck()
ExpectWithOffset(1, err).Should(BeNil(), "cilium pre-flight checks failed")
ExpectCiliumOperatorReady(vm)
switch helpers.GetCurrentIntegration() {
case helpers.CIIntegrationGKE:
err := vm.WaitforPods(helpers.KubeSystemNamespace, "", longTimeout)
ExpectWithOffset(1, err).Should(BeNil(), "kube-system pods were not able to get into ready state after restart")
}
}
// SkipIfBenchmark will skip the test if benchmark is not specified
func SkipIfBenchmark() {
if !config.CiliumTestConfig.Benchmarks {
Skip("Benchmarks are skipped, specify -cilium.Benchmarks")
}
}
// SkipIfIntegration will skip a test if it's running with any of the specified
// integration.
func SkipIfIntegration(integration string) {
if helpers.IsIntegration(integration) {
Skip(fmt.Sprintf(
"This feature is not supported in Cilium %q mode. Skipping test.",
integration))
}
}