You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/03/20 13:31:32 UTC

[GitHub] [apisix-ingress-controller] gxthrj commented on a change in pull request #308: feat: traffic split

gxthrj commented on a change in pull request #308:
URL: https://github.com/apache/apisix-ingress-controller/pull/308#discussion_r598109279



##########
File path: docs/en/latest/concepts/apisix_route.md
##########
@@ -139,12 +139,46 @@ spec:
           - /*
         methods:
           - GET
-      backend:
-        serviceName: foo
-        servicePort: 80
-        resolveGranularity: service
+      backends:
+        - serviceName: foo
+          servicePort: 80
+          resolveGranularity: service
 ```
 
+Weight Based Traffic Split
+--------------------------
+
+There can more than one backend specified in one route rule,
+when multiple backends co-exist there, the traffic split based on weights
+will be applied (which actually uses the [traffic-split](http://apisix.apache.org/docs/apisix/plugins/traffic-split/) plugin in Apache APISIX).
+You can specify weight for each backend, the default weight is `100`.
+
+```yaml
+apiVersion: apisix.apache.org/v2alpha1
+kind: ApisixRoute
+metadata:
+  name: method-route
+spec:
+  http:
+    - name: method
+      match:
+        paths:
+          - /*
+        methods:
+          - GET
+      backends:
+        - serviceName: foo
+          servicePort: 80
+          weight: 100
+        - serviceName: bar
+          servicePort: 81
+          weight: 50
+```
+
+The above `ApisixRoute` has one route rule, which contains two backends `foo` and `bar`, the
+weight ratio is `100:50`, which means `2/3` requests will be sent to service `foo` and `1/3` requests
+will be proxied to serivce `bar`.
+

Review comment:
       Need example to show `match` with `vars`.

##########
File path: pkg/kube/translation/util.go
##########
@@ -0,0 +1,86 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+package translation
+
+import (
+	"errors"
+
+	"go.uber.org/zap"
+	"k8s.io/apimachinery/pkg/util/intstr"
+
+	"github.com/apache/apisix-ingress-controller/pkg/id"
+	configv2alpha1 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2alpha1"
+	"github.com/apache/apisix-ingress-controller/pkg/log"
+	apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1"
+)
+
+func (t *translator) getServiceClusterIPAndPort(backend *configv2alpha1.ApisixRouteHTTPBackend, ar *configv2alpha1.ApisixRoute) (string, int32, error) {
+	svc, err := t.ServiceLister.Services(ar.Namespace).Get(backend.ServiceName)
+	if err != nil {
+		return "", 0, err
+	}
+	svcPort := int32(-1)
+loop:
+	for _, port := range svc.Spec.Ports {
+		switch backend.ServicePort.Type {
+		case intstr.Int:
+			if backend.ServicePort.IntVal == port.Port {
+				svcPort = port.Port
+				break loop
+			}
+		case intstr.String:
+			if backend.ServicePort.StrVal == port.Name {
+				svcPort = port.Port
+				break loop
+			}
+		}
+	}
+	if svcPort == -1 {
+		log.Errorw("ApisixRoute refers to non-existent Service port",
+			zap.Any("ApisixRoute", ar),
+			zap.String("port", backend.ServicePort.String()),
+		)
+		return "", 0, err
+	}
+
+	if backend.ResolveGranularity == "service" && svc.Spec.ClusterIP == "" {
+		log.Errorw("ApisixRoute refers to a headless service but want to use the service level resolve granularity",
+			zap.Any("ApisixRoute", ar),
+			zap.Any("service", svc),
+		)
+		return "", 0, errors.New("conflict headless service and backend resolve granularity")
+	}
+	return svc.Spec.ClusterIP, svcPort, nil
+}
+
+func (t *translator) translateUpstream(namespace, svcName, svcResolveGranularity, svcClusterIP string, svcPort int32) (*apisixv1.Upstream, error) {
+	ups, err := t.TranslateUpstream(namespace, svcName, svcPort)
+	if err != nil {
+		return nil, err
+	}
+	if svcResolveGranularity == "service" {
+		ups.Nodes = []apisixv1.UpstreamNode{
+			{
+				IP:     svcClusterIP,
+				Port:   int(svcPort),
+				Weight: _defaultWeight,
+			},
+		}
+	}
+	ups.FullName = apisixv1.ComposeUpstreamName(namespace, svcName, svcPort)
+	ups.Name = ups.FullName
+	ups.ID = id.GenID(ups.FullName)

Review comment:
       If upstream.ID is nil, what is the behavior of the controller ?

##########
File path: pkg/kube/translation/apisix_route.go
##########
@@ -108,40 +108,29 @@ func (t *translator) TranslateRouteV2alpha1(ar *configv2alpha1.ApisixRoute) ([]*
 		if len(part.Match.Paths) < 1 {
 			return nil, nil, errors.New("empty route paths match")
 		}
-		svc, err := t.ServiceLister.Services(ar.Namespace).Get(part.Backend.ServiceName)
-		if err != nil {
-			return nil, nil, err
+		if part.Backend != nil && len(part.Backends) > 1 {
+			return nil, nil, errors.New("backend and backends are exclusive")
 		}
-		svcPort := int32(-1)
-	loop:
-		for _, port := range svc.Spec.Ports {
-			switch part.Backend.ServicePort.Type {
-			case intstr.Int:
-				if part.Backend.ServicePort.IntVal == port.Port {
-					svcPort = port.Port
-					break loop
-				}
-			case intstr.String:
-				if part.Backend.ServicePort.StrVal == port.Name {
-					svcPort = port.Port
-					break loop
-				}
-			}
-		}
-		if svcPort == -1 {
-			log.Errorw("ApisixRoute refers to non-existent Service port",
-				zap.Any("ApisixRoute", ar),
-				zap.String("port", part.Backend.ServicePort.String()),
-			)
-			return nil, nil, err
+		if part.Backend == nil && len(part.Backends) == 0 {
+			return nil, nil, errors.New("no specified backend")
 		}
+		backends := part.Backends
+		backend := part.Backend
+		if len(backends) > 0 {
+			// Use the first backend as the default backend in Route,
+			// others will be configured in traffic-split plugin.
+			backend = backends[0]

Review comment:
       Confused here, `backend ` will be covered if backends is not empty ?




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

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