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


##########
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,
+                       })

Review Comment:
   Great, as long as we can finish it before the next release. 🫡



-- 
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