Skip to content

Commit bd8fb93

Browse files
committed
Refactor accepted hostnames to parent ref attachment
1 parent fa0d942 commit bd8fb93

File tree

7 files changed

+124
-235
lines changed

7 files changed

+124
-235
lines changed

internal/state/dataplane/configuration.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,12 @@ func (hpr *hostPathRules) upsertListener(l *graph.Listener) {
266266
}
267267

268268
for routeNsName, r := range l.Routes {
269-
hostnames := getHostnames(r, l)
269+
var hostnames []string
270+
for _, p := range r.ParentRefs {
271+
if val, exist := p.Attachment.AcceptedHostnames[string(l.Source.Name)]; exist {
272+
hostnames = val
273+
}
274+
}
270275

271276
for _, h := range hostnames {
272277
if prevListener, exists := hpr.listenersForHost[h]; exists {
@@ -327,23 +332,6 @@ func (hpr *hostPathRules) upsertListener(l *graph.Listener) {
327332
}
328333
}
329334

330-
func getHostnames(r *graph.Route, l *graph.Listener) []string {
331-
var hostnames []string
332-
for _, h := range r.Source.Spec.Hostnames {
333-
if _, exist := l.AcceptedHostnames[string(h)]; exist {
334-
hostnames = append(hostnames, string(h))
335-
}
336-
}
337-
338-
if len(r.Source.Spec.Hostnames) == 0 {
339-
for hostname := range l.AcceptedHostnames {
340-
hostnames = append(hostnames, hostname)
341-
}
342-
}
343-
344-
return hostnames
345-
}
346-
347335
func (hpr *hostPathRules) buildServers() []VirtualServer {
348336
servers := make([]VirtualServer, 0, len(hpr.rulesPerHost)+len(hpr.httpsListeners))
349337

internal/state/dataplane/configuration_test.go

Lines changed: 28 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,25 @@ func TestBuildConfiguration(t *testing.T) {
130130

131131
createInternalRoute := func(
132132
source *v1beta1.HTTPRoute,
133+
listenerName string,
133134
paths []pathAndType,
134135
) *graph.Route {
136+
hostnames := make([]string, 0, len(source.Spec.Hostnames))
137+
for _, h := range source.Spec.Hostnames {
138+
hostnames = append(hostnames, string(h))
139+
}
135140
r := &graph.Route{
136141
Source: source,
137142
Rules: createRules(source, paths),
143+
ParentRefs: []graph.ParentRef{
144+
{
145+
Attachment: &graph.ParentRefAttachmentStatus{
146+
AcceptedHostnames: map[string][]string{
147+
listenerName: hostnames,
148+
},
149+
},
150+
},
151+
},
138152
}
139153
return r
140154
}
@@ -162,7 +176,7 @@ func TestBuildConfiguration(t *testing.T) {
162176
*v1beta1.HTTPRoute, []BackendGroup, *graph.Route,
163177
) {
164178
hr := createRoute(name, hostname, listenerName, paths...)
165-
route := createInternalRoute(hr, paths)
179+
route := createInternalRoute(hr, listenerName, paths)
166180
groups := createExpBackendGroupsForRoute(route)
167181
return hr, groups, route
168182
}
@@ -224,14 +238,6 @@ func TestBuildConfiguration(t *testing.T) {
224238
pathAndType{path: "/valid", pathType: prefix}, pathAndType{path: "/valid", pathType: v1beta1.PathMatchExact},
225239
)
226240

227-
hrNoHost, expHRNoHostGroups, routeNoHost := createTestResources(
228-
"route-no-host",
229-
"",
230-
"listener-80-1",
231-
pathAndType{path: "/", pathType: prefix},
232-
)
233-
routeNoHost.Source.Spec.Hostnames = []v1beta1.Hostname{}
234-
235241
httpsHR1, expHTTPSHR1Groups, httpsRouteHR1 := createTestResources(
236242
"https-hr-1",
237243
"foo.example.com",
@@ -266,6 +272,8 @@ func TestBuildConfiguration(t *testing.T) {
266272
"listener-443-with-hostname",
267273
pathAndType{path: "/", pathType: prefix},
268274
)
275+
// add extra attachment for this route for duplicate listener test
276+
httpsRouteHR5.ParentRefs[0].Attachment.AcceptedHostnames["listener-443-1"] = []string{"example.com"}
269277

270278
httpsHR6, expHTTPSHR6Groups, httpsRouteHR6 := createTestResources(
271279
"https-hr-6",
@@ -360,10 +368,9 @@ func TestBuildConfiguration(t *testing.T) {
360368
Source: &v1beta1.Gateway{},
361369
Listeners: map[string]*graph.Listener{
362370
"listener-80-1": {
363-
Source: listener80,
364-
Valid: true,
365-
Routes: map[types.NamespacedName]*graph.Route{},
366-
AcceptedHostnames: map[string]struct{}{},
371+
Source: listener80,
372+
Valid: true,
373+
Routes: map[types.NamespacedName]*graph.Route{},
367374
},
368375
},
369376
},
@@ -389,18 +396,16 @@ func TestBuildConfiguration(t *testing.T) {
389396
Source: &v1beta1.Gateway{},
390397
Listeners: map[string]*graph.Listener{
391398
"listener-443-1": {
392-
Source: listener443, // nil hostname
393-
Valid: true,
394-
Routes: map[types.NamespacedName]*graph.Route{},
395-
AcceptedHostnames: map[string]struct{}{},
396-
SecretPath: secretPath,
399+
Source: listener443, // nil hostname
400+
Valid: true,
401+
Routes: map[types.NamespacedName]*graph.Route{},
402+
SecretPath: secretPath,
397403
},
398404
"listener-443-with-hostname": {
399-
Source: listener443WithHostname, // non-nil hostname
400-
Valid: true,
401-
Routes: map[types.NamespacedName]*graph.Route{},
402-
AcceptedHostnames: map[string]struct{}{},
403-
SecretPath: secretPath,
405+
Source: listener443WithHostname, // non-nil hostname
406+
Valid: true,
407+
Routes: map[types.NamespacedName]*graph.Route{},
408+
SecretPath: secretPath,
404409
},
405410
},
406411
},
@@ -466,10 +471,6 @@ func TestBuildConfiguration(t *testing.T) {
466471
{Namespace: "test", Name: "hr-1"}: routeHR1,
467472
{Namespace: "test", Name: "hr-2"}: routeHR2,
468473
},
469-
AcceptedHostnames: map[string]struct{}{
470-
"foo.example.com": {},
471-
"bar.example.com": {},
472-
},
473474
},
474475
},
475476
},
@@ -524,60 +525,6 @@ func TestBuildConfiguration(t *testing.T) {
524525
},
525526
msg: "one http listener with two routes for different hostnames",
526527
},
527-
{
528-
graph: &graph.Graph{
529-
GatewayClass: &graph.GatewayClass{
530-
Source: &v1beta1.GatewayClass{},
531-
Valid: true,
532-
},
533-
Gateway: &graph.Gateway{
534-
Source: &v1beta1.Gateway{},
535-
Listeners: map[string]*graph.Listener{
536-
"listener-80-1": {
537-
Source: listener80,
538-
Valid: true,
539-
Routes: map[types.NamespacedName]*graph.Route{
540-
{Namespace: "test", Name: "route-no-host"}: routeNoHost,
541-
},
542-
AcceptedHostnames: map[string]struct{}{
543-
wildcardHostname: {},
544-
},
545-
},
546-
},
547-
},
548-
Routes: map[types.NamespacedName]*graph.Route{
549-
{Namespace: "test", Name: "route-no-host"}: routeNoHost,
550-
},
551-
},
552-
expConf: Configuration{
553-
HTTPServers: []VirtualServer{
554-
{
555-
IsDefault: true,
556-
},
557-
{
558-
Hostname: wildcardHostname,
559-
PathRules: []PathRule{
560-
{
561-
Path: "/",
562-
PathType: PathTypePrefix,
563-
MatchRules: []MatchRule{
564-
{
565-
MatchIdx: 0,
566-
RuleIdx: 0,
567-
BackendGroup: expHRNoHostGroups[0],
568-
Source: hrNoHost,
569-
},
570-
},
571-
},
572-
},
573-
},
574-
},
575-
SSLServers: []VirtualServer{},
576-
Upstreams: []Upstream{fooUpstream},
577-
BackendGroups: []BackendGroup{expHRNoHostGroups[0]},
578-
},
579-
msg: "one listener and route without hostnames",
580-
},
581528
{
582529
graph: &graph.Graph{
583530
GatewayClass: &graph.GatewayClass{
@@ -595,10 +542,6 @@ func TestBuildConfiguration(t *testing.T) {
595542
{Namespace: "test", Name: "https-hr-1"}: httpsRouteHR1,
596543
{Namespace: "test", Name: "https-hr-2"}: httpsRouteHR2,
597544
},
598-
AcceptedHostnames: map[string]struct{}{
599-
"foo.example.com": {},
600-
"bar.example.com": {},
601-
},
602545
},
603546
"listener-443-with-hostname": {
604547
Source: listener443WithHostname,
@@ -607,9 +550,6 @@ func TestBuildConfiguration(t *testing.T) {
607550
Routes: map[types.NamespacedName]*graph.Route{
608551
{Namespace: "test", Name: "https-hr-5"}: httpsRouteHR5,
609552
},
610-
AcceptedHostnames: map[string]struct{}{
611-
"example.com": {},
612-
},
613553
},
614554
},
615555
},
@@ -711,9 +651,6 @@ func TestBuildConfiguration(t *testing.T) {
711651
{Namespace: "test", Name: "hr-3"}: routeHR3,
712652
{Namespace: "test", Name: "hr-4"}: routeHR4,
713653
},
714-
AcceptedHostnames: map[string]struct{}{
715-
"foo.example.com": {},
716-
},
717654
},
718655
"listener-443-1": {
719656
Source: listener443,
@@ -723,9 +660,6 @@ func TestBuildConfiguration(t *testing.T) {
723660
{Namespace: "test", Name: "https-hr-3"}: httpsRouteHR3,
724661
{Namespace: "test", Name: "https-hr-4"}: httpsRouteHR4,
725662
},
726-
AcceptedHostnames: map[string]struct{}{
727-
"foo.example.com": {},
728-
},
729663
},
730664
},
731665
},
@@ -877,9 +811,6 @@ func TestBuildConfiguration(t *testing.T) {
877811
Routes: map[types.NamespacedName]*graph.Route{
878812
{Namespace: "test", Name: "hr-1"}: routeHR1,
879813
},
880-
AcceptedHostnames: map[string]struct{}{
881-
"foo.example.com": {},
882-
},
883814
},
884815
},
885816
},
@@ -902,9 +833,6 @@ func TestBuildConfiguration(t *testing.T) {
902833
Routes: map[types.NamespacedName]*graph.Route{
903834
{Namespace: "test", Name: "hr-1"}: routeHR1,
904835
},
905-
AcceptedHostnames: map[string]struct{}{
906-
"foo.example.com": {},
907-
},
908836
},
909837
},
910838
},
@@ -942,9 +870,6 @@ func TestBuildConfiguration(t *testing.T) {
942870
Routes: map[types.NamespacedName]*graph.Route{
943871
{Namespace: "test", Name: "hr-5"}: routeHR5,
944872
},
945-
AcceptedHostnames: map[string]struct{}{
946-
"foo.example.com": {},
947-
},
948873
},
949874
},
950875
},
@@ -1014,9 +939,6 @@ func TestBuildConfiguration(t *testing.T) {
1014939
Routes: map[types.NamespacedName]*graph.Route{
1015940
{Namespace: "test", Name: "hr-6"}: routeHR6,
1016941
},
1017-
AcceptedHostnames: map[string]struct{}{
1018-
"foo.example.com": {},
1019-
},
1020942
},
1021943
"listener-443-1": {
1022944
Source: listener443,
@@ -1025,9 +947,6 @@ func TestBuildConfiguration(t *testing.T) {
1025947
Routes: map[types.NamespacedName]*graph.Route{
1026948
{Namespace: "test", Name: "https-hr-6"}: httpsRouteHR6,
1027949
},
1028-
AcceptedHostnames: map[string]struct{}{
1029-
"foo.example.com": {},
1030-
},
1031950
},
1032951
},
1033952
},
@@ -1111,9 +1030,6 @@ func TestBuildConfiguration(t *testing.T) {
11111030
Routes: map[types.NamespacedName]*graph.Route{
11121031
{Namespace: "test", Name: "hr-7"}: routeHR7,
11131032
},
1114-
AcceptedHostnames: map[string]struct{}{
1115-
"foo.example.com": {},
1116-
},
11171033
},
11181034
},
11191035
},
@@ -1178,9 +1094,6 @@ func TestBuildConfiguration(t *testing.T) {
11781094
Routes: map[types.NamespacedName]*graph.Route{
11791095
{Namespace: "test", Name: "https-hr-5"}: httpsRouteHR5,
11801096
},
1181-
AcceptedHostnames: map[string]struct{}{
1182-
"example.com": {},
1183-
},
11841097
},
11851098
"listener-443-1": {
11861099
Source: listener443,
@@ -1189,9 +1102,6 @@ func TestBuildConfiguration(t *testing.T) {
11891102
Routes: map[types.NamespacedName]*graph.Route{
11901103
{Namespace: "test", Name: "https-hr-5"}: httpsRouteHR5,
11911104
},
1192-
AcceptedHostnames: map[string]struct{}{
1193-
"example.com": {},
1194-
},
11951105
},
11961106
},
11971107
},

internal/state/graph/gateway_listener.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ type Listener struct {
1919
// Routes holds the routes attached to the Listener.
2020
// Only valid routes are attached.
2121
Routes map[types.NamespacedName]*Route
22-
// AcceptedHostnames is an intersection between the hostnames supported by the Listener and the hostnames
23-
// from the attached routes.
24-
AcceptedHostnames map[string]struct{}
2522
// SecretPath is the path to the secret on disk.
2623
SecretPath string
2724
// Conditions holds the conditions of the Listener.
@@ -147,10 +144,9 @@ func (c *listenerConfigurator) configure(listener v1beta1.Listener) *Listener {
147144
}
148145

149146
l := &Listener{
150-
Source: listener,
151-
Routes: make(map[types.NamespacedName]*Route),
152-
AcceptedHostnames: make(map[string]struct{}),
153-
Valid: true,
147+
Source: listener,
148+
Routes: make(map[types.NamespacedName]*Route),
149+
Valid: true,
154150
}
155151

156152
// resolvers might add different conditions to the listener, so we run them all.

0 commit comments

Comments
 (0)