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 5812cea chore: api server module (#83)
5812cea is described below
commit 5812cea22480aecdea65e3d450ea144448a55205
Author: Alex Zhang <[email protected]>
AuthorDate: Mon Dec 14 11:33:23 2020 +0800
chore: api server module (#83)
related #92 .
---
cmd/ingress/ingress.go | 14 ++++---
go.mod | 3 +-
go.sum | 33 +++++++++++++++-
main.go | 8 ++--
main.go => pkg/api/router/router.go | 32 ++++++++++------
main.go => pkg/api/router/router_test.go | 32 ++++++++++------
pkg/api/server.go | 64 ++++++++++++++++++++++++++++++++
main.go => pkg/api/server_test.go | 40 ++++++++++++++------
pkg/scheduler.go | 61 ------------------------------
main.go => pkg/types/errors.go | 19 +++-------
main.go => pkg/types/errors_test.go | 19 ++++------
11 files changed, 191 insertions(+), 134 deletions(-)
diff --git a/cmd/ingress/ingress.go b/cmd/ingress/ingress.go
index 9d8bf5f..4180f49 100644
--- a/cmd/ingress/ingress.go
+++ b/cmd/ingress/ingress.go
@@ -16,7 +16,6 @@ package ingress
import (
"fmt"
- "net/http"
"os"
"os/signal"
"strings"
@@ -27,7 +26,7 @@ import (
"github.com/spf13/cobra"
"github.com/api7/ingress-controller/conf"
- "github.com/api7/ingress-controller/pkg"
+ "github.com/api7/ingress-controller/pkg/api"
"github.com/api7/ingress-controller/pkg/config"
"github.com/api7/ingress-controller/pkg/ingress/controller"
"github.com/api7/ingress-controller/pkg/log"
@@ -108,10 +107,15 @@ func NewIngressCommand() *cobra.Command {
c.SharedInformerFactory.Start(stop)
}()
+ srv, err := api.NewServer(cfg)
+ if err != nil {
+ dief("failed to create API Server: %s", err)
+ }
+
+ // TODO add sync.WaitGroup
go func() {
- router := pkg.Route()
- if err := http.ListenAndServe(":8080", router);
err != nil && err != http.ErrServerClosed {
- log.Errorf("failed to start http
server: %s", err)
+ if err := srv.Run(stop); err != nil {
+ dief("failed to launch API Server: %s",
err)
}
}()
diff --git a/go.mod b/go.mod
index 8fa7ae8..e42f082 100644
--- a/go.mod
+++ b/go.mod
@@ -3,12 +3,13 @@ module github.com/api7/ingress-controller
go 1.13
require (
+ github.com/gin-gonic/gin v1.6.3
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/gxthrj/apisix-ingress-types v0.1.2
github.com/gxthrj/apisix-types v0.1.0
github.com/gxthrj/seven v0.1.9
- github.com/julienschmidt/httprouter v1.3.0
+ github.com/julienschmidt/httprouter v1.2.0
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 //
indirect
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v1.1.1
diff --git a/go.sum b/go.sum
index 4bf044b..0e95dff 100644
--- a/go.sum
+++ b/go.sum
@@ -50,10 +50,22 @@ github.com/fatih/color v1.7.0/go.mod
h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5Kwzbycv
github.com/fsnotify/fsnotify v1.4.7
h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod
h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/ghodss/yaml v1.0.0/go.mod
h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
+github.com/gin-contrib/sse v0.1.0
h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
+github.com/gin-contrib/sse v0.1.0/go.mod
h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
+github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
+github.com/gin-gonic/gin v1.6.3/go.mod
h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod
h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-kit/kit v0.8.0/go.mod
h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod
h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod
h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
+github.com/go-playground/assert/v2 v2.0.1
h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
+github.com/go-playground/assert/v2 v2.0.1/go.mod
h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
+github.com/go-playground/locales v0.13.0
h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
+github.com/go-playground/locales v0.13.0/go.mod
h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
+github.com/go-playground/universal-translator v0.17.0
h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
+github.com/go-playground/universal-translator v0.17.0/go.mod
h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
+github.com/go-playground/validator/v10 v10.2.0
h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
+github.com/go-playground/validator/v10 v10.2.0/go.mod
h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/go-stack/stack v1.8.0/go.mod
h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gogo/protobuf v0.0.0-20171007142547-342cbe0a0415/go.mod
h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.1.1/go.mod
h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
@@ -73,6 +85,8 @@ github.com/golang/protobuf v1.2.0/go.mod
h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.1/go.mod
h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2
h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod
h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/golang/protobuf v1.3.3
h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
+github.com/golang/protobuf v1.3.3/go.mod
h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/btree v0.0.0-20160524151835-7d79101e329e/go.mod
h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod
h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod
h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
@@ -81,6 +95,8 @@ github.com/google/go-cmp v0.3.0
h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
github.com/google/go-cmp v0.3.0/go.mod
h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf
h1:+RRA9JqSOZFfKrOeqr2z77+8R2RKyh8PG66dcu1V0ck=
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf/go.mod
h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI=
+github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
+github.com/google/gofuzz v1.0.0/go.mod
h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod
h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod
h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod
h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
@@ -135,11 +151,12 @@ github.com/jonboulle/clockwork v0.1.0/go.mod
h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22
github.com/json-iterator/go v0.0.0-20180701071628-ab8a2e0c74be/go.mod
h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.6
h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs=
github.com/json-iterator/go v1.1.6/go.mod
h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
+github.com/json-iterator/go v1.1.9
h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
+github.com/json-iterator/go v1.1.9/go.mod
h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod
h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jtolds/gls v4.20.0+incompatible/go.mod
h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
+github.com/julienschmidt/httprouter v1.2.0
h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g=
github.com/julienschmidt/httprouter v1.2.0/go.mod
h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
-github.com/julienschmidt/httprouter v1.3.0
h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
-github.com/julienschmidt/httprouter v1.3.0/go.mod
h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88
h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod
h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/kisielk/errcheck v1.1.0/go.mod
h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
@@ -153,6 +170,8 @@ github.com/kr/pretty v0.1.0/go.mod
h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod
h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
+github.com/leodido/go-urn v1.2.0
h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
+github.com/leodido/go-urn v1.2.0/go.mod
h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/magiconair/properties v1.8.1/go.mod
h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9/go.mod
h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.4
h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
@@ -160,6 +179,8 @@ github.com/mattn/go-colorable v0.1.4/go.mod
h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc
github.com/mattn/go-isatty v0.0.3/go.mod
h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.8
h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod
h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
+github.com/mattn/go-isatty v0.0.12
h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
+github.com/mattn/go-isatty v0.0.12/go.mod
h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod
h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod
h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod
h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
@@ -170,8 +191,10 @@ github.com/mitchellh/gox v0.4.0/go.mod
h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS4
github.com/mitchellh/iochan v1.0.0/go.mod
h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod
h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod
h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
+github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod
h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod
h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
+github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod
h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1
h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod
h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod
h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
@@ -240,6 +263,10 @@ github.com/tidwall/match v1.0.1/go.mod
h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0
github.com/tidwall/pretty v1.0.2
h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU=
github.com/tidwall/pretty v1.0.2/go.mod
h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod
h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
+github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
+github.com/ugorji/go v1.1.7/go.mod
h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
+github.com/ugorji/go/codec v1.1.7
h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
+github.com/ugorji/go/codec v1.1.7/go.mod
h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod
h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/yudai/gojsondiff v1.0.0
h1:27cbfqXLVEJ1o8I6v3y9lg8Ydm53EKqHXAOMxEGlCOA=
github.com/yudai/gojsondiff v1.0.0/go.mod
h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FBNExI05xg=
@@ -331,6 +358,8 @@ golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod
h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod
h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0
h1:HyfiK1WMnHj5FXFXatD+Qs1A/xC2Run6RzeW1SyHxpc=
golang.org/x/sys v0.0.0-20190624142023-c5567b49c5d0/go.mod
h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200116001909-b77594299b42
h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
+golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod
h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod
h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db/go.mod
h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
diff --git a/main.go b/main.go
index df6b737..2ab11ec 100644
--- a/main.go
+++ b/main.go
@@ -14,11 +14,11 @@
// limitations under the License.
package main
-import (
- "os"
+ import (
+ "fmt"
+ "os"
"github.com/api7/ingress-controller/cmd"
- "github.com/api7/ingress-controller/log"
"github.com/api7/ingress-controller/conf"
)
@@ -26,7 +26,7 @@ func main() {
conf.Init()
root := cmd.NewAPISIXIngressControllerCommand()
if err := root.Execute(); err != nil {
- log.GetLogger().Error(err.Error())
+ fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
diff --git a/main.go b/pkg/api/router/router.go
similarity index 66%
copy from main.go
copy to pkg/api/router/router.go
index df6b737..6f695a0 100644
--- a/main.go
+++ b/pkg/api/router/router.go
@@ -12,21 +12,29 @@
// 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 main
+package router
import (
- "os"
+ "net/http"
- "github.com/api7/ingress-controller/cmd"
- "github.com/api7/ingress-controller/log"
- "github.com/api7/ingress-controller/conf"
+ "github.com/gin-gonic/gin"
)
-func main() {
- conf.Init()
- root := cmd.NewAPISIXIngressControllerCommand()
- if err := root.Execute(); err != nil {
- log.GetLogger().Error(err.Error())
- os.Exit(1)
- }
+type healthzResponse struct {
+ Status string `json:"status"`
+}
+
+func mountHealthz(r *gin.Engine) {
+ r.GET("/healthz", healthz)
+ r.GET("/apisix/healthz", healthz)
+}
+
+func healthz(c *gin.Context) {
+ c.AbortWithStatusJSON(http.StatusOK, healthzResponse{Status: "ok"})
+ return
+}
+
+// Mount mounts all api routers.
+func Mount(r *gin.Engine) {
+ mountHealthz(r)
}
diff --git a/main.go b/pkg/api/router/router_test.go
similarity index 64%
copy from main.go
copy to pkg/api/router/router_test.go
index df6b737..c5a2ee5 100644
--- a/main.go
+++ b/pkg/api/router/router_test.go
@@ -12,21 +12,29 @@
// 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 main
+package router
import (
- "os"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "testing"
- "github.com/api7/ingress-controller/cmd"
- "github.com/api7/ingress-controller/log"
- "github.com/api7/ingress-controller/conf"
+ "github.com/gin-gonic/gin"
+ "github.com/stretchr/testify/assert"
)
-func main() {
- conf.Init()
- root := cmd.NewAPISIXIngressControllerCommand()
- if err := root.Execute(); err != nil {
- log.GetLogger().Error(err.Error())
- os.Exit(1)
- }
+func TestHealthz(t *testing.T) {
+ w := httptest.NewRecorder()
+ c, r := gin.CreateTestContext(w)
+ mountHealthz(r)
+ healthz(c)
+
+ assert.Equal(t, w.Code, http.StatusOK)
+
+ var resp healthzResponse
+ dec := json.NewDecoder(w.Body)
+ assert.Nil(t, dec.Decode(&resp))
+
+ assert.Equal(t, resp, healthzResponse{Status: "ok"})
}
diff --git a/pkg/api/server.go b/pkg/api/server.go
new file mode 100644
index 0000000..484e055
--- /dev/null
+++ b/pkg/api/server.go
@@ -0,0 +1,64 @@
+// 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 api
+
+import (
+ "net"
+
+ "github.com/gin-gonic/gin"
+
+ apirouter "github.com/api7/ingress-controller/pkg/api/router"
+ "github.com/api7/ingress-controller/pkg/config"
+ "github.com/api7/ingress-controller/pkg/log"
+ "github.com/api7/ingress-controller/pkg/types"
+)
+
+// Server represents the API Server in ingress-apisix-controller.
+type Server struct {
+ router *gin.Engine
+ httpListener net.Listener
+}
+
+// NewServer initializes the API Server.
+func NewServer(cfg *config.Config) (*Server, error) {
+ httpListener, err := net.Listen("tcp", cfg.HTTPListen)
+ if err != nil {
+ return nil, err
+ }
+ gin.SetMode(gin.ReleaseMode)
+ router := gin.New()
+ router.Use(gin.Recovery(), gin.Logger())
+ apirouter.Mount(router)
+
+ return &Server{
+ router: router,
+ httpListener: httpListener,
+ }, nil
+}
+
+// Run launches the API Server.
+func (srv *Server) Run(stopCh <-chan struct{}) error {
+ go func() {
+ <-stopCh
+ if err := srv.httpListener.Close(); err != nil {
+ log.Errorf("failed to close http listener: %s", err)
+ }
+ }()
+ if err := srv.router.RunListener(srv.httpListener); err != nil &&
!types.IsUseOfClosedNetConnErr(err) {
+ log.Errorf("failed to start API Server: %s", err)
+ return err
+ }
+ return nil
+}
diff --git a/main.go b/pkg/api/server_test.go
similarity index 54%
copy from main.go
copy to pkg/api/server_test.go
index df6b737..80f56cd 100644
--- a/main.go
+++ b/pkg/api/server_test.go
@@ -12,21 +12,37 @@
// 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 main
+package api
import (
- "os"
+ "testing"
+ "time"
- "github.com/api7/ingress-controller/cmd"
- "github.com/api7/ingress-controller/log"
- "github.com/api7/ingress-controller/conf"
+ "github.com/stretchr/testify/assert"
+
+ "github.com/api7/ingress-controller/pkg/config"
)
-func main() {
- conf.Init()
- root := cmd.NewAPISIXIngressControllerCommand()
- if err := root.Execute(); err != nil {
- log.GetLogger().Error(err.Error())
- os.Exit(1)
- }
+func TestServer(t *testing.T) {
+ cfg := &config.Config{HTTPListen: "127.0.0.1:0"}
+ srv, err := NewServer(cfg)
+ assert.Nil(t, err, "see non-nil error: ", err)
+
+ err = srv.httpListener.Close()
+ assert.Nil(t, err, "see non-nil error: ", err)
+}
+
+func TestServerRun(t *testing.T) {
+ cfg := &config.Config{HTTPListen: "127.0.0.1:0"}
+ srv, err := NewServer(cfg)
+ assert.Nil(t, err, "see non-nil error: ", err)
+
+ stopCh := make(chan struct{})
+ go func() {
+ time.Sleep(2 * time.Second)
+ close(stopCh)
+ }()
+
+ err = srv.Run(stopCh)
+ assert.Nil(t, err, "see non-nil error: ", err)
}
diff --git a/pkg/scheduler.go b/pkg/scheduler.go
deleted file mode 100644
index e667fd1..0000000
--- a/pkg/scheduler.go
+++ /dev/null
@@ -1,61 +0,0 @@
-// 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 pkg
-
-import (
- "github.com/api7/ingress-controller/conf"
- "k8s.io/apimachinery/pkg/labels"
- "k8s.io/api/core/v1"
-)
-
-func ListPods(m map[string]string) ([]*v1.Pod, error){
- podInformer := conf.GetPodInformer()
- selector := labels.Set(m).AsSelector()
- ret, err := podInformer.Lister().List(selector)
- for _, pod := range ret {
- logger.Info(pod.Status.PodIP)
- }
- return ret, err
-}
-
-func ListPodsBySvcName(name string) []*v1.Pod {
- svcInformer := conf.GetSvcInformer()
- selector := labels.NewSelector()
- ret, _ := svcInformer.Lister().List(selector)
- for _, svc := range ret {
- if svc.Name == name {
- logger.Debug(svc.Spec.Selector)
- if pods, err := ListPods(svc.Spec.Selector); err != nil
{
- return []*v1.Pod{}
- } else {
- return pods
- }
- }
- }
- return []*v1.Pod{}
-}
-
-func Scheduler(){
- //jobrunner.Start()
- // 定时10s检测一次
- //jobrunner.Schedule("*/10 * * * * *", Compared{})
-}
-
-//type Compared struct{}
-
-//func (w Compared) Run() {
-// CompareAndAlarm()
-//}
-
diff --git a/main.go b/pkg/types/errors.go
similarity index 71%
copy from main.go
copy to pkg/types/errors.go
index df6b737..4d246a5 100644
--- a/main.go
+++ b/pkg/types/errors.go
@@ -12,21 +12,14 @@
// 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 main
+package types
import (
- "os"
-
- "github.com/api7/ingress-controller/cmd"
- "github.com/api7/ingress-controller/log"
- "github.com/api7/ingress-controller/conf"
+ "strings"
)
-func main() {
- conf.Init()
- root := cmd.NewAPISIXIngressControllerCommand()
- if err := root.Execute(); err != nil {
- log.GetLogger().Error(err.Error())
- os.Exit(1)
- }
+// IsUseOfClosedNetConnErr checks whether the error is due to
+// use of closed network connection.
+func IsUseOfClosedNetConnErr(err error) bool {
+ return strings.Contains(err.Error(), "use of closed network connection")
}
diff --git a/main.go b/pkg/types/errors_test.go
similarity index 72%
copy from main.go
copy to pkg/types/errors_test.go
index df6b737..96dd736 100644
--- a/main.go
+++ b/pkg/types/errors_test.go
@@ -12,21 +12,16 @@
// 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 main
+package types
import (
- "os"
+ "errors"
+ "testing"
- "github.com/api7/ingress-controller/cmd"
- "github.com/api7/ingress-controller/log"
- "github.com/api7/ingress-controller/conf"
+ "github.com/stretchr/testify/assert"
)
-func main() {
- conf.Init()
- root := cmd.NewAPISIXIngressControllerCommand()
- if err := root.Execute(); err != nil {
- log.GetLogger().Error(err.Error())
- os.Exit(1)
- }
+func TestIsUseOfClosedNetConnErr(t *testing.T) {
+ assert.False(t, IsUseOfClosedNetConnErr(errors.New("some errors")))
+ assert.True(t, IsUseOfClosedNetConnErr(errors.New("accept tcp
127.0.0.1:64064: use of closed network connection")))
}