-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathchange_processor_test.go
2303 lines (2112 loc) · 77.8 KB
/
change_processor_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
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 state_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/format"
apiv1 "k8s.io/api/core/v1"
discoveryV1 "k8s.io/api/discovery/v1"
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
v1 "sigs.k8s.io/gateway-api/apis/v1"
"sigs.k8s.io/gateway-api/apis/v1alpha2"
"sigs.k8s.io/gateway-api/apis/v1alpha3"
"sigs.k8s.io/gateway-api/apis/v1beta1"
ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/conditions"
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/controller/index"
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/gatewayclass"
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/helpers"
"github.com/nginxinc/nginx-gateway-fabric/internal/framework/kinds"
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state"
staticConds "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/conditions"
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/graph"
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/validation"
"github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/validation/validationfakes"
)
const (
controllerName = "my.controller"
gcName = "test-class"
)
func createRoute(
name string,
gateway string,
hostname string,
backendRefs ...v1.HTTPBackendRef,
) *v1.HTTPRoute {
return &v1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: name,
Generation: 1,
},
Spec: v1.HTTPRouteSpec{
CommonRouteSpec: v1.CommonRouteSpec{
ParentRefs: []v1.ParentReference{
{
Namespace: (*v1.Namespace)(helpers.GetPointer("test")),
Name: v1.ObjectName(gateway),
SectionName: (*v1.SectionName)(
helpers.GetPointer("listener-80-1"),
),
},
{
Namespace: (*v1.Namespace)(helpers.GetPointer("test")),
Name: v1.ObjectName(gateway),
SectionName: (*v1.SectionName)(
helpers.GetPointer("listener-443-1"),
),
},
},
},
Hostnames: []v1.Hostname{
v1.Hostname(hostname),
},
Rules: []v1.HTTPRouteRule{
{
Matches: []v1.HTTPRouteMatch{
{
Path: &v1.HTTPPathMatch{
Type: helpers.GetPointer(v1.PathMatchPathPrefix),
Value: helpers.GetPointer("/"),
},
},
},
BackendRefs: backendRefs,
},
},
},
}
}
func createGateway(name string) *v1.Gateway {
return &v1.Gateway{
ObjectMeta: metav1.ObjectMeta{
Namespace: "test",
Name: name,
Generation: 1,
},
Spec: v1.GatewaySpec{
GatewayClassName: gcName,
Listeners: []v1.Listener{
{
Name: "listener-80-1",
Hostname: nil,
Port: 80,
Protocol: v1.HTTPProtocolType,
},
},
},
}
}
func createGatewayWithTLSListener(name string, tlsSecret *apiv1.Secret) *v1.Gateway {
gw := createGateway(name)
l := v1.Listener{
Name: "listener-443-1",
Hostname: nil,
Port: 443,
Protocol: v1.HTTPSProtocolType,
TLS: &v1.GatewayTLSConfig{
Mode: helpers.GetPointer(v1.TLSModeTerminate),
CertificateRefs: []v1.SecretObjectReference{
{
Kind: (*v1.Kind)(helpers.GetPointer("Secret")),
Name: v1.ObjectName(tlsSecret.Name),
Namespace: (*v1.Namespace)(&tlsSecret.Namespace),
},
},
},
}
gw.Spec.Listeners = append(gw.Spec.Listeners, l)
return gw
}
func createRouteWithMultipleRules(
name, gateway, hostname string,
rules []v1.HTTPRouteRule,
) *v1.HTTPRoute {
hr := createRoute(name, gateway, hostname)
hr.Spec.Rules = rules
return hr
}
func createHTTPRule(path string, backendRefs ...v1.HTTPBackendRef) v1.HTTPRouteRule {
return v1.HTTPRouteRule{
Matches: []v1.HTTPRouteMatch{
{
Path: &v1.HTTPPathMatch{
Type: helpers.GetPointer(v1.PathMatchPathPrefix),
Value: &path,
},
},
},
BackendRefs: backendRefs,
}
}
func createBackendRef(
kind *v1.Kind,
name v1.ObjectName,
namespace *v1.Namespace,
) v1.HTTPBackendRef {
return v1.HTTPBackendRef{
BackendRef: v1.BackendRef{
BackendObjectReference: v1.BackendObjectReference{
Kind: kind,
Name: name,
Namespace: namespace,
Port: helpers.GetPointer[v1.PortNumber](80),
},
},
}
}
func createRouteBackendRefs(refs []v1.HTTPBackendRef) []graph.RouteBackendRef {
rbrs := make([]graph.RouteBackendRef, 0, len(refs))
for _, ref := range refs {
rbr := graph.RouteBackendRef{
BackendRef: ref.BackendRef,
}
rbrs = append(rbrs, rbr)
}
return rbrs
}
func createAlwaysValidValidators() validation.Validators {
return validation.Validators{
HTTPFieldsValidator: &validationfakes.FakeHTTPFieldsValidator{},
GenericValidator: &validationfakes.FakeGenericValidator{},
PolicyValidator: &validationfakes.FakePolicyValidator{},
}
}
func createScheme() *runtime.Scheme {
scheme := runtime.NewScheme()
utilruntime.Must(v1.Install(scheme))
utilruntime.Must(v1beta1.Install(scheme))
utilruntime.Must(v1alpha3.Install(scheme))
utilruntime.Must(apiv1.AddToScheme(scheme))
utilruntime.Must(discoveryV1.AddToScheme(scheme))
utilruntime.Must(apiext.AddToScheme(scheme))
utilruntime.Must(ngfAPI.AddToScheme(scheme))
return scheme
}
func getListenerByName(gw *graph.Gateway, name string) *graph.Listener {
for _, l := range gw.Listeners {
if l.Name == name {
return l
}
}
return nil
}
var (
cert = []byte(`-----BEGIN CERTIFICATE-----
MIIDLjCCAhYCCQDAOF9tLsaXWjANBgkqhkiG9w0BAQsFADBaMQswCQYDVQQGEwJV
UzELMAkGA1UECAwCQ0ExITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0
ZDEbMBkGA1UEAwwSY2FmZS5leGFtcGxlLm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feZqc
R0iKbhgbOcVVkJJ3K0yAyKwPTumxKHZ6zImZS0c0am+RY9YGq5T7YrzpzcfvpiOU
ffe3RyFT7cfCmfoOhDCtzukCgYB30oLC1RLFOrqn43vCS51zc5zoY44uBzspwwYN
TwvP/ExWMf3VJrDjBCH+T/6sysePbJEImlzM+IwytFpANfiIXEt/48Xf60Nx8gWM
uHyxZZx/NKtDw0V8vX1POnq2A5eiKa+8jRARYKJLYNdfDuwolxvG6bZhkPi/4EtT
3Y18sQKBgHtKbk+7lNJVeswXE5cUG6EDUsDe/2Ua7fXp7FcjqBEoap1LSw+6TXp0
ZgrmKE8ARzM47+EJHUviiq/nupE15g0kJW3syhpU9zZLO7ltB0KIkO9ZRcmUjo8Q
cpLlHMAqbLJ8WYGJCkhiWxyal6hYTyWY4cVkC0xtTl/hUE9IeNKo
-----END RSA PRIVATE KEY-----`)
)
var _ = Describe("ChangeProcessor", func() {
// graph outputs are large, so allow gomega to print everything on test failure
format.MaxLength = 0
Describe("Normal cases of processing changes", func() {
var (
gc = &v1.GatewayClass{
ObjectMeta: metav1.ObjectMeta{
Name: gcName,
Generation: 1,
},
Spec: v1.GatewayClassSpec{
ControllerName: controllerName,
},
}
processor state.ChangeProcessor
)
BeforeEach(OncePerOrdered, func() {
processor = state.NewChangeProcessorImpl(state.ChangeProcessorConfig{
GatewayCtlrName: controllerName,
GatewayClassName: gcName,
Logger: zap.New(),
Validators: createAlwaysValidValidators(),
MustExtractGVK: kinds.NewMustExtractGKV(createScheme()),
})
})
Describe("Process gateway resources", Ordered, func() {
var (
gcUpdated *v1.GatewayClass
diffNsTLSSecret, sameNsTLSSecret *apiv1.Secret
hr1, hr1Updated, hr2 *v1.HTTPRoute
gw1, gw1Updated, gw2 *v1.Gateway
refGrant1, refGrant2 *v1beta1.ReferenceGrant
expGraph *graph.Graph
expRouteHR1, expRouteHR2 *graph.L7Route
gatewayAPICRD, gatewayAPICRDUpdated *metav1.PartialObjectMetadata
routeKey1, routeKey2 graph.RouteKey
)
BeforeAll(func() {
gcUpdated = gc.DeepCopy()
gcUpdated.Generation++
crossNsBackendRef := v1.HTTPBackendRef{
BackendRef: v1.BackendRef{
BackendObjectReference: v1.BackendObjectReference{
Kind: helpers.GetPointer[v1.Kind]("Service"),
Name: "service",
Namespace: helpers.GetPointer[v1.Namespace]("service-ns"),
Port: helpers.GetPointer[v1.PortNumber](80),
},
},
}
hr1 = createRoute("hr-1", "gateway-1", "foo.example.com", crossNsBackendRef)
routeKey1 = graph.CreateRouteKey(hr1)
hr1Updated = hr1.DeepCopy()
hr1Updated.Generation++
hr2 = createRoute("hr-2", "gateway-2", "bar.example.com")
routeKey2 = graph.CreateRouteKey(hr2)
refGrant1 = &v1beta1.ReferenceGrant{
ObjectMeta: metav1.ObjectMeta{
Namespace: "cert-ns",
Name: "ref-grant",
},
Spec: v1beta1.ReferenceGrantSpec{
From: []v1beta1.ReferenceGrantFrom{
{
Group: v1.GroupName,
Kind: kinds.Gateway,
Namespace: "test",
},
},
To: []v1beta1.ReferenceGrantTo{
{
Kind: "Secret",
},
},
},
}
refGrant2 = &v1beta1.ReferenceGrant{
ObjectMeta: metav1.ObjectMeta{
Namespace: "service-ns",
Name: "ref-grant",
},
Spec: v1beta1.ReferenceGrantSpec{
From: []v1beta1.ReferenceGrantFrom{
{
Group: v1.GroupName,
Kind: kinds.HTTPRoute,
Namespace: "test",
},
},
To: []v1beta1.ReferenceGrantTo{
{
Kind: "Service",
},
},
},
}
sameNsTLSSecret = &apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "tls-secret",
Namespace: "test",
},
Type: apiv1.SecretTypeTLS,
Data: map[string][]byte{
apiv1.TLSCertKey: cert,
apiv1.TLSPrivateKeyKey: key,
},
}
diffNsTLSSecret = &apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "different-ns-tls-secret",
Namespace: "cert-ns",
},
Type: apiv1.SecretTypeTLS,
Data: map[string][]byte{
apiv1.TLSCertKey: cert,
apiv1.TLSPrivateKeyKey: key,
},
}
gw1 = createGatewayWithTLSListener("gateway-1", diffNsTLSSecret) // cert in diff namespace than gw
gw1Updated = gw1.DeepCopy()
gw1Updated.Generation++
gw2 = createGatewayWithTLSListener("gateway-2", sameNsTLSSecret)
gatewayAPICRD = &metav1.PartialObjectMetadata{
TypeMeta: metav1.TypeMeta{
Kind: "CustomResourceDefinition",
APIVersion: "apiextensions.k8s.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "gatewayclasses.gateway.networking.k8s.io",
Annotations: map[string]string{
gatewayclass.BundleVersionAnnotation: gatewayclass.SupportedVersion,
},
},
}
gatewayAPICRDUpdated = gatewayAPICRD.DeepCopy()
gatewayAPICRDUpdated.Annotations[gatewayclass.BundleVersionAnnotation] = "v1.99.0"
})
BeforeEach(func() {
expRouteHR1 = &graph.L7Route{
Source: hr1,
RouteType: graph.RouteTypeHTTP,
ParentRefs: []graph.ParentRef{
{
Attachment: &graph.ParentRefAttachmentStatus{
AcceptedHostnames: map[string][]string{"listener-80-1": {"foo.example.com"}},
Attached: true,
},
Gateway: types.NamespacedName{Namespace: "test", Name: "gateway-1"},
SectionName: hr1.Spec.ParentRefs[0].SectionName,
},
{
Attachment: &graph.ParentRefAttachmentStatus{
AcceptedHostnames: map[string][]string{"listener-443-1": {"foo.example.com"}},
Attached: true,
},
Gateway: types.NamespacedName{Namespace: "test", Name: "gateway-1"},
Idx: 1,
SectionName: hr1.Spec.ParentRefs[1].SectionName,
},
},
Spec: graph.L7RouteSpec{
Hostnames: hr1.Spec.Hostnames,
Rules: []graph.RouteRule{
{
BackendRefs: []graph.BackendRef{
{
SvcNsName: types.NamespacedName{Namespace: "service-ns", Name: "service"},
Weight: 1,
},
},
ValidMatches: true,
ValidFilters: true,
Matches: hr1.Spec.Rules[0].Matches,
RouteBackendRefs: createRouteBackendRefs(hr1.Spec.Rules[0].BackendRefs),
},
},
},
Valid: true,
Attachable: true,
Conditions: []conditions.Condition{
staticConds.NewRouteBackendRefRefBackendNotFound(
"spec.rules[0].backendRefs[0].name: Not found: \"service\"",
),
},
}
expRouteHR2 = &graph.L7Route{
Source: hr2,
RouteType: graph.RouteTypeHTTP,
ParentRefs: []graph.ParentRef{
{
Attachment: &graph.ParentRefAttachmentStatus{
AcceptedHostnames: map[string][]string{"listener-80-1": {"bar.example.com"}},
Attached: true,
},
Gateway: types.NamespacedName{Namespace: "test", Name: "gateway-2"},
SectionName: hr2.Spec.ParentRefs[0].SectionName,
},
{
Attachment: &graph.ParentRefAttachmentStatus{
AcceptedHostnames: map[string][]string{"listener-443-1": {"bar.example.com"}},
Attached: true,
},
Gateway: types.NamespacedName{Namespace: "test", Name: "gateway-2"},
Idx: 1,
SectionName: hr2.Spec.ParentRefs[1].SectionName,
},
},
Spec: graph.L7RouteSpec{
Hostnames: hr2.Spec.Hostnames,
Rules: []graph.RouteRule{
{
ValidMatches: true,
ValidFilters: true,
Matches: hr2.Spec.Rules[0].Matches,
RouteBackendRefs: []graph.RouteBackendRef{},
},
},
},
Valid: true,
Attachable: true,
}
// This is the base case expected graph. Tests will manipulate this to add or remove elements
// to fit the expected output of the input under test.
expGraph = &graph.Graph{
GatewayClass: &graph.GatewayClass{
Source: gc,
Valid: true,
},
Gateway: &graph.Gateway{
Source: gw1,
Listeners: []*graph.Listener{
{
Name: "listener-80-1",
Source: gw1.Spec.Listeners[0],
Valid: true,
Attachable: true,
Routes: map[graph.RouteKey]*graph.L7Route{routeKey1: expRouteHR1},
SupportedKinds: []v1.RouteGroupKind{{Kind: kinds.HTTPRoute}},
},
{
Name: "listener-443-1",
Source: gw1.Spec.Listeners[1],
Valid: true,
Attachable: true,
Routes: map[graph.RouteKey]*graph.L7Route{routeKey1: expRouteHR1},
ResolvedSecret: helpers.GetPointer(client.ObjectKeyFromObject(diffNsTLSSecret)),
SupportedKinds: []v1.RouteGroupKind{{Kind: kinds.HTTPRoute}},
},
},
Valid: true,
},
IgnoredGateways: map[types.NamespacedName]*v1.Gateway{},
Routes: map[graph.RouteKey]*graph.L7Route{routeKey1: expRouteHR1},
ReferencedSecrets: map[types.NamespacedName]*graph.Secret{},
ReferencedServices: map[types.NamespacedName]struct{}{
{
Namespace: "service-ns",
Name: "service",
}: {},
},
}
})
When("no upsert has occurred", func() {
It("returns nil graph", func() {
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.NoChange))
Expect(graphCfg).To(BeNil())
Expect(processor.GetLatestGraph()).To(BeNil())
})
})
When("GatewayClass doesn't exist", func() {
When("Gateway API CRD is added", func() {
It("returns empty graph", func() {
processor.CaptureUpsertChange(gatewayAPICRD)
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(&graph.Graph{}, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(&graph.Graph{}, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("Gateways don't exist", func() {
When("the first HTTPRoute is upserted", func() {
It("returns empty graph", func() {
processor.CaptureUpsertChange(hr1)
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(&graph.Graph{}, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(&graph.Graph{}, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the different namespace TLS Secret is upserted", func() {
It("returns nil graph", func() {
processor.CaptureUpsertChange(diffNsTLSSecret)
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.NoChange))
Expect(graphCfg).To(BeNil())
Expect(helpers.Diff(&graph.Graph{}, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the first Gateway is upserted", func() {
It("returns populated graph", func() {
processor.CaptureUpsertChange(gw1)
expGraph.GatewayClass = nil
expGraph.Gateway.Conditions = staticConds.NewGatewayInvalid("GatewayClass doesn't exist")
expGraph.Gateway.Valid = false
expGraph.Gateway.Listeners = nil
// no ref grant exists yet for hr1
expGraph.Routes[routeKey1].Conditions = []conditions.Condition{
staticConds.NewRouteBackendRefRefNotPermitted(
"Backend ref to Service service-ns/service not permitted by any ReferenceGrant",
),
}
expGraph.Routes[routeKey1].ParentRefs[0].Attachment = &graph.ParentRefAttachmentStatus{
AcceptedHostnames: map[string][]string{},
FailedCondition: staticConds.NewRouteNoMatchingParent(),
}
expGraph.Routes[routeKey1].ParentRefs[1].Attachment = &graph.ParentRefAttachmentStatus{
AcceptedHostnames: map[string][]string{},
FailedCondition: staticConds.NewRouteNoMatchingParent(),
}
expGraph.ReferencedSecrets = nil
expGraph.ReferencedServices = nil
expRouteHR1.Spec.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
})
})
When("the GatewayClass is upserted", func() {
It("returns updated graph", func() {
processor.CaptureUpsertChange(gc)
// No ref grant exists yet for gw1
// so the listener is not valid, but still attachable
listener443 := getListenerByName(expGraph.Gateway, "listener-443-1")
listener443.Valid = false
listener443.ResolvedSecret = nil
listener443.Conditions = staticConds.NewListenerRefNotPermitted(
"Certificate ref to secret cert-ns/different-ns-tls-secret not permitted by any ReferenceGrant",
)
expAttachment80 := &graph.ParentRefAttachmentStatus{
AcceptedHostnames: map[string][]string{
"listener-80-1": {"foo.example.com"},
},
Attached: true,
}
expAttachment443 := &graph.ParentRefAttachmentStatus{
AcceptedHostnames: map[string][]string{
"listener-443-1": {"foo.example.com"},
},
Attached: true,
}
listener80 := getListenerByName(expGraph.Gateway, "listener-80-1")
listener80.Routes[routeKey1].ParentRefs[0].Attachment = expAttachment80
listener443.Routes[routeKey1].ParentRefs[1].Attachment = expAttachment443
// no ref grant exists yet for hr1
expGraph.Routes[routeKey1].Conditions = []conditions.Condition{
staticConds.NewRouteInvalidListener(),
staticConds.NewRouteBackendRefRefNotPermitted(
"Backend ref to Service service-ns/service not permitted by any ReferenceGrant",
),
}
expGraph.Routes[routeKey1].ParentRefs[0].Attachment = expAttachment80
expGraph.Routes[routeKey1].ParentRefs[1].Attachment = expAttachment443
expGraph.ReferencedSecrets = nil
expGraph.ReferencedServices = nil
expRouteHR1.Spec.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the ReferenceGrant allowing the Gateway to reference its Secret is upserted", func() {
It("returns updated graph", func() {
processor.CaptureUpsertChange(refGrant1)
// no ref grant exists yet for hr1
expGraph.Routes[routeKey1].Conditions = []conditions.Condition{
staticConds.NewRouteBackendRefRefNotPermitted(
"Backend ref to Service service-ns/service not permitted by any ReferenceGrant",
),
}
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{
Source: diffNsTLSSecret,
}
expGraph.ReferencedServices = nil
expRouteHR1.Spec.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the ReferenceGrant allowing the hr1 to reference the Service in different ns is upserted", func() {
It("returns updated graph", func() {
processor.CaptureUpsertChange(refGrant2)
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{
Source: diffNsTLSSecret,
}
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the Gateway API CRD with bundle version annotation change is processed", func() {
It("returns updated graph", func() {
processor.CaptureUpsertChange(gatewayAPICRDUpdated)
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{
Source: diffNsTLSSecret,
}
expGraph.GatewayClass.Conditions = conditions.NewGatewayClassSupportedVersionBestEffort(
gatewayclass.SupportedVersion,
)
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the Gateway API CRD without bundle version annotation change is processed", func() {
It("returns nil graph", func() {
gatewayAPICRDSameVersion := gatewayAPICRDUpdated.DeepCopy()
processor.CaptureUpsertChange(gatewayAPICRDSameVersion)
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{
Source: diffNsTLSSecret,
}
expGraph.GatewayClass.Conditions = conditions.NewGatewayClassSupportedVersionBestEffort(
gatewayclass.SupportedVersion,
)
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.NoChange))
Expect(graphCfg).To(BeNil())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the Gateway API CRD with bundle version annotation change is processed", func() {
It("returns updated graph", func() {
// change back to supported version
processor.CaptureUpsertChange(gatewayAPICRD)
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{
Source: diffNsTLSSecret,
}
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the first HTTPRoute update with a generation changed is processed", func() {
It("returns populated graph", func() {
processor.CaptureUpsertChange(hr1Updated)
listener443 := getListenerByName(expGraph.Gateway, "listener-443-1")
listener443.Routes[routeKey1].Source.SetGeneration(hr1Updated.Generation)
listener80 := getListenerByName(expGraph.Gateway, "listener-80-1")
listener80.Routes[routeKey1].Source.SetGeneration(hr1Updated.Generation)
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{
Source: diffNsTLSSecret,
}
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
},
)
})
When("the first Gateway update with a generation changed is processed", func() {
It("returns populated graph", func() {
processor.CaptureUpsertChange(gw1Updated)
expGraph.Gateway.Source.Generation = gw1Updated.Generation
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{
Source: diffNsTLSSecret,
}
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the GatewayClass update with generation change is processed", func() {
It("returns populated graph", func() {
processor.CaptureUpsertChange(gcUpdated)
expGraph.GatewayClass.Source.Generation = gcUpdated.Generation
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{
Source: diffNsTLSSecret,
}
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the different namespace TLS secret is upserted again", func() {
It("returns populated graph", func() {
processor.CaptureUpsertChange(diffNsTLSSecret)
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{
Source: diffNsTLSSecret,
}
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("no changes are captured", func() {
It("returns nil graph", func() {
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{
Source: diffNsTLSSecret,
}
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.NoChange))
Expect(graphCfg).To(BeNil())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the same namespace TLS Secret is upserted", func() {
It("returns nil graph", func() {
processor.CaptureUpsertChange(sameNsTLSSecret)
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{
Source: diffNsTLSSecret,
}
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.NoChange))
Expect(graphCfg).To(BeNil())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the second Gateway is upserted", func() {
It("returns populated graph using first gateway", func() {
processor.CaptureUpsertChange(gw2)
expGraph.IgnoredGateways = map[types.NamespacedName]*v1.Gateway{
{Namespace: "test", Name: "gateway-2"}: gw2,
}
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{
Source: diffNsTLSSecret,
}
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the second HTTPRoute is upserted", func() {
It("returns populated graph", func() {
processor.CaptureUpsertChange(hr2)
expGraph.IgnoredGateways = map[types.NamespacedName]*v1.Gateway{
{Namespace: "test", Name: "gateway-2"}: gw2,
}
expGraph.Routes[routeKey2] = expRouteHR2
expGraph.Routes[routeKey2].ParentRefs[0].Attachment = &graph.ParentRefAttachmentStatus{
AcceptedHostnames: map[string][]string{},
FailedCondition: staticConds.NewTODO("Gateway is ignored"),
}
expGraph.Routes[routeKey2].ParentRefs[1].Attachment = &graph.ParentRefAttachmentStatus{
AcceptedHostnames: map[string][]string{},
FailedCondition: staticConds.NewTODO("Gateway is ignored"),
}
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{
Source: diffNsTLSSecret,
}
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the first Gateway is deleted", func() {
It("returns updated graph", func() {
processor.CaptureDeleteChange(
&v1.Gateway{},
types.NamespacedName{Namespace: "test", Name: "gateway-1"},
)
// gateway 2 takes over;
// route 1 has been replaced by route 2
listener80 := getListenerByName(expGraph.Gateway, "listener-80-1")
listener443 := getListenerByName(expGraph.Gateway, "listener-443-1")
expGraph.Gateway.Source = gw2
listener80.Source = gw2.Spec.Listeners[0]
listener443.Source = gw2.Spec.Listeners[1]
delete(listener80.Routes, routeKey1)
delete(listener443.Routes, routeKey1)
listener80.Routes[routeKey2] = expRouteHR2
listener443.Routes[routeKey2] = expRouteHR2
delete(expGraph.Routes, routeKey1)
expGraph.Routes[routeKey2] = expRouteHR2
sameNsTLSSecretRef := helpers.GetPointer(client.ObjectKeyFromObject(sameNsTLSSecret))
listener443.ResolvedSecret = sameNsTLSSecretRef
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(sameNsTLSSecret)] = &graph.Secret{
Source: sameNsTLSSecret,
}
expRouteHR1.Spec.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
expGraph.ReferencedServices = nil
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the second HTTPRoute is deleted", func() {
It("returns updated graph", func() {
processor.CaptureDeleteChange(
&v1.HTTPRoute{},
types.NamespacedName{Namespace: "test", Name: "hr-2"},
)
// gateway 2 still in charge;
// no routes remain
listener80 := getListenerByName(expGraph.Gateway, "listener-80-1")
listener443 := getListenerByName(expGraph.Gateway, "listener-443-1")
expGraph.Gateway.Source = gw2
listener80.Source = gw2.Spec.Listeners[0]
listener443.Source = gw2.Spec.Listeners[1]
delete(listener80.Routes, routeKey1)
delete(listener443.Routes, routeKey1)
expGraph.Routes = map[graph.RouteKey]*graph.L7Route{}
sameNsTLSSecretRef := helpers.GetPointer(client.ObjectKeyFromObject(sameNsTLSSecret))
listener443.ResolvedSecret = sameNsTLSSecretRef
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(sameNsTLSSecret)] = &graph.Secret{
Source: sameNsTLSSecret,
}
expRouteHR1.Spec.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
expGraph.ReferencedServices = nil
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the GatewayClass is deleted", func() {
It("returns updated graph", func() {
processor.CaptureDeleteChange(
&v1.GatewayClass{},
types.NamespacedName{Name: gcName},
)
expGraph.GatewayClass = nil
expGraph.Gateway = &graph.Gateway{
Source: gw2,
Conditions: staticConds.NewGatewayInvalid("GatewayClass doesn't exist"),
}
expGraph.Routes = map[graph.RouteKey]*graph.L7Route{}
expGraph.ReferencedSecrets = nil
expRouteHR1.Spec.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
expGraph.ReferencedServices = nil
changed, graphCfg := processor.Process()
Expect(changed).To(Equal(state.ClusterStateChange))
Expect(helpers.Diff(expGraph, graphCfg)).To(BeEmpty())
Expect(helpers.Diff(expGraph, processor.GetLatestGraph())).To(BeEmpty())
})
})
When("the second Gateway is deleted", func() {
It("returns empty graph", func() {
processor.CaptureDeleteChange(
&v1.Gateway{},