@@ -5,11 +5,12 @@ import (
55 "fmt"
66 "strings"
77
8- "github.com/api7/api7-ingress-controller/internal/controller/config"
8+ "github.com/api7/api7-ingress-controller/api/v1alpha1"
9+ "github.com/api7/api7-ingress-controller/internal/controller/indexer"
10+ "github.com/api7/api7-ingress-controller/internal/provider"
911 "github.com/go-logr/logr"
1012 corev1 "k8s.io/api/core/v1"
1113 discoveryv1 "k8s.io/api/discovery/v1"
12- "k8s.io/apimachinery/pkg/api/meta"
1314 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1415 "k8s.io/apimachinery/pkg/runtime"
1516 "k8s.io/apimachinery/pkg/types"
@@ -22,10 +23,6 @@ import (
2223 "sigs.k8s.io/controller-runtime/pkg/reconcile"
2324 gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
2425 "sigs.k8s.io/gateway-api/apis/v1alpha2"
25-
26- "github.com/api7/api7-ingress-controller/api/v1alpha1"
27- "github.com/api7/api7-ingress-controller/internal/controller/indexer"
28- "github.com/api7/api7-ingress-controller/internal/provider"
2926)
3027
3128// HTTPRouteReconciler reconciles a GatewayClass object.
@@ -125,6 +122,11 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
125122 acceptStatus .msg = err .Error ()
126123 }
127124
125+ if err := r .processHTTPRoutePolicies (tctx , hr ); err != nil {
126+ acceptStatus .status = false
127+ acceptStatus .msg = err .Error ()
128+ }
129+
128130 if err := r .processHTTPRouteBackendRefs (tctx ); err != nil {
129131 resolveRefStatus = status {
130132 status : false ,
@@ -245,26 +247,42 @@ func (r *HTTPRouteReconciler) listHTTPRouteByHTTPRoutePolicy(ctx context.Context
245247 return nil
246248 }
247249
248- var keys = make (map [client. ObjectKey ]struct {})
250+ var keys = make (map [ancestorRefKey ]struct {})
249251 for _ , ref := range httpRoutePolicy .Spec .TargetRefs {
250252 if ref .Kind == "HTTPRoute" {
251- keys [client.ObjectKey {
252- Namespace : obj .GetNamespace (),
253- Name : string (ref .Name ),
254- }] = struct {}{}
253+ key := ancestorRefKey {
254+ Group : gatewayv1 .GroupName ,
255+ Kind : "HTTPRoute" ,
256+ Namespace : gatewayv1 .Namespace (obj .GetNamespace ()),
257+ Name : ref .Name ,
258+ }
259+ if ref .SectionName != nil {
260+ key .SectionName = * ref .SectionName
261+ }
262+ keys [key ] = struct {}{}
255263 }
256264 }
257265 for key := range keys {
258266 var httpRoute gatewayv1.HTTPRoute
259- if err := r .Get (ctx , key , & httpRoute ); err != nil {
260- r .Log .Error (err , "failed to get httproute by HTTPRoutePolicy targetRef" , "HTTPRoutePolicy" , obj .GetName ())
267+ if err := r .Get (ctx , client.ObjectKey {Namespace : string (key .Namespace ), Name : string (key .Name )}, & httpRoute ); err != nil {
268+ r .Log .Error (err , "failed to get httproute by HTTPRoutePolicy targetRef" , "namespace" , obj .GetNamespace (), "name" , obj .GetName ())
269+ if err := r .updateHTTPRoutePolicyStatus (key , * httpRoutePolicy , false , string (v1alpha2 .PolicyReasonTargetNotFound ), "not found HTTPRoute" ); err != nil {
270+ r .Log .Error (err , "failed to update HTTPRoutePolicy Status" )
271+ }
261272 continue
262273 }
263274 requests = append (requests , reconcile.Request {
264- NamespacedName : key ,
275+ NamespacedName : types.NamespacedName {
276+ Namespace : string (key .Namespace ),
277+ Name : string (key .Name ),
278+ },
265279 })
266280 }
267281
282+ if err := r .Status ().Update (ctx , httpRoutePolicy ); err != nil {
283+ r .Log .Error (err , "failed to update HTTPRoutePolicy status" , "namespace" , obj .GetNamespace (), "name" , obj .GetName ())
284+ }
285+
268286 return requests
269287}
270288
@@ -376,38 +394,6 @@ func (r *HTTPRouteReconciler) processHTTPRoute(tctx *provider.TranslateContext,
376394 },
377395 })
378396 }
379-
380- var httpRoutePolicyList v1alpha1.HTTPRoutePolicyList
381- var ruleName string
382- if rule .Name != nil {
383- ruleName = string (* rule .Name )
384- }
385- key := indexer .GenHTTPRoutePolicyIndexKey (v1alpha1 .GroupVersion .Group , "HTTPRoute" , httpRoute .GetNamespace (), httpRoute .GetName (), ruleName )
386- if err := r .List (context .Background (), & httpRoutePolicyList , client.MatchingFields {indexer .HTTPRoutePolicy : key }); err != nil {
387- terror = err
388- continue
389- }
390- for _ , item := range httpRoutePolicyList .Items {
391- tctx .HTTPRoutePolicies [ruleName ] = append (tctx .HTTPRoutePolicies [key ], item .Spec )
392- item .Status .Ancestors = []v1alpha2.PolicyAncestorStatus {
393- {
394- AncestorRef : v1alpha2.ParentReference {
395- Group : nil ,
396- Kind : nil ,
397- Namespace : nil ,
398- Name : gatewayv1 .ObjectName (httpRoute .GetName ()),
399- SectionName : nil ,
400- Port : nil ,
401- },
402- ControllerName : v1alpha2 .GatewayController (config .GetControllerName ()),
403- Conditions : []metav1.Condition {},
404- },
405- }
406- meta .SetStatusCondition (& item .Status .Ancestors [0 ].Conditions , NewCondition (item .Generation , true , "Successfully" ))
407- if err := r .Status ().Update (context .Background (), & item ); err != nil {
408- r .Log .Error (err , "failed to Update policy status" )
409- }
410- }
411397 }
412398
413399 return terror
0 commit comments