Skip to content

Commit 5c8aa78

Browse files
committed
Refactor HTTPRoutePolicy status update and ancestor management
The `updateHTTPRoutePolicyStatus` function is replaced with `modifyHTTPRoutePolicyStatus` to streamline the process, and a new function `clearHTTPRoutePolicyRedundantAncestor` is introduced to remove redundant ancestors. Additionally, the group in the test's targetRefs is updated to `gateway.networking.k8s.io`.
1 parent 1ad0626 commit 5c8aa78

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

internal/controller/httproute_controller.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,9 @@ func (r *HTTPRouteReconciler) listHTTPRouteByHTTPRoutePolicy(ctx context.Context
265265
for key := range keys {
266266
var httpRoute gatewayv1.HTTPRoute
267267
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-
}
268+
r.Log.Error(err, "failed to get HTTPRoute by HTTPRoutePolicy targetRef", "namespace", obj.GetNamespace(), "name", obj.GetName())
269+
r.modifyHTTPRoutePolicyStatus(key, httpRoutePolicy, false, string(v1alpha2.PolicyReasonTargetNotFound), "not found HTTPRoute")
270+
r.Log.Info("status after modified", "key", key, "status", httpRoutePolicy.Status.Ancestors)
272271
continue
273272
}
274273
requests = append(requests, reconcile.Request{
@@ -279,6 +278,9 @@ func (r *HTTPRouteReconciler) listHTTPRouteByHTTPRoutePolicy(ctx context.Context
279278
})
280279
}
281280

281+
r.Log.Info("status before clear", "status", httpRoutePolicy.Status.Ancestors)
282+
r.clearHTTPRoutePolicyRedundantAncestor(httpRoutePolicy)
283+
r.Log.Info("status after clear", "status", httpRoutePolicy.Status.Ancestors)
282284
if err := r.Status().Update(ctx, httpRoutePolicy); err != nil {
283285
r.Log.Error(err, "failed to update HTTPRoutePolicy status", "namespace", obj.GetNamespace(), "name", obj.GetName())
284286
}

internal/controller/httproutepolicy.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controller
22

33
import (
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

119157
type conflictChecker struct {

test/e2e/gatewayapi/httproute.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,11 @@ metadata:
297297
name: http-route-policy-0
298298
spec:
299299
targetRefs:
300-
- group: gateway.apisix.io
300+
- group: gateway.networking.k8s.io
301301
kind: HTTPRoute
302302
name: httpbin
303303
# sectionName: get
304-
- group: gateway.apisix.io
304+
- group: gateway.networking.k8s.io
305305
kind: HTTPRoute
306306
name: httpbin-1
307307
sectionName: get

0 commit comments

Comments
 (0)