Skip to content

Commit 4f25e92

Browse files
committed
Refactor HTTPRoutePolicy handling and update tests
This commit refactors the handling of HTTPRoutePolicy, including moving the `httpRoutePolicyPredicateOnUpdate` function and updating the type definitions. It also updates the e2e test for HTTPRoute to correct a typo in the test description.
1 parent d56a253 commit 4f25e92

File tree

4 files changed

+43
-112
lines changed

4 files changed

+43
-112
lines changed

api/v1alpha1/httproutepolicy_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ type HTTPRoutePolicy struct {
4343
metav1.TypeMeta `json:",inline"`
4444
metav1.ObjectMeta `json:"metadata,omitempty"`
4545

46-
Spec HTTPRoutePolicySpec `json:"spec,omitempty"`
47-
Status gatewayv1alpha2.PolicyStatus `json:"status,omitempty"`
46+
Spec HTTPRoutePolicySpec `json:"spec,omitempty"`
47+
Status PolicyStatus `json:"status,omitempty"`
4848
}
4949

5050
// +kubebuilder:object:root=true

internal/controller/httproute_controller.go

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -97,32 +97,6 @@ func (r *HTTPRouteReconciler) SetupWithManager(mgr ctrl.Manager) error {
9797
Complete(r)
9898
}
9999

