@@ -39,24 +39,38 @@ func canaryFeature(ingresses []networkingv1.Ingress, gatewayResources *i2gw.Gate
3939 return errs
4040 }
4141
42+ // We're dividing ingresses based on rule groups. If any path within a
43+ // rule group is associated with an ingress object containing canary annotations,
44+ // the entire rule group is affected.
45+ canaryEnabled := false
4246 for _ , paths := range ingressPathsByMatchKey {
43- path := paths [0 ]
47+ for _ , path := range paths {
48+ if path .extra .canary .enable {
49+ canaryEnabled = true
50+ }
51+ }
52+ }
4453
45- backendRefs , calculationErrs := calculateBackendRefWeight (paths )
46- errs = append (errs , calculationErrs ... )
54+ if canaryEnabled {
55+ for _ , paths := range ingressPathsByMatchKey {
56+ path := paths [0 ]
4757
48- key := types.NamespacedName {Namespace : path .ingress .Namespace , Name : common .RouteName (rg .Name , rg .Host )}
49- httpRoute , ok := gatewayResources .HTTPRoutes [key ]
50- if ! ok {
51- // If there wasn't an HTTPRoute for this Ingress, we can skip it as something is wrong.
52- // All the available errors will be returned at the end.
53- continue
54- }
58+ backendRefs , calculationErrs := calculateBackendRefWeight (paths )
59+ errs = append (errs , calculationErrs ... )
5560
56- patchHTTPRouteWithBackendRefs (& httpRoute , backendRefs )
57- }
58- if len (errs ) > 0 {
59- return errs
61+ key := types.NamespacedName {Namespace : path .ingress .Namespace , Name : common .RouteName (rg .Name , rg .Host )}
62+ httpRoute , ok := gatewayResources .HTTPRoutes [key ]
63+ if ! ok {
64+ // If there wasn't an HTTPRoute for this Ingress, we can skip it as something is wrong.
65+ // All the available errors will be returned at the end.
66+ continue
67+ }
68+
69+ patchHTTPRouteWithBackendRefs (& httpRoute , backendRefs )
70+ }
71+ if len (errs ) > 0 {
72+ return errs
73+ }
6074 }
6175 }
6276
@@ -92,20 +106,14 @@ func patchHTTPRouteWithBackendRefs(httpRoute *gatewayv1.HTTPRoute, backendRefs [
92106
93107 ruleExists = false
94108
95- for j , rule := range httpRoute .Spec .Rules {
96- foundBackendRef := false
109+ for _ , rule := range httpRoute .Spec .Rules {
97110 for i := range rule .BackendRefs {
98111 if backendRef .Name == rule .BackendRefs [i ].Name {
99112 rule .BackendRefs [i ].Weight = backendRef .Weight
100- foundBackendRef = true
101113 ruleExists = true
102114 break
103115 }
104116 }
105-
106- if ! foundBackendRef {
107- httpRoute .Spec .Rules [j ].BackendRefs = append (rule .BackendRefs , backendRef )
108- }
109117 }
110118
111119 if ! ruleExists {
0 commit comments