Skip to content

Commit

Permalink
chore: controller cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Penner <[email protected]>
  • Loading branch information
matthewpi committed May 30, 2024
1 parent c5e76bf commit a126c85
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 84 deletions.
68 changes: 35 additions & 33 deletions internal/controller/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,37 +685,17 @@ func (r *GatewayReconciler) handleReconcileErrorWithStatus(ctx context.Context,
// filterHTTPRoutesByGateway .
// TODO
func (r *GatewayReconciler) filterHTTPRoutesByGateway(ctx context.Context, gw *gatewayv1.Gateway, routes []gatewayv1.HTTPRoute) []gatewayv1.HTTPRoute {
_log := log.FromContext(
ctx,
"gateway", types.NamespacedName{
Namespace: gw.Namespace,
Name: gw.Name,
},
)
var filtered []gatewayv1.HTTPRoute
for _, route := range routes {
log2 := _log.WithValues("route", types.NamespacedName{
Namespace: route.Namespace,
Name: route.Name,
})

ctx2 := log.IntoContext(ctx, log2)

if !isAttachable(ctx2, gw, &route, route.Status.Parents) {
log2.Info("route is not attachable")
if !isAttachable(ctx, gw, &route, route.Status.Parents) {
continue
}

if !isAllowed(ctx2, r.Client, gw, &route) {
log2.Info("route is not allowed")
if !isAllowed(ctx, r.Client, gw, &route) {
continue
}

//if len(computeHosts(gw, route.Spec.Hostnames)) > 1 {
// log2.Info("couldn't compute hosts")
// continue
//}

// if len(computeHosts(gw, route.Spec.Hostnames)) > 1 {
// continue
// }
filtered = append(filtered, route)
}
return filtered
Expand All @@ -726,9 +706,16 @@ func (r *GatewayReconciler) filterHTTPRoutesByGateway(ctx context.Context, gw *g
func (r *GatewayReconciler) filterGRPCRoutesByGateway(ctx context.Context, gw *gatewayv1.Gateway, routes []gatewayv1.GRPCRoute) []gatewayv1.GRPCRoute {
var filtered []gatewayv1.GRPCRoute
for _, route := range routes {
if isAttachable(ctx, gw, &route, route.Status.Parents) && isAllowed(ctx, r.Client, gw, &route) && len(computeHosts(gw, route.Spec.Hostnames)) > 0 {
filtered = append(filtered, route)
if !isAttachable(ctx, gw, &route, route.Status.Parents) {
continue
}
if !isAllowed(ctx, r.Client, gw, &route) {
continue
}
// if len(computeHosts(gw, route.Spec.Hostnames)) > 1 {
// continue
// }
filtered = append(filtered, route)
}
return filtered
}
Expand All @@ -738,9 +725,13 @@ func (r *GatewayReconciler) filterGRPCRoutesByGateway(ctx context.Context, gw *g
func (r *GatewayReconciler) filterTCPRoutesByGateway(ctx context.Context, gw *gatewayv1.Gateway, routes []gatewayv1alpha2.TCPRoute) []gatewayv1alpha2.TCPRoute {
var filtered []gatewayv1alpha2.TCPRoute
for _, route := range routes {
if isAttachable(ctx, gw, &route, route.Status.Parents) && isAllowed(ctx, r.Client, gw, &route) {
filtered = append(filtered, route)
if !isAttachable(ctx, gw, &route, route.Status.Parents) {
continue
}
if !isAllowed(ctx, r.Client, gw, &route) {
continue
}
filtered = append(filtered, route)
}
return filtered
}
Expand All @@ -750,9 +741,16 @@ func (r *GatewayReconciler) filterTCPRoutesByGateway(ctx context.Context, gw *ga
func (r *GatewayReconciler) filterTLSRoutesByGateway(ctx context.Context, gw *gatewayv1.Gateway, routes []gatewayv1alpha2.TLSRoute) []gatewayv1alpha2.TLSRoute {
var filtered []gatewayv1alpha2.TLSRoute
for _, route := range routes {
if isAttachable(ctx, gw, &route, route.Status.Parents) && isAllowed(ctx, r.Client, gw, &route) && len(computeHosts(gw, route.Spec.Hostnames)) > 0 {
filtered = append(filtered, route)
if !isAttachable(ctx, gw, &route, route.Status.Parents) {
continue
}
if !isAllowed(ctx, r.Client, gw, &route) {
continue
}
// if len(computeHosts(gw, route.Spec.Hostnames)) > 1 {
// continue
// }
filtered = append(filtered, route)
}
return filtered
}
Expand All @@ -762,9 +760,13 @@ func (r *GatewayReconciler) filterTLSRoutesByGateway(ctx context.Context, gw *ga
func (r *GatewayReconciler) filterUDPRoutesByGateway(ctx context.Context, gw *gatewayv1.Gateway, routes []gatewayv1alpha2.UDPRoute) []gatewayv1alpha2.UDPRoute {
var filtered []gatewayv1alpha2.UDPRoute
for _, route := range routes {
if isAttachable(ctx, gw, &route, route.Status.Parents) && isAllowed(ctx, r.Client, gw, &route) {
filtered = append(filtered, route)
if !isAttachable(ctx, gw, &route, route.Status.Parents) {
continue
}
if !isAllowed(ctx, r.Client, gw, &route) {
continue
}
filtered = append(filtered, route)
}
return filtered
}
Expand Down
11 changes: 6 additions & 5 deletions internal/controller/route_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,17 @@ func (r *TCPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

if err := mgr.GetFieldIndexer().IndexField(ctx, &gatewayv1alpha2.TCPRoute{}, gatewayIndex, func(o client.Object) []string {
hr := o.(*gatewayv1alpha2.TCPRoute)
route, ok := o.(*gatewayv1alpha2.TCPRoute)
if !ok {
return nil
}
var gateways []string
for _, parent := range hr.Spec.ParentRefs {
for _, parent := range route.Spec.ParentRefs {
if !gateway.IsGateway(parent) {
continue
}
gateways = append(gateways, types.NamespacedName{
Namespace: gateway.NamespaceDerefOr(parent.Namespace, hr.Namespace),
Namespace: gateway.NamespaceDerefOr(parent.Namespace, route.Namespace),
Name: string(parent.Name),
}.String())
}
Expand Down Expand Up @@ -164,7 +167,6 @@ func (r *TCPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
if err != nil {
return r.handleReconcileErrorWithStatus(ctx, fmt.Errorf("failed to apply Gateway check: %w", err), original, route)
}

if !continueCheck {
break
}
Expand All @@ -180,7 +182,6 @@ func (r *TCPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
if err != nil {
return r.handleReconcileErrorWithStatus(ctx, fmt.Errorf("failed to apply Backend check: %w", err), original, route)
}

if !continueCheck {
break
}
Expand Down
11 changes: 6 additions & 5 deletions internal/controller/route_tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,17 @@ func (r *TLSRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

if err := mgr.GetFieldIndexer().IndexField(ctx, &gatewayv1alpha2.TLSRoute{}, gatewayIndex, func(o client.Object) []string {
hr := o.(*gatewayv1alpha2.TLSRoute)
route, ok := o.(*gatewayv1alpha2.TLSRoute)
if !ok {
return nil
}
var gateways []string
for _, parent := range hr.Spec.ParentRefs {
for _, parent := range route.Spec.ParentRefs {
if !gateway.IsGateway(parent) {
continue
}
gateways = append(gateways, types.NamespacedName{
Namespace: gateway.NamespaceDerefOr(parent.Namespace, hr.Namespace),
Namespace: gateway.NamespaceDerefOr(parent.Namespace, route.Namespace),
Name: string(parent.Name),
}.String())
}
Expand Down Expand Up @@ -165,7 +168,6 @@ func (r *TLSRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
if err != nil {
return r.handleReconcileErrorWithStatus(ctx, fmt.Errorf("failed to apply Gateway check: %w", err), original, route)
}

if !continueCheck {
break
}
Expand All @@ -181,7 +183,6 @@ func (r *TLSRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
if err != nil {
return r.handleReconcileErrorWithStatus(ctx, fmt.Errorf("failed to apply Backend check: %w", err), original, route)
}

if !continueCheck {
break
}
Expand Down
11 changes: 6 additions & 5 deletions internal/controller/route_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,17 @@ func (r *UDPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
}

if err := mgr.GetFieldIndexer().IndexField(ctx, &gatewayv1alpha2.UDPRoute{}, gatewayIndex, func(o client.Object) []string {
hr := o.(*gatewayv1alpha2.UDPRoute)
route, ok := o.(*gatewayv1alpha2.UDPRoute)
if !ok {
return nil
}
var gateways []string
for _, parent := range hr.Spec.ParentRefs {
for _, parent := range route.Spec.ParentRefs {
if !gateway.IsGateway(parent) {
continue
}
gateways = append(gateways, types.NamespacedName{
Namespace: gateway.NamespaceDerefOr(parent.Namespace, hr.Namespace),
Namespace: gateway.NamespaceDerefOr(parent.Namespace, route.Namespace),
Name: string(parent.Name),
}.String())
}
Expand Down Expand Up @@ -164,7 +167,6 @@ func (r *UDPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
if err != nil {
return r.handleReconcileErrorWithStatus(ctx, fmt.Errorf("failed to apply Gateway check: %w", err), original, route)
}

if !continueCheck {
break
}
Expand All @@ -180,7 +182,6 @@ func (r *UDPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
if err != nil {
return r.handleReconcileErrorWithStatus(ctx, fmt.Errorf("failed to apply Backend check: %w", err), original, route)
}

if !continueCheck {
break
}
Expand Down
12 changes: 4 additions & 8 deletions internal/routechecks/route_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ func (h *HTTPRouteInput) GetGVK() schema.GroupVersionKind {
}

func (h *HTTPRouteInput) GetRules() []GenericRule {
var rules []GenericRule
for _, rule := range h.HTTPRoute.Spec.Rules {
rules = append(rules, &HTTPRouteRule{rule})
rules := make([]GenericRule, len(h.HTTPRoute.Spec.Rules))
for i, rule := range h.HTTPRoute.Spec.Rules {
rules[i] = &HTTPRouteRule{rule}
}
return rules
}
Expand All @@ -105,26 +105,22 @@ func (h *HTTPRouteInput) GetGateway(parent gatewayv1.ParentReference) (*gatewayv
if h.gateways == nil {
h.gateways = make(map[gatewayv1.ParentReference]*gatewayv1.Gateway)
}

if gw, exists := h.gateways[parent]; exists {
return gw, nil
}

ns := gateway.NamespaceDerefOr(parent.Namespace, h.GetNamespace())
gw := &gatewayv1.Gateway{}

if err := h.Client.Get(h.Ctx, client.ObjectKey{Namespace: ns, Name: string(parent.Name)}, gw); err != nil {
if !apierrors.IsNotFound(err) {
// if it is not just a not found error, we should return the error as something is bad
return nil, fmt.Errorf("error while getting gateway: %w", err)
}

// Gateway does not exist skip further checks
return nil, fmt.Errorf("gateway %q does not exist: %w", parent.Name, err)
return nil, fmt.Errorf("gateway %q (%q) does not exist: %w", parent.Name, ns, err)
}

h.gateways[parent] = gw

return gw, nil
}

Expand Down
14 changes: 5 additions & 9 deletions internal/routechecks/route_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ func (h *TCPRouteInput) GetNamespace() string {
}

func (h *TCPRouteInput) GetGVK() schema.GroupVersionKind {
return gatewayv1.SchemeGroupVersion.WithKind("TCPRoute")
return gatewayv1alpha2.SchemeGroupVersion.WithKind("TCPRoute")
}

func (h *TCPRouteInput) GetRules() []GenericRule {
var rules []GenericRule
for _, rule := range h.TCPRoute.Spec.Rules {
rules = append(rules, &TCPRouteRule{rule})
rules := make([]GenericRule, len(h.TCPRoute.Spec.Rules))
for i, rule := range h.TCPRoute.Spec.Rules {
rules[i] = &TCPRouteRule{rule}
}
return rules
}
Expand All @@ -106,26 +106,22 @@ func (h *TCPRouteInput) GetGateway(parent gatewayv1.ParentReference) (*gatewayv1
if h.gateways == nil {
h.gateways = make(map[gatewayv1.ParentReference]*gatewayv1.Gateway)
}

if gw, exists := h.gateways[parent]; exists {
return gw, nil
}

ns := gateway.NamespaceDerefOr(parent.Namespace, h.GetNamespace())
gw := &gatewayv1.Gateway{}

if err := h.Client.Get(h.Ctx, client.ObjectKey{Namespace: ns, Name: string(parent.Name)}, gw); err != nil {
if !apierrors.IsNotFound(err) {
// if it is not just a not found error, we should return the error as something is bad
return nil, fmt.Errorf("error while getting gateway: %w", err)
}

// Gateway does not exist skip further checks
return nil, fmt.Errorf("gateway %q does not exist: %w", parent.Name, err)
return nil, fmt.Errorf("gateway %q (%q) does not exist: %w", parent.Name, ns, err)
}

h.gateways[parent] = gw

return gw, nil
}

Expand Down
14 changes: 5 additions & 9 deletions internal/routechecks/route_tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ func (h *TLSRouteInput) GetNamespace() string {
}

func (h *TLSRouteInput) GetGVK() schema.GroupVersionKind {
return gatewayv1.SchemeGroupVersion.WithKind("TLSRoute")
return gatewayv1alpha2.SchemeGroupVersion.WithKind("TLSRoute")
}

func (h *TLSRouteInput) GetRules() []GenericRule {
var rules []GenericRule
for _, rule := range h.TLSRoute.Spec.Rules {
rules = append(rules, &TLSRouteRule{rule})
rules := make([]GenericRule, len(h.TLSRoute.Spec.Rules))
for i, rule := range h.TLSRoute.Spec.Rules {
rules[i] = &TLSRouteRule{rule}
}
return rules
}
Expand All @@ -106,26 +106,22 @@ func (h *TLSRouteInput) GetGateway(parent gatewayv1.ParentReference) (*gatewayv1
if h.gateways == nil {
h.gateways = make(map[gatewayv1.ParentReference]*gatewayv1.Gateway)
}

if gw, exists := h.gateways[parent]; exists {
return gw, nil
}

ns := gateway.NamespaceDerefOr(parent.Namespace, h.GetNamespace())
gw := &gatewayv1.Gateway{}

if err := h.Client.Get(h.Ctx, client.ObjectKey{Namespace: ns, Name: string(parent.Name)}, gw); err != nil {
if !apierrors.IsNotFound(err) {
// if it is not just a not found error, we should return the error as something is bad
return nil, fmt.Errorf("error while getting gateway: %w", err)
}

// Gateway does not exist skip further checks
return nil, fmt.Errorf("gateway %q does not exist: %w", parent.Name, err)
return nil, fmt.Errorf("gateway %q (%q) does not exist: %w", parent.Name, ns, err)
}

h.gateways[parent] = gw

return gw, nil
}

Expand Down
Loading

0 comments on commit a126c85

Please sign in to comment.