@@ -2,6 +2,7 @@ package controller
22
33import (
44 "context"
5+ "slices"
56 "time"
67
78 "github.com/api7/api7-ingress-controller/api/v1alpha1"
@@ -68,7 +69,8 @@ func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateC
6869
6970 for key , policies := range checker .policies {
7071 for _ , policy := range policies {
71- if err := r .updateHTTPRoutePolicyStatus (key , policy , status , reason , message ); err != nil {
72+ r .modifyHTTPRoutePolicyStatus (key , & policy , status , reason , message )
73+ if err := r .Status ().Update (context .Background (), & policy ); err != nil {
7274 r .Log .Error (err , "failed to update HTTPRoutePolicyStatus" )
7375 }
7476 }
@@ -77,7 +79,43 @@ func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateC
7779 return nil
7880}
7981
80- func (r * HTTPRouteReconciler ) updateHTTPRoutePolicyStatus (key ancestorRefKey , policy v1alpha1.HTTPRoutePolicy , status bool , reason , message string ) error {
82+ func (r * HTTPRouteReconciler ) clearHTTPRoutePolicyRedundantAncestor (policy * v1alpha1.HTTPRoutePolicy ) {
83+ var keys = make (map [ancestorRefKey ]struct {})
84+ for _ , ref := range policy .Spec .TargetRefs {
85+ key := ancestorRefKey {
86+ Group : ref .Group ,
87+ Kind : ref .Kind ,
88+ Namespace : gatewayv1 .Namespace (policy .GetNamespace ()),
89+ Name : ref .Name ,
90+ }
91+ if ref .SectionName != nil {
92+ key .SectionName = * ref .SectionName
93+ }
94+ r .Log .Info ("clearHTTPRoutePolicyRedundantAncestor" , "keys[]" , key )
95+ keys [key ] = struct {}{}
96+ }
97+
98+ policy .Status .Ancestors = slices .DeleteFunc (policy .Status .Ancestors , func (ancestor v1alpha2.PolicyAncestorStatus ) bool {
99+ key := ancestorRefKey {
100+ Namespace : gatewayv1 .Namespace (policy .GetNamespace ()),
101+ Name : ancestor .AncestorRef .Name ,
102+ }
103+ if ancestor .AncestorRef .Group != nil {
104+ key .Group = * ancestor .AncestorRef .Group
105+ }
106+ if ancestor .AncestorRef .Kind != nil {
107+ key .Kind = * ancestor .AncestorRef .Kind
108+ }
109+ if ancestor .AncestorRef .SectionName != nil {
110+ key .SectionName = * ancestor .AncestorRef .SectionName
111+ }
112+ r .Log .Info ("clearHTTPRoutePolicyRedundantAncestor" , "key" , key )
113+ _ , ok := keys [key ]
114+ return ! ok
115+ })
116+ }
117+
118+ func (r * HTTPRouteReconciler ) modifyHTTPRoutePolicyStatus (key ancestorRefKey , policy * v1alpha1.HTTPRoutePolicy , status bool , reason , message string ) {
81119 condition := metav1.Condition {
82120 Type : string (v1alpha2 .PolicyConditionAccepted ),
83121 Status : metav1 .ConditionTrue ,
@@ -90,12 +128,13 @@ func (r *HTTPRouteReconciler) updateHTTPRoutePolicyStatus(key ancestorRefKey, po
90128 condition .Status = metav1 .ConditionFalse
91129 }
92130 var hasAncestor bool
93- for _ , ancestor := range policy .Status .Ancestors {
131+ for i , ancestor := range policy .Status .Ancestors {
94132 if ancestor .AncestorRef .Kind != nil && * ancestor .AncestorRef .Kind == key .Kind && ancestor .AncestorRef .Name == key .Name {
95133 ancestor .ControllerName = v1alpha2 .GatewayController (config .GetControllerName ())
96134 ancestor .Conditions = []metav1.Condition {condition }
97135 hasAncestor = true
98136 }
137+ policy .Status .Ancestors [i ] = ancestor
99138 }
100139 if ! hasAncestor {
101140 ref := v1alpha2.ParentReference {
@@ -113,7 +152,6 @@ func (r *HTTPRouteReconciler) updateHTTPRoutePolicyStatus(key ancestorRefKey, po
113152 Conditions : []metav1.Condition {condition },
114153 })
115154 }
116- return r .Status ().Update (context .Background (), & policy )
117155}
118156
119157type conflictChecker struct {
0 commit comments