Skip to content

[feat: gw api] Add eventhandlers for all the gateway resources #4148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

shraddhabang
Copy link
Collaborator

Description

This PR introduces a new event handling system for the Gateway API controllers to manage and process events from various Kubernetes resources efficiently. The implementation includes handlers for LoadBalancer configurations, Gateway classes, and related resources.

TODO: Implement indexers and caches to efficiently fetch the resources.

Sample logs

{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/alb","source":"channel source: 0xc00026fea0"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/alb","source":"channel source: 0xc00026ff10"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/alb","source":"kind source: *v1.GatewayClass"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/alb","source":"kind source: *v1beta1.LoadBalancerConfiguration"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/alb","source":"channel source: 0xc000126150"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/alb","source":"channel source: 0xc0001261c0"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/alb","source":"channel source: 0xc000126230"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/alb","source":"channel source: 0xc0001262a0"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/alb","source":"kind source: *v1beta1.TargetGroupConfiguration"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/alb","source":"kind source: *v1.Service"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/alb","source":"kind source: *v1.HTTPRoute"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/alb","source":"kind source: *v1.GRPCRoute"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting Controller","controller":"gateway.k8s.aws/alb"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/nlb","source":"channel source: 0xc00026fa40"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/nlb","source":"channel source: 0xc00026fab0"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/nlb","source":"kind source: *v1.GatewayClass"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/nlb","source":"kind source: *v1beta1.LoadBalancerConfiguration"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/nlb","source":"channel source: 0xc00026fc70"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/nlb","source":"channel source: 0xc00026fce0"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/nlb","source":"channel source: 0xc00026fd50"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/nlb","source":"channel source: 0xc00026fdc0"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/nlb","source":"channel source: 0xc00026fe30"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/nlb","source":"kind source: *v1beta1.TargetGroupConfiguration"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/nlb","source":"kind source: *v1.Service"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/nlb","source":"kind source: *v1alpha2.TCPRoute"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/nlb","source":"kind source: *v1alpha2.UDPRoute"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting EventSource","controller":"gateway.k8s.aws/nlb","source":"kind source: *v1alpha2.TLSRoute"}
{"level":"info","ts":"2025-04-17T20:03:09Z","msg":"Starting Controller","controller":"gateway.k8s.aws/nlb"}

Checklist

  • Added tests that cover your change (if possible)
  • Added/modified documentation as required (such as the README.md, or the docs directory)
  • Manually tested
  • Made sure the title of the PR is a good description that can go into the release notes

BONUS POINTS checklist: complete for good vibes and maybe prizes?! 🤯

  • Backfilled missing tests for code in same general area 🎉
  • Refactored something and made the world a better place 🌟

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 18, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: shraddhabang

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Apr 18, 2025

// GetImpactedGatewaysFromParentRefs identifies Gateways affected by changes in parent references.
// Returns Gateways that are impacted and managed by the LB controller.
func GetImpactedGatewaysFromParentRefs(ctx context.Context, k8sClient client.Client, parentRefs []gwv1.ParentReference, resourceNamespace string, gwController string) ([]types.NamespacedName, error) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zac-nixon

It looks like the error is always ignored. I think we should either:
1/ Remove the error from the return value.
2/ Continue throwing an error, but logging that an error was encountered.

Good catch. I prefer to log the error to ignore the unknown gateways but continue with the impacted gateways. Fixed now.

continue
}

if IsGatewayManagedByLBController(ctx, k8sClient, gw, gwController) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zac-nixon

It would be good to cache this calculation. Once we know a gw is impacted once, we shouldn't need to re-calculate this again. This would also prevent putting duplicate events into the queue.

Yes. I plan to add the caches later in separate dedicated PR to improve performance. That will also include the indexers. I have added these for the foundation phase.

)

// NewEnqueueRequestsForLoadBalancerConfigurationEvent creates handler for LoadBalancerConfiguration resources
func NewEnqueueRequestsForLoadBalancerConfigurationEvent(gwClassEventChan chan<- event.TypedGenericEvent[*gatewayv1.GatewayClass],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zac-nixon

I think we need the gateway event chan here too, as a change in the lb config could impact the gateway object. We can let the framework handle de-duping events.

If you look at the logic to enqueueImpactedGateways, you will see that I enqueue all the impacted gwclasses first in its channel and then also fetch all the impacted gateways and then filter out those whose gwclasses are already enqueued to avoid dedupes.

ctx context.Context,
svc *corev1.Service,
) {
l4Routes, err := routeutils.ListL4Routes(ctx, h.k8sClient)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zac-nixon

It would be good to log the error happened here. It might not be safe to use l4Routes if an error happens (e.g. it might be nil)

Good point. The idea behind this was to not block the other routes if failure is coming from other kind of route for eg. HTTPRoutes throwing errors but GRPCs are okay so we should atleast enqueue the GRPC route events. But I think you have a point. I have added the errors logs here but I prefer not to block the other non-impacting routes. The nil will be handled gracefully as its an array.

// Returns a new slice containing only routes that reference the specified service.
func FilterRoutesBySvc(routes []preLoadRouteDescriptor, svc *corev1.Service) []preLoadRouteDescriptor {
if svc == nil || len(routes) == 0 {
return []preLoadRouteDescriptor{}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zac-nixon

nit: maybe empty array is safer

Done fixed. :)

@zac-nixon
Copy link
Collaborator

/lgtm /approve

@shraddhabang shraddhabang merged commit 66556cd into kubernetes-sigs:main Apr 18, 2025
8 of 9 checks passed
shraddhabang added a commit to shraddhabang/aws-load-balancer-controller that referenced this pull request Apr 18, 2025
zac-nixon pushed a commit to zac-nixon/aws-load-balancer-controller that referenced this pull request Apr 21, 2025
zac-nixon pushed a commit to zac-nixon/aws-load-balancer-controller that referenced this pull request Apr 22, 2025
niclask25 pushed a commit to niclask25/aws-load-balancer-controller that referenced this pull request Apr 22, 2025
zac-nixon pushed a commit to zac-nixon/aws-load-balancer-controller that referenced this pull request Apr 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants