forked from cilium/cilium
/
assertionHelpers.go
188 lines (158 loc) · 6.91 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
// Copyright 2018-2019 Authors of Cilium
//
// 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 k8sTest
import (
"context"
"fmt"
"time"
. "github.com/cilium/cilium/test/ginkgo-ext"
"github.com/cilium/cilium/test/helpers"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
)
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) {
err := vm.WaitKubeDNS()
ExpectWithOffset(1, err).Should(BeNil(), "kube-dns was not able to get into ready state")
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) {
err := vm.WaitforPods(helpers.KubeSystemNamespace, "-l k8s-app=cilium", longTimeout)
ExpectWithOffset(1, err).Should(BeNil(), "cilium was not able to get into ready state")
err = vm.CiliumPreFlightCheck()
ExpectWithOffset(1, err).Should(BeNil(), "cilium pre-flight checks failed")
}
// ExpectCiliumOperatorReady is a wrapper around helpers/WaitForPods. It asserts that
// the error returned by that function is nil.
func ExpectCiliumOperatorReady(vm *helpers.Kubectl) {
err := vm.WaitforPods(helpers.KubeSystemNamespace, "-l name=cilium-operator", longTimeout)
ExpectWithOffset(1, err).Should(BeNil(), "Cilium operator was not able to get into ready state")
}
// ExpectCiliumRunning is a wrapper around helpers/WaitForNPods. It
// asserts the cilium pods are running on all nodes (but not yet ready!).
func ExpectCiliumRunning(vm *helpers.Kubectl) {
err := vm.WaitforNPodsRunning(helpers.KubeSystemNamespace, "-l k8s-app=cilium", vm.GetNumNodes(), longTimeout)
ExpectWithOffset(1, err).Should(BeNil(), "cilium was not able to get into ready state")
}
// ExpectAllPodsTerminated is a wrapper around helpers/WaitCleanAllTerminatingPods.
// It asserts that the error returned by that function is nil.
func ExpectAllPodsTerminated(vm *helpers.Kubectl) {
err := vm.WaitCleanAllTerminatingPods(helpers.HelperTimeout)
ExpectWithOffset(1, err).To(BeNil(), "terminating containers are not deleted after timeout")
}
// ExpectETCDOperatorReady is a wrapper around helpers/WaitForNPods. It asserts
// the error returned by that function is nil.
func ExpectETCDOperatorReady(vm *helpers.Kubectl) {
// Etcd operator creates 5 nodes (1 cilium-etcd-operator + 1 etcd-operator + 3 etcd nodes),
// the new pods are added when the previous is ready,
// so we need to wait until 5 pods are in ready state.
// This is to avoid cases where a few pods are ready, but the
// new one is not created yet.
By("Waiting for all etcd-operator pods to be ready")
err := vm.WaitforNPods(helpers.KubeSystemNamespace, "-l io.cilium/app=etcd-operator", 5, longTimeout)
warningMessage := ""
if err != nil {
res := vm.Exec(fmt.Sprintf(
"%s -n %s get pods -l io.cilium/app=etcd-operator",
helpers.KubectlCmd, helpers.KubeSystemNamespace))
warningMessage = res.Output().String()
}
Expect(err).To(BeNil(), "etcd-operator is not ready after timeout, pods status:\n %s", warningMessage)
}
// 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.KubeSystemNamespace, "-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.KubeSystemNamespace))
warningMessage = res.Output().String()
}
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) {
DeployCiliumOptionsAndDNS(vm, []string{})
}
// DeployCiliumOptionsAndDNS deploys DNS and cilium with options into the kubernetes cluster
func DeployCiliumOptionsAndDNS(vm *helpers.Kubectl, options []string) {
By("Installing Cilium")
err := vm.CiliumInstall(options)
Expect(err).To(BeNil(), "Cilium cannot be installed")
ExpectCiliumRunning(vm)
By("Installing DNS Deployment")
_ = vm.ApplyDefault(helpers.DNSDeployment())
switch helpers.GetCurrentIntegration() {
case helpers.CIIntegrationFlannel:
By("Installing Flannel")
vm.ApplyDefault(helpers.GetFilePath("../examples/kubernetes/addons/flannel/flannel.yaml"))
default:
}
ExpectCiliumReady(vm)
ExpectCiliumOperatorReady(vm)
ExpectKubeDNSReady(vm)
}
// SkipIfFlannel will skip the test if it's running over Flannel datapath mode.
func SkipIfFlannel() {
if helpers.GetCurrentIntegration() == helpers.CIIntegrationFlannel {
Skip(fmt.Sprintf(
"This feature is not supported in Cilium %q mode. Skipping test.",
helpers.CIIntegrationFlannel))
}
}
func deleteCiliumDS(kubectl *helpers.Kubectl) {
// Do not assert on success in AfterEach intentionally to avoid
// incomplete teardown.
_ = kubectl.DeleteResource("ds", fmt.Sprintf("-n %s cilium", helpers.KubeSystemNamespace))
Expect(waitToDeleteCilium(kubectl, logger)).To(BeNil(), "timed out deleting Cilium pods")
}
func deleteETCDOperator(kubectl *helpers.Kubectl) {
// Do not assert on success in AfterEach intentionally to avoid
// incomplete teardown.
_ = kubectl.DeleteResource("deploy", fmt.Sprintf("-n %s -l io.cilium/app=etcd-operator", helpers.KubeSystemNamespace))
_ = kubectl.DeleteResource("pod", fmt.Sprintf("-n %s -l io.cilium/app=etcd-operator", helpers.KubeSystemNamespace))
_ = kubectl.WaitCleanAllTerminatingPods(helpers.HelperTimeout)
}
func waitToDeleteCilium(kubectl *helpers.Kubectl, logger *logrus.Entry) error {
var (
pods []string
err error
)
ctx, cancel := context.WithTimeout(context.Background(), helpers.HelperTimeout)
defer cancel()
status := 1
for status > 0 {
select {
case <-ctx.Done():
return fmt.Errorf("timed out waiting to delete Cilium: pods still remaining: %s", pods)
default:
}
pods, err = kubectl.GetCiliumPodsContext(ctx, helpers.KubeSystemNamespace)
status := len(pods)
logger.Infof("Cilium pods terminating '%d' err='%v' pods='%v'", status, err, pods)
if status == 0 {
return nil
}
time.Sleep(1 * time.Second)
}
return nil
}