-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
cli.go
181 lines (143 loc) · 7.08 KB
/
cli.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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2018-2020 Authors of Cilium
package k8sTest
import (
"fmt"
"strings"
. "github.com/cilium/cilium/test/ginkgo-ext"
"github.com/cilium/cilium/test/helpers"
. "github.com/onsi/gomega"
)
var _ = Describe("K8sCLI", func() {
SkipContextIf(func() bool {
return helpers.DoesNotRunOnGKE() && helpers.DoesNotRunOnEKS()
}, "CLI", func() {
var kubectl *helpers.Kubectl
var ciliumFilename string
BeforeAll(func() {
kubectl = helpers.CreateKubectl(helpers.K8s1VMName(), logger)
ciliumFilename = helpers.TimestampFilename("cilium.yaml")
DeployCiliumAndDNS(kubectl, ciliumFilename)
ExpectCiliumReady(kubectl)
})
AfterAll(func() {
UninstallCiliumFromManifest(kubectl, ciliumFilename)
})
JustAfterEach(func() {
kubectl.ValidateNoErrorsInLogs(CurrentGinkgoTestDescription().Duration)
})
Context("Identity CLI testing", func() {
const (
manifestYAML = "test-cli.yaml"
fooID = "foo"
fooNode = "k8s1"
// These labels are automatically added to all pods in the default namespace.
defaultLabels = "k8s:io.cilium.k8s.namespace.labels.kubernetes.io/metadata.name=default " +
"k8s:io.cilium.k8s.policy.cluster=default " +
"k8s:io.cilium.k8s.policy.serviceaccount=default " +
"k8s:io.kubernetes.pod.namespace=default"
)
var (
cliManifest string
ciliumPod string
err error
identity int64
)
BeforeAll(func() {
cliManifest = helpers.ManifestGet(kubectl.BasePath(), manifestYAML)
res := kubectl.ApplyDefault(cliManifest)
res.ExpectSuccess("Unable to apply %s", cliManifest)
err = kubectl.WaitforPods(helpers.DefaultNamespace, "-l id", helpers.HelperTimeout)
Expect(err).Should(BeNil(), "The pods were not ready after timeout")
ciliumPod, err = kubectl.GetCiliumPodOnNode(fooNode)
Expect(err).Should(BeNil())
err := kubectl.WaitForCEPIdentity(helpers.DefaultNamespace, fooID)
Expect(err).Should(BeNil())
ep, err := kubectl.GetCiliumEndpoint(helpers.DefaultNamespace, fooID)
Expect(err).Should(BeNil(), fmt.Sprintf("Unable to get CEP for pod %s", fooID))
identity = ep.Identity.ID
})
AfterAll(func() {
_ = kubectl.Delete(cliManifest)
ExpectAllPodsTerminated(kubectl)
})
It("Test identity list", func() {
By("Testing 'cilium identity list' for an endpoint's identity")
cmd := fmt.Sprintf("cilium identity list k8s:id=%s %s", fooID, defaultLabels)
res := kubectl.ExecPodCmd(helpers.CiliumNamespace, ciliumPod, cmd)
res.ExpectSuccess(fmt.Sprintf("Unable to get identity list output for label k8s:id=%s %s", fooID, defaultLabels))
resSingleOut := res.SingleOut()
containsIdentity := strings.Contains(resSingleOut, fmt.Sprintf("%d", identity))
Expect(containsIdentity).To(BeTrue(), "Identity %d of endpoint %s not in 'cilium identity list' output", identity, resSingleOut)
By("Testing 'cilium identity list' for reserved identities")
res = kubectl.ExecPodCmd(helpers.CiliumNamespace, ciliumPod, "cilium identity list")
res.ExpectSuccess("Unable to get identity list output")
resSingleOut = res.SingleOut()
reservedIdentities := []string{"health", "host", "world", "init"}
for _, id := range reservedIdentities {
By("Checking that reserved identity '%s' is in 'cilium identity list' output", id)
containsReservedIdentity := strings.Contains(resSingleOut, id)
Expect(containsReservedIdentity).To(BeTrue(), "Reserved identity '%s' not in 'cilium identity list' output", id)
}
})
It("Test cilium bpf metrics list", func() {
demoManifest := helpers.ManifestGet(kubectl.BasePath(), "demo-named-port.yaml")
app1Service := "app1-service"
l3L4DenyPolicy := helpers.ManifestGet(kubectl.BasePath(), "l3-l4-policy-deny.yaml")
namespaceForTest := helpers.GenerateNamespaceForTest("")
kubectl.NamespaceDelete(namespaceForTest)
kubectl.NamespaceCreate(namespaceForTest).ExpectSuccess("could not create namespace")
kubectl.Apply(helpers.ApplyOptions{FilePath: demoManifest, Namespace: namespaceForTest}).ExpectSuccess("could not create resource")
err := kubectl.WaitforPods(namespaceForTest, "-l zgroup=testapp", helpers.HelperTimeout)
Expect(err).To(BeNil(),
"testapp pods are not ready after timeout in namespace %q", namespaceForTest)
_, err = kubectl.CiliumPolicyAction(
namespaceForTest, l3L4DenyPolicy, helpers.KubectlApply, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Cannot apply L3 Deny Policy")
ciliumPodK8s1, err := kubectl.GetCiliumPodOnNode(helpers.K8s1)
ExpectWithOffset(2, err).Should(BeNil(), "Cannot get cilium pod on k8s1")
ciliumPodK8s2, err := kubectl.GetCiliumPodOnNode(helpers.K8s2)
ExpectWithOffset(2, err).Should(BeNil(), "Cannot get cilium pod on k8s2")
countBeforeK8s1, _ := helpers.GetBPFPacketsCount(kubectl, ciliumPodK8s1, "Policy denied by denylist", "ingress")
countBeforeK8s2, _ := helpers.GetBPFPacketsCount(kubectl, ciliumPodK8s2, "Policy denied by denylist", "ingress")
appPods := helpers.GetAppPods([]string{helpers.App2}, namespaceForTest, kubectl, "id")
clusterIP, _, err := kubectl.GetServiceHostPort(namespaceForTest, app1Service)
Expect(err).To(BeNil(), "Cannot get service in %q namespace", namespaceForTest)
res := kubectl.ExecPodCmd(
namespaceForTest, appPods[helpers.App2],
helpers.CurlFail("http://%s/public", clusterIP))
res.ExpectFail("Unexpected connection from %q to 'http://%s/public'",
appPods[helpers.App2], clusterIP)
countAfterK8s1, _ := helpers.GetBPFPacketsCount(kubectl, ciliumPodK8s1, "Policy denied by denylist", "ingress")
countAfterK8s2, _ := helpers.GetBPFPacketsCount(kubectl, ciliumPodK8s2, "Policy denied by denylist", "ingress")
Expect((countAfterK8s1 + countAfterK8s2) - (countBeforeK8s1 + countBeforeK8s2)).To(Equal(3))
_, err = kubectl.CiliumPolicyAction(
namespaceForTest, l3L4DenyPolicy, helpers.KubectlDelete, helpers.HelperTimeout)
Expect(err).Should(BeNil(), "Cannot delete L3 Policy")
kubectl.NamespaceDelete(namespaceForTest)
})
})
Context("stdout/stderr testing", func() {
var (
ciliumPod string
err error
)
BeforeAll(func() {
ciliumPod, err = kubectl.GetCiliumPodOnNode("k8s1")
Expect(err).Should(BeNil())
})
It("Root command help should print to stdout", func() {
res := kubectl.ExecPodCmd(helpers.CiliumNamespace, ciliumPod, "cilium help")
Expect(res.Stdout()).Should(ContainSubstring("Use \"cilium [command] --help\" for more information about a command."))
})
It("Subcommand help should print to stdout", func() {
res := kubectl.ExecPodCmd(helpers.CiliumNamespace, ciliumPod, "cilium help bpf")
Expect(res.Stdout()).Should(ContainSubstring("Use \"cilium bpf [command] --help\" for more information about a command."))
})
It("Failed subcommand should print help to stdout", func() {
res := kubectl.ExecPodCmd(helpers.CiliumNamespace, ciliumPod, "cilium endpoint confi 173")
Expect(res.Stdout()).Should(ContainSubstring("Use \"cilium endpoint [command] --help\" for more information about a command."))
})
})
})
})