Skip to content

Commit 6692f13

Browse files
committed
add listener hostname check for specififcity
1 parent bc34d59 commit 6692f13

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

internal/mode/static/state/dataplane/configuration.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,27 @@ func BuildConfiguration(
7070

7171
// buildPassthroughServers builds TLSPassthroughServers from TLSRoutes attaches to listeners.
7272
func buildPassthroughServers(g *graph.Graph) []Layer4VirtualServer {
73-
passthroughServers := make([]Layer4VirtualServer, 0)
73+
passthroughServersMap := make(map[graph.L4RouteKey][]Layer4VirtualServer)
74+
routeToListenerHostname := make(map[graph.L4RouteKey]*v1.Hostname)
75+
7476
for _, l := range g.Gateway.Listeners {
7577
if !l.Valid {
7678
continue
7779
}
78-
for _, r := range l.L4Routes {
80+
for key, r := range l.L4Routes {
7981
if !r.Valid {
8082
continue
8183
}
84+
85+
if _, ok := passthroughServersMap[key]; ok {
86+
if listenerHostnameMoreSpecific(l.Source.Hostname, routeToListenerHostname[key]) {
87+
continue
88+
}
89+
passthroughServersMap[key] = []Layer4VirtualServer{}
90+
}
91+
8292
for _, h := range r.Spec.Hostnames {
83-
passthroughServers = append(passthroughServers, Layer4VirtualServer{
93+
passthroughServersMap[key] = append(passthroughServersMap[key], Layer4VirtualServer{
8494
Hostname: string(h),
8595
UpstreamName: r.Spec.BackendRef.ServicePortReference(),
8696
Port: int32(l.Source.Port),
@@ -89,6 +99,12 @@ func buildPassthroughServers(g *graph.Graph) []Layer4VirtualServer {
8999
}
90100
}
91101

102+
passthroughServers := make([]Layer4VirtualServer, 0, len(passthroughServersMap))
103+
104+
for _, r := range passthroughServersMap {
105+
passthroughServers = append(passthroughServers, r...)
106+
}
107+
92108
return passthroughServers
93109
}
94110

internal/mode/static/state/dataplane/configuration_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"k8s.io/apimachinery/pkg/runtime/schema"
1414
"k8s.io/apimachinery/pkg/types"
1515
"k8s.io/apimachinery/pkg/util/intstr"
16+
"k8s.io/utils/ptr"
1617
"sigs.k8s.io/controller-runtime/pkg/client"
1718
v1 "sigs.k8s.io/gateway-api/apis/v1"
1819
"sigs.k8s.io/gateway-api/apis/v1alpha2"
@@ -3418,6 +3419,7 @@ func TestCreatePassthroughServers(t *testing.T) {
34183419
Source: v1.Listener{
34193420
Protocol: v1.TLSProtocolType,
34203421
Port: 443,
3422+
Hostname: ptr.To[v1.Hostname]("*.example.com"),
34213423
},
34223424
Routes: make(map[graph.RouteKey]*graph.L7Route),
34233425
L4Routes: map[graph.L4RouteKey]*graph.L4Route{
@@ -3453,6 +3455,43 @@ func TestCreatePassthroughServers(t *testing.T) {
34533455
},
34543456
Valid: true,
34553457
},
3458+
{
3459+
Name: "testingListener2",
3460+
Source: v1.Listener{
3461+
Protocol: v1.TLSProtocolType,
3462+
Port: 443,
3463+
Hostname: ptr.To[v1.Hostname]("cafe.example.com"),
3464+
},
3465+
Routes: make(map[graph.RouteKey]*graph.L7Route),
3466+
L4Routes: map[graph.L4RouteKey]*graph.L4Route{
3467+
{NamespacedName: types.NamespacedName{
3468+
Namespace: "default",
3469+
Name: "secure-app",
3470+
}}: {
3471+
Spec: graph.L4RouteSpec{
3472+
Hostnames: []v1.Hostname{"app.example.com", "cafe.example.com"},
3473+
BackendRef: graph.BackendRef{
3474+
SvcNsName: types.NamespacedName{
3475+
Namespace: "default",
3476+
Name: "secure-app",
3477+
},
3478+
ServicePort: apiv1.ServicePort{
3479+
Name: "https",
3480+
Protocol: "TCP",
3481+
Port: 8443,
3482+
TargetPort: intstr.IntOrString{
3483+
Type: intstr.Int,
3484+
IntVal: 8443,
3485+
},
3486+
},
3487+
Valid: true,
3488+
},
3489+
},
3490+
Valid: true,
3491+
},
3492+
},
3493+
Valid: true,
3494+
},
34563495
},
34573496
},
34583497
}

0 commit comments

Comments
 (0)