forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chaos.go
264 lines (206 loc) · 8.55 KB
/
Chaos.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
// Copyright 2017-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"
. "github.com/cilium/cilium/test/ginkgo-ext"
"github.com/cilium/cilium/test/helpers"
. "github.com/onsi/gomega"
)
var _ = Describe("K8sChaosTest", func() {
var (
kubectl *helpers.Kubectl
demoDSPath = helpers.ManifestGet("demo_ds.yaml")
testDSService = "testds-service"
)
BeforeAll(func() {
kubectl = helpers.CreateKubectl(helpers.K8s1VMName(), logger)
DeployCiliumAndDNS(kubectl)
})
AfterFailed(func() {
kubectl.CiliumReport(helpers.KubeSystemNamespace,
"cilium service list",
"cilium endpoint list")
})
JustAfterEach(func() {
kubectl.ValidateNoErrorsInLogs(CurrentGinkgoTestDescription().Duration)
})
AfterAll(func() {
ExpectAllPodsTerminated(kubectl)
kubectl.CloseSSHClient()
})
Context("Connectivity demo application", func() {
BeforeEach(func() {
kubectl.ApplyDefault(demoDSPath).ExpectSuccess("DS deployment cannot be applied")
err := kubectl.WaitforPods(
helpers.DefaultNamespace, fmt.Sprintf("-l zgroup=testDS"), helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Pods are not ready after timeout")
})
AfterEach(func() {
kubectl.Delete(demoDSPath).ExpectSuccess(
"%s deployment cannot be deleted", demoDSPath)
ExpectAllPodsTerminated(kubectl)
})
// connectivityTest performs a few test inside:
// - tests connectivity of all client pods to the backend pods directly via ping
// - tests connectivity of all client pods to the ClusterIP of the test-ds service via curl
// - tests connectivity of all client pods to the DNS name for the test-ds service via curl
connectivityTest := func() {
pods, err := kubectl.GetPodNames(helpers.DefaultNamespace, "zgroup=testDSClient")
Expect(err).To(BeNil(), "Cannot get pods names")
Expect(len(pods)).To(BeNumerically(">", 0), "No pods available to test connectivity")
dsPods, err := kubectl.GetPodsIPs(helpers.DefaultNamespace, "zgroup=testDS")
Expect(err).To(BeNil(), "Cannot get daemonset pods IPS")
Expect(len(dsPods)).To(BeNumerically(">", 0), "No pods available to test connectivity")
By("Waiting for kube-dns entry for service testds-service")
err = kubectl.WaitForKubeDNSEntry(testDSService, helpers.DefaultNamespace)
ExpectWithOffset(1, err).To(BeNil(), "DNS entry is not ready after timeout")
By("Getting ClusterIP For testds-service")
host, _, err := kubectl.GetServiceHostPort(helpers.DefaultNamespace, "testds-service")
ExpectWithOffset(1, err).To(BeNil(), "unable to get ClusterIP and port for service testds-service")
for _, pod := range pods {
for _, ip := range dsPods {
By("Pinging testds pod with IP %q from client pod %q", ip, pod)
res := kubectl.ExecPodCmd(
helpers.DefaultNamespace, pod, helpers.Ping(ip))
log.Debugf("Pod %s ping %v", pod, ip)
ExpectWithOffset(1, res).To(helpers.CMDSuccess(),
"Cannot ping from %q to %q", pod, ip)
}
By("Curling testds-service via ClusterIP %q", host)
res := kubectl.ExecPodCmd(
helpers.DefaultNamespace, pod, helpers.CurlFail("http://%s:80/", host))
ExpectWithOffset(1, res).To(helpers.CMDSuccess(),
"Cannot curl from %q to testds-service via ClusterIP", pod)
By("Curling testds-service via DNS hostname")
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, pod, helpers.CurlFail("http://%s:80/", testDSService))
ExpectWithOffset(1, res).To(helpers.CMDSuccess(),
"Cannot curl from %q to testds-service via DNS hostname", pod)
}
}
It("Endpoint can still connect while Cilium is not running", func() {
By("Waiting for deployed pods to be ready")
err := kubectl.WaitforPods(
helpers.DefaultNamespace,
fmt.Sprintf("-l zgroup=testDSClient"), helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Pods are not ready after timeout")
err = kubectl.CiliumEndpointWaitReady()
Expect(err).To(BeNil(), "Endpoints are not ready after timeout")
By("Checking connectivity before restarting Cilium")
connectivityTest()
By("Deleting cilium pods")
res := kubectl.Exec(fmt.Sprintf("%s -n %s delete pods -l k8s-app=cilium",
helpers.KubectlCmd, helpers.KubeSystemNamespace))
res.ExpectSuccess()
ExpectAllPodsTerminated(kubectl)
ExpectCiliumReady(kubectl)
err = kubectl.CiliumEndpointWaitReady()
Expect(err).To(BeNil(), "Endpoints are not ready after Cilium restarts")
By("Checking connectivity after restarting Cilium")
connectivityTest()
By("Uninstall cilium pods")
res = kubectl.DeleteResource(
"ds", fmt.Sprintf("-n %s cilium", helpers.KubeSystemNamespace))
res.ExpectSuccess("Cilium DS cannot be deleted")
ExpectAllPodsTerminated(kubectl)
By("Checking connectivity after uninstalling Cilium")
connectivityTest()
By("Install cilium pods")
err = kubectl.CiliumInstall([]string{})
Expect(err).To(BeNil(), "Cilium cannot be installed")
ExpectCiliumReady(kubectl)
err = kubectl.CiliumEndpointWaitReady()
Expect(err).To(BeNil(), "Endpoints are not ready after timeout")
By("Checking connectivity after reinstalling Cilium")
connectivityTest()
})
})
Context("Restart with long lived connections", func() {
var (
netperfManifest = helpers.ManifestGet("netperf-deployment.yaml")
netperfPolicy = helpers.ManifestGet("netperf-policy.yaml")
netperfServiceName = "netperf-service"
podsIps map[string]string
netperfClient = "netperf-client"
netperfServer = "netperf-server"
)
BeforeAll(func() {
kubectl.ApplyDefault(netperfManifest).ExpectSuccess("Netperf cannot be deployed")
err := kubectl.WaitforPods(
helpers.DefaultNamespace,
fmt.Sprintf("-l zgroup=testapp"), helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Pods are not ready after timeout")
podsIps, err = kubectl.GetPodsIPs(helpers.DefaultNamespace, "zgroup=testapp")
Expect(err).To(BeNil(), "Cannot get pods ips")
_, _, err = kubectl.GetServiceHostPort(helpers.DefaultNamespace, netperfServiceName)
Expect(err).To(BeNil(), "cannot get service netperf ip")
})
AfterAll(func() {
_ = kubectl.Delete(netperfManifest)
})
AfterEach(func() {
_ = kubectl.Delete(netperfPolicy)
})
restartCilium := func() {
ciliumFilter := "k8s-app=cilium"
By("Deleting all cilium pods")
res := kubectl.Exec(fmt.Sprintf(
"%s -n %s delete pods -l %s",
helpers.KubectlCmd, helpers.KubeSystemNamespace, ciliumFilter))
res.ExpectSuccess("Failed to delete cilium pods")
By("Waiting cilium pods to terminate")
ExpectAllPodsTerminated(kubectl)
By("Waiting for cilium pods to be ready")
err := kubectl.WaitforPods(
helpers.KubeSystemNamespace, fmt.Sprintf("-l %s", ciliumFilter), helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Pods are not ready after timeout")
err = kubectl.CiliumEndpointWaitReady()
Expect(err).To(BeNil(), "Endpoints are not ready after timeout")
}
It("TCP connection is not dropped when cilium restarts", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
res := kubectl.ExecPodCmdBackground(
ctx,
helpers.DefaultNamespace,
netperfClient,
fmt.Sprintf("netperf -l 300 -t TCP_STREAM -H %s", podsIps[netperfServer]))
restartCilium()
By("Stopping netperf client test")
cancel()
res.WaitUntilFinish()
res.ExpectSuccess("Failed while cilium was restarting")
})
It("L3/L4 policies still work while Cilium is restarted", func() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
res := kubectl.ExecPodCmdBackground(
ctx,
helpers.DefaultNamespace,
netperfClient,
fmt.Sprintf("netperf -l 300 -t TCP_STREAM -H %s", podsIps[netperfServer]))
By("Installing the L3-L4 Policy")
_, err := kubectl.CiliumPolicyAction(
helpers.DefaultNamespace, netperfPolicy, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Cannot install %q policy", netperfPolicy)
restartCilium()
By("Stopping netperf client test")
cancel()
res.WaitUntilFinish()
res.ExpectSuccess("Failed while cilium was restarting")
})
})
})