-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathdriver_test.go
More file actions
1552 lines (1369 loc) · 48.8 KB
/
driver_test.go
File metadata and controls
1552 lines (1369 loc) · 48.8 KB
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package managerdriver
import (
"encoding/json"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"k8s.io/apimachinery/pkg/util/intstr"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
common "github.com/ngrok/ngrok-operator/api/common/v1alpha1"
ingressv1alpha1 "github.com/ngrok/ngrok-operator/api/ingress/v1alpha1"
ngrokv1alpha1 "github.com/ngrok/ngrok-operator/api/ngrok/v1alpha1"
"github.com/ngrok/ngrok-operator/internal/controller"
"github.com/ngrok/ngrok-operator/internal/errors"
"github.com/ngrok/ngrok-operator/internal/testutils"
"github.com/ngrok/ngrok-operator/internal/trafficpolicy"
"github.com/ngrok/ngrok-operator/internal/util"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
)
const defaultManagerName = "ngrok-ingress-controller"
var _ = Describe("Driver", func() {
var driver *Driver
var scheme = runtime.NewScheme()
cname := "cnametarget.com"
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(ingressv1alpha1.AddToScheme(scheme))
utilruntime.Must(gatewayv1.Install(scheme))
utilruntime.Must(gatewayv1alpha2.Install(scheme))
utilruntime.Must(ngrokv1alpha1.AddToScheme(scheme))
BeforeEach(func() {
driver = NewDriver(
GinkgoLogr,
scheme,
testutils.DefaultControllerName,
types.NamespacedName{Name: defaultManagerName},
WithGatewayEnabled(false),
WithSyncAllowConcurrent(true),
)
})
Describe("Seed", func() {
It("Should not error", func() {
err := driver.Seed(GinkgoT().Context(), fake.NewClientBuilder().WithScheme(scheme).Build())
Expect(err).ToNot(HaveOccurred())
})
It("Should add all the found items to the store", func() {
i1 := testutils.NewTestIngressV1("test-ingress", "test-namespace")
i2 := testutils.NewTestIngressV1("test-ingress-2", "test-namespace")
ic1 := testutils.NewTestIngressClass("test-ingress-class", true, true)
ic2 := testutils.NewTestIngressClass("test-ingress-class-2", true, true)
d1 := testutils.NewDomainV1("test-domain.com", "test-namespace")
d2 := testutils.NewDomainV1("test-domain-2.com", "test-namespace")
c1 := testutils.NewCloudEndpoint()
c2 := testutils.NewCloudEndpoint()
obs := []runtime.Object{ic1, ic2, i1, i2, d1, d2, c1, c2}
c := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(obs...).Build()
err := driver.Seed(GinkgoT().Context(), c)
Expect(err).ToNot(HaveOccurred())
for _, obj := range obs {
foundObj, found, err := driver.store.Get(obj)
Expect(err).ToNot(HaveOccurred())
Expect(found).To(BeTrue())
Expect(foundObj).ToNot(BeNil())
Expect(foundObj).To(Equal(obj))
}
})
It("Should not seed namespace-scoped resources from outside the watched namespace", func() {
watchedNS := "watched-ns"
otherNS := "other-ns"
// Resources in the watched namespace
watchedIngress := testutils.NewTestIngressV1("ingress-watched", watchedNS)
watchedDomain := testutils.NewDomainV1("watched.example.com", watchedNS)
watchedService := testutils.NewTestServiceV1("svc-watched", watchedNS)
// Resources in another namespace (should be excluded when namespace-scoped)
otherIngress := testutils.NewTestIngressV1("ingress-other", otherNS)
otherDomain := testutils.NewDomainV1("other.example.com", otherNS)
otherService := testutils.NewTestServiceV1("svc-other", otherNS)
// Cluster-scoped resources (should always be included)
ic := testutils.NewTestIngressClass("ngrok-class", true, true)
allObjs := []runtime.Object{
watchedIngress, watchedDomain, watchedService,
otherIngress, otherDomain, otherService,
ic,
}
c := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(allObjs...).Build()
err := driver.Seed(GinkgoT().Context(), c, client.InNamespace(watchedNS))
Expect(err).ToNot(HaveOccurred())
// Watched namespace resources should be in the store
_, found, err := driver.store.Get(watchedIngress)
Expect(err).ToNot(HaveOccurred())
Expect(found).To(BeTrue(), "watched ingress should be in store")
_, found, err = driver.store.Get(watchedDomain)
Expect(err).ToNot(HaveOccurred())
Expect(found).To(BeTrue(), "watched domain should be in store")
_, found, err = driver.store.Get(watchedService)
Expect(err).ToNot(HaveOccurred())
Expect(found).To(BeTrue(), "watched service should be in store")
// Other namespace resources should NOT be in the store
_, found, err = driver.store.Get(otherIngress)
Expect(err).ToNot(HaveOccurred())
Expect(found).To(BeFalse(), "other-ns ingress should NOT be in store")
_, found, err = driver.store.Get(otherDomain)
Expect(err).ToNot(HaveOccurred())
Expect(found).To(BeFalse(), "other-ns domain should NOT be in store")
_, found, err = driver.store.Get(otherService)
Expect(err).ToNot(HaveOccurred())
Expect(found).To(BeFalse(), "other-ns service should NOT be in store")
// Cluster-scoped resources should still be in the store
_, found, err = driver.store.Get(ic)
Expect(err).ToNot(HaveOccurred())
Expect(found).To(BeTrue(), "cluster-scoped IngressClass should be in store")
})
})
Describe("DeleteIngress", func() {
It("Should remove the ingress from the store", func() {
i1 := testutils.NewTestIngressV1("test-ingress", "test-namespace")
c := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(i1).Build()
err := driver.Seed(GinkgoT().Context(), c)
Expect(err).ToNot(HaveOccurred())
err = driver.DeleteNamedIngress(types.NamespacedName{
Namespace: "test-namespace",
Name: "test-ingress",
})
Expect(err).ToNot(HaveOccurred())
foundObj, found, err := driver.store.Get(i1)
Expect(err).ToNot(HaveOccurred())
Expect(found).To(BeFalse())
Expect(foundObj).To(BeNil())
})
})
Describe("Sync", func() {
Context("When there are no ingresses in the store", func() {
It("Should not create anything or error", func() {
c := fake.NewClientBuilder().WithScheme(scheme).Build()
err := driver.Sync(GinkgoT().Context(), c)
Expect(err).ToNot(HaveOccurred())
domains := &ingressv1alpha1.DomainList{}
err = c.List(GinkgoT().Context(), &ingressv1alpha1.DomainList{})
Expect(err).ToNot(HaveOccurred())
Expect(domains.Items).To(HaveLen(0))
agentendpoints := &ngrokv1alpha1.AgentEndpointList{}
err = c.List(GinkgoT().Context(), &ngrokv1alpha1.AgentEndpointList{})
Expect(err).ToNot(HaveOccurred())
Expect(agentendpoints.Items).To(HaveLen(0))
cloudendpoints := &ngrokv1alpha1.CloudEndpointList{}
err = c.List(GinkgoT().Context(), &ngrokv1alpha1.CloudEndpointList{})
Expect(err).ToNot(HaveOccurred())
Expect(cloudendpoints.Items).To(HaveLen(0))
})
})
Context("When the old edges mapping-strategy is used, it defaults to endpoint", func() {
It("Should create AgentEndpoints", func() {
i1 := testutils.NewTestIngressV1("test-ingress", "test-namespace")
if i1.Annotations == nil {
i1.Annotations = map[string]string{}
}
i1.Annotations["k8s.ngrok.com/mapping-strategy"] = "edges"
i2 := testutils.NewTestIngressV1("test-ingress-2", "test-namespace")
if i2.Annotations == nil {
i2.Annotations = map[string]string{}
}
i2.Annotations["k8s.ngrok.com/mapping-strategy"] = "edges"
ic1 := testutils.NewTestIngressClass("test-ingress-class", true, true)
ic2 := testutils.NewTestIngressClass("test-ingress-class-2", true, true)
s := testutils.NewTestServiceV1("example", "test-namespace")
obs := []runtime.Object{ic1, ic2, i1, i2, s}
c := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(obs...).Build()
for _, obj := range obs {
err := driver.store.Update(obj)
Expect(err).ToNot(HaveOccurred())
}
err := driver.Seed(GinkgoT().Context(), c)
Expect(err).ToNot(HaveOccurred())
err = driver.Sync(GinkgoT().Context(), c)
Expect(err).ToNot(HaveOccurred())
foundDomain := &ingressv1alpha1.Domain{}
err = c.Get(GinkgoT().Context(), types.NamespacedName{
Namespace: "test-namespace",
Name: "example-com",
}, foundDomain)
Expect(err).ToNot(HaveOccurred())
Expect(foundDomain.Spec.Domain).To(Equal(i1.Spec.Rules[0].Host))
agentEndpoints := &ngrokv1alpha1.AgentEndpointList{}
err = c.List(GinkgoT().Context(), agentEndpoints, client.InNamespace("test-namespace"))
Expect(err).ToNot(HaveOccurred())
Expect(len(agentEndpoints.Items)).To(Equal(1))
agentEndpoint := agentEndpoints.Items[0]
Expect(agentEndpoint.Spec.URL).To(Equal("https://" + i1.Spec.Rules[0].Host))
})
})
When("A service specifies an appProtocol", func() {
var (
httpService *v1.Service
httpsService *v1.Service
ingress *netv1.Ingress
c client.WithWatch
namespace = "app-proto-namespace"
agentEndpoints *ngrokv1alpha1.AgentEndpointList
cloudEndpoints *ngrokv1alpha1.CloudEndpointList
ic = testutils.NewTestIngressClass("app-proto-ingress-class", true, true)
setIngressTargetService = func(i *netv1.Ingress, s *v1.Service) {
// Modify the ingress to include the service
i.Spec.Rules = []netv1.IngressRule{
{
Host: "foo.ngrok.io",
IngressRuleValue: netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{
{
Path: "/",
PathType: ptr.To(netv1.PathTypePrefix),
Backend: netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: s.Name,
Port: netv1.ServiceBackendPort{
Name: s.Spec.Ports[0].Name,
},
},
},
},
},
},
},
},
}
}
)
BeforeEach(func() {
httpService = &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "http-service",
Namespace: namespace,
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{
{
Port: 80,
Name: "http",
TargetPort: intstr.FromInt(80),
},
},
},
}
httpsService = &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "https-service",
Namespace: namespace,
Annotations: map[string]string{
"k8s.ngrok.com/app-protocols": `{"https": "https"}`,
},
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{
{
Port: 443,
Name: "https",
TargetPort: intstr.FromInt(443),
},
},
},
}
ingress = &netv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ingress",
Namespace: namespace,
Annotations: map[string]string{"k8s.ngrok.com/mapping-strategy": "edges"},
},
Spec: netv1.IngressSpec{
IngressClassName: &ic.Name,
Rules: []netv1.IngressRule{},
},
}
})
JustBeforeEach(func() {
// Add the services and ingress to the fake client and the store
objs := []runtime.Object{ic, httpService, httpsService, ingress}
c = fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(objs...).Build()
for _, obj := range objs {
Expect(driver.store.Update(obj)).To(BeNil())
}
// Seed & Sync
Expect(driver.Seed(GinkgoT().Context(), c)).To(BeNil())
Expect(driver.Sync(GinkgoT().Context(), c)).To(BeNil())
// Find the agent endpoints in this namespace
agentEndpoints = &ngrokv1alpha1.AgentEndpointList{}
err := c.List(GinkgoT().Context(), agentEndpoints, client.InNamespace(namespace))
Expect(err).ToNot(HaveOccurred())
// Find the cloud endpoints in this namespace
cloudEndpoints = &ngrokv1alpha1.CloudEndpointList{}
err = c.List(GinkgoT().Context(), cloudEndpoints, client.InNamespace(namespace))
Expect(err).ToNot(HaveOccurred())
})
When("The appProtocol is unknown", func() {
BeforeEach(func() {
// Set an unknown appProtocol on the httpService
httpService.Spec.Ports[0].AppProtocol = ptr.To("unknown")
// Modify the ingress to include the httpService
setIngressTargetService(ingress, httpService)
})
It("Should ignore the unknown appProtocol", func() {
// We expect one agent endpoint to be created
Expect(len(agentEndpoints.Items)).To(Equal(1))
By("Creating an agent endpoint with no appProtocol and the correct upstream")
foundAgentEndpoint := agentEndpoints.Items[0]
Expect(foundAgentEndpoint.Spec.Upstream.ProxyProtocolVersion).To(BeNil())
Expect(foundAgentEndpoint.Spec.Upstream.URL).To(Equal("http://http-service.app-proto-namespace:80"))
Expect(foundAgentEndpoint.Spec.Upstream.Protocol).To(BeNil())
})
})
When("The appProtocol is http", func() {
BeforeEach(func() {
// Set the appProtocol on the httpService
httpService.Spec.Ports[0].AppProtocol = ptr.To("http")
// Modify the ingress to include the httpService
setIngressTargetService(ingress, httpService)
})
It("Should create an AgentEndpoint with appProtocol http1", func() {
// We expect one AgentEndpoint to be created
Expect(len(agentEndpoints.Items)).To(Equal(1))
By("Creating an AgentEndpoint with appProtocol http1")
foundAgentEndpoint := agentEndpoints.Items[0]
Expect(foundAgentEndpoint.Spec.Upstream.Protocol).To(Equal(ptr.To(common.ApplicationProtocol_HTTP1)))
Expect(foundAgentEndpoint.Spec.Upstream.URL).To(Equal("http://http-service.app-proto-namespace:80"))
})
})
When("The appProtocol is k8s.ngrok.com/http2", func() {
BeforeEach(func() {
// Set the appProtocol on the httpService
httpsService.Spec.Ports[0].AppProtocol = ptr.To("k8s.ngrok.com/http2")
// Modify the ingress to include the httpsService
setIngressTargetService(ingress, httpsService)
})
It("Should create an AgentEndpoint with an upstream protocol of http2", func() {
// We expect one AgentEndpoint to be created
Expect(len(agentEndpoints.Items)).To(Equal(1))
By("Creating an AgentEndpoint with appProtocol http2")
foundAgentEndpoint := agentEndpoints.Items[0]
Expect(foundAgentEndpoint.Spec.Upstream.Protocol).To(Equal(ptr.To(common.ApplicationProtocol_HTTP2)))
Expect(foundAgentEndpoint.Spec.Upstream.URL).To(Equal("https://https-service.app-proto-namespace:443"))
})
})
When("The appProtocol is kubernetes.io/h2c", func() {
BeforeEach(func() {
// Set the appProtocol on the httpService
httpsService.Spec.Ports[0].AppProtocol = ptr.To("kubernetes.io/h2c")
// Modify the ingress to include the httpsService
setIngressTargetService(ingress, httpsService)
})
It("Should create an AgentEndpoint with appProtocol http2", func() {
// We expect one AgentEndpoint to be created
Expect(len(agentEndpoints.Items)).To(Equal(1))
By("Creating an AgentEndpoint with appProtocol http2")
foundAgentEndpoint := agentEndpoints.Items[0]
Expect(foundAgentEndpoint.Spec.Upstream.Protocol).To(Equal(ptr.To(common.ApplicationProtocol_HTTP2)))
Expect(foundAgentEndpoint.Spec.Upstream.URL).To(Equal("https://https-service.app-proto-namespace:443"))
})
})
})
When("An ingress specifies a traffic policy", func() {
var (
c client.WithWatch
namespace = "edge-tp-test-namespace"
httpService *v1.Service
ingress *netv1.Ingress
trafficPolicy *ngrokv1alpha1.NgrokTrafficPolicy
foundAgentEndpoints *ngrokv1alpha1.AgentEndpointList
foundCloudEndpoints *ngrokv1alpha1.CloudEndpointList
ic = testutils.NewTestIngressClass("edge-tp-ingress-class", true, true)
)
BeforeEach(func() {
pol := trafficpolicy.NewTrafficPolicy()
pol.AddRuleOnHTTPRequest(trafficpolicy.Rule{
Name: "test-name",
Actions: []trafficpolicy.Action{
trafficpolicy.NewCompressResponseAction(nil),
},
})
rawPolicy, err := json.Marshal(pol)
Expect(err).ToNot(HaveOccurred())
trafficPolicy = &ngrokv1alpha1.NgrokTrafficPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "test-policy",
Namespace: namespace,
},
Spec: ngrokv1alpha1.NgrokTrafficPolicySpec{
Policy: rawPolicy,
},
}
httpService = &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "http-service",
Namespace: namespace,
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{
{
Port: 80,
Name: "http",
TargetPort: intstr.FromInt(80),
},
},
},
}
ingress = &netv1.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "test-ingress",
Namespace: namespace,
},
Spec: netv1.IngressSpec{
IngressClassName: &ic.Name,
Rules: []netv1.IngressRule{
{
Host: "foo.ngrok.io",
IngressRuleValue: netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{
{
Path: "/",
PathType: ptr.To(netv1.PathTypePrefix),
Backend: netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: httpService.Name,
Port: netv1.ServiceBackendPort{
Name: httpService.Spec.Ports[0].Name,
},
},
},
},
},
},
},
},
},
},
}
})
JustBeforeEach(func() {
// Add the services and ingress to the fake client and the store
objs := []runtime.Object{ic, trafficPolicy, httpService, ingress}
c = fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(objs...).Build()
for _, obj := range objs {
Expect(driver.store.Update(obj)).To(BeNil())
}
// Seed & Sync
Expect(driver.Seed(GinkgoT().Context(), c)).To(Succeed())
Expect(driver.Sync(GinkgoT().Context(), c)).To(Succeed())
// Find the AgentEndpoints in this namespace
foundAgentEndpoints = &ngrokv1alpha1.AgentEndpointList{}
Expect(c.List(GinkgoT().Context(), foundAgentEndpoints, client.InNamespace(namespace))).To(Succeed())
// Find the CloudEndpoints in this namespace
foundCloudEndpoints = &ngrokv1alpha1.CloudEndpointList{}
Expect(c.List(GinkgoT().Context(), foundCloudEndpoints, client.InNamespace(namespace))).To(Succeed())
})
When("The the ingress is using the old edges mapping strategy", func() {
BeforeEach(func() {
controller.AddAnnotations(ingress, map[string]string{
"k8s.ngrok.com/mapping-strategy": "edges",
})
})
It("Should create an AgentEndpoint", func() {
Expect(len(foundAgentEndpoints.Items)).To(Equal(1))
Expect(len(foundCloudEndpoints.Items)).To(Equal(0))
})
When("The traffic policy exists", func() {
BeforeEach(func() {
controller.AddAnnotations(ingress, map[string]string{
"k8s.ngrok.com/traffic-policy": trafficPolicy.Name,
})
})
It("Should use the traffic policy", func() {
foundAgentEndpoint := foundAgentEndpoints.Items[0]
By("Having the traffic policy on the AgentEndpoint")
Expect(foundAgentEndpoint.Spec.TrafficPolicy.Inline).ToNot(BeNil())
pol, err := trafficpolicy.NewTrafficPolicyFromJSON(foundAgentEndpoint.Spec.TrafficPolicy.Inline)
Expect(err).ToNot(HaveOccurred())
Expect(pol.OnHTTPRequest).To(ContainElement(
trafficpolicy.Rule{
Name: "test-name",
Actions: []trafficpolicy.Action{
{
Type: "compress-response",
Config: map[string]interface{}{},
},
},
},
))
})
})
})
When("The ingress is using the default mapping strategy", func() {
It("Should only create an AgentEndpoint", func() {
Expect(len(foundAgentEndpoints.Items)).To(Equal(1))
Expect(len(foundCloudEndpoints.Items)).To(Equal(0))
})
When("The traffic policy exists", func() {
BeforeEach(func() {
controller.AddAnnotations(ingress, map[string]string{
"k8s.ngrok.com/traffic-policy": trafficPolicy.Name,
})
})
It("Should include the traffic policy", func() {
agentEndpoint := foundAgentEndpoints.Items[0]
pol, err := trafficpolicy.NewTrafficPolicyFromJSON(agentEndpoint.Spec.TrafficPolicy.Inline)
Expect(err).ToNot(HaveOccurred())
Expect(pol.OnHTTPRequest).To(ContainElement(
trafficpolicy.Rule{
Name: "test-name",
Actions: []trafficpolicy.Action{
{
Type: "compress-response",
Config: map[string]interface{}{},
},
},
},
))
})
})
})
})
When("The defaultDomainReclaimPolicy is set", func() {
var (
defaultDomainReclaimPolicy ingressv1alpha1.DomainReclaimPolicy
objs []runtime.Object
c client.WithWatch
)
BeforeEach(func() {
objs = []runtime.Object{
testutils.NewTestIngressClass("test-ingress-class", true, true),
testutils.NewTestIngressV1("test-ingress", "test-namespace"),
testutils.NewTestServiceV1("example", "test-namespace"),
}
})
JustBeforeEach(func(ctx SpecContext) {
driver = NewDriver(
GinkgoLogr,
scheme,
testutils.DefaultControllerName,
types.NamespacedName{Name: defaultManagerName},
WithGatewayEnabled(false),
WithSyncAllowConcurrent(true),
WithDefaultDomainReclaimPolicy(defaultDomainReclaimPolicy),
)
c = fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(objs...).Build()
Expect(driver.Seed(ctx, c)).To(Succeed())
Expect(driver.Sync(ctx, c)).To(Succeed())
})
When("policy is Delete", func() {
BeforeEach(func() {
defaultDomainReclaimPolicy = ingressv1alpha1.DomainReclaimPolicyDelete
})
When("no domains exist", func() {
It("should create new domains with ReclaimPolicy set to Delete", func(ctx SpecContext) {
domains := &ingressv1alpha1.DomainList{}
Expect(c.List(ctx, domains)).To(Succeed())
Expect(domains.Items).To(HaveLen(1))
Expect(domains.Items[0].Spec.ReclaimPolicy).To(Equal(ingressv1alpha1.DomainReclaimPolicyDelete))
})
})
When("a domain already exists", func() {
BeforeEach(func() {
d := testutils.NewDomainV1("example.com", "test-namespace")
d.ObjectMeta.SetCreationTimestamp(metav1.Now())
d.Spec.ReclaimPolicy = ingressv1alpha1.DomainReclaimPolicyRetain
objs = append(objs, d)
})
It("should not modify the reclaim policy of existing domains", func(ctx SpecContext) {
domains := &ingressv1alpha1.DomainList{}
Expect(c.List(ctx, domains)).To(Succeed())
Expect(domains.Items).To(HaveLen(1))
Expect(domains.Items[0].Spec.ReclaimPolicy).To(Equal(ingressv1alpha1.DomainReclaimPolicyRetain))
})
})
})
When("policy is Retain", func() {
BeforeEach(func() {
defaultDomainReclaimPolicy = ingressv1alpha1.DomainReclaimPolicyRetain
})
When("no domains exist", func() {
It("should create new domains with ReclaimPolicy set to Retain", func(ctx SpecContext) {
domains := &ingressv1alpha1.DomainList{}
Expect(c.List(ctx, domains)).To(Succeed())
Expect(domains.Items).To(HaveLen(1))
Expect(domains.Items[0].Spec.ReclaimPolicy).To(Equal(ingressv1alpha1.DomainReclaimPolicyRetain))
})
})
When("a domain already exists", func() {
BeforeEach(func() {
d := testutils.NewDomainV1("example.com", "test-namespace")
d.ObjectMeta.SetCreationTimestamp(metav1.Now())
d.Spec.ReclaimPolicy = ingressv1alpha1.DomainReclaimPolicyDelete
objs = append(objs, d)
})
It("should not modify the reclaim policy of existing domains", func(ctx SpecContext) {
domains := &ingressv1alpha1.DomainList{}
Expect(c.List(ctx, domains)).To(Succeed())
Expect(domains.Items).To(HaveLen(1))
Expect(domains.Items[0].Spec.ReclaimPolicy).To(Equal(ingressv1alpha1.DomainReclaimPolicyDelete))
})
})
})
})
When("An ingress has internal domain hostnames", func() {
It("Should not create Domain CRDs for internal domains", func(ctx SpecContext) {
// Create an ingress with both a regular and internal domain
ingress := testutils.NewTestIngressV1("test-ingress", "test-namespace")
ingress.Spec.Rules = []netv1.IngressRule{
{
Host: "app.example.com",
IngressRuleValue: netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{
{
Path: "/",
Backend: netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: "example",
Port: netv1.ServiceBackendPort{Number: 80},
},
},
},
},
},
},
},
{
Host: "service.namespace.internal",
IngressRuleValue: netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{
{
Path: "/",
Backend: netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: "example",
Port: netv1.ServiceBackendPort{Number: 80},
},
},
},
},
},
},
},
}
ic := testutils.NewTestIngressClass("test-ingress-class", true, true)
s := testutils.NewTestServiceV1("example", "test-namespace")
objs := []runtime.Object{ic, ingress, s}
c := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(objs...).Build()
Expect(driver.Seed(ctx, c)).To(Succeed())
Expect(driver.Sync(ctx, c)).To(Succeed())
// Verify only the non-internal domain was created
domains := &ingressv1alpha1.DomainList{}
Expect(c.List(ctx, domains)).To(Succeed())
Expect(domains.Items).To(HaveLen(1))
Expect(domains.Items[0].Spec.Domain).To(Equal("app.example.com"))
// Verify no domain was created for the internal hostname
internalDomain := &ingressv1alpha1.Domain{}
err := c.Get(ctx, types.NamespacedName{
Namespace: "test-namespace",
Name: "service-namespace-internal",
}, internalDomain)
Expect(err).To(HaveOccurred())
Expect(apierrors.IsNotFound(err)).To(BeTrue())
})
It("Should not create Domain CRDs when all hosts are internal domains", func(ctx SpecContext) {
// Create an ingress with only internal domains
ingress := testutils.NewTestIngressV1("test-ingress", "test-namespace")
ingress.Spec.Rules = []netv1.IngressRule{
{
Host: "foo.internal",
IngressRuleValue: netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{
{
Path: "/",
Backend: netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: "example",
Port: netv1.ServiceBackendPort{Number: 80},
},
},
},
},
},
},
},
{
Host: "bar.internal",
IngressRuleValue: netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{
{
Path: "/",
Backend: netv1.IngressBackend{
Service: &netv1.IngressServiceBackend{
Name: "example",
Port: netv1.ServiceBackendPort{Number: 80},
},
},
},
},
},
},
},
}
ic := testutils.NewTestIngressClass("test-ingress-class", true, true)
s := testutils.NewTestServiceV1("example", "test-namespace")
objs := []runtime.Object{ic, ingress, s}
c := fake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(objs...).Build()
// Reset driver to clear any state from previous tests
driver = NewDriver(
GinkgoLogr,
scheme,
testutils.DefaultControllerName,
types.NamespacedName{Name: defaultManagerName},
WithGatewayEnabled(false),
WithSyncAllowConcurrent(true),
)
Expect(driver.Seed(ctx, c)).To(Succeed())
Expect(driver.Sync(ctx, c)).To(Succeed())
// Verify no domains were created
domains := &ingressv1alpha1.DomainList{}
Expect(c.List(ctx, domains)).To(Succeed())
Expect(domains.Items).To(HaveLen(0))
})
})
})
Describe("calculateIngressLoadBalancerIPStatus", func() {
var domains []ingressv1alpha1.Domain
var ingress *netv1.Ingress
var c client.WithWatch
var status []netv1.IngressLoadBalancerIngress
JustBeforeEach(func() {
c = fake.NewClientBuilder().
WithLists(
&ingressv1alpha1.DomainList{
Items: domains,
},
).
WithScheme(scheme).
Build()
domainsByDomain, err := getDomainsByDomain(GinkgoT().Context(), c)
Expect(err).ToNot(HaveOccurred())
status = calculateIngressLoadBalancerIPStatus(ingress, domainsByDomain)
})
addIngressHostname := func(i *netv1.Ingress, hostname string) {
if i.Spec.Rules == nil {
i.Spec.Rules = []netv1.IngressRule{}
}
i.Spec.Rules = append(i.Spec.Rules, netv1.IngressRule{
Host: hostname,
})
}
newTestDomain := func(name, domain string, cnameTarget *string) ingressv1alpha1.Domain {
return ingressv1alpha1.Domain{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: ingressv1alpha1.DomainSpec{
Domain: domain,
},
Status: ingressv1alpha1.DomainStatus{
Domain: domain,
CNAMETarget: cnameTarget,
},
}
}
newTestDomainList := func(domains ...ingressv1alpha1.Domain) []ingressv1alpha1.Domain {
return domains
}
When("the CNAME is present", func() {
BeforeEach(func() {
ingress = testutils.NewTestIngressV1("test-ingress", "test-namespace")
domains = newTestDomainList(
newTestDomain(
"example-com",
"example.com",
&cname,
),
)
})
It("should return the CNAME as the status", func() {
Expect(len(status)).To(Equal(1))
Expect(status[0].Hostname).To(Equal(cname))
})
})
When("no matching domain is found", func() {
BeforeEach(func() {
ingress = testutils.NewTestIngressV1("test-ingress", "test-namespace")
domains = newTestDomainList(
newTestDomain(
"another-domain-com",
"another-domain.com",
&cname,
),
)
})
It("should return an empty status", func() {
Expect(len(status)).To(Equal(0))
})
})
When("the CNAME target is nil and the domain.status.domain is empty", func() {
BeforeEach(func() {
ingress = testutils.NewTestIngressV1("test-ingress", "test-namespace")
domains = newTestDomainList(
ingressv1alpha1.Domain{
ObjectMeta: metav1.ObjectMeta{
Name: "example-com",
},
Spec: ingressv1alpha1.DomainSpec{
Domain: "example.com",
},
Status: ingressv1alpha1.DomainStatus{},
},
)
})
It("should return an empty status", func() {
Expect(len(status)).To(Equal(0))
})
})
When("the domain is a non-wildcard ngrok managed domain", func() {
BeforeEach(func() {
ingress = testutils.NewTestIngressV1("test-ingress", "test-namespace")
ingress.Spec = netv1.IngressSpec{
Rules: []netv1.IngressRule{
{
Host: "example.ngrok.io",
},
},
}
domains = newTestDomainList(
newTestDomain(
"example-ngrok-io",
"example.ngrok.io",
nil,
),
)
})
It("should have a status hostname matching the domain", func() {
Expect(len(status)).To(Equal(1))
Expect(status[0].Hostname).To(Equal("example.ngrok.io"))
})
})
When("the domain is a wildcard ngrok managed domain", func() {
BeforeEach(func() {
ingress = testutils.NewTestIngressV1("test-ingress", "test-namespace")
ingress.Spec = netv1.IngressSpec{
Rules: []netv1.IngressRule{
{
Host: "*.example.ngrok.io",
},
},
}
domains = newTestDomainList(
newTestDomain(
"wildcard-example-ngrok-io",
"*.example.ngrok.io",
nil,
),
)
})
It("should have a .Status[].Hostname equal to the domain without the wildcard", func() {
Expect(len(status)).To(Equal(1))
Expect(status[0].Hostname).To(Equal("example.ngrok.io"))
})
})
When("There are multiple domains", func() {
cname1 := "cnametarget1.com"
cname2 := "cnametarget2.com"
BeforeEach(func() {
ingress = testutils.NewTestIngressV1("test-ingress", "test-namespace")
addIngressHostname(ingress, "test-domain1.com")
addIngressHostname(ingress, "test-domain2.com")