Skip to content

Commit

Permalink
fix remove hostnames
Browse files Browse the repository at this point in the history
  • Loading branch information
salonichf5 committed Jan 27, 2025
1 parent 518e97e commit 2dfb22a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
31 changes: 12 additions & 19 deletions internal/mode/static/state/graph/route_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,24 +344,24 @@ func bindRoutesToListeners(

isolateL7RouteListeners(routes, gw.Listeners)

l4routes := make([]*L4Route, 0, len(l4Routes))
layer4routes := make([]*L4Route, 0, len(l4Routes))
for _, r := range l4Routes {
l4routes = append(l4routes, r)
layer4routes = append(layer4routes, r)
}

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

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

for _, r := range l4routes {
for _, r := range layer4routes {
bindL4RouteToListeners(r, gw, namespaces, portHostnamesMap)
}

isolateL4RouteListeners(l4routes, gw.Listeners)
isolateL4RouteListeners(layer4routes, gw.Listeners)
}

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

hostnamesToRemoves := make([]string, 0, len(acceptedHostnames))
hostnamesToRemoves := make(map[string]struct{}, len(acceptedHostnames))
for listenerName, hostnames := range acceptedHostnames {
if len(hostnames) == 0 {
continue
Expand All @@ -407,7 +407,7 @@ func isolateHostnamesForParentRefs(parentRef []ParentRef, listenerHostnameMap ma
continue
}
if h == lHostname && listenerName != lName {
hostnamesToRemoves = append(hostnamesToRemoves, h)
hostnamesToRemoves[h] = struct{}{}
}
}
}
Expand All @@ -419,18 +419,11 @@ func isolateHostnamesForParentRefs(parentRef []ParentRef, listenerHostnameMap ma
}

// removeHostnames removes the hostnames that are part of toRemove slice.
func removeHostnames(hostnames []string, toRemove []string) []string {
func removeHostnames(hostnames []string, toRemove map[string]struct{}) []string {
result := make([]string, 0, len(hostnames))
for _, h := range hostnames {
keep := true
for _, r := range toRemove {
if h == r {
keep = false
break
}
}
if keep {
result = append(result, h)
for _, hostname := range hostnames {
if _, exists := toRemove[hostname]; !exists {
result = append(result, hostname)
}
}
return result
Expand Down
24 changes: 16 additions & 8 deletions internal/mode/static/state/graph/route_common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2680,25 +2680,33 @@ func TestRemoveHostnames(t *testing.T) {
tests := []struct {
name string
hostnames []string
removeHostnames []string
removeHostnames map[string]struct{}
expectedHostnames []string
}{
{
name: "remove multiple hostnames",
hostnames: []string{"foo.example.com", "bar.example.com", "bar.com", "*.wildcard.com"},
removeHostnames: []string{"foo.example.com", "bar.example.com"},
name: "remove multiple hostnames",
hostnames: []string{"foo.example.com", "bar.example.com", "bar.com", "*.wildcard.com"},
removeHostnames: map[string]struct{}{
"foo.example.com": {},
"bar.example.com": {},
},
expectedHostnames: []string{"bar.com", "*.wildcard.com"},
},
{
name: "remove all hostnames",
hostnames: []string{"foo.example.com", "bar.example.com", "bar.com", "*.wildcard.com"},
removeHostnames: []string{"foo.example.com", "bar.example.com", "bar.com", "*.wildcard.com"},
name: "remove all hostnames",
hostnames: []string{"foo.example.com", "bar.example.com", "bar.com", "*.wildcard.com"},
removeHostnames: map[string]struct{}{
"foo.example.com": {},
"bar.example.com": {},
"bar.com": {},
"*.wildcard.com": {},
},
expectedHostnames: []string{},
},
{
name: "remove no hostnames",
hostnames: []string{"foo.example.com", "bar.example.com", "bar.com", "*.wildcard.com"},
removeHostnames: []string{},
removeHostnames: map[string]struct{}{},
expectedHostnames: []string{"foo.example.com", "bar.example.com", "bar.com", "*.wildcard.com"},
},
}
Expand Down

0 comments on commit 2dfb22a

Please sign in to comment.