Skip to content

Commit

Permalink
feat: gateway Controller watches HTTPRoutes
Browse files Browse the repository at this point in the history
The Gateway controller now watches for all the accepted HTTPRoutes that
reference it as a parent.

Signed-off-by: Mattia Lavacca <lavacca.mattia@gmail.com>
  • Loading branch information
mlavacca committed May 22, 2023
1 parent 523abce commit 066634f
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions internal/controllers/gateway/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/go-logr/logr"
"github.com/kong/go-kong/kong"
"github.com/samber/lo"
"github.com/samber/mo"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -130,6 +131,15 @@ func (r *GatewayReconciler) SetupWithManager(mgr ctrl.Manager) error {
return err
}

// if a HTTPRoute gets accepted by a Gateway, we need to make sure to trigger
// reconciliation on the gateway, as we need to update the number of attachedRoutes.
if err := c.Watch(
&source.Kind{Type: &gatewayv1beta1.HTTPRoute{}},
handler.EnqueueRequestsFromMapFunc(r.listGatewaysForHTTPRoute),
); err != nil {
return err
}

// watch ReferenceGrants, which may invalidate or allow cross-namespace TLSConfigs
if r.enableReferenceGrant {
if err := c.Watch(
Expand Down Expand Up @@ -266,6 +276,43 @@ func (r *GatewayReconciler) listGatewaysForService(svc client.Object) (recs []re
return
}

// TODO
func (r *GatewayReconciler) listGatewaysForHTTPRoute(obj client.Object) []reconcile.Request {
httpRoute, ok := obj.(*gatewayv1beta1.HTTPRoute)
if !ok {
r.Log.Error(
fmt.Errorf("unexpected object type"),
"httproute watch predicate received unexpected object type",
"expected", "*gatewayv1beta1.HTTPRoute", "found", reflect.TypeOf(obj),
)
return nil
}
recs := []reconcile.Request{}
for _, routeParentStatus := range httpRoute.Status.Parents {
gatewayNamespace := httpRoute.Namespace
parentRef := routeParentStatus.ParentRef
if (parentRef.Group != nil && parentRef.Group != &gatewayV1beta1Group) ||
(parentRef.Kind != nil && parentRef.Kind != util.StringToGatewayAPIKindPtr("HTTPRoute")) {
continue
}
if parentRef.Namespace != nil {
gatewayNamespace = string(*parentRef.Namespace)
}
if lo.ContainsBy(routeParentStatus.Conditions, func(condition metav1.Condition) bool {
return condition.Type == string(gatewayv1beta1.RouteConditionAccepted) &&
condition.Status == metav1.ConditionTrue
}) {
recs = append(recs, reconcile.Request{
NamespacedName: k8stypes.NamespacedName{
Namespace: gatewayNamespace,
Name: string(parentRef.Name),
},
})
}
}
return recs
}

// isGatewayService is a watch predicate that filters out events for objects that aren't
// the gateway service referenced by --publish-service or --publish-service-udp.
func (r *GatewayReconciler) isGatewayService(obj client.Object) bool {
Expand Down

0 comments on commit 066634f

Please sign in to comment.