@@ -80,20 +80,7 @@ func (r *HTTPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
8080 ).
8181 Watches (& v1alpha1.HTTPRoutePolicy {},
8282 handler .EnqueueRequestsFromMapFunc (r .listHTTPRouteByHTTPRoutePolicy ),
83- builder .WithPredicates (
84- predicate.Funcs {
85- CreateFunc : func (e event.CreateEvent ) bool {
86- return true
87- },
88- DeleteFunc : func (e event.DeleteEvent ) bool {
89- return true
90- },
91- UpdateFunc : r .httpRoutePolicyPredicateOnUpdate ,
92- GenericFunc : func (e event.GenericEvent ) bool {
93- return false
94- },
95- },
96- ),
83+ builder .WithPredicates (httpRoutePolicyPredicateFuncs (r .genericEvent )),
9784 ).
9885 WatchesRawSource (
9986 source .Channel (
@@ -358,7 +345,7 @@ func (r *HTTPRouteReconciler) listHTTPRouteByHTTPRoutePolicy(ctx context.Context
358345 }
359346
360347 var httpRoute gatewayv1.HTTPRoute
361- if err := r .Get (ctx , client. ObjectKey { Namespace : key . Namespace , Name : key . Name } , & httpRoute ); err != nil {
348+ if err := r .Get (ctx , key , & httpRoute ); err != nil {
362349 r .Log .Error (err , "failed to get HTTPRoute by HTTPRoutePolicy targetRef" , "namespace" , key .Namespace , "name" , key .Name )
363350 continue
364351 }
@@ -376,39 +363,22 @@ func (r *HTTPRouteReconciler) listHTTPRouteByHTTPRoutePolicy(ctx context.Context
376363 }
377364 }
378365 keys [key ] = struct {}{}
379- requests = append (requests , reconcile.Request {
380- NamespacedName : types.NamespacedName {
381- Namespace : key .Namespace ,
382- Name : key .Name ,
383- },
384- })
366+ requests = append (requests , reconcile.Request {NamespacedName : key })
385367 }
386368
387369 return requests
388370}
389371
390372func (r * HTTPRouteReconciler ) listHTTPRouteForGenericEvent (ctx context.Context , obj client.Object ) (requests []reconcile.Request ) {
391- var namespacedNameMap = make (map [types.NamespacedName ]struct {})
392-
393- switch v := obj .(type ) {
373+ switch obj .(type ) {
394374 case * v1alpha1.BackendTrafficPolicy :
395- requests = r .listHTTPRoutesForBackendTrafficPolicy (ctx , v )
375+ return r .listHTTPRoutesForBackendTrafficPolicy (ctx , obj )
396376 case * v1alpha1.HTTPRoutePolicy :
397- for _ , ref := range v .Spec .TargetRefs {
398- namespacedName := types.NamespacedName {Namespace : v .GetNamespace (), Name : string (ref .Name )}
399- if _ , ok := namespacedNameMap [namespacedName ]; ! ok {
400- namespacedNameMap [namespacedName ] = struct {}{}
401- if err := r .Get (ctx , namespacedName , new (gatewayv1.HTTPRoute )); err != nil {
402- r .Log .Info ("failed to Get HTTPRoute" , "namespace" , namespacedName .Namespace , "name" , namespacedName .Name )
403- continue
404- }
405- requests = append (requests , reconcile.Request {NamespacedName : namespacedName })
406- }
407- }
377+ return r .listHTTPRouteByHTTPRoutePolicy (ctx , obj )
408378 default :
409- r .Log .Error (fmt .Errorf ("unexpected object type" ), "failed to convert object to BackendTrafficPolicy" )
379+ r .Log .Error (fmt .Errorf ("unexpected object type" ), "failed to convert object to BackendTrafficPolicy or HTTPRoutePolicy" )
380+ return nil
410381 }
411- return requests
412382}
413383
414384func (r * HTTPRouteReconciler ) processHTTPRouteBackendRefs (tctx * provider.TranslateContext ) error {
@@ -528,28 +498,41 @@ func (r *HTTPRouteReconciler) processHTTPRoute(tctx *provider.TranslateContext,
528498 return terror
529499}
530500
531- func (r * HTTPRouteReconciler ) httpRoutePolicyPredicateOnUpdate (e event.UpdateEvent ) bool {
532- oldPolicy , ok0 := e .ObjectOld .(* v1alpha1.HTTPRoutePolicy )
533- newPolicy , ok1 := e .ObjectNew .(* v1alpha1.HTTPRoutePolicy )
534- if ! ok0 || ! ok1 {
535- return false
536- }
537- var discardsRefs = make (map [string ]v1alpha2.LocalPolicyTargetReferenceWithSectionName )
538- for _ , ref := range oldPolicy .Spec .TargetRefs {
539- key := indexer .GenHTTPRoutePolicyIndexKey (string (ref .Group ), string (ref .Kind ), e .ObjectOld .GetNamespace (), string (ref .Name ), "" )
540- discardsRefs [key ] = ref
541- }
542- for _ , ref := range newPolicy .Spec .TargetRefs {
543- key := indexer .GenHTTPRoutePolicyIndexKey (string (ref .Group ), string (ref .Kind ), e .ObjectOld .GetNamespace (), string (ref .Name ), "" )
544- delete (discardsRefs , key )
545- }
546- if len (discardsRefs ) > 0 {
547- dump := oldPolicy .DeepCopy ()
548- dump .Spec .TargetRefs = make ([]v1alpha2.LocalPolicyTargetReferenceWithSectionName , 0 , len (discardsRefs ))
549- for _ , ref := range discardsRefs {
550- dump .Spec .TargetRefs = append (dump .Spec .TargetRefs , ref )
551- }
552- r .genericEvent <- event.GenericEvent {Object : dump }
501+ func httpRoutePolicyPredicateFuncs (channel chan event.GenericEvent ) predicate.Predicate {
502+ return predicate.Funcs {
503+ CreateFunc : func (e event.CreateEvent ) bool {
504+ return true
505+ },
506+ DeleteFunc : func (e event.DeleteEvent ) bool {
507+ return true
508+ },
509+ UpdateFunc : func (e event.UpdateEvent ) bool {
510+ oldPolicy , ok0 := e .ObjectOld .(* v1alpha1.HTTPRoutePolicy )
511+ newPolicy , ok1 := e .ObjectNew .(* v1alpha1.HTTPRoutePolicy )
512+ if ! ok0 || ! ok1 {
513+ return false
514+ }
515+ var discardsRefs = make (map [string ]v1alpha2.LocalPolicyTargetReferenceWithSectionName )
516+ for _ , ref := range oldPolicy .Spec .TargetRefs {
517+ key := indexer .GenHTTPRoutePolicyIndexKey (string (ref .Group ), string (ref .Kind ), e .ObjectOld .GetNamespace (), string (ref .Name ), "" )
518+ discardsRefs [key ] = ref
519+ }
520+ for _ , ref := range newPolicy .Spec .TargetRefs {
521+ key := indexer .GenHTTPRoutePolicyIndexKey (string (ref .Group ), string (ref .Kind ), e .ObjectOld .GetNamespace (), string (ref .Name ), "" )
522+ delete (discardsRefs , key )
523+ }
524+ if len (discardsRefs ) > 0 {
525+ dump := oldPolicy .DeepCopy ()
526+ dump .Spec .TargetRefs = make ([]v1alpha2.LocalPolicyTargetReferenceWithSectionName , 0 , len (discardsRefs ))
527+ for _ , ref := range discardsRefs {
528+ dump .Spec .TargetRefs = append (dump .Spec .TargetRefs , ref )
529+ }
530+ channel <- event.GenericEvent {Object : dump }
531+ }
532+ return true
533+ },
534+ GenericFunc : func (e event.GenericEvent ) bool {
535+ return false
536+ },
553537 }
554- return true
555538}
0 commit comments