13
13
package controller
14
14
15
15
import (
16
+ "cmp"
16
17
"context"
17
18
"slices"
18
19
@@ -59,26 +60,25 @@ func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateC
59
60
var (
60
61
policy = list .Items [i ]
61
62
namespacedName = types.NamespacedName {Namespace : policy .GetNamespace (), Name : policy .GetName ()}
62
-
63
- status = false
64
- reason = string (v1alpha2 .PolicyReasonConflicted )
65
- message = "HTTPRoutePolicy conflict with others target to the HTTPRoute"
63
+ condition metav1.Condition
66
64
)
67
- if _ , conflict := conflicts [namespacedName ]; ! conflict {
68
- status = true
69
- reason = string (v1alpha2 .PolicyReasonAccepted )
70
- message = ""
71
-
65
+ if _ , conflict := conflicts [namespacedName ]; conflict {
66
+ condition . Status = metav1 . ConditionFalse
67
+ condition . Reason = string (v1alpha2 .PolicyReasonConflicted )
68
+ condition . Message = "HTTPRoutePolicy conflict with others target to the HTTPRoute "
69
+ } else {
72
70
tctx .HTTPRoutePolicies = append (tctx .HTTPRoutePolicies , policy )
73
71
}
74
- modifyHTTPRoutePolicyStatus (httpRoute .Spec .ParentRefs , & policy , status , reason , message )
75
- tctx .StatusUpdaters = append (tctx .StatusUpdaters , & policy )
72
+
73
+ if updated := setAncestorsForHTTPRoutePolicyStatus (httpRoute .Spec .ParentRefs , & policy , condition ); updated {
74
+ tctx .StatusUpdaters = append (tctx .StatusUpdaters , & policy )
75
+ }
76
76
}
77
77
78
78
return nil
79
79
}
80
80
81
- func (r * HTTPRouteReconciler ) updateHTTPRoutePolicyStatusOnDeleting (nn types.NamespacedName ) error {
81
+ func (r * HTTPRouteReconciler ) updateHTTPRoutePolicyStatusOnDeleting (ctx context. Context , nn types.NamespacedName ) error {
82
82
var (
83
83
list v1alpha1.HTTPRoutePolicyList
84
84
key = indexer .GenIndexKeyWithGK (gatewayv1 .GroupName , "HTTPRoute" , nn .Namespace , nn .Name )
@@ -104,7 +104,7 @@ func (r *HTTPRouteReconciler) updateHTTPRoutePolicyStatusOnDeleting(nn types.Nam
104
104
parentRefs = append (parentRefs , httpRoute .Spec .ParentRefs ... )
105
105
}
106
106
// delete AncestorRef which is not exist in the all parentRefs for each policy
107
- updateDeleteAncestors (r .Client , r .Log , policy , parentRefs )
107
+ updateDeleteAncestors (ctx , r .Client , r .Log , policy , parentRefs )
108
108
}
109
109
110
110
return nil
@@ -124,33 +124,32 @@ func (r *IngressReconciler) processHTTPRoutePolicies(tctx *provider.TranslateCon
124
124
}
125
125
126
126
var (
127
- status = false
128
- reason = string (v1alpha2 .PolicyReasonConflicted )
129
- message = "HTTPRoutePolicy conflict with others target to the Ingress"
127
+ condition metav1.Condition
130
128
)
131
- if conflict := checkPoliciesConflict (list .Items ); ! conflict {
132
- status = true
133
- reason = string (v1alpha2 .PolicyReasonAccepted )
134
- message = ""
135
-
129
+ if conflict := checkPoliciesConflict (list .Items ); conflict {
130
+ condition . Status = metav1 . ConditionFalse
131
+ condition . Reason = string (v1alpha2 .PolicyReasonConflicted )
132
+ condition . Message = "HTTPRoutePolicy conflict with others target to the Ingress "
133
+ } else {
136
134
tctx .HTTPRoutePolicies = list .Items
137
135
}
138
136
139
137
for i := range list .Items {
140
138
policy := list .Items [i ]
141
- modifyHTTPRoutePolicyStatus (tctx .RouteParentRefs , & policy , status , reason , message )
142
- tctx .StatusUpdaters = append (tctx .StatusUpdaters , & policy )
139
+ if updated := setAncestorsForHTTPRoutePolicyStatus (tctx .RouteParentRefs , & policy , condition ); updated {
140
+ tctx .StatusUpdaters = append (tctx .StatusUpdaters , & policy )
141
+ }
143
142
}
144
143
145
144
return nil
146
145
}
147
146
148
- func (r * IngressReconciler ) updateHTTPRoutePolicyStatusOnDeleting (nn types.NamespacedName ) error {
147
+ func (r * IngressReconciler ) updateHTTPRoutePolicyStatusOnDeleting (ctx context. Context , nn types.NamespacedName ) error {
149
148
var (
150
149
list v1alpha1.HTTPRoutePolicyList
151
150
key = indexer .GenIndexKeyWithGK (networkingv1 .GroupName , "Ingress" , nn .Namespace , nn .Name )
152
151
)
153
- if err := r .List (context . Background () , & list , client.MatchingFields {indexer .PolicyTargetRefs : key }); err != nil {
152
+ if err := r .List (ctx , & list , client.MatchingFields {indexer .PolicyTargetRefs : key }); err != nil {
154
153
return err
155
154
}
156
155
var (
@@ -164,7 +163,7 @@ func (r *IngressReconciler) updateHTTPRoutePolicyStatusOnDeleting(nn types.Names
164
163
parentRef , ok := ingress2ParentRef [namespacedName ]
165
164
if ! ok {
166
165
var ingress networkingv1.Ingress
167
- if err := r .Get (context . Background () , namespacedName , & ingress ); err != nil {
166
+ if err := r .Get (ctx , namespacedName , & ingress ); err != nil {
168
167
continue
169
168
}
170
169
ingressClass , err := r .getIngressClass (& ingress )
@@ -173,33 +172,29 @@ func (r *IngressReconciler) updateHTTPRoutePolicyStatusOnDeleting(nn types.Names
173
172
}
174
173
parentRef = gatewayv1.ParentReference {
175
174
Group : ptr .To (gatewayv1 .Group (ingressClass .GroupVersionKind ().Group )),
176
- Kind : ptr .To (gatewayv1 .Kind ("IngressClass" )),
175
+ Kind : ptr .To (gatewayv1 .Kind (KindIngressClass )),
177
176
Name : gatewayv1 .ObjectName (ingressClass .Name ),
178
177
}
179
178
ingress2ParentRef [namespacedName ] = parentRef
180
179
}
181
180
parentRefs = append (parentRefs , parentRef )
182
181
}
183
182
// delete AncestorRef which is not exist in the all parentRefs
184
- updateDeleteAncestors (r .Client , r .Log , policy , parentRefs )
183
+ updateDeleteAncestors (ctx , r .Client , r .Log , policy , parentRefs )
185
184
}
186
185
187
186
return nil
188
187
}
189
188
190
- func modifyHTTPRoutePolicyStatus (parentRefs []gatewayv1.ParentReference , policy * v1alpha1.HTTPRoutePolicy , status bool , reason , message string ) {
191
- condition := metav1.Condition {
192
- Type : string (v1alpha2 .PolicyConditionAccepted ),
193
- Status : metav1 .ConditionTrue ,
189
+ func setAncestorsForHTTPRoutePolicyStatus (parentRefs []gatewayv1.ParentReference , policy * v1alpha1.HTTPRoutePolicy , condition metav1. Condition ) bool {
190
+ return SetAncestors ( & policy . Status , parentRefs , metav1.Condition {
191
+ Type : cmp . Or ( condition . Type , string (v1alpha2 .PolicyConditionAccepted ) ),
192
+ Status : cmp . Or ( condition . Status , metav1 .ConditionTrue ) ,
194
193
ObservedGeneration : policy .GetGeneration (),
195
194
LastTransitionTime : metav1 .Now (),
196
- Reason : reason ,
197
- Message : message ,
198
- }
199
- if ! status {
200
- condition .Status = metav1 .ConditionFalse
201
- }
202
- _ = SetAncestors (& policy .Status , parentRefs , condition )
195
+ Reason : cmp .Or (condition .Reason , string (v1alpha2 .PolicyReasonAccepted )),
196
+ Message : condition .Message ,
197
+ })
203
198
}
204
199
205
200
// checkPoliciesConflict determines if there is a conflict among the given HTTPRoutePolicy objects based on their priority values.
@@ -234,15 +229,15 @@ func findPoliciesWhichTargetRefTheRule(ruleName *gatewayv1.SectionName, kind str
234
229
}
235
230
236
231
// updateDeleteAncestors removes ancestor references from HTTPRoutePolicy statuses that are no longer present in the provided parentRefs.
237
- func updateDeleteAncestors (client client.Client , logger logr.Logger , policy v1alpha1.HTTPRoutePolicy , parentRefs []gatewayv1.ParentReference ) {
232
+ func updateDeleteAncestors (ctx context. Context , client client.Client , logger logr.Logger , policy v1alpha1.HTTPRoutePolicy , parentRefs []gatewayv1.ParentReference ) {
238
233
length := len (policy .Status .Ancestors )
239
- policy .Status .Ancestors = slices .DeleteFunc (policy .Status .Ancestors , func (status v1alpha2.PolicyAncestorStatus ) bool {
234
+ policy .Status .Ancestors = slices .DeleteFunc (policy .Status .Ancestors , func (ancestor v1alpha2.PolicyAncestorStatus ) bool {
240
235
return ! slices .ContainsFunc (parentRefs , func (ref gatewayv1.ParentReference ) bool {
241
- return parentRefValueEqual (status .AncestorRef , ref )
236
+ return parentRefValueEqual (ancestor .AncestorRef , ref )
242
237
})
243
238
})
244
239
if length != len (policy .Status .Ancestors ) {
245
- if err := client .Status ().Update (context . Background () , & policy ); err != nil {
240
+ if err := client .Status ().Update (ctx , & policy ); err != nil {
246
241
logger .Error (err , "failed to update HTTPRoutePolicy status" )
247
242
}
248
243
}
0 commit comments