55 "github.com/go-logr/logr"
66 corev1 "k8s.io/api/core/v1"
77 "k8s.io/apimachinery/pkg/types"
8+ "k8s.io/apimachinery/pkg/util/sets"
89 "k8s.io/client-go/tools/record"
910 "k8s.io/client-go/util/workqueue"
1011 elbv2gw "sigs.k8s.io/aws-load-balancer-controller/apis/gateway/v1beta1"
@@ -13,66 +14,122 @@ import (
1314 "sigs.k8s.io/controller-runtime/pkg/event"
1415 "sigs.k8s.io/controller-runtime/pkg/handler"
1516 "sigs.k8s.io/controller-runtime/pkg/reconcile"
17+ gwalpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
1618)
1719
1820// NewEnqueueRequestsForTargetGroupConfigurationEvent creates handler for TargetGroupConfiguration resources
19- func NewEnqueueRequestsForTargetGroupConfigurationEvent (svcEventChan chan <- event.TypedGenericEvent [* corev1.Service ],
21+ func NewEnqueueRequestsForTargetGroupConfigurationEvent (svcEventChan chan <- event.TypedGenericEvent [* corev1.Service ], tcpRouteEventChan chan <- event. TypedGenericEvent [ * gwalpha2. TCPRoute ],
2022 k8sClient client.Client , eventRecorder record.EventRecorder , logger logr.Logger ) handler.TypedEventHandler [* elbv2gw.TargetGroupConfiguration , reconcile.Request ] {
2123 return & enqueueRequestsForTargetGroupConfigurationEvent {
22- svcEventChan : svcEventChan ,
23- k8sClient : k8sClient ,
24- eventRecorder : eventRecorder ,
25- logger : logger ,
24+ svcEventChan : svcEventChan ,
25+ tcpRouteEventChan : tcpRouteEventChan ,
26+ k8sClient : k8sClient ,
27+ eventRecorder : eventRecorder ,
28+ logger : logger ,
2629 }
2730}
2831
2932var _ handler.TypedEventHandler [* elbv2gw.TargetGroupConfiguration , reconcile.Request ] = (* enqueueRequestsForTargetGroupConfigurationEvent )(nil )
3033
3134// enqueueRequestsForTargetGroupConfigurationEvent handles TargetGroupConfiguration events
3235type enqueueRequestsForTargetGroupConfigurationEvent struct {
33- svcEventChan chan <- event.TypedGenericEvent [* corev1.Service ]
34- k8sClient client.Client
35- eventRecorder record.EventRecorder
36- logger logr.Logger
36+ svcEventChan chan <- event.TypedGenericEvent [* corev1.Service ]
37+ tcpRouteEventChan chan <- event.TypedGenericEvent [* gwalpha2.TCPRoute ]
38+ k8sClient client.Client
39+ eventRecorder record.EventRecorder
40+ logger logr.Logger
3741}
3842
3943func (h * enqueueRequestsForTargetGroupConfigurationEvent ) Create (ctx context.Context , e event.TypedCreateEvent [* elbv2gw.TargetGroupConfiguration ], queue workqueue.TypedRateLimitingInterface [reconcile.Request ]) {
4044 tgconfigNew := e .Object
4145 h .logger .V (1 ).Info ("enqueue targetgroupconfiguration create event" , "targetgroupconfiguration" , tgconfigNew .Name )
42- h .enqueueImpactedService (ctx , tgconfigNew , queue )
46+ h .enqueueImpactedObject (ctx , tgconfigNew , queue )
4347}
4448
4549func (h * enqueueRequestsForTargetGroupConfigurationEvent ) Update (ctx context.Context , e event.TypedUpdateEvent [* elbv2gw.TargetGroupConfiguration ], queue workqueue.TypedRateLimitingInterface [reconcile.Request ]) {
4650 tgconfigNew := e .ObjectNew
4751 h .logger .V (1 ).Info ("enqueue targetgroupconfiguration update event" , "targetgroupconfiguration" , tgconfigNew .Name )
48- h .enqueueImpactedService (ctx , tgconfigNew , queue )
52+ h .enqueueImpactedObject (ctx , tgconfigNew , queue )
4953}
5054
5155func (h * enqueueRequestsForTargetGroupConfigurationEvent ) Delete (ctx context.Context , e event.TypedDeleteEvent [* elbv2gw.TargetGroupConfiguration ], queue workqueue.TypedRateLimitingInterface [reconcile.Request ]) {
5256 tgconfig := e .Object
5357 h .logger .V (1 ).Info ("enqueue targetgroupconfiguration delete event" , "targetgroupconfiguration" , tgconfig .Name )
54- h .enqueueImpactedService (ctx , tgconfig , queue )
58+ h .enqueueImpactedObject (ctx , tgconfig , queue )
5559}
5660
5761func (h * enqueueRequestsForTargetGroupConfigurationEvent ) Generic (ctx context.Context , e event.TypedGenericEvent [* elbv2gw.TargetGroupConfiguration ], queue workqueue.TypedRateLimitingInterface [reconcile.Request ]) {
5862 tgconfig := e .Object
5963 h .logger .V (1 ).Info ("enqueue targetgroupconfiguration generic event" , "targetgroupconfiguration" , tgconfig .Name )
60- h .enqueueImpactedService (ctx , tgconfig , queue )
64+ h .enqueueImpactedObject (ctx , tgconfig , queue )
6165}
6266
63- func (h * enqueueRequestsForTargetGroupConfigurationEvent ) enqueueImpactedService (ctx context.Context , tgconfig * elbv2gw.TargetGroupConfiguration , queue workqueue.TypedRateLimitingInterface [reconcile.Request ]) {
64- svcName := types.NamespacedName {Namespace : tgconfig .Namespace , Name : tgconfig .Spec .TargetReference .Name }
65- svc := & corev1.Service {}
66- if err := h .k8sClient .Get (ctx , svcName , svc ); err != nil {
67- h .logger .V (1 ).Info ("ignoring targetgroupconfiguration event for unknown service" ,
67+ func (h * enqueueRequestsForTargetGroupConfigurationEvent ) enqueueImpactedObject (ctx context.Context , tgconfig * elbv2gw.TargetGroupConfiguration , queue workqueue.TypedRateLimitingInterface [reconcile.Request ]) {
68+ objName := types.NamespacedName {Namespace : tgconfig .Namespace , Name : tgconfig .Spec .TargetReference .Name }
69+
70+ if tgconfig .Spec .TargetReference .Kind == nil || * tgconfig .Spec .TargetReference .Kind == "Service" {
71+ svc := & corev1.Service {}
72+ if err := h .k8sClient .Get (ctx , objName , svc ); err != nil {
73+ h .logger .V (1 ).Info ("ignoring targetgroupconfiguration event for unknown service" ,
74+ "targetgroupconfiguration" , k8s .NamespacedName (tgconfig ),
75+ "service" , k8s .NamespacedName (svc ))
76+ return
77+ }
78+ h .logger .V (1 ).Info ("enqueue service for targetgroupconfiguration event" ,
6879 "targetgroupconfiguration" , k8s .NamespacedName (tgconfig ),
6980 "service" , k8s .NamespacedName (svc ))
70- return
81+ h .svcEventChan <- event.TypedGenericEvent [* corev1.Service ]{
82+ Object : svc ,
83+ }
84+ }
85+
86+ // TODO - We should probably use an indexer here, we have a task to do this.
87+ if tgconfig .Spec .TargetReference .Kind != nil && * tgconfig .Spec .TargetReference .Kind == "Gateway" && h .tcpRouteEventChan != nil {
88+ tcpRouteList := & gwalpha2.TCPRouteList {}
89+
90+ if err := h .k8sClient .List (ctx , tcpRouteList ); err != nil {
91+ h .logger .V (1 ).Info ("failed to list tcp routes for target group configuration event" , "targetgroupconfiguration" , k8s .NamespacedName (tgconfig ))
92+ return
93+ }
94+
95+ impactedRoutes := getImpactedTCPRoutes (tcpRouteList , tgconfig )
96+ for i := range impactedRoutes {
97+ h .tcpRouteEventChan <- event.TypedGenericEvent [* gwalpha2.TCPRoute ]{
98+ Object : impactedRoutes [i ],
99+ }
100+ }
101+
71102 }
72- h .logger .V (1 ).Info ("enqueue service for targetgroupconfiguration event" ,
73- "targetgroupconfiguration" , k8s .NamespacedName (tgconfig ),
74- "service" , k8s .NamespacedName (svc ))
75- h .svcEventChan <- event.TypedGenericEvent [* corev1.Service ]{
76- Object : svc ,
103+ }
104+
105+ func getImpactedTCPRoutes (list * gwalpha2.TCPRouteList , tgconfig * elbv2gw.TargetGroupConfiguration ) []* gwalpha2.TCPRoute {
106+ seen := sets.Set [types.NamespacedName ]{}
107+ res := make ([]* gwalpha2.TCPRoute , 0 )
108+
109+ for i , route := range list .Items {
110+ nsn := k8s .NamespacedName (& route )
111+ for _ , rule := range route .Spec .Rules {
112+ for _ , beRef := range rule .BackendRefs {
113+ if beRef .Kind != nil && * beRef .Kind == "Gateway" {
114+ if string (beRef .Name ) == tgconfig .Spec .TargetReference .Name {
115+
116+ // The route backend ns
117+ var routeNs string
118+ if beRef .Namespace == nil {
119+ routeNs = route .Namespace
120+ } else {
121+ routeNs = string (* beRef .Namespace )
122+ }
123+
124+ if routeNs == tgconfig .Namespace && ! seen .Has (nsn ) {
125+ res = append (res , & list .Items [i ])
126+ seen .Insert (nsn )
127+ }
128+
129+ }
130+ }
131+ }
132+ }
77133 }
134+ return res
78135}
0 commit comments