Skip to content

Commit 675b9f1

Browse files
committed
BUG: add the if not exist condition to map matching with TLS Route
1 parent 73260f2 commit 675b9f1

File tree

5 files changed

+32
-32
lines changed

5 files changed

+32
-32
lines changed

k8s/gate/haproxy/frontends.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,10 @@ func (b *HaproxyConfMgrImpl) newFrontend(params newFrontendParams) (*models.Fron
278278
Expr: "req_ssl_sni,map(" + sniMap.FullPath() + ")",
279279
},
280280
{
281-
// tcp-request content set-var(txn.sni_match) req_ssl_sni,regsub(^[^.]*,,),map(sni.map)
282281
// tcp-request content set-var(txn.sni_match,ifnotexists) req_ssl_sni,map_end(sniDomainWildcardMap.map)
283282
Type: "content",
284283
Action: "set-var",
285-
VarName: "sni_match",
284+
VarName: "sni_match,ifnotexists",
286285
VarScope: "txn",
287286
Expr: "req_ssl_sni,map_end(" + sniDomainWildcardMap.FullPath() + ")",
288287
},

k8s/gate/haproxy/routes-maps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (b *RouteMgrImpl) fillMapsForHTTPRoutes() {
171171
case store.StatusUnchanged:
172172
continue
173173
case store.StatusUpserted:
174-
err := b.onUpsertedHTTPRoute(routeKey, route, mapExact, mapPrefix, mapRegex, mapDomainWPathExact,acceptedHostnamesForRoute)
174+
err := b.onUpsertedHTTPRoute(routeKey, route, mapExact, mapPrefix, mapRegex, mapDomainWPathExact, acceptedHostnamesForRoute)
175175
errs.Add(err)
176176
case store.StatusDeleted:
177177
err := b.onDeletedHTTPRoute(routeKey, route, mapExact, mapPrefix, mapRegex, mapDomainWPathExact)

k8s/gate/haproxy/routes.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,26 @@ type RouteMgrImpl struct {
5353
}
5454

5555
func (b *RouteMgrImpl) onUpsertedHTTPRoute(routeKey k8stypes.NamespacedName, route *tree.HTTPRoute,
56-
mapExact, mapPrefix, mapRegex, mapDomainWPathExact *maps.MapData,acceptedHostnamesForRoute []string) error {
56+
mapExact, mapPrefix, mapRegex, mapDomainWPathExact *maps.MapData, acceptedHostnamesForRoute []string,
57+
) error {
5758
if route.Valid {
5859
return b.onValidHTTPRouteUpserted(routeKey, route, mapExact, mapPrefix, mapRegex, mapDomainWPathExact, acceptedHostnamesForRoute)
5960
}
6061
return b.onInvalidHTTPRouteUpserted(routeKey, route, mapExact, mapPrefix, mapRegex, mapDomainWPathExact)
6162
}
6263

6364
func (b *RouteMgrImpl) onUpsertedTLSRoute(routeKey k8stypes.NamespacedName, route *tree.TLSRoute,
64-
mapSNI, mapSNIDomainWildcardMap *maps.MapData, acceptedHostnamesForRoute []string) error {
65+
mapSNI, mapSNIDomainWildcardMap *maps.MapData, acceptedHostnamesForRoute []string,
66+
) error {
6567
if route.Valid {
6668
return b.onValidTLSRouteUpserted(routeKey, route, mapSNI, mapSNIDomainWildcardMap, acceptedHostnamesForRoute)
6769
}
6870
return b.onInvalidTLSRouteUpserted(routeKey, route, mapSNI, mapSNIDomainWildcardMap)
6971
}
7072

7173
func (b *RouteMgrImpl) onValidTLSRouteUpserted(_ k8stypes.NamespacedName,
72-
tlsRoute *tree.TLSRoute, mapSNI, mapSNIDomainWildcardMap *maps.MapData, acceptedHostnamesForRoute []string) error {
74+
tlsRoute *tree.TLSRoute, mapSNI, mapSNIDomainWildcardMap *maps.MapData, acceptedHostnamesForRoute []string,
75+
) error {
7376
for _, tlsRouteRule := range tlsRoute.Rules {
7477
// if !rule.Valid {
7578
// find the old rule in route.TreeStatus.OldTreeResource.Rules, name is optional
@@ -215,7 +218,8 @@ func (RouteMgrImpl) onDeletedHTTPRoute(_ k8stypes.NamespacedName, route *tree.HT
215218
}
216219

217220
func (b *RouteMgrImpl) onValidHTTPRouteUpserted(_ k8stypes.NamespacedName, route *tree.HTTPRoute,
218-
mapExact, mapPrefix, mapRegex, mapDomainWPathExact *maps.MapData,acceptedHostnamesForRoute []string) error { //revive:disable:function-length,cognitive-complexity
221+
mapExact, mapPrefix, mapRegex, mapDomainWPathExact *maps.MapData, acceptedHostnamesForRoute []string,
222+
) error { //revive:disable:function-length,cognitive-complexity
219223
hostnames := route.K8sResource.Spec.Hostnames
220224
for _, rule := range route.Rules {
221225
// if !rule.Valid {
@@ -322,7 +326,6 @@ func (b *RouteMgrImpl) onValidHTTPRouteUpserted(_ k8stypes.NamespacedName, route
322326
}
323327
}
324328

325-
326329
if rule.Valid {
327330
for _, hostname := range acceptedHostnamesForRoute {
328331
fullpath := string(hostname) + path

k8s/gate/utils/utils.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,14 @@ func GetHostnamesForRouteWithListener(listenerHostname *string, routeHostnames [
198198
}
199199

200200
// to avoid duplicates
201-
matched:= map[string]struct{}{}
201+
matched := map[string]struct{}{}
202202
for _, routeHostname := range routeHostnames {
203203
// If the listener hostname is a wildcard, check if it matches any route hostname.
204204
if hostnamesMatchingRouteAndListener(routeHostname, *listenerHostname) {
205-
matched [*listenerHostname]= struct{}{}
205+
matched[*listenerHostname] = struct{}{}
206206
} else if hostnamesMatchingRouteAndListener(*listenerHostname, routeHostname) {
207207
// If the route hostname is a wildcard, check if it matches any listener hostname.
208-
matched[routeHostname]= struct{}{}
208+
matched[routeHostname] = struct{}{}
209209
}
210210
}
211211
result := make([]string, len(matched))
@@ -225,7 +225,7 @@ func hostnamesMatchingRouteAndListener(pattern, hostname string) bool {
225225
}
226226

227227
// pattern is of the form "*.example.com"
228-
return strings.HasSuffix(hostname, pattern[1:])
228+
return strings.HasSuffix(hostname, pattern[1:])
229229
}
230230

231231
func ConvertSliceWithFunc[U, V any](arg []U, f func(U) V) []V {

k8s/gate/utils/utils_test.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
package utils // revive:disable:var-naming
1515

1616
import (
17+
"slices"
1718
"testing"
1819
"time"
1920

20-
"slices"
21-
2221
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2322
"sigs.k8s.io/controller-runtime/pkg/client"
2423
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
@@ -70,8 +69,7 @@ func TestSortByCreationTimestamp_Gateways_SameTimestamp(t *testing.T) {
7069
}
7170
}
7271

73-
74-
func TestGetHostnamesForRouteWithListener (t *testing.T) {
72+
func TestGetHostnamesForRouteWithListener(t *testing.T) {
7573
hostname := "test.example.com"
7674
hostnameWildCard := "*.example.com"
7775
otherHostname := "other.example.com"
@@ -81,25 +79,25 @@ func TestGetHostnamesForRouteWithListener (t *testing.T) {
8179

8280
for i, test := range []struct {
8381
listenerHostname *string
84-
routeHostnames []string
85-
expected []string
82+
routeHostnames []string
83+
expected []string
8684
}{
87-
{&hostname, []string{} , []string{hostname}},
88-
{&hostname, []string{hostname} , []string{hostname}},
89-
{&hostname, []string{hostname,otherHostname}, []string{hostname}},
90-
{nil, []string{hostname,fooHostname} , []string{hostname,fooHostname}},
91-
{nil, []string{} , []string{}},
92-
{&hostnameWildCard, []string{} , []string{hostnameWildCard}},
93-
{&hostnameWildCard, []string{hostname} , []string{hostname}},
94-
{&hostnameWildCard, []string{hostname,fooExtendedHostname,otherOrgHostname} , []string{fooExtendedHostname, hostname}},
95-
{&hostname, []string{hostnameWildCard} , []string{hostname}},
96-
{&hostname, []string{hostnameWildCard,hostname} , []string{hostname}},
97-
{&hostname, []string{hostnameWildCard, otherHostname,otherOrgHostname}, []string{hostname}},
98-
{&hostnameWildCard, []string{hostnameWildCard} , []string{hostnameWildCard}},
99-
{&hostnameWildCard, []string{"*.foo.com"} , []string{}},
85+
{&hostname, []string{}, []string{hostname}},
86+
{&hostname, []string{hostname}, []string{hostname}},
87+
{&hostname, []string{hostname, otherHostname}, []string{hostname}},
88+
{nil, []string{hostname, fooHostname}, []string{hostname, fooHostname}},
89+
{nil, []string{}, []string{}},
90+
{&hostnameWildCard, []string{}, []string{hostnameWildCard}},
91+
{&hostnameWildCard, []string{hostname}, []string{hostname}},
92+
{&hostnameWildCard, []string{hostname, fooExtendedHostname, otherOrgHostname}, []string{fooExtendedHostname, hostname}},
93+
{&hostname, []string{hostnameWildCard}, []string{hostname}},
94+
{&hostname, []string{hostnameWildCard, hostname}, []string{hostname}},
95+
{&hostname, []string{hostnameWildCard, otherHostname, otherOrgHostname}, []string{hostname}},
96+
{&hostnameWildCard, []string{hostnameWildCard}, []string{hostnameWildCard}},
97+
{&hostnameWildCard, []string{"*.foo.com"}, []string{}},
10098
} {
10199
if result := GetHostnamesForRouteWithListener(test.listenerHostname, test.routeHostnames); !slices.Equal(result, test.expected) {
102100
t.Errorf("test #%d, Expected %s, got %s", i, test.expected, result)
103101
}
104102
}
105-
}
103+
}

0 commit comments

Comments
 (0)