|
| 1 | +package eventhandlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/go-logr/logr" |
| 6 | + "k8s.io/client-go/tools/record" |
| 7 | + "k8s.io/client-go/util/workqueue" |
| 8 | + elbv2gw "sigs.k8s.io/aws-load-balancer-controller/apis/gateway/v1beta1" |
| 9 | + "sigs.k8s.io/aws-load-balancer-controller/pkg/k8s" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 14 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 15 | +) |
| 16 | + |
| 17 | +// NewEnqueueRequestsForLoadBalancerConfigurationEvent creates handler for LoadBalancerConfiguration resources |
| 18 | +func NewEnqueueRequestsForLoadBalancerConfigurationEvent(gwClassEventChan chan<- event.TypedGenericEvent[*gatewayv1.GatewayClass], |
| 19 | + k8sClient client.Client, eventRecorder record.EventRecorder, gwController string, logger logr.Logger) handler.TypedEventHandler[*elbv2gw.LoadBalancerConfiguration, reconcile.Request] { |
| 20 | + return &enqueueRequestsForLoadBalancerConfigurationEvent{ |
| 21 | + gwClassEventChan: gwClassEventChan, |
| 22 | + k8sClient: k8sClient, |
| 23 | + eventRecorder: eventRecorder, |
| 24 | + gwController: gwController, |
| 25 | + logger: logger, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +var _ handler.TypedEventHandler[*elbv2gw.LoadBalancerConfiguration, reconcile.Request] = (*enqueueRequestsForLoadBalancerConfigurationEvent)(nil) |
| 30 | + |
| 31 | +// enqueueRequestsForLoadBalancerConfigurationEvent handles LoadBalancerConfiguration events |
| 32 | +type enqueueRequestsForLoadBalancerConfigurationEvent struct { |
| 33 | + gwClassEventChan chan<- event.TypedGenericEvent[*gatewayv1.GatewayClass] |
| 34 | + k8sClient client.Client |
| 35 | + eventRecorder record.EventRecorder |
| 36 | + gwController string |
| 37 | + logger logr.Logger |
| 38 | +} |
| 39 | + |
| 40 | +func (h *enqueueRequestsForLoadBalancerConfigurationEvent) Create(ctx context.Context, e event.TypedCreateEvent[*elbv2gw.LoadBalancerConfiguration], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) { |
| 41 | + lbconfigNew := e.Object |
| 42 | + h.logger.V(1).Info("enqueue loadbalancerconfiguration create event", "loadbalancerconfiguration", lbconfigNew.Name) |
| 43 | + h.enqueueImpactedService(ctx, lbconfigNew, queue) |
| 44 | +} |
| 45 | + |
| 46 | +func (h *enqueueRequestsForLoadBalancerConfigurationEvent) Update(ctx context.Context, e event.TypedUpdateEvent[*elbv2gw.LoadBalancerConfiguration], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) { |
| 47 | + lbconfigNew := e.ObjectNew |
| 48 | + h.logger.V(1).Info("enqueue loadbalancerconfiguration update event", "loadbalancerconfiguration", lbconfigNew.Name) |
| 49 | + h.enqueueImpactedService(ctx, lbconfigNew, queue) |
| 50 | +} |
| 51 | + |
| 52 | +func (h *enqueueRequestsForLoadBalancerConfigurationEvent) Delete(ctx context.Context, e event.TypedDeleteEvent[*elbv2gw.LoadBalancerConfiguration], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) { |
| 53 | + lbconfig := e.Object |
| 54 | + h.logger.V(1).Info("enqueue loadbalancerconfiguration delete event", "loadbalancerconfiguration", lbconfig.Name) |
| 55 | + h.enqueueImpactedService(ctx, lbconfig, queue) |
| 56 | +} |
| 57 | + |
| 58 | +func (h *enqueueRequestsForLoadBalancerConfigurationEvent) Generic(ctx context.Context, e event.TypedGenericEvent[*elbv2gw.LoadBalancerConfiguration], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) { |
| 59 | + lbconfig := e.Object |
| 60 | + h.logger.V(1).Info("enqueue loadbalancerconfiguration generic event", "loadbalancerconfiguration", lbconfig.Name) |
| 61 | + h.enqueueImpactedService(ctx, lbconfig, queue) |
| 62 | +} |
| 63 | + |
| 64 | +func (h *enqueueRequestsForLoadBalancerConfigurationEvent) enqueueImpactedService(ctx context.Context, lbconfig *elbv2gw.LoadBalancerConfiguration, queue workqueue.TypedRateLimitingInterface[reconcile.Request]) { |
| 65 | + gwClasses := GetImpactedGatewayClassesFromLbConfig(ctx, h.k8sClient, lbconfig, h.gwController) |
| 66 | + for _, gwClass := range gwClasses { |
| 67 | + h.logger.V(1).Info("enqueue gatewayClass for loadbalancerconfiguration event", |
| 68 | + "loadbalancerconfiguration", k8s.NamespacedName(lbconfig), |
| 69 | + "gatewayclass", k8s.NamespacedName(gwClass)) |
| 70 | + h.gwClassEventChan <- event.TypedGenericEvent[*gatewayv1.GatewayClass]{ |
| 71 | + Object: gwClass, |
| 72 | + } |
| 73 | + } |
| 74 | + gateways := GetImpactedGatewaysFromLbConfig(ctx, h.k8sClient, lbconfig, h.gwController) |
| 75 | + for _, gw := range gateways { |
| 76 | + if _, isAlreadyEnqueued := gwClasses[string(gw.Spec.GatewayClassName)]; !isAlreadyEnqueued { |
| 77 | + h.logger.V(1).Info("enqueue gateway for loadbalancerconfiguration event", |
| 78 | + "loadbalancerconfiguration", k8s.NamespacedName(lbconfig), |
| 79 | + "gateway", k8s.NamespacedName(gw)) |
| 80 | + queue.Add(reconcile.Request{NamespacedName: k8s.NamespacedName(gw)}) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments