Copilot commented on code in PR #2540:
URL: 
https://github.com/apache/apisix-ingress-controller/pull/2540#discussion_r2317638973


##########
internal/adc/translator/httproute.go:
##########
@@ -466,32 +467,89 @@ func (t *Translator) TranslateHTTPRoute(tctx 
*provider.TranslateContext, httpRou
        labels := label.GenLabel(httpRoute)
 
        for ruleIndex, rule := range rules {
-               upstream := adctypes.NewDefaultUpstream()
-               var backendErr error
+               service := adctypes.NewDefaultService()
+               service.Labels = labels
+
+               service.Name = 
adctypes.ComposeServiceNameWithRule(httpRoute.Namespace, httpRoute.Name, 
fmt.Sprintf("%d", ruleIndex))
+               service.ID = id.GenID(service.Name)
+               service.Hosts = hosts
+
+               var (
+                       upstreams         = make([]*adctypes.Upstream, 0)
+                       weightedUpstreams = 
make([]adctypes.TrafficSplitConfigRuleWeightedUpstream, 0)
+                       backendErr        error
+               )
+
                for _, backend := range rule.BackendRefs {
                        if backend.Namespace == nil {
                                namespace := 
gatewayv1.Namespace(httpRoute.Namespace)
                                backend.Namespace = &namespace
                        }
+                       upstream := adctypes.NewDefaultUpstream()
                        upNodes, err := t.translateBackendRef(tctx, 
backend.BackendRef, DefaultEndpointFilter)
                        if err != nil {
                                backendErr = err
                                continue
                        }
+                       if len(upNodes) == 0 {
+                               continue
+                       }
+
                        
t.AttachBackendTrafficPolicyToUpstream(backend.BackendRef, 
tctx.BackendTrafficPolicies, upstream)
-                       upstream.Nodes = append(upstream.Nodes, upNodes...)
+                       upstream.Nodes = upNodes
+                       upstreams = append(upstreams, upstream)
                }
 
-               // todo: support multiple backends
-               service := adctypes.NewDefaultService()
-               service.Labels = labels
+               // Handle multiple backends with traffic-split plugin
+               if len(upstreams) == 0 {
+                       // Create a default upstream if no valid backends
+                       upstream := adctypes.NewDefaultUpstream()
+                       service.Upstream = upstream
+               } else if len(upstreams) == 1 {
+                       // Single backend - use directly as service upstream
+                       service.Upstream = upstreams[0]
+               } else {
+                       // Multiple backends - use traffic-split plugin
+                       service.Upstream = upstreams[0]
+                       upstreams = upstreams[1:]
+
+                       // Set weight in traffic-split for the default upstream
+                       weight := apiv2.DefaultWeight
+                       if rule.BackendRefs[0].Weight != nil {
+                               weight = int(*rule.BackendRefs[0].Weight)
+                       }
+                       weightedUpstreams = append(weightedUpstreams, 
adctypes.TrafficSplitConfigRuleWeightedUpstream{
+                               Weight: weight,
+                       })
 
-               service.Name = 
adctypes.ComposeServiceNameWithRule(httpRoute.Namespace, httpRoute.Name, 
fmt.Sprintf("%d", ruleIndex))
-               service.ID = id.GenID(service.Name)
-               service.Hosts = hosts
-               service.Upstream = upstream
+                       // Set other upstreams in traffic-split
+                       for i, upstream := range upstreams {
+                               weight := apiv2.DefaultWeight
+                               // get weight from the backend ref start from 
the second backend

Review Comment:
   The comment has a grammatical error. It should read 'get weight from the 
backend ref starting from the second backend' or 'get weight from the backend 
refs starting from the second backend'.
   ```suggestion
                                // get weight from the backend refs starting 
from the second backend
   ```



##########
internal/adc/translator/httproute.go:
##########
@@ -466,32 +467,89 @@ func (t *Translator) TranslateHTTPRoute(tctx 
*provider.TranslateContext, httpRou
        labels := label.GenLabel(httpRoute)
 
        for ruleIndex, rule := range rules {
-               upstream := adctypes.NewDefaultUpstream()
-               var backendErr error
+               service := adctypes.NewDefaultService()
+               service.Labels = labels
+
+               service.Name = 
adctypes.ComposeServiceNameWithRule(httpRoute.Namespace, httpRoute.Name, 
fmt.Sprintf("%d", ruleIndex))
+               service.ID = id.GenID(service.Name)
+               service.Hosts = hosts
+
+               var (
+                       upstreams         = make([]*adctypes.Upstream, 0)
+                       weightedUpstreams = 
make([]adctypes.TrafficSplitConfigRuleWeightedUpstream, 0)
+                       backendErr        error
+               )
+
                for _, backend := range rule.BackendRefs {
                        if backend.Namespace == nil {
                                namespace := 
gatewayv1.Namespace(httpRoute.Namespace)
                                backend.Namespace = &namespace
                        }
+                       upstream := adctypes.NewDefaultUpstream()
                        upNodes, err := t.translateBackendRef(tctx, 
backend.BackendRef, DefaultEndpointFilter)
                        if err != nil {
                                backendErr = err
                                continue
                        }
+                       if len(upNodes) == 0 {
+                               continue
+                       }
+
                        
t.AttachBackendTrafficPolicyToUpstream(backend.BackendRef, 
tctx.BackendTrafficPolicies, upstream)
-                       upstream.Nodes = append(upstream.Nodes, upNodes...)
+                       upstream.Nodes = upNodes
+                       upstreams = append(upstreams, upstream)
                }
 
-               // todo: support multiple backends
-               service := adctypes.NewDefaultService()
-               service.Labels = labels
+               // Handle multiple backends with traffic-split plugin
+               if len(upstreams) == 0 {
+                       // Create a default upstream if no valid backends
+                       upstream := adctypes.NewDefaultUpstream()
+                       service.Upstream = upstream
+               } else if len(upstreams) == 1 {
+                       // Single backend - use directly as service upstream
+                       service.Upstream = upstreams[0]
+               } else {
+                       // Multiple backends - use traffic-split plugin
+                       service.Upstream = upstreams[0]
+                       upstreams = upstreams[1:]
+
+                       // Set weight in traffic-split for the default upstream
+                       weight := apiv2.DefaultWeight
+                       if rule.BackendRefs[0].Weight != nil {
+                               weight = int(*rule.BackendRefs[0].Weight)
+                       }
+                       weightedUpstreams = append(weightedUpstreams, 
adctypes.TrafficSplitConfigRuleWeightedUpstream{
+                               Weight: weight,
+                       })
 
-               service.Name = 
adctypes.ComposeServiceNameWithRule(httpRoute.Namespace, httpRoute.Name, 
fmt.Sprintf("%d", ruleIndex))
-               service.ID = id.GenID(service.Name)
-               service.Hosts = hosts
-               service.Upstream = upstream
+                       // Set other upstreams in traffic-split
+                       for i, upstream := range upstreams {
+                               weight := apiv2.DefaultWeight
+                               // get weight from the backend ref start from 
the second backend
+                               if i+1 < len(rule.BackendRefs) && 
rule.BackendRefs[i+1].Weight != nil {
+                                       weight = 
int(*rule.BackendRefs[i+1].Weight)
+                               }

Review Comment:
   The weight assignment logic is incorrect. The loop index `i` corresponds to 
`upstreams[i]`, but the weight is taken from `rule.BackendRefs[i+1]`. This 
creates an off-by-one error since `upstreams` starts from the second backend 
(index 1 in BackendRefs), so the weight should come from 
`rule.BackendRefs[i+1]` which is actually `rule.BackendRefs[i+2]` in the 
original BackendRefs array.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@apisix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to