forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fqdn.go
325 lines (269 loc) · 12.6 KB
/
fqdn.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package k8sTest
import (
"fmt"
"net"
. "github.com/onsi/gomega"
. "github.com/cilium/cilium/test/ginkgo-ext"
"github.com/cilium/cilium/test/helpers"
)
// The 5.4 CI job is intended to catch BPF complexity regressions and as such
// doesn't need to execute this test suite.
var _ = SkipDescribeIf(helpers.RunsOn54Kernel, "K8sAgentFQDNTest", func() {
var (
kubectl *helpers.Kubectl
demoManifest = ""
ciliumFilename string
apps = []string{helpers.App2, helpers.App3}
appPods map[string]string
// The IPs are updated in BeforeAll
worldTarget = "vagrant-cache.ci.cilium.io"
worldTargetIP = "147.75.38.95"
worldInvalidTarget = "cilium.io"
worldInvalidTargetIP = "104.198.14.52"
)
BeforeAll(func() {
// In case the IPs changed from above, update them here
var lookupErr error
err := helpers.WithTimeout(func() bool {
addrs, err2 := net.LookupHost(worldTarget)
if err2 != nil {
lookupErr = fmt.Errorf("error looking up target domain: %s", err2)
return false
}
worldTargetIP = addrs[0]
return true
}, fmt.Sprintf("Could not get %s IP", worldTarget), &helpers.TimeoutConfig{Timeout: helpers.HelperTimeout})
Expect(err).Should(BeNil(), "Error obtaining IP for test: %s", lookupErr)
lookupErr = nil
err = helpers.WithTimeout(func() bool {
addrs, err2 := net.LookupHost(worldInvalidTarget)
if err2 != nil {
lookupErr = fmt.Errorf("error looking up target domain: %s", err2)
return false
}
worldInvalidTargetIP = addrs[0]
return true
}, fmt.Sprintf("Could not get %s IP", worldInvalidTarget), &helpers.TimeoutConfig{Timeout: helpers.HelperTimeout})
Expect(err).Should(BeNil(), "Error obtaining IP for test: %s", lookupErr)
kubectl = helpers.CreateKubectl(helpers.K8s1VMName(), logger)
demoManifest = helpers.ManifestGet(kubectl.BasePath(), "demo.yaml")
ciliumFilename = helpers.TimestampFilename("cilium.yaml")
DeployCiliumAndDNS(kubectl, ciliumFilename)
By("Applying demo manifest")
res := kubectl.ApplyDefault(demoManifest)
res.ExpectSuccess("Demo config cannot be deployed")
err = kubectl.WaitforPods(helpers.DefaultNamespace, "-l zgroup=testapp", helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Testapp is not ready after timeout")
appPods = helpers.GetAppPods(apps, helpers.DefaultNamespace, kubectl, "id")
// Validate that coredns is reachable from test pods
err = kubectl.NslookupInPod(helpers.DefaultNamespace, appPods[helpers.App2], "kube-dns.kube-system.svc.cluster.local")
Expect(err).Should(BeNil(), "Error reaching kube-dns before test: %s", err)
})
AfterFailed(func() {
kubectl.CiliumReport("cilium service list", "cilium endpoint list")
})
AfterAll(func() {
_ = kubectl.Delete(demoManifest)
ExpectAllPodsTerminated(kubectl)
UninstallCiliumFromManifest(kubectl, ciliumFilename)
kubectl.CloseSSHClient()
})
AfterEach(func() {
_ = kubectl.Exec(fmt.Sprintf("%s delete --all cnp", helpers.KubectlCmd))
})
It("Restart Cilium validate that FQDN is still working", func() {
// Test functionality:
// - When Cilium is running) Connectivity from App2 application can
// connect to DNS because dns-proxy filter the DNS request. If the
// connection is made correctly the IP is whitelisted by the FQDN rule
// until the DNS TTL expires.
// - When Cilium is not running) The DNS-proxy is not working, so the IP
// connectivity to an existing IP that was queried before will work,
// meanwhile connections using new DNS request will fail.
// - On restart) Cilium will restore the IPS that were white-listted in
// the FQDN and connection will work as normal.
ciliumPodK8s1, err := kubectl.GetCiliumPodOnNode(helpers.K8s1)
Expect(err).Should(BeNil(), "Cannot get cilium pod on k8s1")
monitorRes1, monitorCancel1 := kubectl.MonitorStart(ciliumPodK8s1)
ciliumPodK8s2, err := kubectl.GetCiliumPodOnNode(helpers.K8s2)
Expect(err).Should(BeNil(), "Cannot get cilium pod on k8s2")
monitorRes2, monitorCancel2 := kubectl.MonitorStart(ciliumPodK8s2)
defer func() {
monitorCancel1()
monitorCancel2()
helpers.WriteToReportFile(monitorRes1.CombineOutput().Bytes(), "fqdn-restart-cilium-monitor-k8s1.log")
helpers.WriteToReportFile(monitorRes2.CombineOutput().Bytes(), "fqdn-restart-cilium-monitor-k8s2.log")
}()
connectivityTest := func() {
By("Testing that connection from %q to %q should work",
appPods[helpers.App2], worldTarget)
res := kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail(worldTarget))
ExpectWithOffset(1, res).To(helpers.CMDSuccess(), "%q cannot curl to %q",
appPods[helpers.App2], worldTarget)
By("Testing that connection from %q to %q shouldn't work",
appPods[helpers.App2], worldInvalidTarget)
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail(worldInvalidTarget))
ExpectWithOffset(1, res).ShouldNot(helpers.CMDSuccess(),
"%q can curl to %q when it should fail", appPods[helpers.App2], worldInvalidTarget)
By("Testing that connection from %q to %q works",
appPods[helpers.App2], worldTargetIP)
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail(worldTargetIP))
res.ExpectSuccess("%q cannot curl to %q during restart", helpers.App2, worldTargetIP)
By("Testing that connection from %q to %q should not work",
appPods[helpers.App2], worldInvalidTargetIP)
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail(worldInvalidTargetIP))
res.ExpectFail("%q can connect when it should not work", helpers.App2)
}
fqndProxyPolicy := helpers.ManifestGet(kubectl.BasePath(), "fqdn-proxy-policy.yaml")
_, err = kubectl.CiliumPolicyAction(
helpers.DefaultNamespace, fqndProxyPolicy,
helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).To(BeNil(), "Cannot install fqdn proxy policy")
connectivityTest()
By("restarting cilium pods")
// kill pid 1 in each cilium pod
cmd := fmt.Sprintf("%[1]s get pods -l k8s-app=cilium -n %[2]s | tail -n +2 | cut -d ' ' -f 1 | xargs -I{} %[1]s exec -n %[2]s {} -- kill 1",
helpers.KubectlCmd, helpers.CiliumNamespace)
quit, run := kubectl.RepeatCommandInBackground(cmd)
channelClosed := false
defer func() {
if !channelClosed {
close(quit)
}
}()
<-run // waiting for first run to finish
By("Testing connectivity when cilium is restoring using IPS without DNS")
res := kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail(worldTargetIP))
res.ExpectSuccess("%q cannot curl to %q during restart", helpers.App2, worldTargetIP)
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail(worldInvalidTargetIP))
res.ExpectFail("%q can connect when it should not work", helpers.App2)
// This test is failing consistently in quarantine, see #11213. Disable it for now
// to verify the rest of the test is running stable in quarantine. Once this is the
// case we could move the rest of the test out of quarantine and quarantine only
// this part.
if false {
// Re-run connectivity test while Cilium is still restarting. This should succeed as the same
// DNS names were used in a connectivity test before the restart.
connectivityTest()
}
channelClosed = true
close(quit)
ExpectAllPodsTerminated(kubectl)
ExpectCiliumReady(kubectl)
// Restart monitoring after Cilium restart
monitorRes1After, monitorCancel1After := kubectl.MonitorStart(ciliumPodK8s1)
monitorRes2After, monitorCancel2After := kubectl.MonitorStart(ciliumPodK8s2)
defer func() {
monitorCancel1After()
monitorCancel2After()
helpers.WriteToReportFile(monitorRes1After.CombineOutput().Bytes(), "fqdn-after-restart-cilium-monitor-k8s1.log")
helpers.WriteToReportFile(monitorRes2After.CombineOutput().Bytes(), "fqdn-after-restart-cilium-monitor-k8s2.log")
}()
// @TODO This endpoint ready call SHOULD NOT be here
// Here some packets can be lost due to two different scenarios:
//
// 1) On restore the endpoint/fqdn policies, the identity ID for the
// CIDRSet can be different, so if one endpoint start to regenerate and
// other still have the old identity things can mess around and some
// IPs are not white listed correctly. To prevent this, a restore for
// local-identities will be added in the future.
//
// 2) On restore, the Kubernetes watcher is sending the CNP back to
// Cilium, and before the endoint is restored the CNP can be applied
// without the ToCIDRSet, this means that there is no TOCIDR rule in
// the cilium policy and traffic will be drop.
// As mentioned above, these endpoints ready should not be there, the only
// reason to have this piece of code here is to reduce a flaky test.
err = kubectl.CiliumEndpointWaitReady()
Expect(err).To(BeNil(), "Endpoints are not ready after Cilium restarts")
By("Testing connectivity when cilium is *restored* using IPS without DNS")
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail(worldTargetIP))
res.ExpectSuccess("%q cannot curl to %q after restart", helpers.App2, worldTargetIP)
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail(worldInvalidTargetIP))
res.ExpectFail("%q can connect when it should not work", helpers.App2)
By("Testing connectivity using DNS request when cilium is restored correctly")
connectivityTest()
})
SkipItIf(helpers.RunsOnAKS, "Validate that multiple specs are working correctly", func() {
// To make sure that UUID in multiple specs are plumbed correctly to
// Cilium Policy
fqdnPolicy := helpers.ManifestGet(kubectl.BasePath(), "fqdn-proxy-multiple-specs.yaml")
world1Target := worldTarget
world2Target := worldInvalidTarget
_, err := kubectl.CiliumPolicyAction(
helpers.DefaultNamespace, fqdnPolicy,
helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).To(BeNil(), "Cannot install fqdn proxy policy")
By("Validating APP2 policy connectivity")
res := kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail("--retry 5 "+world1Target))
res.ExpectSuccess("Can't connect to a valid target when it should work")
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail(world2Target))
res.ExpectFail("Can connect to a valid target when it should NOT work")
By("Validating APP3 policy connectivity")
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App3],
helpers.CurlWithRetries(world2Target, 5, true))
res.ExpectSuccess("Can't connect to a valid target when it should work")
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App3],
helpers.CurlFail(world1Target))
res.ExpectFail("Can connect to a valid target when it should NOT work")
})
SkipItIf(helpers.RunsOnAKS, "Validate that FQDN policy continues to work after being updated", func() {
// To make sure that UUID in multiple specs are plumbed correctly to
// Cilium Policy
fqdnPolicy := helpers.ManifestGet(kubectl.BasePath(), "fqdn-proxy-multiple-specs.yaml")
world1Target := worldTarget
world2Target := worldInvalidTarget
_, err := kubectl.CiliumPolicyAction(
helpers.DefaultNamespace, fqdnPolicy,
helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).To(BeNil(), "Cannot install fqdn proxy policy")
By("Validating APP2 policy connectivity")
res := kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail("--retry 5 "+world1Target))
res.ExpectSuccess("Can't connect to a valid target when it should work")
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail(world2Target))
res.ExpectFail("Can connect to a valid target when it should NOT work")
By("Updating the policy to include an extra FQDN allow statement")
fqdnPolicy2 := helpers.ManifestGet(kubectl.BasePath(), "fqdn-proxy-multiple-specs-v2.yaml")
_, err = kubectl.CiliumPolicyAction(
helpers.DefaultNamespace, fqdnPolicy2,
helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).To(BeNil(), "Cannot install fqdn proxy policy")
By("Validating APP2 policy connectivity after policy change")
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail("--retry 5 "+world1Target))
res.ExpectSuccess("Can't connect to a valid target when it should work")
res = kubectl.ExecPodCmd(
helpers.DefaultNamespace, appPods[helpers.App2],
helpers.CurlFail(world2Target))
res.ExpectFail("Can connect to a valid target when it should NOT work")
})
})