-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathactuator_test.go
489 lines (415 loc) · 16.9 KB
/
actuator_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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package controller
import (
"encoding/json"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/gardener/gardener/pkg/apis/resources/v1alpha1"
. "github.com/gardener/gardener/pkg/utils/test/matchers"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
istionetworkingClientGo "istio.io/client-go/pkg/apis/networking/v1alpha3"
istionetworkingv1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/stackitcloud/gardener-extension-acl/pkg/controller/config"
"github.com/stackitcloud/gardener-extension-acl/pkg/envoyfilters"
"github.com/stackitcloud/gardener-extension-acl/pkg/extensionspec"
)
var _ = Describe("actuator test", func() {
var (
a *actuator
shootNamespace1, shootNamespace2 string
istioNamespace1, istioNamespace2 string
istioNamespace1Selector, istioNamespace2Selector map[string]string
)
BeforeEach(func() {
shootNamespace1 = createNewShootNamespace()
istioNamespace1 = createNewIstioNamespace()
istioNamespace1Selector = map[string]string{
"app": "istio-ingressgateway",
"istio": istioNamespace1,
}
createNewEnvoyFilter(shootNamespace1, istioNamespace1)
createNewGateway("kube-apiserver", shootNamespace1, istioNamespace1Selector)
createNewIstioDeployment(istioNamespace1, istioNamespace1Selector)
createNewCluster(shootNamespace1)
createNewInfrastructure(shootNamespace1)
a = getNewActuator()
})
AfterEach(func() {
deleteNamespace(shootNamespace1)
deleteNamespace(istioNamespace1)
})
Describe("reconciliation of an ACL extension object", func() {
It("should create managed resource containing acl-api-shoot and acl-vpn-shoot EnvoyFilter object", func() {
extSpec := extensionspec.ExtensionSpec{
Rule: &envoyfilters.ACLRule{
Cidrs: []string{"1.2.3.4/24"},
Action: "ALLOW",
Type: "remote_ip",
},
}
extSpecJSON, err := json.Marshal(extSpec)
Expect(err).To(BeNil())
ext := createNewExtension(shootNamespace1, extSpecJSON)
Expect(ext).To(Not(BeNil()))
Expect(a.Reconcile(ctx, logger, ext)).To(Succeed())
mr := &v1alpha1.ManagedResource{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: ResourceNameSeed, Namespace: shootNamespace1}, mr)).To(Succeed())
secret := &corev1.Secret{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: mr.Spec.SecretRefs[0].Name, Namespace: shootNamespace1}, secret)).To(Succeed())
Expect(secret.Data["seed"]).To(ContainSubstring("1.2.3.4"))
Expect(secret.Data["seed"]).To(ContainSubstring("acl-api-" + shootNamespace1))
Expect(secret.Data["seed"]).To(ContainSubstring("acl-vpn-" + shootNamespace1))
})
It("should record the last seen istio namespace in the status of the extension object", func() {
// arrange
extSpec := extensionspec.ExtensionSpec{
Rule: &envoyfilters.ACLRule{
Cidrs: []string{"1.2.3.4/24"},
Action: "ALLOW",
Type: "remote_ip",
},
}
extSpecJSON, err := json.Marshal(extSpec)
Expect(err).To(BeNil())
ext := createNewExtension(shootNamespace1, extSpecJSON)
Expect(ext).To(Not(BeNil()))
// act
Expect(a.Reconcile(ctx, logger, ext)).To(Succeed())
// assert
ext = &extensionsv1alpha1.Extension{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: shootNamespace1, Name: "acl"}, ext)).To(Succeed())
extState := &ExtensionState{}
Expect(ext.Status.State).ToNot(BeNil())
Expect(ext.Status.State.Raw).ToNot(BeNil())
Expect(json.Unmarshal(ext.Status.State.Raw, &extState)).To(Succeed())
Expect(extState.IstioNamespace).ToNot(BeNil())
Expect(*extState.IstioNamespace).To(Equal(istioNamespace1))
})
// gardener >= v1.89, including https://github.com/gardener/gardener/pull/9038
Context("ingress-nginx is exposed via istio", func() {
BeforeEach(func() {
gateway := createNewGateway("nginx-ingress-controller", "garden", map[string]string{
"app": "istio-ingressgateway",
"istio": "ingressgateway",
})
DeferCleanup(func() {
Expect(k8sClient.Delete(ctx, gateway)).To(Or(Succeed(), BeNotFoundError()))
})
})
It("should create managed resource including acl-ingress-shoot EnvoyFilter object", func() {
extSpec := extensionspec.ExtensionSpec{
Rule: &envoyfilters.ACLRule{
Cidrs: []string{"1.2.3.4/24"},
Action: "ALLOW",
Type: "remote_ip",
},
}
extSpecJSON, err := json.Marshal(extSpec)
Expect(err).To(BeNil())
ext := createNewExtension(shootNamespace1, extSpecJSON)
Expect(ext).To(Not(BeNil()))
Expect(a.Reconcile(ctx, logger, ext)).To(Succeed())
mr := &v1alpha1.ManagedResource{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: ResourceNameSeed, Namespace: shootNamespace1}, mr)).To(Succeed())
secret := &corev1.Secret{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: mr.Spec.SecretRefs[0].Name, Namespace: shootNamespace1}, secret)).To(Succeed())
Expect(secret.Data["seed"]).To(ContainSubstring("acl-ingress-" + shootNamespace1))
})
})
// gardener < v1.89
Context("ingress-nginx is not exposed via istio", func() {
It("should create managed resource not including acl-ingress-shoot EnvoyFilter object", func() {
extSpec := extensionspec.ExtensionSpec{
Rule: &envoyfilters.ACLRule{
Cidrs: []string{"1.2.3.4/24"},
Action: "ALLOW",
Type: "remote_ip",
},
}
extSpecJSON, err := json.Marshal(extSpec)
Expect(err).To(BeNil())
ext := createNewExtension(shootNamespace1, extSpecJSON)
Expect(ext).To(Not(BeNil()))
Expect(a.Reconcile(ctx, logger, ext)).To(Succeed())
mr := &v1alpha1.ManagedResource{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: ResourceNameSeed, Namespace: shootNamespace1}, mr)).To(Succeed())
secret := &corev1.Secret{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: mr.Spec.SecretRefs[0].Name, Namespace: shootNamespace1}, secret)).To(Succeed())
Expect(secret.Data["seed"]).NotTo(ContainSubstring("acl-ingress-" + shootNamespace1))
})
})
})
Describe("reconciliation of an extension object with other ACL extensions being present", func() {
BeforeEach(func() {
shootNamespace2 = createNewShootNamespace()
createNewEnvoyFilter(shootNamespace2, istioNamespace1)
createNewGateway("kube-apiserver", shootNamespace2, istioNamespace1Selector)
createNewCluster(shootNamespace2)
createNewInfrastructure(shootNamespace2)
})
AfterEach(func() {
deleteNamespace(shootNamespace2)
})
It("should not fail when the Gateway resource can't be found for an extension other than the one being reconciled (e.g. for hibernated clusters)", func() {
// arrange
extSpec1 := extensionspec.ExtensionSpec{
Rule: &envoyfilters.ACLRule{
Cidrs: []string{"1.2.3.4/24"},
Action: "ALLOW",
Type: "remote_ip",
},
}
extSpecJSON1, err := json.Marshal(extSpec1)
Expect(err).To(BeNil())
ext1 := createNewExtension(shootNamespace1, extSpecJSON1)
Expect(ext1).To(Not(BeNil()))
// contents of the seconds extension don't matter, it just needs to exist
ext2 := createNewExtension(shootNamespace2, []byte("{}"))
Expect(ext2).To(Not(BeNil()))
// simulate a hibernated cluster by deleting the Gateway object
gw2 := &istionetworkingv1beta1.Gateway{
ObjectMeta: metav1.ObjectMeta{
Name: "kube-apiserver",
Namespace: shootNamespace2,
},
}
Expect(k8sClient.Delete(ctx, gw2)).To(Succeed())
// act (reconcile the existing extension)
Expect(a.Reconcile(ctx, logger, ext1)).To(Succeed())
// assert
mr := &v1alpha1.ManagedResource{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: ResourceNameSeed, Namespace: shootNamespace1}, mr)).To(Succeed())
secret := &corev1.Secret{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: mr.Spec.SecretRefs[0].Name, Namespace: shootNamespace1}, secret)).To(Succeed())
Expect(secret.Data["seed"]).To(ContainSubstring("1.2.3.4"))
Expect(secret.Data["seed"]).To(ContainSubstring(istioNamespace1))
Expect(secret.Data["seed"]).To(ContainSubstring("acl-vpn-" + shootNamespace1))
})
})
Describe("a shoot switching the istio namespace (e.g. when being migrated to HA)", func() {
It("should modify the EnvoyFilter objects accordingly", func() {
By("1) creating the EnvoyFilter object correctly in the ORIGINAL namespace")
// arrange
extSpec := extensionspec.ExtensionSpec{
Rule: &envoyfilters.ACLRule{
Cidrs: []string{"1.2.3.4/24"},
Action: "ALLOW",
Type: "remote_ip",
},
}
extSpecJSON, err := json.Marshal(extSpec)
Expect(err).To(BeNil())
ext := createNewExtension(shootNamespace1, extSpecJSON)
Expect(ext).To(Not(BeNil()))
// act
Expect(a.Reconcile(ctx, logger, ext)).To(Succeed())
// assert
mr := &v1alpha1.ManagedResource{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: ResourceNameSeed, Namespace: shootNamespace1}, mr)).To(Succeed())
secret := &corev1.Secret{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: mr.Spec.SecretRefs[0].Name, Namespace: shootNamespace1}, secret)).To(Succeed())
Expect(secret.Data["seed"]).To(ContainSubstring("1.2.3.4"))
Expect(secret.Data["seed"]).To(ContainSubstring(istioNamespace1))
Expect(secret.Data["seed"]).To(ContainSubstring("acl-vpn-" + shootNamespace1))
By("2) allowing for the shoot to switch to a different Istio namespace")
istioNamespace2 = createNewIstioNamespace()
istioNamespace2Selector = map[string]string{
"app": "istio-ingressgateway",
"istio": istioNamespace2,
}
// switching the istio namespace by recreating the gateway in the
// shoot namespace with a new selector and creating a new
// EnvoyFilter for the shoot and Istio Deployment in the second
// istio namespace
gw := &istionetworkingv1beta1.Gateway{
ObjectMeta: metav1.ObjectMeta{
Name: "kube-apiserver",
Namespace: shootNamespace1,
},
}
Expect(k8sClient.Delete(ctx, gw)).To(Succeed())
createNewGateway("kube-apiserver", shootNamespace1, istioNamespace2Selector)
createNewIstioDeployment(istioNamespace2, istioNamespace2Selector)
createNewEnvoyFilter(shootNamespace1, istioNamespace2)
By("3) creating the EnvoyFilter object correctly in the NEW namespace")
// arrange
ext = &extensionsv1alpha1.Extension{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: shootNamespace1, Name: "acl"}, ext)).To(Succeed())
// act
Expect(a.Reconcile(ctx, logger, ext)).To(Succeed())
// assert
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: ResourceNameSeed, Namespace: shootNamespace1}, mr)).To(Succeed())
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: mr.Spec.SecretRefs[0].Name, Namespace: shootNamespace1}, secret)).To(Succeed())
Expect(secret.Data["seed"]).To(ContainSubstring(istioNamespace2))
Expect(secret.Data["seed"]).To(ContainSubstring("acl-vpn-" + shootNamespace1))
By("4) should have removed the EnvoyFilter object in the ORIGINAL namespace")
Expect(secret.Data["seed"]).NotTo(ContainSubstring(istioNamespace1))
})
})
Describe("deletion of a hibernated cluster (no Gateway resource exists)", func() {
It("should properly clean up according ManagedResource", func() {
// arrange
extSpec := extensionspec.ExtensionSpec{
Rule: &envoyfilters.ACLRule{
Cidrs: []string{"1.2.3.4/24"},
Action: "ALLOW",
Type: "remote_ip",
},
}
extSpecJSON, err := json.Marshal(extSpec)
Expect(err).To(BeNil())
ext := createNewExtension(shootNamespace1, extSpecJSON)
Expect(ext).To(Not(BeNil()))
Expect(a.Reconcile(ctx, logger, ext)).To(Succeed())
mr := &v1alpha1.ManagedResource{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: ResourceNameSeed, Namespace: shootNamespace1}, mr)).To(Succeed())
// simulate a hibernated cluster by deleting the Gateway object
gw := &istionetworkingv1beta1.Gateway{
ObjectMeta: metav1.ObjectMeta{
Name: "kube-apiserver",
Namespace: shootNamespace1,
},
}
Expect(k8sClient.Delete(ctx, gw)).To(Succeed())
// act
Expect(a.Delete(ctx, logger, ext)).To(Succeed())
// assert
mr = &v1alpha1.ManagedResource{}
err = k8sClient.Get(ctx, types.NamespacedName{Name: ResourceNameSeed, Namespace: shootNamespace1}, mr)
Expect(err).ToNot(BeNil())
Expect(apierrors.IsNotFound(err)).To(BeTrue())
})
})
})
var _ = Describe("actuator unit test", func() {
var (
a *actuator
namespace string
istioNamespace string
)
BeforeEach(func() {
var istioNamespaceSelector map[string]string
namespace = createNewShootNamespace()
istioNamespace = createNewIstioNamespace()
istioNamespaceSelector = map[string]string{
"app": "istio-ingressgateway",
"istio": istioNamespace,
}
createNewGateway("kube-apiserver", namespace, istioNamespaceSelector)
createNewIstioDeployment(istioNamespace, istioNamespaceSelector)
createNewCluster(namespace)
createNewInfrastructure(namespace)
a = getNewActuator()
})
AfterEach(func() {
deleteNamespace(namespace)
})
// TODO: test case can be removed together with the migration code in the
// triggerWebhook() function, the test only checks that the deprecated hash
// annotation is properly removed
Describe("triggerWebhook", func() {
When("the envoyfilter has a hash annotation", func() {
It("Should remove the hash annotation", func() {
envoyFilter := &istionetworkingClientGo.EnvoyFilter{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
Namespace: istioNamespace,
Annotations: map[string]string{
"acl-ext-rule-hash": "should-be-removed",
},
},
}
Expect(k8sClient.Create(ctx, envoyFilter)).To(Succeed())
Expect(a.triggerWebhook(ctx, namespace, istioNamespace)).To(Succeed())
Expect(k8sClient.Get(
ctx, types.NamespacedName{
Name: envoyFilter.Name,
Namespace: envoyFilter.Namespace,
},
envoyFilter,
)).To(Succeed())
Expect(envoyFilter.Annotations).ToNot(HaveKey("acl-ext-rule-hash"))
})
})
})
Describe("ValidateExtensionSpec", func() {
When("there is an extension resource with one valid rule", func() {
It("Should not return an error", func() {
extSpec := &extensionspec.ExtensionSpec{}
addRuleToSpec(extSpec, "DENY", "source_ip", "0.0.0.0/0")
Expect(ValidateExtensionSpec(extSpec)).To(Succeed())
})
})
When("there is an extension resource without rules", func() {
It("Should return an error", func() {
extSpec := &extensionspec.ExtensionSpec{}
Expect(ValidateExtensionSpec(extSpec)).To(Equal(ErrSpecRule))
})
})
When("there is an extension resource with a rule with invalid rule type", func() {
It("Should return the correct error", func() {
extSpec := &extensionspec.ExtensionSpec{}
addRuleToSpec(extSpec, "DENY", "nonexistent", "0.0.0.0/0")
Expect(ValidateExtensionSpec(extSpec)).To(Equal(ErrSpecType))
})
})
When("there is an extension resource with a rule with invalid rule action", func() {
It("Should return the correct error", func() {
extSpec := &extensionspec.ExtensionSpec{}
addRuleToSpec(extSpec, "NONEXISTENT", "remote_ip", "0.0.0.0/0")
Expect(ValidateExtensionSpec(extSpec)).To(Equal(ErrSpecAction))
})
})
When("there is an extension resource with CIDR", func() {
It("Should return the correct error", func() {
extSpec := &extensionspec.ExtensionSpec{}
addRuleToSpec(extSpec, "DENY", "remote_ip", "n0n3x1st3/nt")
// we're not testing for a specific error, as they come from the
// net package here - no need for us to test these
Expect(ValidateExtensionSpec(extSpec)).ToNot(Succeed())
})
})
When("there is an extension resource with a rule without CIDR", func() {
It("Should return the correct error", func() {
extSpec := &extensionspec.ExtensionSpec{}
extSpec.Rule = &envoyfilters.ACLRule{
Action: "DENY",
Type: "remote_ip",
}
// we're not testing for a specific error, as they come from the
// net package here - no need for us to test these
Expect(ValidateExtensionSpec(extSpec)).To(Equal(ErrSpecCIDR))
})
})
When("there is an extension resource with a rule with invalid CIDR", func() {
It("Should return the correct error", func() {
extSpec := &extensionspec.ExtensionSpec{}
addRuleToSpec(extSpec, "DENY", "remote_ip", "n0n3x1st3/nt")
// we're not testing for a specific error, as they come from the
// net package here - no need for us to test these
Expect(ValidateExtensionSpec(extSpec)).ToNot(Succeed())
})
})
})
})
func getNewActuator() *actuator {
return &actuator{
client: k8sClient,
config: cfg,
extensionConfig: config.Config{
ChartPath: "../../charts",
},
}
}
func addRuleToSpec(extSpec *extensionspec.ExtensionSpec, action, ruleType, cidr string) {
extSpec.Rule = &envoyfilters.ACLRule{
Cidrs: []string{
cidr,
},
Action: action,
Type: ruleType,
}
}