ronething commented on code in PR #2540:
URL:
https://github.com/apache/apisix-ingress-controller/pull/2540#discussion_r2318429890
##########
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:
Yes, we are aware this issue currently exists. It's not limited to
HTTPRoute, similar problems also exist in the current translate logic of
APISIXRoute. After discussing with @AlinsRan this morning, we've decided to
address this uniformly in the next pull request. This pull request is solely
intended to resolve the previous issue where multiple Upstreams were not
correctly utilizing traffic-split plugin.
What do you think?
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]