Skip to content

Commit 4eeed09

Browse files
committed
[feat: gw api] Add eventhandlers for all the gateway resources
1 parent 5997205 commit 4eeed09

35 files changed

+2855
-91
lines changed

config/rbac/role.yaml

+132
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,72 @@ rules:
128128
verbs:
129129
- patch
130130
- update
131+
- apiGroups:
132+
- gateway.k8s.aws
133+
resources:
134+
- loadbalancerconfigurations
135+
verbs:
136+
- get
137+
- list
138+
- watch
139+
- apiGroups:
140+
- gateway.k8s.aws
141+
resources:
142+
- loadbalancerconfigurations/finalizers
143+
verbs:
144+
- update
145+
- apiGroups:
146+
- gateway.k8s.aws
147+
resources:
148+
- loadbalancerconfigurations/status
149+
verbs:
150+
- get
151+
- patch
152+
- update
153+
- apiGroups:
154+
- gateway.k8s.aws
155+
resources:
156+
- targetgroupconfigurations
157+
verbs:
158+
- get
159+
- list
160+
- watch
161+
- apiGroups:
162+
- gateway.k8s.aws
163+
resources:
164+
- targetgroupconfigurations/finalizers
165+
verbs:
166+
- update
167+
- apiGroups:
168+
- gateway.k8s.aws
169+
resources:
170+
- targetgroupconfigurations/status
171+
verbs:
172+
- get
173+
- patch
174+
- update
175+
- apiGroups:
176+
- gateway.networking.k8s.io
177+
resources:
178+
- gatewayclasses
179+
verbs:
180+
- get
181+
- list
182+
- watch
183+
- apiGroups:
184+
- gateway.networking.k8s.io
185+
resources:
186+
- gatewayclasses/finalizers
187+
verbs:
188+
- update
189+
- apiGroups:
190+
- gateway.networking.k8s.io
191+
resources:
192+
- gatewayclasses/status
193+
verbs:
194+
- get
195+
- patch
196+
- update
131197
- apiGroups:
132198
- gateway.networking.k8s.io
133199
resources:
@@ -150,6 +216,50 @@ rules:
150216
- get
151217
- patch
152218
- update
219+
- apiGroups:
220+
- gateway.networking.k8s.io
221+
resources:
222+
- grpcroutes
223+
verbs:
224+
- get
225+
- list
226+
- watch
227+
- apiGroups:
228+
- gateway.networking.k8s.io
229+
resources:
230+
- grpcroutes/finalizers
231+
verbs:
232+
- update
233+
- apiGroups:
234+
- gateway.networking.k8s.io
235+
resources:
236+
- grpcroutes/status
237+
verbs:
238+
- get
239+
- patch
240+
- update
241+
- apiGroups:
242+
- gateway.networking.k8s.io
243+
resources:
244+
- httproutes
245+
verbs:
246+
- get
247+
- list
248+
- watch
249+
- apiGroups:
250+
- gateway.networking.k8s.io
251+
resources:
252+
- httproutes/finalizers
253+
verbs:
254+
- update
255+
- apiGroups:
256+
- gateway.networking.k8s.io
257+
resources:
258+
- httproutes/status
259+
verbs:
260+
- get
261+
- patch
262+
- update
153263
- apiGroups:
154264
- gateway.networking.k8s.io
155265
resources:
@@ -172,6 +282,28 @@ rules:
172282
- get
173283
- patch
174284
- update
285+
- apiGroups:
286+
- gateway.networking.k8s.io
287+
resources:
288+
- tlsroutes
289+
verbs:
290+
- get
291+
- list
292+
- watch
293+
- apiGroups:
294+
- gateway.networking.k8s.io
295+
resources:
296+
- tlsroutes/finalizers
297+
verbs:
298+
- update
299+
- apiGroups:
300+
- gateway.networking.k8s.io
301+
resources:
302+
- tlsroutes/status
303+
verbs:
304+
- get
305+
- patch
306+
- update
175307
- apiGroups:
176308
- gateway.networking.k8s.io
177309
resources:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package eventhandlers
2+
3+
import (
4+
"context"
5+
"github.com/go-logr/logr"
6+
"k8s.io/apimachinery/pkg/api/equality"
7+
"k8s.io/client-go/tools/record"
8+
"k8s.io/client-go/util/workqueue"
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+
// NewEnqueueRequestsForGatewayClassEvent creates handler for GatewayClass resources
18+
func NewEnqueueRequestsForGatewayClassEvent(
19+
k8sClient client.Client, eventRecorder record.EventRecorder, gwController string, logger logr.Logger) handler.TypedEventHandler[*gatewayv1.GatewayClass, reconcile.Request] {
20+
return &enqueueRequestsForGatewayClassEvent{
21+
k8sClient: k8sClient,
22+
eventRecorder: eventRecorder,
23+
gwController: gwController,
24+
logger: logger,
25+
}
26+
}
27+
28+
var _ handler.TypedEventHandler[*gatewayv1.GatewayClass, reconcile.Request] = (*enqueueRequestsForGatewayClassEvent)(nil)
29+
30+
// enqueueRequestsForGatewayClassEvent handles GatewayClass events
31+
type enqueueRequestsForGatewayClassEvent struct {
32+
k8sClient client.Client
33+
eventRecorder record.EventRecorder
34+
gwController string
35+
logger logr.Logger
36+
}
37+
38+
func (h *enqueueRequestsForGatewayClassEvent) Create(ctx context.Context, e event.TypedCreateEvent[*gatewayv1.GatewayClass], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
39+
gwClassNew := e.Object
40+
h.logger.V(1).Info("enqueue gatewayclass create event", "gatewayclass", gwClassNew.Name)
41+
h.enqueueImpactedGateways(ctx, gwClassNew, queue)
42+
}
43+
44+
func (h *enqueueRequestsForGatewayClassEvent) Update(ctx context.Context, e event.TypedUpdateEvent[*gatewayv1.GatewayClass], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
45+
gwClassOld := e.ObjectOld
46+
gwClassNew := e.ObjectNew
47+
48+
// we only care below update event:
49+
// 1. GatewayClass spec updates
50+
// 3. GatewayClass deletions
51+
if equality.Semantic.DeepEqual(gwClassOld.Spec, gwClassNew.Spec) &&
52+
equality.Semantic.DeepEqual(gwClassOld.DeletionTimestamp.IsZero(), gwClassNew.DeletionTimestamp.IsZero()) {
53+
return
54+
}
55+
56+
h.logger.V(1).Info("enqueue gatewayclass update event", "gatewayclass", gwClassNew.Name)
57+
h.enqueueImpactedGateways(ctx, gwClassNew, queue)
58+
}
59+
60+
// Delete is not implemented for this handler as GatewayClass deletion should be finalized and is prevented while referenced by Gateways
61+
func (h *enqueueRequestsForGatewayClassEvent) Delete(ctx context.Context, e event.TypedDeleteEvent[*gatewayv1.GatewayClass], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
62+
}
63+
64+
func (h *enqueueRequestsForGatewayClassEvent) Generic(ctx context.Context, e event.TypedGenericEvent[*gatewayv1.GatewayClass], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
65+
gwClass := e.Object
66+
h.enqueueImpactedGateways(ctx, gwClass, queue)
67+
}
68+
69+
func (h *enqueueRequestsForGatewayClassEvent) enqueueImpactedGateways(ctx context.Context, gwClass *gatewayv1.GatewayClass, queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
70+
gwList := GetGatewaysManagedByLBController(ctx, h.k8sClient, h.gwController)
71+
72+
for _, gw := range gwList {
73+
if string(gw.Spec.GatewayClassName) == gwClass.Name {
74+
h.logger.V(1).Info("enqueue gateway for gatewayclass event",
75+
"gatewayclass", gwClass.GetName(),
76+
"gateway", k8s.NamespacedName(gw))
77+
queue.Add(reconcile.Request{NamespacedName: k8s.NamespacedName(gw)})
78+
}
79+
}
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
"sigs.k8s.io/aws-load-balancer-controller/pkg/gateway/constants"
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+
// NewEnqueueRequestsForGRPCRouteEvent creates handler for GRPCRoute resources
18+
func NewEnqueueRequestsForGRPCRouteEvent(
19+
k8sClient client.Client, eventRecorder record.EventRecorder, logger logr.Logger) handler.TypedEventHandler[*gatewayv1.GRPCRoute, reconcile.Request] {
20+
return &enqueueRequestsForGRPCRouteEvent{
21+
k8sClient: k8sClient,
22+
eventRecorder: eventRecorder,
23+
logger: logger,
24+
}
25+
}
26+
27+
var _ handler.TypedEventHandler[*gatewayv1.GRPCRoute, reconcile.Request] = (*enqueueRequestsForGRPCRouteEvent)(nil)
28+
29+
// enqueueRequestsForGRPCRouteEvent handles GRPCRoute events
30+
type enqueueRequestsForGRPCRouteEvent struct {
31+
k8sClient client.Client
32+
eventRecorder record.EventRecorder
33+
logger logr.Logger
34+
}
35+
36+
func (h *enqueueRequestsForGRPCRouteEvent) Create(ctx context.Context, e event.TypedCreateEvent[*gatewayv1.GRPCRoute], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
37+
routeNew := e.Object
38+
h.logger.V(1).Info("enqueue grpcroute create event", "grpcroute", routeNew.Name)
39+
h.enqueueImpactedGateways(ctx, routeNew, queue)
40+
}
41+
42+
func (h *enqueueRequestsForGRPCRouteEvent) Update(ctx context.Context, e event.TypedUpdateEvent[*gatewayv1.GRPCRoute], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
43+
routeNew := e.ObjectNew
44+
h.logger.V(1).Info("enqueue grpcroute update event", "grpcroute", routeNew.Name)
45+
h.enqueueImpactedGateways(ctx, routeNew, queue)
46+
}
47+
48+
func (h *enqueueRequestsForGRPCRouteEvent) Delete(ctx context.Context, e event.TypedDeleteEvent[*gatewayv1.GRPCRoute], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
49+
route := e.Object
50+
h.logger.V(1).Info("enqueue grpcroute delete event", "grpcroute", route.Name)
51+
h.enqueueImpactedGateways(ctx, route, queue)
52+
}
53+
54+
func (h *enqueueRequestsForGRPCRouteEvent) Generic(ctx context.Context, e event.TypedGenericEvent[*gatewayv1.GRPCRoute], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
55+
route := e.Object
56+
h.logger.V(1).Info("enqueue grpcroute generic event", "grpcroute", route.Name)
57+
h.enqueueImpactedGateways(ctx, route, queue)
58+
}
59+
60+
func (h *enqueueRequestsForGRPCRouteEvent) enqueueImpactedGateways(ctx context.Context, route *gatewayv1.GRPCRoute, queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
61+
gateways, err := GetImpactedGatewaysFromParentRefs(ctx, h.k8sClient, route.Spec.ParentRefs, route.Namespace, constants.ALBGatewayController)
62+
if err != nil {
63+
h.logger.V(1).Info("ignoring unknown gateways referred by", "grpcroute", route.Name, "error", err)
64+
}
65+
for _, gw := range gateways {
66+
h.logger.V(1).Info("enqueue gateway for grpcroute event",
67+
"grpcroute", k8s.NamespacedName(route),
68+
"gateway", gw)
69+
queue.Add(reconcile.Request{NamespacedName: gw})
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
"sigs.k8s.io/aws-load-balancer-controller/pkg/gateway/constants"
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+
// NewEnqueueRequestsForHTTPRouteEvent creates handler for HTTPRoute resources
18+
func NewEnqueueRequestsForHTTPRouteEvent(
19+
k8sClient client.Client, eventRecorder record.EventRecorder, logger logr.Logger) handler.TypedEventHandler[*gatewayv1.HTTPRoute, reconcile.Request] {
20+
return &enqueueRequestsForHTTPRouteEvent{
21+
k8sClient: k8sClient,
22+
eventRecorder: eventRecorder,
23+
logger: logger,
24+
}
25+
}
26+
27+
var _ handler.TypedEventHandler[*gatewayv1.HTTPRoute, reconcile.Request] = (*enqueueRequestsForHTTPRouteEvent)(nil)
28+
29+
// enqueueRequestsForHTTPRouteEvent handles HTTPRoute events
30+
type enqueueRequestsForHTTPRouteEvent struct {
31+
k8sClient client.Client
32+
eventRecorder record.EventRecorder
33+
logger logr.Logger
34+
}
35+
36+
func (h *enqueueRequestsForHTTPRouteEvent) Create(ctx context.Context, e event.TypedCreateEvent[*gatewayv1.HTTPRoute], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
37+
routeNew := e.Object
38+
h.logger.V(1).Info("enqueue httproute create event", "httproute", routeNew.Name)
39+
h.enqueueImpactedGateways(ctx, routeNew, queue)
40+
}
41+
42+
func (h *enqueueRequestsForHTTPRouteEvent) Update(ctx context.Context, e event.TypedUpdateEvent[*gatewayv1.HTTPRoute], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
43+
routeNew := e.ObjectNew
44+
h.logger.V(1).Info("enqueue httproute update event", "httproute", routeNew.Name)
45+
h.enqueueImpactedGateways(ctx, routeNew, queue)
46+
}
47+
48+
func (h *enqueueRequestsForHTTPRouteEvent) Delete(ctx context.Context, e event.TypedDeleteEvent[*gatewayv1.HTTPRoute], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
49+
route := e.Object
50+
h.logger.V(1).Info("enqueue httproute delete event", "httproute", route.Name)
51+
h.enqueueImpactedGateways(ctx, route, queue)
52+
}
53+
54+
func (h *enqueueRequestsForHTTPRouteEvent) Generic(ctx context.Context, e event.TypedGenericEvent[*gatewayv1.HTTPRoute], queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
55+
route := e.Object
56+
h.logger.V(1).Info("enqueue grpcroute generic event", "grpcroute", route.Name)
57+
h.enqueueImpactedGateways(ctx, route, queue)
58+
}
59+
60+
func (h *enqueueRequestsForHTTPRouteEvent) enqueueImpactedGateways(ctx context.Context, route *gatewayv1.HTTPRoute, queue workqueue.TypedRateLimitingInterface[reconcile.Request]) {
61+
gateways, err := GetImpactedGatewaysFromParentRefs(ctx, h.k8sClient, route.Spec.ParentRefs, route.Namespace, constants.ALBGatewayController)
62+
if err != nil {
63+
h.logger.V(1).Info("ignoring unknown gateways referred by", "httproute", route.Name, "error", err)
64+
}
65+
for _, gw := range gateways {
66+
h.logger.V(1).Info("enqueue gateway for httproute event",
67+
"httproute", k8s.NamespacedName(route),
68+
"gateway", gw)
69+
queue.Add(reconcile.Request{NamespacedName: gw})
70+
}
71+
}

0 commit comments

Comments
 (0)