This is an automated email from the ASF dual-hosted git repository.

tokers pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-ingress-controller.git


The following commit(s) were added to refs/heads/master by this push:
     new fb53d65  chore: change the load balancer type and enhance the value 
check (#226)
fb53d65 is described below

commit fb53d655e8f7c7f74b9d7685ed9e85fdea22c8cc
Author: Alex Zhang <[email protected]>
AuthorDate: Wed Feb 3 14:41:17 2021 +0800

    chore: change the load balancer type and enhance the value check (#226)
---
 pkg/ingress/apisix/upstream.go                     | 47 ++++++++++++----------
 pkg/kube/apisix/apis/config/v1/types.go            | 25 +++++-------
 .../apisix/apis/config/v1/zz_generated.deepcopy.go | 22 +++++++++-
 pkg/types/apisix/v1/types.go                       | 24 +++++++++++
 test/e2e/endpoints/endpoints.go                    |  2 +-
 5 files changed, 81 insertions(+), 39 deletions(-)

diff --git a/pkg/ingress/apisix/upstream.go b/pkg/ingress/apisix/upstream.go
index 960e48a..da6b5f2 100644
--- a/pkg/ingress/apisix/upstream.go
+++ b/pkg/ingress/apisix/upstream.go
@@ -15,6 +15,7 @@
 package apisix
 
 import (
+       "errors"
        "strconv"
 
        "github.com/apache/apisix-ingress-controller/pkg/ingress/endpoint"
@@ -24,8 +25,6 @@ import (
 )
 
 const (
-       RR             = "roundrobin"
-       CHASH          = "chash"
        ApisixUpstream = "ApisixUpstream"
 )
 
@@ -72,26 +71,32 @@ func (aub *ApisixUpstreamBuilder) Convert() 
([]*apisix.Upstream, error) {
                        Nodes:           nodes,
                        FromKind:        fromKind,
                }
-               lbType := RR
-               if lb != nil {
-                       lbType = lb["type"].(string)
-               }
-               switch {
-               case lbType == CHASH:
-                       upstream.Type = lbType
-                       hashOn := lb["hashOn"]
-                       key := lb["key"]
-                       if hashOn != nil {
-                               ho := hashOn.(string)
-                               upstream.HashOn = ho
-                       }
-                       if key != nil {
-                               k := key.(string)
-                               upstream.Key = k
+               if lb == nil || lb.Type == "" {
+                       upstream.Type = apisix.LbRoundRobin
+               } else {
+                       switch lb.Type {
+                       case apisix.LbRoundRobin, apisix.LbLeastConn, 
apisix.LbEwma:
+                               upstream.Type = lb.Type
+                       case apisix.LbConsistentHash:
+                               upstream.Type = lb.Type
+                               upstream.Key = lb.Key
+                               switch lb.HashOn {
+                               case apisix.HashOnVars:
+                                       fallthrough
+                               case apisix.HashOnHeader:
+                                       fallthrough
+                               case apisix.HashOnCookie:
+                                       fallthrough
+                               case apisix.HashOnConsumer:
+                                       fallthrough
+                               case apisix.HashOnVarsCombination:
+                                       upstream.HashOn = lb.HashOn
+                               default:
+                                       return nil, errors.New("invalid hashOn 
value")
+                               }
+                       default:
+                               return nil, errors.New("invalid load balancer 
type")
                        }
-               default:
-                       lbType = RR
-                       upstream.Type = lbType
                }
                upstreams = append(upstreams, upstream)
        }
diff --git a/pkg/kube/apisix/apis/config/v1/types.go 
b/pkg/kube/apisix/apis/config/v1/types.go
index b8db3cd..f60c1cc 100644
--- a/pkg/kube/apisix/apis/config/v1/types.go
+++ b/pkg/kube/apisix/apis/config/v1/types.go
@@ -89,25 +89,18 @@ type ApisixUpstreamSpec struct {
 
 // Port is the port-specific configurations.
 type Port struct {
-       Port         int          `json:"port,omitempty"`
-       LoadBalancer LoadBalancer `json:"loadbalancer,omitempty"`
+       Port         int           `json:"port,omitempty"`
+       LoadBalancer *LoadBalancer `json:"loadbalancer,omitempty"`
 }
 
 // LoadBalancer describes the load balancing parameters.
-type LoadBalancer map[string]interface{}
-
-func (p LoadBalancer) DeepCopyInto(out *LoadBalancer) {
-       b, _ := json.Marshal(&p)
-       _ = json.Unmarshal(b, out)
-}
-
-func (p *LoadBalancer) DeepCopy() *LoadBalancer {
-       if p == nil {
-               return nil
-       }
-       out := new(LoadBalancer)
-       p.DeepCopyInto(out)
-       return out
+type LoadBalancer struct {
+       Type string `json:"type" yaml:"type"`
+       // The HashOn and Key fields are required when Type is "chash".
+       // HashOn represents the key fetching scope.
+       HashOn string `json:"hashOn,omitempty" yaml:"hashOn,omitempty"`
+       // Key represents the hash key.
+       Key string `json:"key,omitempty" yaml:"key,omitempty"`
 }
 
 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
diff --git a/pkg/kube/apisix/apis/config/v1/zz_generated.deepcopy.go 
b/pkg/kube/apisix/apis/config/v1/zz_generated.deepcopy.go
index ead5481..bb9e309 100644
--- a/pkg/kube/apisix/apis/config/v1/zz_generated.deepcopy.go
+++ b/pkg/kube/apisix/apis/config/v1/zz_generated.deepcopy.go
@@ -427,6 +427,22 @@ func (in *Http) DeepCopy() *Http {
 }
 
 // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, 
writing into out. in must be non-nil.
+func (in *LoadBalancer) DeepCopyInto(out *LoadBalancer) {
+       *out = *in
+       return
+}
+
+// DeepCopy is an autogenerated deepcopy function, copying the receiver, 
creating a new LoadBalancer.
+func (in *LoadBalancer) DeepCopy() *LoadBalancer {
+       if in == nil {
+               return nil
+       }
+       out := new(LoadBalancer)
+       in.DeepCopyInto(out)
+       return out
+}
+
+// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, 
writing into out. in must be non-nil.
 func (in *Path) DeepCopyInto(out *Path) {
        *out = *in
        out.Backend = in.Backend
@@ -471,7 +487,11 @@ func (in *Plugin) DeepCopy() *Plugin {
 // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, 
writing into out. in must be non-nil.
 func (in *Port) DeepCopyInto(out *Port) {
        *out = *in
-       in.LoadBalancer.DeepCopyInto(&out.LoadBalancer)
+       if in.LoadBalancer != nil {
+               in, out := &in.LoadBalancer, &out.LoadBalancer
+               *out = new(LoadBalancer)
+               **out = **in
+       }
        return
 }
 
diff --git a/pkg/types/apisix/v1/types.go b/pkg/types/apisix/v1/types.go
index 7c78a9b..9f1094c 100644
--- a/pkg/types/apisix/v1/types.go
+++ b/pkg/types/apisix/v1/types.go
@@ -16,6 +16,30 @@ package v1
 
 import "encoding/json"
 
+const (
+       // HashOnVars means the hash scope is variable.
+       HashOnVars = "vars"
+       // HashVarsCombination means the hash scope is the
+       // variable combination.
+       HashOnVarsCombination = "vars_combinations"
+       // HashOnHeader means the hash scope is HTTP request
+       // headers.
+       HashOnHeader = "header"
+       // HashOnCookie means the hash scope is HTTP Cookie.
+       HashOnCookie = "cookie"
+       // HashOnConsumer means the hash scope is APISIX consumer.
+       HashOnConsumer = "consumer"
+
+       // LbRoundRobin is the round robin load balancer.
+       LbRoundRobin = "roundrobin"
+       // LbConsistentHash is the consistent hash load balancer.
+       LbConsistentHash = "chash"
+       // LbEwma is the ewma load balancer.
+       LbEwma = "ewma"
+       // LbLeaseConn is the least connection load balancer.
+       LbLeastConn = "least_conn"
+)
+
 // Route apisix route object
 // +k8s:deepcopy-gen=true
 type Route struct {
diff --git a/test/e2e/endpoints/endpoints.go b/test/e2e/endpoints/endpoints.go
index 8beae6a..16e30cf 100644
--- a/test/e2e/endpoints/endpoints.go
+++ b/test/e2e/endpoints/endpoints.go
@@ -39,7 +39,7 @@ spec:
   ports:
     - port: %d
       loadbalancer:
-        type: roundbin
+        type: roundrobin
 `, backendSvc, backendSvcPort[0])
                assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(ups))
                assert.Nil(ginkgo.GinkgoT(), 
s.EnsureNumApisixUpstreamsCreated(1), "checking number of upstreams")

Reply via email to