Skip to content

Commit 3d10bcd

Browse files
committed
fix remove hostnames
1 parent 518e97e commit 3d10bcd

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

internal/mode/static/state/graph/route_common.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -344,24 +344,24 @@ func bindRoutesToListeners(
344344

345345
isolateL7RouteListeners(routes, gw.Listeners)
346346

347-
l4routes := make([]*L4Route, 0, len(l4Routes))
347+
l4RouteSlice := make([]*L4Route, 0, len(l4Routes))
348348
for _, r := range l4Routes {
349-
l4routes = append(l4routes, r)
349+
l4RouteSlice = append(l4RouteSlice, r)
350350
}
351351

352352
// Sort the slice by timestamp and name so that we process the routes in the priority order
353-
sort.Slice(l4routes, func(i, j int) bool {
354-
return ngfSort.LessClientObject(l4routes[i].Source, l4routes[j].Source)
353+
sort.Slice(l4RouteSlice, func(i, j int) bool {
354+
return ngfSort.LessClientObject(l4RouteSlice[i].Source, l4RouteSlice[j].Source)
355355
})
356356

357357
// portHostnamesMap exists to detect duplicate hostnames on the same port
358358
portHostnamesMap := make(map[string]struct{})
359359

360-
for _, r := range l4routes {
360+
for _, r := range l4RouteSlice {
361361
bindL4RouteToListeners(r, gw, namespaces, portHostnamesMap)
362362
}
363363

364-
isolateL4RouteListeners(l4routes, gw.Listeners)
364+
isolateL4RouteListeners(l4RouteSlice, gw.Listeners)
365365
}
366366

367367
// isolateL7RouteListeners ensures listener isolation for all L7Routes.
@@ -395,7 +395,7 @@ func isolateHostnamesForParentRefs(parentRef []ParentRef, listenerHostnameMap ma
395395
for _, ref := range parentRef {
396396
acceptedHostnames := ref.Attachment.AcceptedHostnames
397397

398-
hostnamesToRemoves := make([]string, 0, len(acceptedHostnames))
398+
hostnamesToRemoves := make(map[string]struct{}, len(acceptedHostnames))
399399
for listenerName, hostnames := range acceptedHostnames {
400400
if len(hostnames) == 0 {
401401
continue
@@ -407,7 +407,7 @@ func isolateHostnamesForParentRefs(parentRef []ParentRef, listenerHostnameMap ma
407407
continue
408408
}
409409
if h == lHostname && listenerName != lName {
410-
hostnamesToRemoves = append(hostnamesToRemoves, h)
410+
hostnamesToRemoves[h] = struct{}{}
411411
}
412412
}
413413
}
@@ -419,18 +419,11 @@ func isolateHostnamesForParentRefs(parentRef []ParentRef, listenerHostnameMap ma
419419
}
420420

421421
// removeHostnames removes the hostnames that are part of toRemove slice.
422-
func removeHostnames(hostnames []string, toRemove []string) []string {
422+
func removeHostnames(hostnames []string, toRemove map[string]struct{}) []string {
423423
result := make([]string, 0, len(hostnames))
424-
for _, h := range hostnames {
425-
keep := true
426-
for _, r := range toRemove {
427-
if h == r {
428-
keep = false
429-
break
430-
}
431-
}
432-
if keep {
433-
result = append(result, h)
424+
for _, hostname := range hostnames {
425+
if _, exists := toRemove[hostname]; !exists {
426+
result = append(result, hostname)
434427
}
435428
}
436429
return result

internal/mode/static/state/graph/route_common_test.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,25 +2680,33 @@ func TestRemoveHostnames(t *testing.T) {
26802680
tests := []struct {
26812681
name string
26822682
hostnames []string
2683-
removeHostnames []string
2683+
removeHostnames map[string]struct{}
26842684
expectedHostnames []string
26852685
}{
26862686
{
2687-
name: "remove multiple hostnames",
2688-
hostnames: []string{"foo.example.com", "bar.example.com", "bar.com", "*.wildcard.com"},
2689-
removeHostnames: []string{"foo.example.com", "bar.example.com"},
2687+
name: "remove multiple hostnames",
2688+
hostnames: []string{"foo.example.com", "bar.example.com", "bar.com", "*.wildcard.com"},
2689+
removeHostnames: map[string]struct{}{
2690+
"foo.example.com": {},
2691+
"bar.example.com": {},
2692+
},
26902693
expectedHostnames: []string{"bar.com", "*.wildcard.com"},
26912694
},
26922695
{
2693-
name: "remove all hostnames",
2694-
hostnames: []string{"foo.example.com", "bar.example.com", "bar.com", "*.wildcard.com"},
2695-
removeHostnames: []string{"foo.example.com", "bar.example.com", "bar.com", "*.wildcard.com"},
2696+
name: "remove all hostnames",
2697+
hostnames: []string{"foo.example.com", "bar.example.com", "bar.com", "*.wildcard.com"},
2698+
removeHostnames: map[string]struct{}{
2699+
"foo.example.com": {},
2700+
"bar.example.com": {},
2701+
"bar.com": {},
2702+
"*.wildcard.com": {},
2703+
},
26962704
expectedHostnames: []string{},
26972705
},
26982706
{
26992707
name: "remove no hostnames",
27002708
hostnames: []string{"foo.example.com", "bar.example.com", "bar.com", "*.wildcard.com"},
2701-
removeHostnames: []string{},
2709+
removeHostnames: map[string]struct{}{},
27022710
expectedHostnames: []string{"foo.example.com", "bar.example.com", "bar.com", "*.wildcard.com"},
27032711
},
27042712
}

0 commit comments

Comments
 (0)