Skip to content

Commit 5fd91d1

Browse files
committed
CLEANUP/MINOR: remove so code duplication by implementing a common helper function listenersForParentRefs
1 parent 9e656f5 commit 5fd91d1

1 file changed

Lines changed: 13 additions & 29 deletions

File tree

k8s/gate/haproxy/routes-maps.go

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222

2323
k8stypes "k8s.io/apimachinery/pkg/types"
24+
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
2425

2526
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/haproxy/storage/maps"
2627
"github.com/haproxytech/haproxy-unified-gateway/k8s/gate/logging"
@@ -184,16 +185,13 @@ func (b *RouteMgrImpl) applyHTTPRouteListenerMaps(
184185
listenerRouteWildcardMap.ApplyRoute(routeOrigin, wildcardEntries)
185186
}
186187

187-
// listenersForRoute returns all gateway Listener objects referenced by the route's parentRefs,
188+
// listenersForParentRefs returns all gateway Listener objects referenced by parentRefs,
188189
// regardless of hostname matching. Deleted gateways and missing section names are skipped.
189-
func (b *RouteMgrImpl) listenersForRoute(route *tree.HTTPRoute) []*tree.Listener {
190-
if route.K8sResource == nil {
191-
return nil
192-
}
190+
func (b *RouteMgrImpl) listenersForParentRefs(parentRefs []gatewayv1.ParentReference, namespace string) []*tree.Listener {
193191
controllerStore := b.topManager.controllerStore
194192
var result []*tree.Listener
195-
for _, parentRef := range route.K8sResource.Spec.ParentRefs {
196-
gwKey := tree.GetParentRefNamespacedName(parentRef, route.K8sResource.Namespace)
193+
for _, parentRef := range parentRefs {
194+
gwKey := tree.GetParentRefNamespacedName(parentRef, namespace)
197195
treeGw, ok := controllerStore.GateTree.Gateways[gwKey]
198196
if !ok || treeGw.TreeStatus.Status == store.StatusDeleted {
199197
continue
@@ -212,6 +210,13 @@ func (b *RouteMgrImpl) listenersForRoute(route *tree.HTTPRoute) []*tree.Listener
212210
return result
213211
}
214212

213+
func (b *RouteMgrImpl) listenersForRoute(route *tree.HTTPRoute) []*tree.Listener {
214+
if route.K8sResource == nil {
215+
return nil
216+
}
217+
return b.listenersForParentRefs(route.K8sResource.Spec.ParentRefs, route.K8sResource.Namespace)
218+
}
219+
215220
func (b *RouteMgrImpl) fillMapsForTLSRoutes() {
216221
var errs utils.Errors
217222
controllerStore := b.topManager.controllerStore
@@ -386,32 +391,11 @@ func (b *RouteMgrImpl) applyTLSRouteListenerMaps(
386391
listenerRouteWildcardMap.ApplyRoute(routeOrigin, wildcardEntries)
387392
}
388393

389-
// listenersForTLSRoute returns all gateway Listener objects referenced by the TLSRoute's parentRefs,
390-
// regardless of hostname matching. Deleted gateways and missing section names are skipped.
391394
func (b *RouteMgrImpl) listenersForTLSRoute(route *tree.TLSRoute) []*tree.Listener {
392395
if route.K8sResource == nil {
393396
return nil
394397
}
395-
controllerStore := b.topManager.controllerStore
396-
var result []*tree.Listener
397-
for _, parentRef := range route.K8sResource.Spec.ParentRefs {
398-
gwKey := tree.GetParentRefNamespacedName(parentRef, route.K8sResource.Namespace)
399-
treeGw, ok := controllerStore.GateTree.Gateways[gwKey]
400-
if !ok || treeGw.TreeStatus.Status == store.StatusDeleted {
401-
continue
402-
}
403-
if parentRef.SectionName != nil {
404-
l, ok := treeGw.Listeners[string(*parentRef.SectionName)]
405-
if ok {
406-
result = append(result, l)
407-
}
408-
} else {
409-
for _, l := range treeGw.Listeners {
410-
result = append(result, l)
411-
}
412-
}
413-
}
414-
return result
398+
return b.listenersForParentRefs(route.K8sResource.Spec.ParentRefs, route.K8sResource.Namespace)
415399
}
416400

417401
func (b *RouteMgrImpl) fillMapsForHTTPRoutes() {

0 commit comments

Comments
 (0)