11package controller
22
33import (
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+ }
0 commit comments