|
| 1 | +package eventhandlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/go-logr/logr" |
| 6 | + "k8s.io/client-go/util/workqueue" |
| 7 | + elbv2gw "sigs.k8s.io/aws-load-balancer-controller/apis/gateway/v1beta1" |
| 8 | + "sigs.k8s.io/aws-load-balancer-controller/pkg/gateway/routeutils" |
| 9 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 13 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 14 | +) |
| 15 | + |
| 16 | +// NewEnqueueRequestsForListenerRuleConfigurationEvent creates handler for ListenerRuleConfiguration resources |
| 17 | +func NewEnqueueRequestsForListenerRuleConfigurationEvent(httpRouteEventChan chan<- event.TypedGenericEvent[*gatewayv1.HTTPRoute], |
| 18 | + grpcRouteEventChan chan<- event.TypedGenericEvent[*gatewayv1.GRPCRoute], k8sClient client.Client, logger logr.Logger) handler.TypedEventHandler[*elbv2gw.ListenerRuleConfiguration, reconcile.Request] { |
| 19 | + return &enqueueRequestsForListenerRuleConfigurationEvent{ |
| 20 | + httpRouteEventChan: httpRouteEventChan, |
| 21 | + grpcRouteEventChan: grpcRouteEventChan, |
| 22 | + k8sClient: k8sClient, |
| 23 | + logger: logger, |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +var _ handler.TypedEventHandler[*elbv2gw.ListenerRuleConfiguration, reconcile.Request] = (*enqueueRequestsForListenerRuleConfigurationEvent)(nil) |
| 28 | + |
| 29 | +// enqueueRequestsForListenerRuleConfigurationEvent handles ListenerRuleConfiguration events |
| 30 | +type enqueueRequestsForListenerRuleConfigurationEvent struct { |
| 31 | + httpRouteEventChan chan<- event.TypedGenericEvent[*gatewayv1.HTTPRoute] |
| 32 | + grpcRouteEventChan chan<- event.TypedGenericEvent[*gatewayv1.GRPCRoute] |
| 33 | + k8sClient client.Client |
| 34 | + logger logr.Logger |
| 35 | +} |
| 36 | + |
| 37 | +func (h *enqueueRequestsForListenerRuleConfigurationEvent) Create(ctx context.Context, e event.TypedCreateEvent[*elbv2gw.ListenerRuleConfiguration], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) { |
| 38 | + ruleConfig := e.Object |
| 39 | + h.logger.V(1).Info("enqueue listenerruleconfiguration create event", "listenerruleconfiguration", ruleConfig.Name) |
| 40 | + h.enqueueImpactedRoutes(ctx, ruleConfig, queue) |
| 41 | +} |
| 42 | + |
| 43 | +func (h *enqueueRequestsForListenerRuleConfigurationEvent) Update(ctx context.Context, e event.TypedUpdateEvent[*elbv2gw.ListenerRuleConfiguration], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) { |
| 44 | + ruleConfig := e.ObjectNew |
| 45 | + h.logger.V(1).Info("enqueue listenerruleconfiguration update event", "listenerruleconfiguration", ruleConfig.Name) |
| 46 | + h.enqueueImpactedRoutes(ctx, ruleConfig, queue) |
| 47 | +} |
| 48 | + |
| 49 | +func (h *enqueueRequestsForListenerRuleConfigurationEvent) Delete(ctx context.Context, e event.TypedDeleteEvent[*elbv2gw.ListenerRuleConfiguration], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) { |
| 50 | + ruleConfig := e.Object |
| 51 | + h.logger.V(1).Info("enqueue listenerruleconfiguration delete event", "listenerruleconfiguration", ruleConfig.Name) |
| 52 | + h.enqueueImpactedRoutes(ctx, ruleConfig, queue) |
| 53 | +} |
| 54 | + |
| 55 | +func (h *enqueueRequestsForListenerRuleConfigurationEvent) Generic(ctx context.Context, e event.TypedGenericEvent[*elbv2gw.ListenerRuleConfiguration], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) { |
| 56 | + ruleConfig := e.Object |
| 57 | + h.logger.V(1).Info("enqueue listenerruleconfiguration generic event", "listenerruleconfiguration", ruleConfig.Name) |
| 58 | + h.enqueueImpactedRoutes(ctx, ruleConfig, queue) |
| 59 | +} |
| 60 | + |
| 61 | +func (h *enqueueRequestsForListenerRuleConfigurationEvent) enqueueImpactedRoutes(ctx context.Context, ruleConfig *elbv2gw.ListenerRuleConfiguration, queue workqueue.TypedRateLimitingInterface[reconcile.Request]) { |
| 62 | + // Find all L7 Routes that reference this ListenerRuleConfiguration via ExtensionRef |
| 63 | + |
| 64 | + l7Routes, err := routeutils.ListL7Routes(ctx, h.k8sClient) |
| 65 | + if err != nil { |
| 66 | + h.logger.V(1).Info("ignoring to enqueue impacted L7 routes ", "error: ", err) |
| 67 | + } |
| 68 | + filteredRoutesByListenerRuleCfg := routeutils.FilterRoutesByListenerRuleCfg(l7Routes, ruleConfig) |
| 69 | + for _, route := range filteredRoutesByListenerRuleCfg { |
| 70 | + routeType := route.GetRouteKind() |
| 71 | + switch routeType { |
| 72 | + case routeutils.HTTPRouteKind: |
| 73 | + h.logger.V(1).Info("enqueue httproute for listenerruleconfiguration event", |
| 74 | + "listenerruleconfiguration", ruleConfig.Name, |
| 75 | + "httproute", route.GetRouteNamespacedName()) |
| 76 | + h.httpRouteEventChan <- event.TypedGenericEvent[*gatewayv1.HTTPRoute]{ |
| 77 | + Object: route.GetRawRoute().(*gatewayv1.HTTPRoute), |
| 78 | + } |
| 79 | + case routeutils.GRPCRouteKind: |
| 80 | + h.logger.V(1).Info("enqueue grpcroute for listenerruleconfiguration event", |
| 81 | + "listenerruleconfiguration", ruleConfig.Name, |
| 82 | + "grpcroute", route.GetRouteNamespacedName()) |
| 83 | + h.grpcRouteEventChan <- event.TypedGenericEvent[*gatewayv1.GRPCRoute]{ |
| 84 | + Object: route.GetRawRoute().(*gatewayv1.GRPCRoute), |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments