You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by to...@apache.org on 2021/02/02 08:55:21 UTC

[apisix-ingress-controller] 01/02: chore: support grpc scheme in ApisixUpstream

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

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

commit 5c18a32827d8bad3671a341859f5dc2e002e5c48
Author: Alex Zhang <zc...@gmail.com>
AuthorDate: Mon Feb 1 17:43:14 2021 +0800

    chore: support grpc scheme in ApisixUpstream
---
 go.sum                                  |   2 +
 pkg/apisix/upstream.go                  |   2 +
 pkg/ingress/apisix/upstream.go          |   9 +++
 pkg/ingress/controller/endpoint.go      |   4 +-
 pkg/kube/apisix/apis/config/v1/types.go |  11 ++++
 pkg/types/apisix/v1/types.go            |   1 +
 test/e2e/e2e.go                         |   1 +
 test/e2e/features/scheme.go             | 106 ++++++++++++++++++++++++++++++++
 test/e2e/go.mod                         |   1 +
 test/e2e/go.sum                         |   2 +
 test/e2e/ingress/namespace.go           |   3 +-
 test/e2e/ingress/sanity.go              |   3 +-
 test/e2e/scaffold/scaffold.go           |   5 ++
 13 files changed, 147 insertions(+), 3 deletions(-)

diff --git a/go.sum b/go.sum
index d45702a..670ae5c 100644
--- a/go.sum
+++ b/go.sum
@@ -613,6 +613,7 @@ google.golang.org/genproto v0.0.0-20200204135345-fa8e72b47b90/go.mod h1:GmwEX6Z4
 google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
 google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
 google.golang.org/genproto v0.0.0-20200305110556-506484158171/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
+google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY=
 google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
 google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
 google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
@@ -620,6 +621,7 @@ google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ij
 google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
 google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
 google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
+google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk=
 google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
 google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
 google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
diff --git a/pkg/apisix/upstream.go b/pkg/apisix/upstream.go
index 5e10c60..43d6fd7 100644
--- a/pkg/apisix/upstream.go
+++ b/pkg/apisix/upstream.go
@@ -67,6 +67,7 @@ type upstreamReqBody struct {
 	Key    string        `json:"key,omitempty"`
 	Nodes  upstreamNodes `json:"nodes"`
 	Desc   string        `json:"desc"`
+	Scheme string        `json:"scheme"`
 }
 
 type upstreamItem struct {
@@ -199,6 +200,7 @@ func (u *upstreamClient) Create(ctx context.Context, obj *v1.Upstream) (*v1.Upst
 		Key:    obj.Key,
 		Nodes:  nodes,
 		Desc:   obj.Name,
+		Scheme: obj.Scheme,
 	})
 	if err != nil {
 		return nil, err
diff --git a/pkg/ingress/apisix/upstream.go b/pkg/ingress/apisix/upstream.go
index 960e48a..ab5c0f2 100644
--- a/pkg/ingress/apisix/upstream.go
+++ b/pkg/ingress/apisix/upstream.go
@@ -15,6 +15,7 @@
 package apisix
 
 import (
+	"fmt"
 	"strconv"
 
 	"github.com/apache/apisix-ingress-controller/pkg/ingress/endpoint"
@@ -49,6 +50,10 @@ func (aub *ApisixUpstreamBuilder) Convert() ([]*apisix.Upstream, error) {
 	rv := ar.ObjectMeta.ResourceVersion
 	Ports := ar.Spec.Ports
 	for _, r := range Ports {
+		if r.Scheme != "" && r.Scheme != configv1.SchemeHTTP && r.Scheme != configv1.SchemeGRPC {
+			return nil, fmt.Errorf("bad scheme %s", r.Scheme)
+		}
+
 		port := r.Port
 		// apisix route name = namespace_svcName_svcPort = apisix service name
 		apisixUpstreamName := ns + "_" + name + "_" + strconv.Itoa(int(port))
@@ -72,6 +77,10 @@ func (aub *ApisixUpstreamBuilder) Convert() ([]*apisix.Upstream, error) {
 			Nodes:           nodes,
 			FromKind:        fromKind,
 		}
+		if r.Scheme != "" {
+			upstream.Scheme = r.Scheme
+		}
+
 		lbType := RR
 		if lb != nil {
 			lbType = lb["type"].(string)
diff --git a/pkg/ingress/controller/endpoint.go b/pkg/ingress/controller/endpoint.go
index a0f4f12..c2937d6 100644
--- a/pkg/ingress/controller/endpoint.go
+++ b/pkg/ingress/controller/endpoint.go
@@ -172,7 +172,9 @@ func (c *endpointsController) handleSyncErr(obj interface{}, err error) {
 		return
 	}
 	if c.workqueue.NumRequeues(obj) < _maxRetries {
-		log.Infof("sync endpoints %+v failed, will retry", obj)
+		log.Infow("sync endpoints failed, will retry",
+			zap.Any("object", obj),
+		)
 		c.workqueue.AddRateLimited(obj)
 	} else {
 		c.workqueue.Forget(obj)
diff --git a/pkg/kube/apisix/apis/config/v1/types.go b/pkg/kube/apisix/apis/config/v1/types.go
index b8db3cd..e4800ad 100644
--- a/pkg/kube/apisix/apis/config/v1/types.go
+++ b/pkg/kube/apisix/apis/config/v1/types.go
@@ -91,8 +91,19 @@ type ApisixUpstreamSpec struct {
 type Port struct {
 	Port         int          `json:"port,omitempty"`
 	LoadBalancer LoadBalancer `json:"loadbalancer,omitempty"`
+	// The scheme used to talk with the upstream.
+	// Now value can be http, grpc.
+	// +optional
+	Scheme string `json:"scheme,omitempty" yaml:"scheme,omitempty"`
 }
 
+var (
+	// SchemeHTTP represents the HTTP protocol.
+	SchemeHTTP = "http"
+	// SchemeGRPC represents the GRPC protocol.
+	SchemeGRPC = "grpc"
+)
+
 // LoadBalancer describes the load balancing parameters.
 type LoadBalancer map[string]interface{}
 
diff --git a/pkg/types/apisix/v1/types.go b/pkg/types/apisix/v1/types.go
index 7c78a9b..3ac580a 100644
--- a/pkg/types/apisix/v1/types.go
+++ b/pkg/types/apisix/v1/types.go
@@ -77,6 +77,7 @@ type Upstream struct {
 	Key             string `json:"key,omitempty" yaml:"key,omitempty"`
 	Nodes           []Node `json:"nodes,omitempty" yaml:"nodes,omitempty"`
 	FromKind        string `json:"from_kind,omitempty" yaml:"from_kind,omitempty"`
+	Scheme          string `json:"scheme,omitempty" yaml:"scheme,omitempty"`
 }
 
 // Node the node in upstream
diff --git a/test/e2e/e2e.go b/test/e2e/e2e.go
index 9640ef8..8c5052d 100644
--- a/test/e2e/e2e.go
+++ b/test/e2e/e2e.go
@@ -16,6 +16,7 @@ package e2e
 
 import (
 	_ "github.com/apache/apisix-ingress-controller/test/e2e/endpoints"
+	_ "github.com/apache/apisix-ingress-controller/test/e2e/features"
 	_ "github.com/apache/apisix-ingress-controller/test/e2e/ingress"
 )
 
diff --git a/test/e2e/features/scheme.go b/test/e2e/features/scheme.go
new file mode 100644
index 0000000..20e7a84
--- /dev/null
+++ b/test/e2e/features/scheme.go
@@ -0,0 +1,106 @@
+// 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 features
+
+import (
+	"context"
+	"time"
+
+	"github.com/onsi/ginkgo"
+	"github.com/stretchr/testify/assert"
+	"google.golang.org/grpc"
+	"google.golang.org/grpc/examples/helloworld/helloworld"
+
+	"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
+)
+
+var _ = ginkgo.Describe("choose scheme", func() {
+	s := scaffold.NewDefaultScaffold()
+	ginkgo.FIt("grpc", func() {
+		err := s.CreateResourceFromString(`
+apiVersion: v1
+kind: Pod
+metadata:
+  name: grpc-server
+  labels:
+    app: grpc-server
+spec:
+  containers:
+  - name: grcp-server
+    image: docker.io/tokers/grpc_server_example:latest
+---
+apiVersion: v1
+kind: Service
+metadata:
+  name: grpc-server-service
+spec:
+  selector:
+    app: grpc-server
+  ports:
+  - name: grpc
+    port: 50051
+    protocol: TCP
+    targetPort: 50051
+`)
+		assert.Nil(ginkgo.GinkgoT(), err)
+		assert.Nil(ginkgo.GinkgoT(), s.CreateResourceFromString(`
+apiVersion: apisix.apache.org/v1
+kind: ApisixUpstream
+metadata:
+  name: grpc-server-service
+spec:
+  ports:
+    - port: 50051
+      scheme: grpc
+`))
+		time.Sleep(2 * time.Second)
+		ups, err := s.ListApisixUpstreams()
+		assert.Nil(ginkgo.GinkgoT(), err)
+		assert.Len(ginkgo.GinkgoT(), ups, 1)
+
+		err = s.CreateResourceFromString(`
+apiVersion: apisix.apache.org/v1
+kind: ApisixRoute
+metadata:
+ name: grpc-route
+spec:
+ rules:
+ - host: grpc.local
+   http:
+     paths:
+     - backend:
+         serviceName: grpc_server_service
+         servicePort: 50051
+       path: /helloworld.Greeter/SayHello
+`)
+		assert.Nil(ginkgo.GinkgoT(), err)
+
+		ep, err := s.GetAPISIXEndpoint()
+		assert.Nil(ginkgo.GinkgoT(), err)
+		grpcConn, err := grpc.DialContext(context.TODO(), ep,
+			grpc.WithBlock(),
+			grpc.WithInsecure(),
+		)
+		assert.Nil(ginkgo.GinkgoT(), err)
+		cli := helloworld.NewGreeterClient(grpcConn)
+		hr := &helloworld.HelloRequest{
+			Name: "Alex",
+		}
+		resp, err := cli.SayHello(context.TODO(), hr)
+		assert.Nil(ginkgo.GinkgoT(), err)
+		assert.Equal(ginkgo.GinkgoT(), resp.Message, "Alex")
+	})
+})
diff --git a/test/e2e/go.mod b/test/e2e/go.mod
index 410a084..68e421b 100644
--- a/test/e2e/go.mod
+++ b/test/e2e/go.mod
@@ -8,6 +8,7 @@ require (
 	github.com/gruntwork-io/terratest v0.31.2
 	github.com/onsi/ginkgo v1.14.2
 	github.com/stretchr/testify v1.6.1
+	google.golang.org/grpc v1.27.1
 	k8s.io/api v0.20.2
 	k8s.io/apimachinery v0.20.2
 )
diff --git a/test/e2e/go.sum b/test/e2e/go.sum
index bd5bae6..f8e48f6 100644
--- a/test/e2e/go.sum
+++ b/test/e2e/go.sum
@@ -835,6 +835,7 @@ google.golang.org/genproto v0.0.0-20200204135345-fa8e72b47b90/go.mod h1:GmwEX6Z4
 google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
 google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
 google.golang.org/genproto v0.0.0-20200305110556-506484158171/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
+google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY=
 google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
 google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
 google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
@@ -844,6 +845,7 @@ google.golang.org/grpc v1.23.1/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyac
 google.golang.org/grpc v1.24.0/go.mod h1:XDChyiUovWa60DnaeDeZmSW86xtLtjtZbwvSiRnRtcA=
 google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
 google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
+google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk=
 google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
 google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
 google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
diff --git a/test/e2e/ingress/namespace.go b/test/e2e/ingress/namespace.go
index b2868fe..9f4d1f9 100644
--- a/test/e2e/ingress/namespace.go
+++ b/test/e2e/ingress/namespace.go
@@ -21,9 +21,10 @@ import (
 	"net/http"
 	"time"
 
-	"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
 	"github.com/onsi/ginkgo"
 	"github.com/stretchr/testify/assert"
+
+	"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
 )
 
 var _ = ginkgo.Describe("namespacing filtering", func() {
diff --git a/test/e2e/ingress/sanity.go b/test/e2e/ingress/sanity.go
index d31884d..009efb3 100644
--- a/test/e2e/ingress/sanity.go
+++ b/test/e2e/ingress/sanity.go
@@ -19,9 +19,10 @@ import (
 	"net/http"
 	"time"
 
-	"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
 	"github.com/onsi/ginkgo"
 	"github.com/stretchr/testify/assert"
+
+	"github.com/apache/apisix-ingress-controller/test/e2e/scaffold"
 )
 
 type ip struct {
diff --git a/test/e2e/scaffold/scaffold.go b/test/e2e/scaffold/scaffold.go
index 6e3e851..18bee6d 100644
--- a/test/e2e/scaffold/scaffold.go
+++ b/test/e2e/scaffold/scaffold.go
@@ -129,6 +129,11 @@ func (s *Scaffold) DefaultHTTPBackend() (string, []int32) {
 	return s.httpbinService.Name, ports
 }
 
+// GetAPISIXEndpoint returns the service and port (as an endpoint).
+func (s *Scaffold) GetAPISIXEndpoint() (string, error) {
+	return s.apisixServiceURL()
+}
+
 // NewAPISIXClient creates the default HTTP client.
 func (s *Scaffold) NewAPISIXClient() *httpexpect.Expect {
 	host, err := s.apisixServiceURL()