100-
func (r *HTTPRouteReconciler) httpRoutePolicyPredicateOnUpdate(e event.UpdateEvent) bool {
101-
oldPolicy, ok0 := e.ObjectOld.(*v1alpha1.HTTPRoutePolicy)
102-
newPolicy, ok1 := e.ObjectNew.(*v1alpha1.HTTPRoutePolicy)
103-
if !ok0 || !ok1 {
104-
return false
105-
}
106-
var discardsRefs = make(map[string]v1alpha2.LocalPolicyTargetReferenceWithSectionName)
107-
for _, ref := range oldPolicy.Spec.TargetRefs {
108-
key := indexer.GenHTTPRoutePolicyIndexKey(string(ref.Group), string(ref.Kind), e.ObjectOld.GetNamespace(), string(ref.Name), "")
109-
discardsRefs[key] = ref
110-
}
111-
for _, ref := range newPolicy.Spec.TargetRefs {
112-
key := indexer.GenHTTPRoutePolicyIndexKey(string(ref.Group), string(ref.Kind), e.ObjectOld.GetNamespace(), string(ref.Name), "")
113-
delete(discardsRefs, key)
114-
}
115-
if len(discardsRefs) > 0 {
116-
dump := oldPolicy.DeepCopy()
117-
dump.Spec.TargetRefs = make([]v1alpha2.LocalPolicyTargetReferenceWithSectionName, 0, len(discardsRefs))
118-
for _, ref := range discardsRefs {
119-
dump.Spec.TargetRefs = append(dump.Spec.TargetRefs, ref)
120-
}
121-
r.genericEvent <- event.GenericEvent{Object: dump}
122-
}
123-
return true
124-
}
125-
126100
func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
127101
hr := new(gatewayv1.HTTPRoute)
128102
if err := r.Get(ctx, req.NamespacedName, hr); err != nil {
@@ -221,6 +195,7 @@ func (r *HTTPRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
221195
if err := r.Status().Update(ctx, hr); err != nil {
222196
return ctrl.Result{}, err
223197
}
198+
UpdateStatus(r.Client, r.Log, tctx)
224199
return ctrl.Result{}, nil
225200
}
226201

@@ -311,10 +286,10 @@ func (r *HTTPRouteReconciler) listHTTPRouteByHTTPRoutePolicy(ctx context.Context
311286
return nil
312287
}
313288

314-
var keys = make(map[ancestorRefKey]struct{})
289+
var keys = make(map[targetRefKey]struct{})
315290
for _, ref := range httpRoutePolicy.Spec.TargetRefs {
316291
if ref.Kind == "HTTPRoute" {
317-
key := ancestorRefKey{
292+
key := targetRefKey{
318293
Group: gatewayv1.GroupName,
319294
Namespace: gatewayv1.Namespace(obj.GetNamespace()),
320295
Name: ref.Name,
@@ -329,8 +304,6 @@ func (r *HTTPRouteReconciler) listHTTPRouteByHTTPRoutePolicy(ctx context.Context
329304
var httpRoute gatewayv1.HTTPRoute
330305
if err := r.Get(ctx, client.ObjectKey{Namespace: string(key.Namespace), Name: string(key.Name)}, &httpRoute); err != nil {
331306
r.Log.Error(err, "failed to get HTTPRoute by HTTPRoutePolicy targetRef", "namespace", key.Namespace, "name", key.Name)
332-
r.modifyHTTPRoutePolicyStatus(key, httpRoutePolicy, false, string(v1alpha2.PolicyReasonTargetNotFound), "not found target HTTPRoute")
333-
r.Log.Info("status after modified", "key", key, "status", httpRoutePolicy.Status.Ancestors)
334307
continue
335308
}
336309
if key.SectionName != "" {
@@ -343,7 +316,6 @@ func (r *HTTPRouteReconciler) listHTTPRouteByHTTPRoutePolicy(ctx context.Context
343316
}
344317
if !matchRuleName {
345318
r.Log.Error(errors.Errorf("failed to get HTTPRoute rule by HTTPRoutePolicy targetRef"), "namespace", key.Namespace, "name", key.Name, "sectionName", key.SectionName)
346-
r.modifyHTTPRoutePolicyStatus(key, httpRoutePolicy, false, string(v1alpha2.PolicyReasonTargetNotFound), "not found target HTTPRoute rule")
347319
continue
348320
}
349321
}
@@ -355,15 +327,6 @@ func (r *HTTPRouteReconciler) listHTTPRouteByHTTPRoutePolicy(ctx context.Context
355327
})
356328
}
357329

358-
r.Log.Info("status before clear", "status", httpRoutePolicy.Status.Ancestors)
359-
r.clearHTTPRoutePolicyRedundantAncestor(httpRoutePolicy)
360-
r.Log.Info("status after clear", "status", httpRoutePolicy.Status.Ancestors)
361-
if httpRoutePolicy.GetDeletionTimestamp().IsZero() {
362-
if err := r.Status().Update(ctx, httpRoutePolicy); err != nil {
363-
r.Log.Error(err, "failed to update HTTPRoutePolicy status", "namespace", obj.GetNamespace(), "name", obj.GetName())
364-
}
365-
}
366-
367330
return requests
368331
}
369332

@@ -502,3 +465,29 @@ func (r *HTTPRouteReconciler) processHTTPRoute(tctx *provider.TranslateContext,
502465

503466
return terror
504467
}
468+
469+
func (r *HTTPRouteReconciler) httpRoutePolicyPredicateOnUpdate(e event.UpdateEvent) bool {
470+
oldPolicy, ok0 := e.ObjectOld.(*v1alpha1.HTTPRoutePolicy)
471+
newPolicy, ok1 := e.ObjectNew.(*v1alpha1.HTTPRoutePolicy)
472+
if !ok0 || !ok1 {
473+
return false
474+
}
475+
var discardsRefs = make(map[string]v1alpha2.LocalPolicyTargetReferenceWithSectionName)
476+
for _, ref := range oldPolicy.Spec.TargetRefs {
477+
key := indexer.GenHTTPRoutePolicyIndexKey(string(ref.Group), string(ref.Kind), e.ObjectOld.GetNamespace(), string(ref.Name), "")
478+
discardsRefs[key] = ref
479+
}
480+
for _, ref := range newPolicy.Spec.TargetRefs {
481+
key := indexer.GenHTTPRoutePolicyIndexKey(string(ref.Group), string(ref.Kind), e.ObjectOld.GetNamespace(), string(ref.Name), "")
482+
delete(discardsRefs, key)
483+
}
484+
if len(discardsRefs) > 0 {
485+
dump := oldPolicy.DeepCopy()
486+
dump.Spec.TargetRefs = make([]v1alpha2.LocalPolicyTargetReferenceWithSectionName, 0, len(discardsRefs))
487+
for _, ref := range discardsRefs {
488+
dump.Spec.TargetRefs = append(dump.Spec.TargetRefs, ref)
489+
}
490+
r.genericEvent <- event.GenericEvent{Object: dump}
491+
}
492+
return true
493+
}

internal/controller/httproutepolicy.go

Lines changed: 11 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ package controller
22

33
import (
44
"context"
5-
"slices"
65
"time"
76

87
"github.com/api7/api7-ingress-controller/api/v1alpha1"
9-
"github.com/api7/api7-ingress-controller/internal/controller/config"
108
"github.com/api7/api7-ingress-controller/internal/controller/indexer"
119
"github.com/api7/api7-ingress-controller/internal/provider"
1210
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -20,7 +18,7 @@ func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateC
2018
var (
2119
checker = conflictChecker{
2220
httpRoute: httpRoute,
23-
policies: make(map[ancestorRefKey][]v1alpha1.HTTPRoutePolicy),
21+
policies: make(map[targetRefKey][]v1alpha1.HTTPRoutePolicy),
2422
}
2523
listForAllRules v1alpha1.HTTPRoutePolicyList
2624
key = indexer.GenHTTPRoutePolicyIndexKey(v1alpha1.GroupVersion.Group, "HTTPRoute", httpRoute.GetNamespace(), httpRoute.GetName(), "")
@@ -67,51 +65,18 @@ func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateC
6765
tctx.HTTPRoutePolicies = make(map[string][]v1alpha1.HTTPRoutePolicy)
6866
}
6967

70-
for key, policies := range checker.policies {
71-
for _, policy := range policies {
72-
r.modifyHTTPRoutePolicyStatus(key, &policy, status, reason, message)
73-
if err := r.Status().Update(context.Background(), &policy); err != nil {
74-
r.Log.Error(err, "failed to update HTTPRoutePolicyStatus")
75-
}
68+
for _, policies := range checker.policies {
69+
for i := range policies {
70+
policy := policies[i]
71+
r.modifyHTTPRoutePolicyStatus(httpRoute, &policy, status, reason, message)
72+
tctx.StatusUpdaters = append(tctx.StatusUpdaters, &policy)
7673
}
7774
}
7875

7976
return nil
8077
}
8178

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-
Namespace: gatewayv1.Namespace(policy.GetNamespace()),
88-
Name: ref.Name,
89-
}
90-
if ref.SectionName != nil {
91-
key.SectionName = *ref.SectionName
92-
}
93-
r.Log.Info("clearHTTPRoutePolicyRedundantAncestor", "keys[]", key)
94-
keys[key] = struct{}{}
95-
}
96-
97-
policy.Status.Ancestors = slices.DeleteFunc(policy.Status.Ancestors, func(ancestor v1alpha2.PolicyAncestorStatus) bool {
98-
key := ancestorRefKey{
99-
Namespace: gatewayv1.Namespace(policy.GetNamespace()),
100-
Name: ancestor.AncestorRef.Name,
101-
}
102-
if ancestor.AncestorRef.Group != nil {
103-
key.Group = *ancestor.AncestorRef.Group
104-
}
105-
if ancestor.AncestorRef.SectionName != nil {
106-
key.SectionName = *ancestor.AncestorRef.SectionName
107-
}
108-
r.Log.Info("clearHTTPRoutePolicyRedundantAncestor", "key", key)
109-
_, ok := keys[key]
110-
return !ok
111-
})
112-
}
113-
114-
func (r *HTTPRouteReconciler) modifyHTTPRoutePolicyStatus(key ancestorRefKey, policy *v1alpha1.HTTPRoutePolicy, status bool, reason, message string) {
79+
func (r *HTTPRouteReconciler) modifyHTTPRoutePolicyStatus(httpRoute *gatewayv1.HTTPRoute, policy *v1alpha1.HTTPRoutePolicy, status bool, reason, message string) {
11580
condition := metav1.Condition{
11681
Type: string(v1alpha2.PolicyConditionAccepted),
11782
Status: metav1.ConditionTrue,
@@ -123,47 +88,24 @@ func (r *HTTPRouteReconciler) modifyHTTPRoutePolicyStatus(key ancestorRefKey, po
12388
if !status {
12489
condition.Status = metav1.ConditionFalse
12590
}
126-
var hasAncestor bool
127-
for i, ancestor := range policy.Status.Ancestors {
128-
if ancestor.AncestorRef.Name == key.Name {
129-
ancestor.ControllerName = v1alpha2.GatewayController(config.GetControllerName())
130-
ancestor.Conditions = []metav1.Condition{condition}
131-
hasAncestor = true
132-
}
133-
policy.Status.Ancestors[i] = ancestor
134-
}
135-
if !hasAncestor {
136-
ref := v1alpha2.ParentReference{
137-
Group: &key.Group,
138-
Namespace: &key.Namespace,
139-
Name: key.Name,
140-
}
141-
if key.SectionName != "" {
142-
ref.SectionName = &key.SectionName
143-
}
144-
policy.Status.Ancestors = append(policy.Status.Ancestors, v1alpha2.PolicyAncestorStatus{
145-
AncestorRef: ref,
146-
ControllerName: v1alpha2.GatewayController(config.GetControllerName()),
147-
Conditions: []metav1.Condition{condition},
148-
})
149-
}
91+
_ = SetAncestors(&policy.Status, httpRoute.Spec.ParentRefs, condition)
15092
}
15193

15294
type conflictChecker struct {
15395
httpRoute *gatewayv1.HTTPRoute
154-
policies map[ancestorRefKey][]v1alpha1.HTTPRoutePolicy
96+
policies map[targetRefKey][]v1alpha1.HTTPRoutePolicy
15597
conflict bool
15698
}
15799

158-
type ancestorRefKey struct {
100+
type targetRefKey struct {
159101
Group gatewayv1.Group
160102
Namespace gatewayv1.Namespace
161103
Name gatewayv1.ObjectName
162104
SectionName gatewayv1.SectionName
163105
}
164106

165107
func (c *conflictChecker) append(sectionName string, policy v1alpha1.HTTPRoutePolicy) {
166-
key := ancestorRefKey{
108+
key := targetRefKey{
167109
Group: gatewayv1.GroupName,
168110
Namespace: gatewayv1.Namespace(c.httpRoute.GetNamespace()),
169111
Name: gatewayv1.ObjectName(c.httpRoute.GetName()),

test/e2e/gatewayapi/httproute.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ spec:
421421
Status(404)
422422
})
423423

424-
FIt("HTTPRoute Vars Match", func() {
424+
It("HTTPRoute Vars Match", func() {
425425
By("create HTTPRoute")
426426
ResourceApplied("HTTPRoute", "httpbin", varsRoute, 1)
427427

0 commit comments

Comments
 (0)