Skip to content

Commit aa9772c

Browse files
committed
Refactor and dedupe routes
1 parent 6b6c667 commit aa9772c

25 files changed

+1415
-2307
lines changed

internal/mode/static/handler.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,23 +245,18 @@ func (h *eventHandlerImpl) updateStatuses(ctx context.Context, logger logr.Logge
245245
if h.cfg.updateGatewayClassStatus {
246246
gcReqs = status.PrepareGatewayClassRequests(graph.GatewayClass, graph.IgnoredGatewayClasses, transitionTime)
247247
}
248-
hrReqs := status.PrepareHTTPRouteRequests(
249-
graph.HTTPRoutes,
248+
routeReqs := status.PrepareRouteRequests(
249+
graph.Routes,
250250
transitionTime,
251251
h.latestReloadResult,
252252
h.cfg.gatewayCtlrName,
253253
)
254-
grReqs := status.PrepareGRPCRouteRequests(
255-
graph.GRPCRoutes, transitionTime,
256-
h.latestReloadResult,
257-
h.cfg.gatewayCtlrName,
258-
)
254+
259255
polReqs := status.PrepareBackendTLSPolicyRequests(graph.BackendTLSPolicies, transitionTime, h.cfg.gatewayCtlrName)
260256

261-
reqs := make([]frameworkStatus.UpdateRequest, 0, len(gcReqs)+len(hrReqs)+len(grReqs)+len(polReqs))
257+
reqs := make([]frameworkStatus.UpdateRequest, 0, len(gcReqs)+len(routeReqs)+len(polReqs))
262258
reqs = append(reqs, gcReqs...)
263-
reqs = append(reqs, hrReqs...)
264-
reqs = append(reqs, grReqs...)
259+
reqs = append(reqs, routeReqs...)
265260
reqs = append(reqs, polReqs...)
266261

267262
h.cfg.statusUpdater.UpdateGroup(ctx, groupAllExceptGateways, reqs...)

internal/mode/static/state/change_processor_test.go

Lines changed: 100 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,18 @@ func createBackendRef(
170170
}
171171
}
172172

173+
func createRouteBackendRefs(refs []v1.HTTPBackendRef) []graph.RouteBackendRef {
174+
rbrs := make([]graph.RouteBackendRef, 0, len(refs))
175+
for _, ref := range refs {
176+
rbr := graph.RouteBackendRef{
177+
BackendRef: ref.BackendRef,
178+
Filters: []any{},
179+
}
180+
rbrs = append(rbrs, rbr)
181+
}
182+
return rbrs
183+
}
184+
173185
func createAlwaysValidValidators() validation.Validators {
174186
http := &validationfakes.FakeHTTPFieldsValidator{}
175187

@@ -286,9 +298,10 @@ var _ = Describe("ChangeProcessor", func() {
286298
gw1, gw1Updated, gw2 *v1.Gateway
287299
refGrant1, refGrant2 *v1beta1.ReferenceGrant
288300
expGraph *graph.Graph
289-
expRouteHR1, expRouteHR2 *graph.HTTPRoute
301+
expRouteHR1, expRouteHR2 *graph.L7Route
290302
hr1Name, hr2Name types.NamespacedName
291303
gatewayAPICRD, gatewayAPICRDUpdated *metav1.PartialObjectMetadata
304+
routeKey1, routeKey2 graph.RouteKey
292305
)
293306
BeforeAll(func() {
294307
gcUpdated = gc.DeepCopy()
@@ -308,12 +321,22 @@ var _ = Describe("ChangeProcessor", func() {
308321
hr1 = createRoute("hr-1", "gateway-1", "foo.example.com", crossNsBackendRef)
309322
hr1Name = types.NamespacedName{Namespace: hr1.Namespace, Name: hr1.Name}
310323

324+
routeKey1 = graph.RouteKey{
325+
NamespacedName: hr1Name,
326+
RouteType: graph.RouteTypeHTTP,
327+
}
328+
311329
hr1Updated = hr1.DeepCopy()
312330
hr1Updated.Generation++
313331

314332
hr2 = createRoute("hr-2", "gateway-2", "bar.example.com")
315333
hr2Name = types.NamespacedName{Namespace: "test", Name: "hr-2"}
316334

335+
routeKey2 = graph.RouteKey{
336+
NamespacedName: hr2Name,
337+
RouteType: graph.RouteTypeHTTP,
338+
}
339+
317340
refGrant1 = &v1beta1.ReferenceGrant{
318341
ObjectMeta: metav1.ObjectMeta{
319342
Namespace: "cert-ns",
@@ -404,8 +427,10 @@ var _ = Describe("ChangeProcessor", func() {
404427
gatewayAPICRDUpdated.Annotations[gatewayclass.BundleVersionAnnotation] = "v1.99.0"
405428
})
406429
BeforeEach(func() {
407-
expRouteHR1 = &graph.HTTPRoute{
408-
Source: hr1,
430+
expRouteHR1 = &graph.L7Route{
431+
Source: hr1,
432+
RouteType: graph.RouteTypeHTTP,
433+
SrcParentRefs: hr1.Spec.ParentRefs,
409434
ParentRefs: []graph.ParentRef{
410435
{
411436
Attachment: &graph.ParentRefAttachmentStatus{
@@ -423,16 +448,21 @@ var _ = Describe("ChangeProcessor", func() {
423448
Idx: 1,
424449
},
425450
},
426-
Rules: []graph.Rule{
427-
{
428-
BackendRefs: []graph.BackendRef{
429-
{
430-
SvcNsName: types.NamespacedName{Namespace: "service-ns", Name: "service"},
431-
Weight: 1,
451+
Spec: graph.L7RouteSpec{
452+
Hostnames: hr1.Spec.Hostnames,
453+
Rules: []graph.RouteRule{
454+
{
455+
BackendRefs: []graph.BackendRef{
456+
{
457+
SvcNsName: types.NamespacedName{Namespace: "service-ns", Name: "service"},
458+
Weight: 1,
459+
},
432460
},
461+
ValidMatches: true,
462+
ValidFilters: true,
463+
Matches: hr1.Spec.Rules[0].Matches,
464+
RouteBackendRefs: createRouteBackendRefs(hr1.Spec.Rules[0].BackendRefs),
433465
},
434-
ValidMatches: true,
435-
ValidFilters: true,
436466
},
437467
},
438468
Valid: true,
@@ -444,8 +474,10 @@ var _ = Describe("ChangeProcessor", func() {
444474
},
445475
}
446476

447-
expRouteHR2 = &graph.HTTPRoute{
448-
Source: hr2,
477+
expRouteHR2 = &graph.L7Route{
478+
Source: hr2,
479+
RouteType: graph.RouteTypeHTTP,
480+
SrcParentRefs: hr2.Spec.ParentRefs,
449481
ParentRefs: []graph.ParentRef{
450482
{
451483
Attachment: &graph.ParentRefAttachmentStatus{
@@ -463,7 +495,17 @@ var _ = Describe("ChangeProcessor", func() {
463495
Idx: 1,
464496
},
465497
},
466-
Rules: []graph.Rule{{ValidMatches: true, ValidFilters: true}},
498+
Spec: graph.L7RouteSpec{
499+
Hostnames: hr2.Spec.Hostnames,
500+
Rules: []graph.RouteRule{
501+
{
502+
ValidMatches: true,
503+
ValidFilters: true,
504+
Matches: hr2.Spec.Rules[0].Matches,
505+
RouteBackendRefs: []graph.RouteBackendRef{},
506+
},
507+
},
508+
},
467509
Valid: true,
468510
Attachable: true,
469511
}
@@ -479,36 +521,27 @@ var _ = Describe("ChangeProcessor", func() {
479521
Source: gw1,
480522
Listeners: []*graph.Listener{
481523
{
482-
Name: "listener-80-1",
483-
Source: gw1.Spec.Listeners[0],
484-
Valid: true,
485-
Attachable: true,
486-
GRPCRoutes: map[types.NamespacedName]*graph.GRPCRoute{},
487-
HTTPRoutes: map[types.NamespacedName]*graph.HTTPRoute{
488-
{Namespace: "test", Name: "hr-1"}: expRouteHR1,
489-
},
524+
Name: "listener-80-1",
525+
Source: gw1.Spec.Listeners[0],
526+
Valid: true,
527+
Attachable: true,
528+
Routes: map[graph.RouteKey]*graph.L7Route{routeKey1: expRouteHR1},
490529
SupportedKinds: []v1.RouteGroupKind{{Kind: "HTTPRoute"}},
491530
},
492531
{
493-
Name: "listener-443-1",
494-
Source: gw1.Spec.Listeners[1],
495-
Valid: true,
496-
Attachable: true,
497-
GRPCRoutes: map[types.NamespacedName]*graph.GRPCRoute{},
498-
HTTPRoutes: map[types.NamespacedName]*graph.HTTPRoute{
499-
{Namespace: "test", Name: "hr-1"}: expRouteHR1,
500-
},
532+
Name: "listener-443-1",
533+
Source: gw1.Spec.Listeners[1],
534+
Valid: true,
535+
Attachable: true,
536+
Routes: map[graph.RouteKey]*graph.L7Route{routeKey1: expRouteHR1},
501537
ResolvedSecret: helpers.GetPointer(client.ObjectKeyFromObject(diffNsTLSSecret)),
502538
SupportedKinds: []v1.RouteGroupKind{{Kind: "HTTPRoute"}},
503539
},
504540
},
505541
Valid: true,
506542
},
507-
IgnoredGateways: map[types.NamespacedName]*v1.Gateway{},
508-
GRPCRoutes: map[types.NamespacedName]*graph.GRPCRoute{},
509-
HTTPRoutes: map[types.NamespacedName]*graph.HTTPRoute{
510-
{Namespace: "test", Name: "hr-1"}: expRouteHR1,
511-
},
543+
IgnoredGateways: map[types.NamespacedName]*v1.Gateway{},
544+
Routes: map[graph.RouteKey]*graph.L7Route{routeKey1: expRouteHR1},
512545
ReferencedSecrets: map[types.NamespacedName]*graph.Secret{},
513546
ReferencedServices: map[types.NamespacedName]struct{}{
514547
{
@@ -569,24 +602,24 @@ var _ = Describe("ChangeProcessor", func() {
569602
expGraph.Gateway.Listeners = nil
570603

571604
// no ref grant exists yet for hr1
572-
expGraph.HTTPRoutes[hr1Name].Conditions = []conditions.Condition{
605+
expGraph.Routes[routeKey1].Conditions = []conditions.Condition{
573606
staticConds.NewRouteBackendRefRefNotPermitted(
574607
"Backend ref to Service service-ns/service not permitted by any ReferenceGrant",
575608
),
576609
}
577-
expGraph.HTTPRoutes[hr1Name].ParentRefs[0].Attachment = &graph.ParentRefAttachmentStatus{
610+
expGraph.Routes[routeKey1].ParentRefs[0].Attachment = &graph.ParentRefAttachmentStatus{
578611
AcceptedHostnames: map[string][]string{},
579612
FailedCondition: staticConds.NewRouteNoMatchingParent(),
580613
}
581-
expGraph.HTTPRoutes[hr1Name].ParentRefs[1].Attachment = &graph.ParentRefAttachmentStatus{
614+
expGraph.Routes[routeKey1].ParentRefs[1].Attachment = &graph.ParentRefAttachmentStatus{
582615
AcceptedHostnames: map[string][]string{},
583616
FailedCondition: staticConds.NewRouteNoMatchingParent(),
584617
}
585618

586619
expGraph.ReferencedSecrets = nil
587620
expGraph.ReferencedServices = nil
588621

589-
expRouteHR1.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
622+
expRouteHR1.Spec.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
590623

591624
changed, graphCfg := processor.Process()
592625
Expect(changed).To(Equal(state.ClusterStateChange))
@@ -624,23 +657,23 @@ var _ = Describe("ChangeProcessor", func() {
624657
}
625658

626659
listener80 := getListenerByName(expGraph.Gateway, "listener-80-1")
627-
listener80.HTTPRoutes[hr1Name].ParentRefs[0].Attachment = expAttachment80
628-
listener443.HTTPRoutes[hr1Name].ParentRefs[1].Attachment = expAttachment443
660+
listener80.Routes[routeKey1].ParentRefs[0].Attachment = expAttachment80
661+
listener443.Routes[routeKey1].ParentRefs[1].Attachment = expAttachment443
629662

630663
// no ref grant exists yet for hr1
631-
expGraph.HTTPRoutes[hr1Name].Conditions = []conditions.Condition{
664+
expGraph.Routes[routeKey1].Conditions = []conditions.Condition{
632665
staticConds.NewRouteInvalidListener(),
633666
staticConds.NewRouteBackendRefRefNotPermitted(
634667
"Backend ref to Service service-ns/service not permitted by any ReferenceGrant",
635668
),
636669
}
637-
expGraph.HTTPRoutes[hr1Name].ParentRefs[0].Attachment = expAttachment80
638-
expGraph.HTTPRoutes[hr1Name].ParentRefs[1].Attachment = expAttachment443
670+
expGraph.Routes[routeKey1].ParentRefs[0].Attachment = expAttachment80
671+
expGraph.Routes[routeKey1].ParentRefs[1].Attachment = expAttachment443
639672

640673
expGraph.ReferencedSecrets = nil
641674
expGraph.ReferencedServices = nil
642675

643-
expRouteHR1.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
676+
expRouteHR1.Spec.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
644677

645678
changed, graphCfg := processor.Process()
646679
Expect(changed).To(Equal(state.ClusterStateChange))
@@ -653,7 +686,7 @@ var _ = Describe("ChangeProcessor", func() {
653686
processor.CaptureUpsertChange(refGrant1)
654687

655688
// no ref grant exists yet for hr1
656-
expGraph.HTTPRoutes[hr1Name].Conditions = []conditions.Condition{
689+
expGraph.Routes[routeKey1].Conditions = []conditions.Condition{
657690
staticConds.NewRouteBackendRefRefNotPermitted(
658691
"Backend ref to Service service-ns/service not permitted by any ReferenceGrant",
659692
),
@@ -663,7 +696,7 @@ var _ = Describe("ChangeProcessor", func() {
663696
}
664697

665698
expGraph.ReferencedServices = nil
666-
expRouteHR1.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
699+
expRouteHR1.Spec.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
667700

668701
changed, graphCfg := processor.Process()
669702
Expect(changed).To(Equal(state.ClusterStateChange))
@@ -743,10 +776,10 @@ var _ = Describe("ChangeProcessor", func() {
743776
processor.CaptureUpsertChange(hr1Updated)
744777

745778
listener443 := getListenerByName(expGraph.Gateway, "listener-443-1")
746-
listener443.HTTPRoutes[hr1Name].Source.Generation = hr1Updated.Generation
779+
listener443.Routes[routeKey1].Source.SetGeneration(hr1Updated.Generation)
747780

748781
listener80 := getListenerByName(expGraph.Gateway, "listener-80-1")
749-
listener80.HTTPRoutes[hr1Name].Source.Generation = hr1Updated.Generation
782+
listener80.Routes[routeKey1].Source.SetGeneration(hr1Updated.Generation)
750783
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(diffNsTLSSecret)] = &graph.Secret{
751784
Source: diffNsTLSSecret,
752785
}
@@ -852,12 +885,12 @@ var _ = Describe("ChangeProcessor", func() {
852885
expGraph.IgnoredGateways = map[types.NamespacedName]*v1.Gateway{
853886
{Namespace: "test", Name: "gateway-2"}: gw2,
854887
}
855-
expGraph.HTTPRoutes[hr2Name] = expRouteHR2
856-
expGraph.HTTPRoutes[hr2Name].ParentRefs[0].Attachment = &graph.ParentRefAttachmentStatus{
888+
expGraph.Routes[routeKey2] = expRouteHR2
889+
expGraph.Routes[routeKey2].ParentRefs[0].Attachment = &graph.ParentRefAttachmentStatus{
857890
AcceptedHostnames: map[string][]string{},
858891
FailedCondition: staticConds.NewTODO("Gateway is ignored"),
859892
}
860-
expGraph.HTTPRoutes[hr2Name].ParentRefs[1].Attachment = &graph.ParentRefAttachmentStatus{
893+
expGraph.Routes[routeKey2].ParentRefs[1].Attachment = &graph.ParentRefAttachmentStatus{
861894
AcceptedHostnames: map[string][]string{},
862895
FailedCondition: staticConds.NewTODO("Gateway is ignored"),
863896
}
@@ -886,19 +919,19 @@ var _ = Describe("ChangeProcessor", func() {
886919
expGraph.Gateway.Source = gw2
887920
listener80.Source = gw2.Spec.Listeners[0]
888921
listener443.Source = gw2.Spec.Listeners[1]
889-
delete(listener80.HTTPRoutes, hr1Name)
890-
delete(listener443.HTTPRoutes, hr1Name)
891-
listener80.HTTPRoutes[hr2Name] = expRouteHR2
892-
listener443.HTTPRoutes[hr2Name] = expRouteHR2
893-
delete(expGraph.HTTPRoutes, hr1Name)
894-
expGraph.HTTPRoutes[hr2Name] = expRouteHR2
922+
delete(listener80.Routes, routeKey1)
923+
delete(listener443.Routes, routeKey1)
924+
listener80.Routes[routeKey2] = expRouteHR2
925+
listener443.Routes[routeKey2] = expRouteHR2
926+
delete(expGraph.Routes, routeKey1)
927+
expGraph.Routes[routeKey2] = expRouteHR2
895928
sameNsTLSSecretRef := helpers.GetPointer(client.ObjectKeyFromObject(sameNsTLSSecret))
896929
listener443.ResolvedSecret = sameNsTLSSecretRef
897930
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(sameNsTLSSecret)] = &graph.Secret{
898931
Source: sameNsTLSSecret,
899932
}
900933

901-
expRouteHR1.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
934+
expRouteHR1.Spec.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
902935
expGraph.ReferencedServices = nil
903936

904937
changed, graphCfg := processor.Process()
@@ -922,16 +955,16 @@ var _ = Describe("ChangeProcessor", func() {
922955
expGraph.Gateway.Source = gw2
923956
listener80.Source = gw2.Spec.Listeners[0]
924957
listener443.Source = gw2.Spec.Listeners[1]
925-
delete(listener80.HTTPRoutes, hr1Name)
926-
delete(listener443.HTTPRoutes, hr1Name)
927-
expGraph.HTTPRoutes = map[types.NamespacedName]*graph.HTTPRoute{}
958+
delete(listener80.Routes, routeKey1)
959+
delete(listener443.Routes, routeKey1)
960+
expGraph.Routes = map[graph.RouteKey]*graph.L7Route{}
928961
sameNsTLSSecretRef := helpers.GetPointer(client.ObjectKeyFromObject(sameNsTLSSecret))
929962
listener443.ResolvedSecret = sameNsTLSSecretRef
930963
expGraph.ReferencedSecrets[client.ObjectKeyFromObject(sameNsTLSSecret)] = &graph.Secret{
931964
Source: sameNsTLSSecret,
932965
}
933966

934-
expRouteHR1.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
967+
expRouteHR1.Spec.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
935968
expGraph.ReferencedServices = nil
936969

937970
changed, graphCfg := processor.Process()
@@ -952,10 +985,10 @@ var _ = Describe("ChangeProcessor", func() {
952985
Source: gw2,
953986
Conditions: staticConds.NewGatewayInvalid("GatewayClass doesn't exist"),
954987
}
955-
expGraph.HTTPRoutes = map[types.NamespacedName]*graph.HTTPRoute{}
988+
expGraph.Routes = map[graph.RouteKey]*graph.L7Route{}
956989
expGraph.ReferencedSecrets = nil
957990

958-
expRouteHR1.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
991+
expRouteHR1.Spec.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
959992
expGraph.ReferencedServices = nil
960993

961994
changed, graphCfg := processor.Process()
@@ -971,7 +1004,7 @@ var _ = Describe("ChangeProcessor", func() {
9711004
types.NamespacedName{Namespace: "test", Name: "gateway-2"},
9721005
)
9731006

974-
expRouteHR1.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
1007+
expRouteHR1.Spec.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
9751008
expGraph.ReferencedServices = nil
9761009

9771010
changed, graphCfg := processor.Process()
@@ -987,7 +1020,7 @@ var _ = Describe("ChangeProcessor", func() {
9871020
types.NamespacedName{Namespace: "test", Name: "hr-1"},
9881021
)
9891022

990-
expRouteHR1.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
1023+
expRouteHR1.Spec.Rules[0].BackendRefs[0].SvcNsName = types.NamespacedName{}
9911024
expGraph.ReferencedServices = nil
9921025

9931026
changed, graphCfg := processor.Process()

0 commit comments

Comments
 (0)