-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
431 lines (392 loc) · 12.5 KB
/
main_test.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package main
import (
"context"
"sync"
"testing"
"time"
"github.com/combust-labs/netpol-tool/pkg/tool"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/fake"
clienttesting "k8s.io/client-go/testing"
"k8s.io/client-go/tools/cache"
)
func TestOne(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
watcherStarted := make(chan struct{})
// Create the fake client.
client := fake.NewSimpleClientset()
expectedWatchers := 3
registeredWatchers := 0
registeredWatchersLock := &sync.Mutex{}
// A catch-all watch reactor that allows us to inject the watcherStarted channel.
client.PrependWatchReactor("*", func(action clienttesting.Action) (handled bool, ret watch.Interface, err error) {
registeredWatchersLock.Lock()
defer registeredWatchersLock.Unlock()
registeredWatchers = registeredWatchers + 1
gvr := action.GetResource()
ns := action.GetNamespace()
watch, err := client.Tracker().Watch(gvr, ns)
if err != nil {
return false, nil, err
}
if registeredWatchers == expectedWatchers {
close(watcherStarted)
}
return true, watch, nil
})
// We will create an informer that writes added pods to a channel.
informers := informers.NewSharedInformerFactory(client, 0)
podInformer := informers.Core().V1().Pods().Informer()
podInformer.AddEventHandler(&cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
pod := obj.(*corev1.Pod)
t.Logf(" ---> pod added: %s/%s", pod.Namespace, pod.Name)
},
})
netpolInformer := informers.Networking().V1().NetworkPolicies().Informer()
netpolInformer.AddEventHandler(&cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
netpol := obj.(*networkingv1.NetworkPolicy)
t.Logf(" ---> netpol added: %s/%s", netpol.Namespace, netpol.Name)
},
})
namespaceInformer := informers.Core().V1().Namespaces().Informer()
namespaceInformer.AddEventHandler(&cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
ns := obj.(*corev1.Namespace)
t.Logf(" ---> namespace added: %s", ns.Name)
},
DeleteFunc: func(obj interface{}) {
ns := obj.(*corev1.Namespace)
t.Logf(" ---> namespace deleted: %s", ns.Name)
},
})
// Make sure informers are running.
informers.Start(ctx.Done())
// This is not required in tests, but it serves as a proof-of-concept by
// ensuring that the informer goroutine have warmed up and called List before
// we send any events to it.
cache.WaitForCacheSync(ctx.Done(), podInformer.HasSynced)
// The fake client doesn't support resource version. Any writes to the client
// after the informer's initial LIST and before the informer establishing the
// watcher will be missed by the informer. Therefore we wait until the watcher
// starts.
// Note that the fake client isn't designed to work with informer. It
// doesn't support resource version. It's encouraged to use a real client
// in an integration/E2E test if you need to test complex behavior with
// informer/controllers.
select {
case <-watcherStarted:
case <-time.After(time.Second * 2):
}
for _, ns := range fixture_namespaces.Items {
_, err := client.CoreV1().Namespaces().Create(context.TODO(), &ns, metav1.CreateOptions{})
if err != nil {
t.Fatalf("error injecting namespace add: %v", err)
}
}
for _, pod := range fixture_pods.Items {
_, err := client.CoreV1().Pods(pod.ObjectMeta.Namespace).Create(context.TODO(), &pod, metav1.CreateOptions{})
if err != nil {
t.Fatalf("error injecting pod add: %v", err)
}
}
for _, netpol := range fixture_network_policies.Items {
_, err := client.NetworkingV1().NetworkPolicies(netpol.ObjectMeta.Namespace).Create(context.TODO(), &netpol, metav1.CreateOptions{})
if err != nil {
t.Fatalf("error injecting network policy add: %v", err)
}
}
<-time.After(time.Second * 5)
loaded_namespaces, err := client.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
t.Fatalf("error listing namespaces: %v", err)
}
loaded_netpols, err := client.NetworkingV1().NetworkPolicies("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
t.Fatalf("error listing network policies: %v", err)
}
loaded_pods, err := client.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
t.Fatalf("error listing pods: %v", err)
}
assert.Equal(t, len(fixture_namespaces.Items), len(loaded_namespaces.Items))
assert.Equal(t, len(fixture_network_policies.Items), len(loaded_netpols.Items))
assert.Equal(t, len(fixture_pods.Items), len(loaded_pods.Items))
doErr := tool.NewProcessor(&tool.Args{
Namespaces: loaded_namespaces.Items,
NetworkPolicies: loaded_netpols.Items,
Pods: loaded_pods.Items,
}).Do(types.NamespacedName{})
assert.Nil(t, doErr)
for _, ns := range loaded_namespaces.Items {
client.CoreV1().Namespaces().Delete(context.Background(), ns.ObjectMeta.Name, metav1.DeleteOptions{})
}
<-time.After(time.Second * 2)
}
var fixture_pods = corev1.PodList{
Items: []corev1.Pod{
corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "cert-manager-xxxx1-yyy1",
Namespace: "cert-manager",
Labels: map[string]string{
"app": "cert-manager",
"app.kubernetes.io/component": "controller",
"app.kubernetes.io/instance": "cert-manager",
"app.kubernetes.io/name": "cert-manager",
"app.kubernetes.io/version": "v1.1.0.1",
},
},
},
corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "cert-manager-cainjector-xxxx1-yyy1",
Namespace: "cert-manager",
Labels: map[string]string{
"app": "cainjector",
"app.kubernetes.io/component": "cainjector",
"app.kubernetes.io/instance": "cert-manager",
"app.kubernetes.io/name": "cainjector",
"app.kubernetes.io/version": "v1.1.0.1",
},
},
},
corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "cert-manager-webhook-xxxx1-yyy1",
Namespace: "cert-manager",
Labels: map[string]string{
"app": "webhook",
"app.kubernetes.io/component": "cainjector",
"app.kubernetes.io/instance": "cert-manager",
"app.kubernetes.io/name": "webhook",
"app.kubernetes.io/version": "v1.1.0.1",
},
},
},
corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "cert-manager-istio-csr-xxxx1-yyy1",
Namespace: "istio-system",
Labels: map[string]string{
"app": "cert-manager-istio-csr",
"app.kubernetes.io/instance": "cert-manager-istio-csr",
"app.kubernetes.io/name": "cert-manager-istio-csr",
},
},
},
corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "istiod-1-17-0-xxxx1-yyy1",
Namespace: "istio-system",
Annotations: map[string]string{
"sidecar.istio.io/inject": "false",
},
Labels: map[string]string{
"app": "istiod",
"istio": "istiod",
"istio.io/rev": "1-17-0",
"operator.istio.io/component": "Pilot",
"sidecar.istio.io/inject": "false",
},
},
},
corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "istiod-1-18-0-xxxx1-yyy1",
Namespace: "istio-system",
Annotations: map[string]string{
"sidecar.istio.io/inject": "false",
},
Labels: map[string]string{
"app": "istiod",
"istio": "istiod",
"istio.io/rev": "1-18-0",
"operator.istio.io/component": "Pilot",
"sidecar.istio.io/inject": "false",
},
},
},
},
}
var fixture_namespaces = corev1.NamespaceList{
Items: []corev1.Namespace{
corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "cert-manager",
},
},
corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "istio-system",
},
},
corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ns-1",
Labels: map[string]string{
"istio-injection": "enabled",
},
},
},
corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ns-2",
Labels: map[string]string{
"istio.io/rev": "1-18-0",
},
},
},
},
}
func core_protocol(input string) *corev1.Protocol {
return func() *corev1.Protocol {
v := corev1.Protocol(input)
return &v
}()
}
var fixture_network_policies = networkingv1.NetworkPolicyList{
Items: []networkingv1.NetworkPolicy{
defaultDeny("cert-manager"),
defaultDeny("istio-system"),
networkingv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "istiod-allow-egress-cert-manager",
Namespace: "istio-system",
},
Spec: networkingv1.NetworkPolicySpec{
PodSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "istiod",
},
},
PolicyTypes: []networkingv1.PolicyType{networkingv1.PolicyTypeEgress},
Egress: []networkingv1.NetworkPolicyEgressRule{
networkingv1.NetworkPolicyEgressRule{
Ports: []networkingv1.NetworkPolicyPort{
networkingv1.NetworkPolicyPort{
Protocol: core_protocol("TCP"),
Port: &intstr.IntOrString{
Type: intstr.Int,
IntVal: 15020,
},
},
},
To: []networkingv1.NetworkPolicyPeer{
networkingv1.NetworkPolicyPeer{
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"kubernetes.io/metadata.name": "cert-manager",
},
},
PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "cert-manager",
"app.kubernetes.io/component": "controller",
},
},
},
},
},
}, // Egress
},
}, // istiod-allow-egress-cert-manager.istio-system
networkingv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "allow-communication-istiods",
Namespace: "istio-system",
},
Spec: networkingv1.NetworkPolicySpec{
PodSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "istiod",
},
},
PolicyTypes: []networkingv1.PolicyType{networkingv1.PolicyTypeEgress, networkingv1.PolicyTypeIngress},
Egress: []networkingv1.NetworkPolicyEgressRule{
networkingv1.NetworkPolicyEgressRule{
Ports: []networkingv1.NetworkPolicyPort{
networkingv1.NetworkPolicyPort{
Protocol: core_protocol("TCP"),
Port: &intstr.IntOrString{
Type: intstr.Int,
IntVal: 15020,
},
},
},
To: []networkingv1.NetworkPolicyPeer{
networkingv1.NetworkPolicyPeer{
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"kubernetes.io/metadata.name": "istio-system",
},
},
PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "istiod",
},
},
},
},
},
}, // Egress
Ingress: []networkingv1.NetworkPolicyIngressRule{
networkingv1.NetworkPolicyIngressRule{
Ports: []networkingv1.NetworkPolicyPort{
networkingv1.NetworkPolicyPort{
Protocol: core_protocol("TCP"),
Port: &intstr.IntOrString{
Type: intstr.Int,
IntVal: 15020,
},
},
},
From: []networkingv1.NetworkPolicyPeer{
networkingv1.NetworkPolicyPeer{
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"kubernetes.io/metadata.name": "istio-system",
},
},
PodSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "istiod",
},
},
},
},
},
}, // Ingress
},
}, // allow-communication-istiods.istio-system
},
}
func defaultDeny(ns string) networkingv1.NetworkPolicy {
return networkingv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "default-deny",
Namespace: ns,
},
Spec: networkingv1.NetworkPolicySpec{
PodSelector: metav1.LabelSelector{
MatchLabels: map[string]string{}, // all pods
},
PolicyTypes: []networkingv1.PolicyType{networkingv1.PolicyTypeEgress, networkingv1.PolicyTypeIngress},
Egress: []networkingv1.NetworkPolicyEgressRule{
networkingv1.NetworkPolicyEgressRule{},
},
Ingress: []networkingv1.NetworkPolicyIngressRule{
networkingv1.NetworkPolicyIngressRule{},
},
},
}
}