@@ -216,22 +216,10 @@ func (r *routeReconciler) getRoute(ctx context.Context, req ctrl.Request) (core.
216216}
217217
218218func updateRouteListenerStatus (ctx context.Context , k8sClient client.Client , route core.Route ) error {
219- gw := & gwv1.Gateway {}
220-
221- gwNamespace := route .Namespace ()
222- if route .Spec ().ParentRefs ()[0 ].Namespace != nil {
223- gwNamespace = string (* route .Spec ().ParentRefs ()[0 ].Namespace )
224- }
225- gwName := types.NamespacedName {
226- Namespace : gwNamespace ,
227- // TODO assume one parent for now and point to service network
228- Name : string (route .Spec ().ParentRefs ()[0 ].Name ),
229- }
230-
231- if err := k8sClient .Get (ctx , gwName , gw ); err != nil {
232- return fmt .Errorf ("update route listener: gw not found, gw: %s, err: %w" , gwName , err )
219+ gw , err := findFirstParentGw (ctx , k8sClient , route )
220+ if err != nil {
221+ return fmt .Errorf ("failed to get gateway for route %s: %w" , route .Name (), err )
233222 }
234-
235223 return UpdateGWListenerStatus (ctx , k8sClient , gw )
236224}
237225
@@ -240,42 +228,37 @@ func (r *routeReconciler) isRouteRelevant(ctx context.Context, route core.Route)
240228 r .log .Infof (ctx , "Ignore Route which has no ParentRefs gateway %s " , route .Name ())
241229 return false
242230 }
231+ // if route has gateway parentRef that is managed by lattice gateway controller,
232+ // then it is relevant
233+ gw , err := findFirstParentGw (ctx , r .client , route )
234+ return gw != nil && err == nil
235+ }
243236
237+ func findFirstParentGw (
238+ ctx context.Context ,
239+ client client.Client ,
240+ route core.Route ,
241+ ) (* gwv1.Gateway , error ) {
244242 gw := & gwv1.Gateway {}
245-
246243 gwNamespace := route .Namespace ()
247- if route .Spec ().ParentRefs ()[0 ].Namespace != nil {
248- gwNamespace = string (* route .Spec ().ParentRefs ()[0 ].Namespace )
249- }
250- gwName := types.NamespacedName {
251- Namespace : gwNamespace ,
252- Name : string (route .Spec ().ParentRefs ()[0 ].Name ),
253- }
254-
255- if err := r .client .Get (ctx , gwName , gw ); err != nil {
256- r .log .Infof (ctx , "Could not find gateway %s with err %s. Ignoring route %+v whose ParentRef gateway object" +
257- " is not defined." , gwName .String (), err , route .Spec ())
258- return false
259- }
260-
261- // make sure gateway is an aws-vpc-lattice
262- gwClass := & gwv1.GatewayClass {}
263- gwClassName := types.NamespacedName {
264- Name : string (gw .Spec .GatewayClassName ),
265- }
266-
267- if err := r .client .Get (ctx , gwClassName , gwClass ); err != nil {
268- r .log .Infof (ctx , "Ignore Route not controlled by any GatewayClass %s, %s" , route .Name (), route .Namespace ())
269- return false
270- }
271-
272- if gwClass .Spec .ControllerName == config .LatticeGatewayControllerName {
273- r .log .Infof (ctx , "Found aws-vpc-lattice for Route for %s, %s" , route .Name (), route .Namespace ())
274- return true
244+ fails := []string {}
245+ for _ , parentRef := range route .Spec ().ParentRefs () {
246+ if parentRef .Namespace != nil {
247+ gwNamespace = string (* parentRef .Namespace )
248+ }
249+ gwName := types.NamespacedName {
250+ Namespace : gwNamespace ,
251+ Name : string (parentRef .Name ),
252+ }
253+ if err := client .Get (ctx , gwName , gw ); err != nil {
254+ continue
255+ }
256+ if k8s .IsManagedGateway (ctx , client , gw ) {
257+ return gw , nil
258+ }
259+ fails = append (fails , gwName .String ())
275260 }
276-
277- r .log .Infof (ctx , "Ignore non aws-vpc-lattice Route %s, %s" , route .Name (), route .Namespace ())
278- return false
261+ return nil , fmt .Errorf ("failed to get gateway, name %s" , fails )
279262}
280263
281264func (r * routeReconciler ) buildAndDeployModel (
@@ -315,6 +298,19 @@ func (r *routeReconciler) buildAndDeployModel(
315298 return stack , err
316299}
317300
301+ func (r * routeReconciler ) findFirstParentRef (ctx context.Context , route core.Route ) (gwv1.ParentReference , error ) {
302+ gw , err := findFirstParentGw (ctx , r .client , route )
303+ if err != nil {
304+ return gwv1.ParentReference {}, err
305+ }
306+ for _ , parentRef := range route .Spec ().ParentRefs () {
307+ if string (parentRef .Name ) == gw .Name {
308+ return parentRef , nil
309+ }
310+ }
311+ return gwv1.ParentReference {}, fmt .Errorf ("parentRef not found for route %s" , route .Name ())
312+ }
313+
318314func (r * routeReconciler ) reconcileUpsert (ctx context.Context , req ctrl.Request , route core.Route ) error {
319315 r .log .Infow (ctx , "reconcile, adding or updating" , "name" , req .Name )
320316 r .eventRecorder .Event (route .K8sObject (), corev1 .EventTypeNormal ,
@@ -336,9 +332,12 @@ func (r *routeReconciler) reconcileUpsert(ctx context.Context, req ctrl.Request,
336332
337333 if backendRefIPFamiliesErr != nil {
338334 httpRouteOld := route .DeepCopy ()
335+ parentRef , err := r .findFirstParentRef (ctx , route )
336+ if err != nil {
337+ return err
338+ }
339339
340- route .Status ().UpdateParentRefs (route .Spec ().ParentRefs ()[0 ], config .LatticeGatewayControllerName )
341-
340+ route .Status ().UpdateParentRefs (parentRef , config .LatticeGatewayControllerName )
342341 route .Status ().UpdateRouteCondition (metav1.Condition {
343342 Type : string (gwv1 .RouteConditionAccepted ),
344343 Status : metav1 .ConditionFalse ,
@@ -357,7 +356,12 @@ func (r *routeReconciler) reconcileUpsert(ctx context.Context, req ctrl.Request,
357356 if _ , err := r .buildAndDeployModel (ctx , route ); err != nil {
358357 if services .IsConflictError (err ) {
359358 // Stop reconciliation of this route if the route cannot be owned / has conflict
360- route .Status ().UpdateParentRefs (route .Spec ().ParentRefs ()[0 ], config .LatticeGatewayControllerName )
359+ parentRef , parentRefErr := r .findFirstParentRef (ctx , route )
360+ if parentRefErr != nil {
361+ // if parentRef not found, we cannot update route status
362+ return parentRefErr
363+ }
364+ route .Status ().UpdateParentRefs (parentRef , config .LatticeGatewayControllerName )
361365 route .Status ().UpdateRouteCondition (metav1.Condition {
362366 Type : string (gwv1 .RouteConditionAccepted ),
363367 Status : metav1 .ConditionFalse ,
@@ -553,7 +557,7 @@ func (r *routeReconciler) validateRouteParentRefs(ctx context.Context, route cor
553557
554558 parentStatus := gwv1.RouteParentStatus {
555559 ParentRef : parentRef ,
556- ControllerName : "application-networking.k8s.aws/gateway-api-controller" ,
560+ ControllerName : config . LatticeGatewayControllerName ,
557561 Conditions : []metav1.Condition {},
558562 }
559563
0 commit comments