Skip to content

Commit b8ec7c4

Browse files
committed
feat: update HTTPRoutePolicy status on HTTPRoute/Ingress deleting
1 parent c134251 commit b8ec7c4

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

internal/controller/httproutepolicy.go

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package controller
22

33
import (
4+
"cmp"
45
"context"
56
"time"
67

78
networkingv1 "k8s.io/api/networking/v1"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/apimachinery/pkg/types"
911
"k8s.io/utils/ptr"
12+
"k8s.io/utils/strings/slices"
1013
"sigs.k8s.io/controller-runtime/pkg/client"
1114
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
1215
"sigs.k8s.io/gateway-api/apis/v1alpha2"
@@ -23,14 +26,33 @@ func (r *HTTPRouteReconciler) processHTTPRoutePolicies(tctx *provider.TranslateC
2326
object: httpRoute,
2427
policies: make(map[targetRefKey][]v1alpha1.HTTPRoutePolicy),
2528
}
26-
listForAllRules v1alpha1.HTTPRoutePolicyList
27-
key = indexer.GenHTTPRoutePolicyIndexKey(gatewayv1.GroupName, "HTTPRoute", httpRoute.GetNamespace(), httpRoute.GetName(), "")
29+
list v1alpha1.HTTPRoutePolicyList
30+
key = indexer.GenIndexKeyWithGK(gatewayv1.GroupName, "HTTPRoute", httpRoute.GetNamespace(), httpRoute.GetName())
2831
)
29-
if err := r.List(context.Background(), &listForAllRules, client.MatchingFields{indexer.PolicyTargetRefs: key}); err != nil {
32+
if err := r.List(context.Background(), &list, client.MatchingFields{indexer.PolicyTargetRefs: key}); err != nil {
3033
return err
3134
}
3235

33-
for _, item := range listForAllRules.Items {
36+
tctx.HTTPRoutePolicies = list.Items
37+
if len(tctx.HTTPRoutePolicies) == 0 {
38+
return nil
39+
}
40+
41+
var conflict = false
42+
Loop:
43+
for _, rule := range httpRoute.Spec.Rules {
44+
if rule.Name == nil || *rule.Name == "" {
45+
priority := tctx.HTTPRoutePolicies[0].Spec.Priority
46+
for _, policy := range tctx.HTTPRoutePolicies {
47+
if !ptr.Equal(priority, policy.Spec.Priority) {
48+
conflict = true
49+
break Loop
50+
}
51+
}
52+
}
53+
}
54+
55+
for _, item := range list.Items {
3456
checker.append("", item)
3557
tctx.HTTPRoutePolicies["*"] = append(tctx.HTTPRoutePolicies["*"], item)
3658
}
@@ -105,7 +127,7 @@ func (r *IngressReconciler) processHTTPRoutePolicies(tctx *provider.TranslateCon
105127
conflict: false,
106128
}
107129
list v1alpha1.HTTPRoutePolicyList
108-
key = indexer.GenHTTPRoutePolicyIndexKey(networkingv1.GroupName, "Ingress", ingress.GetNamespace(), ingress.GetName(), "")
130+
key = indexer.GenIndexKeyWithGK(networkingv1.GroupName, "Ingress", ingress.GetNamespace(), ingress.GetName())
109131
)
110132
if err := r.List(context.Background(), &list, client.MatchingFields{indexer.PolicyTargetRefs: key}); err != nil {
111133
return err
@@ -188,3 +210,29 @@ func (c *conflictChecker) append(sectionName string, policy v1alpha1.HTTPRoutePo
188210
}
189211
}
190212
}
213+
214+
func isHTTPRoutePolicyConflictOnHTTPRoute(rules []gatewayv1.HTTPRouteRule, policies []v1alpha1.HTTPRoutePolicy) bool {
215+
var m = make(map[targetRefKey]v1alpha1.HTTPRoutePolicy)
216+
for _, policy := range policies {
217+
for _, ref := range policy.Spec.TargetRefs {
218+
var sectionName gatewayv1.SectionName
219+
if ref.SectionName != nil {
220+
sectionName = *ref.SectionName
221+
}
222+
key := targetRefKey{
223+
Group: ref.Group,
224+
Namespace: gatewayv1.Namespace(policy.GetNamespace()),
225+
Name: ref.Name,
226+
SectionName: sectionName,
227+
}
228+
m[key] = policy
229+
}
230+
}
231+
for _, rule := range rules {
232+
if rule.Name == nil || *rule.Name == "" {
233+
234+
} else {
235+
236+
}
237+
}
238+
}

internal/controller/indexer/indexer.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,19 +322,20 @@ func HTTPRouteExtensionIndexFunc(rawObj client.Object) []string {
322322
return keys
323323
}
324324

325-
func GenHTTPRoutePolicyIndexKey(group, kind, namespace, name, sectionName string) string {
326-
return schema.GroupKind{Group: group, Kind: kind}.String() + "/" + client.ObjectKey{Namespace: namespace, Name: name}.String() + "/" + sectionName
327-
}
325+
// func GenHTTPRoutePolicyIndexKey(group, kind, namespace, name, sectionName string) string {
326+
// return schema.GroupKind{Group: group, Kind: kind}.String() + "/" + client.ObjectKey{Namespace: namespace, Name: name}.String() + "/" + sectionName
327+
// }
328328

329329
func HTTPRoutePolicyIndexFunc(rawObj client.Object) []string {
330330
hrp := rawObj.(*v1alpha1.HTTPRoutePolicy)
331331
var keys = make([]string, 0, len(hrp.Spec.TargetRefs))
332+
var m = make(map[string]struct{})
332333
for _, ref := range hrp.Spec.TargetRefs {
333-
var sectionName string
334-
if ref.SectionName != nil {
335-
sectionName = string(*ref.SectionName)
334+
key := GenIndexKeyWithGK(string(ref.Group), string(ref.Kind), hrp.GetNamespace(), string(ref.Name))
335+
if _, ok := m[key]; !ok {
336+
m[key] = struct{}{}
337+
keys = append(keys, key)
336338
}
337-
keys = append(keys, GenHTTPRoutePolicyIndexKey(string(ref.Group), string(ref.Kind), hrp.GetNamespace(), string(ref.Name), sectionName))
338339
}
339340
return keys
340341
}

internal/provider/provider.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type TranslateContext struct {
3838
BackendTrafficPolicies map[types.NamespacedName]*v1alpha1.BackendTrafficPolicy
3939
GatewayProxies map[ResourceKind]v1alpha1.GatewayProxy
4040
ResourceParentRefs map[ResourceKind][]ResourceKind
41-
HTTPRoutePolicies map[string][]v1alpha1.HTTPRoutePolicy
41+
HTTPRoutePolicies []v1alpha1.HTTPRoutePolicy
4242

4343
StatusUpdaters []client.Object
4444
}
@@ -53,6 +53,5 @@ func NewDefaultTranslateContext(ctx context.Context) *TranslateContext {
5353
BackendTrafficPolicies: make(map[types.NamespacedName]*v1alpha1.BackendTrafficPolicy),
5454
GatewayProxies: make(map[ResourceKind]v1alpha1.GatewayProxy),
5555
ResourceParentRefs: make(map[ResourceKind][]ResourceKind),
56-
HTTPRoutePolicies: make(map[string][]v1alpha1.HTTPRoutePolicy),
5756
}
5857
}

0 commit comments

Comments
 (0)