@@ -2,8 +2,9 @@ package controller
22
33import (
44 "context"
5- "encoding/json "
5+ "slices "
66
7+ "github.com/go-logr/logr"
78 networkingv1 "k8s.io/api/networking/v1"
89 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
910 "k8s.io/apimachinery/pkg/types"
@@ -41,8 +42,6 @@ func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateC
4142 }
4243 }
4344 }
44- data , _ := json .MarshalIndent (conflicts , "" , " " )
45- r .Log .Info ("conflicts policies" , "data" , string (data ))
4645
4746 for i := range list .Items {
4847 var (
@@ -67,6 +66,39 @@ func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateC
6766 return nil
6867}
6968
69+ func (r * HTTPRouteReconciler ) updateHTTPRouteStatusOnDeleting (nn types.NamespacedName ) error {
70+ var (
71+ list v1alpha1.HTTPRoutePolicyList
72+ key = indexer .GenIndexKeyWithGK (gatewayv1 .GroupName , "HTTPRoute" , nn .Namespace , nn .Name )
73+ )
74+ if err := r .List (context .Background (), & list , client.MatchingFields {indexer .PolicyTargetRefs : key }); err != nil {
75+ return err
76+ }
77+ var (
78+ objs = make (map [types.NamespacedName ]struct {})
79+ parentRefs []gatewayv1.ParentReference
80+ )
81+ // collect all parentRefs
82+ for _ , policy := range list .Items {
83+ for _ , ref := range policy .Spec .TargetRefs {
84+ var obj = types.NamespacedName {Namespace : policy .GetNamespace (), Name : string (ref .Name )}
85+ if _ , ok := objs [obj ]; ! ok {
86+ objs [obj ] = struct {}{}
87+
88+ var httpRoute gatewayv1.HTTPRoute
89+ if err := r .Get (context .Background (), obj , & httpRoute ); err != nil {
90+ continue
91+ }
92+ parentRefs = append (parentRefs , httpRoute .Spec .ParentRefs ... )
93+ }
94+ }
95+ }
96+ // delete AncestorRef which is not exist in the all parentRefs for each policy
97+ updateDeleteAncestors (r .Client , r .Log , list .Items , parentRefs )
98+
99+ return nil
100+ }
101+
70102func (r * IngressReconciler ) processHTTPRoutePolicies (tctx * provider.TranslateContext , ingress * networkingv1.Ingress ) error {
71103 var (
72104 list v1alpha1.HTTPRoutePolicyList
@@ -102,6 +134,47 @@ func (r *IngressReconciler) processHTTPRoutePolicies(tctx *provider.TranslateCon
102134 return nil
103135}
104136
137+ func (r * IngressReconciler ) updateHTTPRoutePolicyStatusOnDeleting (nn types.NamespacedName ) error {
138+ var (
139+ list v1alpha1.HTTPRoutePolicyList
140+ key = indexer .GenIndexKeyWithGK (networkingv1 .GroupName , "Ingress" , nn .Namespace , nn .Name )
141+ )
142+ if err := r .List (context .Background (), & list , client.MatchingFields {indexer .PolicyTargetRefs : key }); err != nil {
143+ return err
144+ }
145+ var (
146+ objs = make (map [types.NamespacedName ]struct {})
147+ parentRefs []gatewayv1.ParentReference
148+ )
149+ // collect all parentRefs
150+ for _ , policy := range list .Items {
151+ for _ , ref := range policy .Spec .TargetRefs {
152+ var obj = types.NamespacedName {Namespace : policy .GetNamespace (), Name : string (ref .Name )}
153+ if _ , ok := objs [obj ]; ! ok {
154+ objs [obj ] = struct {}{}
155+
156+ var ingress networkingv1.Ingress
157+ if err := r .Get (context .Background (), obj , & ingress ); err != nil {
158+ continue
159+ }
160+ ingressClass , err := r .getIngressClass (& ingress )
161+ if err != nil {
162+ continue
163+ }
164+ parentRefs = append (parentRefs , gatewayv1.ParentReference {
165+ Group : ptr .To (gatewayv1 .Group (ingressClass .GroupVersionKind ().Group )),
166+ Kind : ptr .To (gatewayv1 .Kind ("IngressClass" )),
167+ Name : gatewayv1 .ObjectName (ingressClass .Name ),
168+ })
169+ }
170+ }
171+ }
172+ // delete AncestorRef which is not exist in the all parentRefs
173+ updateDeleteAncestors (r .Client , r .Log , list .Items , parentRefs )
174+
175+ return nil
176+ }
177+
105178func modifyHTTPRoutePolicyStatus (parentRefs []gatewayv1.ParentReference , policy * v1alpha1.HTTPRoutePolicy , status bool , reason , message string ) {
106179 condition := metav1.Condition {
107180 Type : string (v1alpha2 .PolicyConditionAccepted ),
@@ -141,3 +214,20 @@ func findPolicyWhichTargetRefTheRule(ruleName *gatewayv1.SectionName, kind strin
141214 }
142215 return
143216}
217+
218+ func updateDeleteAncestors (client client.Client , logger logr.Logger , policies []v1alpha1.HTTPRoutePolicy , parentRefs []gatewayv1.ParentReference ) {
219+ for i := range policies {
220+ policy := policies [i ]
221+ length := len (policy .Status .Ancestors )
222+ policy .Status .Ancestors = slices .DeleteFunc (policy .Status .Ancestors , func (status v1alpha2.PolicyAncestorStatus ) bool {
223+ return ! slices .ContainsFunc (parentRefs , func (ref gatewayv1.ParentReference ) bool {
224+ return parentRefValueEqual (status .AncestorRef , ref )
225+ })
226+ })
227+ if length != len (policy .Status .Ancestors ) {
228+ if err := client .Status ().Update (context .Background (), & policy ); err != nil {
229+ logger .Error (err , "failed to update HTTPRoutePolicy status" )
230+ }
231+ }
232+ }
233+ }
0 commit comments