@@ -3,6 +3,7 @@ package controller
33import (
44 "fmt"
55
6+ "k8s.io/utils/ptr"
67 gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
78 gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
89
@@ -11,6 +12,7 @@ import (
1112 "github.com/api7/api7-ingress-controller/internal/controller/indexer"
1213 "github.com/api7/api7-ingress-controller/internal/provider"
1314 "github.com/go-logr/logr"
15+ "k8s.io/apimachinery/pkg/api/meta"
1416 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1517 "k8s.io/apimachinery/pkg/runtime/schema"
1618 "k8s.io/apimachinery/pkg/types"
@@ -26,10 +28,14 @@ func (p PolicyTargetKey) String() string {
2628 return p .NsName .String () + "/" + p .GroupKind .String ()
2729}
2830
29- func ProcessBackendTrafficPolicy (c client.Client ,
31+ func ProcessBackendTrafficPolicy (
32+ c client.Client ,
3033 log logr.Logger ,
31- tctx * provider.TranslateContext ) {
32- conflicts := map [string ]v1alpha1.BackendTrafficPolicy {}
34+ tctx * provider.TranslateContext ,
35+ ) {
36+ conflicts := map [string ]* v1alpha1.BackendTrafficPolicy {}
37+ servicePortNameMap := map [string ]bool {}
38+ policyMap := map [types.NamespacedName ]* v1alpha1.BackendTrafficPolicy {}
3339 for _ , service := range tctx .Services {
3440 backendTrafficPolicyList := & v1alpha1.BackendTrafficPolicyList {}
3541 if err := c .List (tctx , backendTrafficPolicyList ,
@@ -43,58 +49,61 @@ func ProcessBackendTrafficPolicy(c client.Client,
4349 if len (backendTrafficPolicyList .Items ) == 0 {
4450 continue
4551 }
46-
47- portNameExist := make (map [string ]bool , len (service .Spec .Ports ))
4852 for _ , port := range service .Spec .Ports {
49- portNameExist [port .Name ] = true
53+ key := fmt .Sprintf ("%s/%s/%s" , service .Namespace , service .Name , port .Name )
54+ servicePortNameMap [key ] = true
55+ }
56+
57+ for _ , p := range backendTrafficPolicyList .Items {
58+ policyMap [types.NamespacedName {
59+ Name : p .Name ,
60+ Namespace : p .Namespace ,
61+ }] = p .DeepCopy ()
5062 }
51- for _ , policy := range backendTrafficPolicyList .Items {
52- targetRefs := policy .Spec .TargetRefs
53- updated := false
54- for _ , targetRef := range targetRefs {
55- sectionName := targetRef .SectionName
56- key := PolicyTargetKey {
57- NsName : types.NamespacedName {Namespace : service .Namespace , Name : service .Name },
58- GroupKind : schema.GroupKind {Group : "" , Kind : "Service" },
59- }
60- condition := NewPolicyCondition (policy .Generation , true , "Policy has been accepted" )
61- if sectionName != nil && ! portNameExist [string (* sectionName )] {
62- condition = NewPolicyCondition (policy .Generation , false , fmt .Sprintf ("SectionName %s not found in Service %s/%s" , * sectionName , service .Namespace , service .Name ))
63- processPolicyStatus (& policy , tctx , condition , & updated )
64- continue
65- }
66- if p , ok := conflicts [key .String ()]; ok && (p .Name == policy .Name && p .Namespace == policy .Namespace ) {
67- condition = NewPolicyConflictCondition (policy .Generation , fmt .Sprintf ("Unable to target Service %s/%s, because it conflicts with another BackendTrafficPolicy" , service .Namespace , service .Name ))
68- processPolicyStatus (& policy , tctx , condition , & updated )
69- continue
70- }
71- conflicts [key .String ()] = policy
72- processPolicyStatus (& policy , tctx , condition , & updated )
63+ }
64+
65+ for _ , p := range policyMap {
66+ policy := p .DeepCopy ()
67+ targetRefs := policy .Spec .TargetRefs
68+ updated := false
69+ for _ , targetRef := range targetRefs {
70+ sectionName := targetRef .SectionName
71+ key := PolicyTargetKey {
72+ NsName : types.NamespacedName {Namespace : p .GetNamespace (), Name : string (targetRef .Name )},
73+ GroupKind : schema.GroupKind {Group : "" , Kind : "Service" },
7374 }
74- if _ , ok := tctx . BackendTrafficPolicies [types. NamespacedName {
75- Name : policy .Name ,
76- Namespace : policy .Namespace ,
77- }]; ok {
75+ condition := NewPolicyCondition ( policy . Generation , true , "Policy has been accepted" )
76+ if sectionName != nil && ! servicePortNameMap [ fmt . Sprintf ( "%s/%s/%s" , policy .Namespace , string ( targetRef . Name ), * sectionName )] {
77+ condition = NewPolicyCondition ( policy .Generation , false , fmt . Sprintf ( "No section name %s found in Service %s/%s" , * sectionName , policy . Namespace , targetRef . Name ))
78+ processPolicyStatus ( policy , tctx , condition , & updated )
7879 continue
7980 }
80-
81- if updated {
82- tctx .StatusUpdaters = append (tctx .StatusUpdaters , policy .DeepCopy ())
81+ if _ , ok := conflicts [key .String ()]; ok {
82+ condition = NewPolicyConflictCondition (policy .Generation , fmt .Sprintf ("Unable to target Service %s/%s, because it conflicts with another BackendTrafficPolicy" , policy .Namespace , targetRef .Name ))
83+ processPolicyStatus (policy , tctx , condition , & updated )
84+ continue
8385 }
84-
85- tctx . BackendTrafficPolicies [types. NamespacedName {
86- Name : policy . Name ,
87- Namespace : policy . Namespace ,
88- }] = policy .DeepCopy ()
86+ conflicts [ key . String ()] = policy
87+ processPolicyStatus ( policy , tctx , condition , & updated )
88+ }
89+ if updated {
90+ tctx . StatusUpdaters = append ( tctx . StatusUpdaters , policy .DeepCopy () )
8991 }
9092 }
93+ for _ , policy := range conflicts {
94+ tctx .BackendTrafficPolicies [types.NamespacedName {
95+ Name : policy .Name ,
96+ Namespace : policy .Namespace ,
97+ }] = policy
98+ }
9199}
92100
93101func processPolicyStatus (policy * v1alpha1.BackendTrafficPolicy ,
94102 tctx * provider.TranslateContext ,
95103 condition metav1.Condition ,
96- updated * bool ) {
97- if ok := SetAncestors (& policy .Status , tctx .ParentRefs , condition ); ok {
104+ updated * bool ,
105+ ) {
106+ if ok := SetAncestors (& policy .Status , tctx .RouteParentRefs , condition ); ok {
98107 * updated = true
99108 }
100109}
@@ -115,17 +124,21 @@ func SetAncestors(status *v1alpha1.PolicyStatus, parentRefs []gatewayv1.ParentRe
115124}
116125
117126func SetAncestorStatus (status * v1alpha1.PolicyStatus , ancestorStatus gatewayv1alpha2.PolicyAncestorStatus ) bool {
127+ if len (ancestorStatus .Conditions ) == 0 {
128+ return false
129+ }
130+ condition := ancestorStatus .Conditions [0 ]
118131 for _ , c := range status .Ancestors {
119- if c .AncestorRef == ancestorStatus .AncestorRef {
120- if len (c .Conditions ) == 0 || len (ancestorStatus .Conditions ) == 0 {
121- c .Conditions = ancestorStatus .Conditions
122- return true
123- }
124- if c .Conditions [0 ].ObservedGeneration < ancestorStatus .Conditions [0 ].ObservedGeneration {
125- c .Conditions = ancestorStatus .Conditions
126- return true
132+ if c .AncestorRef .Name == ancestorStatus .AncestorRef .Name &&
133+ ptr .Equal (c .AncestorRef .Namespace , ancestorStatus .AncestorRef .Namespace ) &&
134+ ptr .Equal (c .AncestorRef .Group , ancestorStatus .AncestorRef .Group ) &&
135+ ptr .Equal (c .AncestorRef .Kind , ancestorStatus .AncestorRef .Kind ) &&
136+ c .ControllerName == ancestorStatus .ControllerName {
137+ if ! VerifyConditions (& c .Conditions , condition ) {
138+ return false
127139 }
128- return false
140+ meta .SetStatusCondition (& c .Conditions , condition )
141+ return true
129142 }
130143 }
131144 status .Ancestors = append (status .Ancestors , ancestorStatus )
0 commit comments