From 4e813bb7ee62e2f7275bb2e6d6c00a9fc8708d0c Mon Sep 17 00:00:00 2001 From: starsz Date: Mon, 1 Feb 2021 16:06:59 +0800 Subject: [PATCH 1/6] feat: support feat info (#1404) --- api/cmd/managerapi.go | 5 ++- api/internal/handler/tool/tool.go | 49 ++++++++++++++++++++++++++ api/internal/handler/tool/tool_test.go | 38 ++++++++++++++++++++ api/internal/route.go | 2 ++ api/internal/utils/utils.go | 16 +++++++++ api/test/e2e/info_test.go | 40 +++++++++++++++++++++ 6 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 api/internal/handler/tool/tool.go create mode 100644 api/internal/handler/tool/tool_test.go create mode 100644 api/test/e2e/info_test.go diff --git a/api/cmd/managerapi.go b/api/cmd/managerapi.go index d0dc1d02e1..41127ee773 100644 --- a/api/cmd/managerapi.go +++ b/api/cmd/managerapi.go @@ -73,6 +73,8 @@ func NewManagerAPICommand() *cobra.Command { return nil }) + utils.SetHashAndVersion(GitHash, Version) + droplet.Option.Orchestrator = func(mws []droplet.Middleware) []droplet.Middleware { var newMws []droplet.Middleware // default middleware order: resp_reshape, auto_input, traffic_log @@ -90,6 +92,7 @@ func NewManagerAPICommand() *cobra.Command { log.Errorf("init stores fail: %w", err) panic(err) } + // routes r := internal.SetUpRouter() addr := fmt.Sprintf("%s:%d", conf.ServerHost, conf.ServerPort) @@ -146,7 +149,7 @@ func newStopCommand() *cobra.Command { if syscall.ENOENT.Error() != err.Error() { fmt.Fprintf(os.Stderr, "failed to get manager-api pid: %s\n", err) } else { - fmt.Fprintf(os.Stderr, "pid path %s not found, is manager-api running?\n", conf.PIDPath) + fmt.Fprintf(os.Stderr, "pid path %s not found, is manager-api running?\n", conf.PIDPath) } return } diff --git a/api/internal/handler/tool/tool.go b/api/internal/handler/tool/tool.go new file mode 100644 index 0000000000..174fdda3b8 --- /dev/null +++ b/api/internal/handler/tool/tool.go @@ -0,0 +1,49 @@ +/* + * 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 tool + +import ( + "github.com/apisix/manager-api/internal/handler" + "github.com/apisix/manager-api/internal/utils" + "github.com/gin-gonic/gin" + "github.com/shiningrush/droplet" + wgin "github.com/shiningrush/droplet/wrapper/gin" +) + +type Handler struct { +} + +type InfoOutput struct { + Hash string `json:"commit_hash"` + Version string `json:"version"` +} + +func NewHandler() (handler.RouteRegister, error) { + return &Handler{}, nil +} + +func (h *Handler) ApplyRoute(r *gin.Engine) { + r.GET("/info", wgin.Wraps(h.Info)) +} + +func (h *Handler) Info(c droplet.Context) (interface{}, error) { + hash, version := utils.GetHashAndVersion() + return &InfoOutput{ + Hash: hash, + Version: version, + }, nil +} diff --git a/api/internal/handler/tool/tool_test.go b/api/internal/handler/tool/tool_test.go new file mode 100644 index 0000000000..59683d5e09 --- /dev/null +++ b/api/internal/handler/tool/tool_test.go @@ -0,0 +1,38 @@ +/* + * 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 tool + +import ( + "testing" + + "github.com/apisix/manager-api/internal/utils" + "github.com/shiningrush/droplet" + "github.com/stretchr/testify/assert" +) + +func TestInfo_Get(t *testing.T) { + h := Handler{} + ctx := droplet.NewContext() + + utils.SetHashAndVersion("94d952f", "master") + ret, err := h.Info(ctx) + assert.Nil(t, err) + assert.Equal(t, &InfoOutput{ + Hash: "94d952f", + Version: "master", + }, ret) +} diff --git a/api/internal/route.go b/api/internal/route.go index ac78a36002..26fe1a41b9 100644 --- a/api/internal/route.go +++ b/api/internal/route.go @@ -41,6 +41,7 @@ import ( "github.com/apisix/manager-api/internal/handler/server_info" "github.com/apisix/manager-api/internal/handler/service" "github.com/apisix/manager-api/internal/handler/ssl" + "github.com/apisix/manager-api/internal/handler/tool" "github.com/apisix/manager-api/internal/handler/upstream" "github.com/apisix/manager-api/internal/log" ) @@ -76,6 +77,7 @@ func SetUpRouter() *gin.Engine { label.NewHandler, data_loader.NewHandler, data_loader.NewImportHandler, + tool.NewHandler, } for i := range factories { diff --git a/api/internal/utils/utils.go b/api/internal/utils/utils.go index 3661bcb691..2666ca952d 100644 --- a/api/internal/utils/utils.go +++ b/api/internal/utils/utils.go @@ -33,6 +33,11 @@ import ( var _sf *sonyflake.Sonyflake +var ( + GitHash string + Version string +) + func init() { saltStr, ok := os.LookupEnv("FLAKE_SALT") var salt uint16 @@ -206,3 +211,14 @@ func ValueEqual(a interface{}, b interface{}) bool { } return bytes.Equal(aBytes, bBytes) } + +// set the hash and version +func SetHashAndVersion(hash, version string) { + GitHash = hash + Version = version +} + +// get the hash and version +func GetHashAndVersion() (string, string) { + return GitHash, Version +} diff --git a/api/test/e2e/info_test.go b/api/test/e2e/info_test.go new file mode 100644 index 0000000000..8934492b06 --- /dev/null +++ b/api/test/e2e/info_test.go @@ -0,0 +1,40 @@ +/* + * 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 e2e + +import ( + "net/http" + "testing" +) + +func TestInfo(t *testing.T) { + tests := []HttpTestCase{ + { + Desc: "get info", + Object: ManagerApiExpect(t), + Method: http.MethodGet, + Path: "/info", + Headers: map[string]string{"Authorization": token}, + ExpectStatus: http.StatusOK, + ExpectBody: []string{"commit_hash", "\"version\":\"master\""}, + }, + } + + for _, tc := range tests { + testCaseCheck(tc, t) + } +} From 696ba5d0e00a112de691a4bad4d95f39a18283da Mon Sep 17 00:00:00 2001 From: starsz Date: Tue, 2 Feb 2021 15:17:01 +0800 Subject: [PATCH 2/6] update: feat info --- api/build.sh | 2 +- api/cmd/managerapi.go | 2 +- api/internal/handler/tool/tool.go | 5 +++-- api/internal/handler/tool/tool_test.go | 10 ++++++---- api/internal/utils/utils.go | 16 --------------- api/internal/utils/version.go | 27 ++++++++++++++++++++++++++ 6 files changed, 38 insertions(+), 24 deletions(-) create mode 100644 api/internal/utils/version.go diff --git a/api/build.sh b/api/build.sh index 70ed743447..6cd249ad22 100755 --- a/api/build.sh +++ b/api/build.sh @@ -20,7 +20,7 @@ set -e VERSION=$(cat ./api/VERSION) GITHASH=$(cat ./.githash 2> /dev/null || HASH="ref: HEAD"; while [[ $HASH == ref\:* ]]; do HASH="$(cat ".git/$(echo $HASH | cut -d \ -f 2)")"; done; echo ${HASH:0:7}) -GOLDFLAGS="-X github.com/apisix/manager-api/cmd.Version=${VERSION} -X github.com/apisix/manager-api/cmd.GitHash=${GITHASH}" +GOLDFLAGS="-X github.com/apisix/manager-api/internal/utils.version=${VERSION} -X github.com/apisix/manager-api/internal/utils.gitHash=${GITHASH}" # Enter dry-run mode if [ "$1" == "--dry-run" ]; then diff --git a/api/cmd/managerapi.go b/api/cmd/managerapi.go index 41127ee773..419dc87cfe 100644 --- a/api/cmd/managerapi.go +++ b/api/cmd/managerapi.go @@ -73,7 +73,7 @@ func NewManagerAPICommand() *cobra.Command { return nil }) - utils.SetHashAndVersion(GitHash, Version) + GitHash, Version = utils.GetHashAndVersion() droplet.Option.Orchestrator = func(mws []droplet.Middleware) []droplet.Middleware { var newMws []droplet.Middleware diff --git a/api/internal/handler/tool/tool.go b/api/internal/handler/tool/tool.go index 174fdda3b8..0d90e096b3 100644 --- a/api/internal/handler/tool/tool.go +++ b/api/internal/handler/tool/tool.go @@ -17,11 +17,12 @@ package tool import ( - "github.com/apisix/manager-api/internal/handler" - "github.com/apisix/manager-api/internal/utils" "github.com/gin-gonic/gin" "github.com/shiningrush/droplet" wgin "github.com/shiningrush/droplet/wrapper/gin" + + "github.com/apisix/manager-api/internal/handler" + "github.com/apisix/manager-api/internal/utils" ) type Handler struct { diff --git a/api/internal/handler/tool/tool_test.go b/api/internal/handler/tool/tool_test.go index 59683d5e09..dc1cf0a1bd 100644 --- a/api/internal/handler/tool/tool_test.go +++ b/api/internal/handler/tool/tool_test.go @@ -19,20 +19,22 @@ package tool import ( "testing" - "github.com/apisix/manager-api/internal/utils" "github.com/shiningrush/droplet" "github.com/stretchr/testify/assert" + + "github.com/apisix/manager-api/internal/utils" ) func TestInfo_Get(t *testing.T) { h := Handler{} ctx := droplet.NewContext() - utils.SetHashAndVersion("94d952f", "master") + hash, version := utils.GetHashAndVersion() + ret, err := h.Info(ctx) assert.Nil(t, err) assert.Equal(t, &InfoOutput{ - Hash: "94d952f", - Version: "master", + Hash: hash, + Version: version, }, ret) } diff --git a/api/internal/utils/utils.go b/api/internal/utils/utils.go index 2666ca952d..3661bcb691 100644 --- a/api/internal/utils/utils.go +++ b/api/internal/utils/utils.go @@ -33,11 +33,6 @@ import ( var _sf *sonyflake.Sonyflake -var ( - GitHash string - Version string -) - func init() { saltStr, ok := os.LookupEnv("FLAKE_SALT") var salt uint16 @@ -211,14 +206,3 @@ func ValueEqual(a interface{}, b interface{}) bool { } return bytes.Equal(aBytes, bBytes) } - -// set the hash and version -func SetHashAndVersion(hash, version string) { - GitHash = hash - Version = version -} - -// get the hash and version -func GetHashAndVersion() (string, string) { - return GitHash, Version -} diff --git a/api/internal/utils/version.go b/api/internal/utils/version.go new file mode 100644 index 0000000000..d4bec4ab00 --- /dev/null +++ b/api/internal/utils/version.go @@ -0,0 +1,27 @@ +/* + * 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 utils + +var ( + gitHash string + version string +) + +// get the hash and version +func GetHashAndVersion() (string, string) { + return gitHash, version +} From b61a3b9b1219f922eb285da2d20ae01afb9b63d4 Mon Sep 17 00:00:00 2001 From: starsz Date: Tue, 2 Feb 2021 17:52:10 +0800 Subject: [PATCH 3/6] update: fix ci --- api/test/e2e/info_test.go | 1 - api/test/shell/cli_test.sh | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/api/test/e2e/info_test.go b/api/test/e2e/info_test.go index 8934492b06..a3bd887f88 100644 --- a/api/test/e2e/info_test.go +++ b/api/test/e2e/info_test.go @@ -28,7 +28,6 @@ func TestInfo(t *testing.T) { Object: ManagerApiExpect(t), Method: http.MethodGet, Path: "/info", - Headers: map[string]string{"Authorization": token}, ExpectStatus: http.StatusOK, ExpectBody: []string{"commit_hash", "\"version\":\"master\""}, }, diff --git a/api/test/shell/cli_test.sh b/api/test/shell/cli_test.sh index c272c367dd..5ec96cc1a6 100755 --- a/api/test/shell/cli_test.sh +++ b/api/test/shell/cli_test.sh @@ -52,7 +52,7 @@ clean_logfile() { trap clean_up EXIT export GO111MODULE=on -go build -o ./manager-api -ldflags "-X github.com/apisix/manager-api/cmd.Version=${VERSION} -X github.com/apisix/manager-api/cmd.GitHash=${GITHASH}" ./cmd/manager +go build -o ./manager-api -ldflags "-X github.com/apisix/manager-api/internal/utils.version=${VERSION} -X github.com/apisix/manager-api/internal/utils.gitHash=${GITHASH}" ./cmd/manager # default level: warn, path: logs/error.log From b22303d4836884f1a97e00b2bb6eb7ac7f6bbd90 Mon Sep 17 00:00:00 2001 From: starsz Date: Wed, 3 Feb 2021 10:50:47 +0800 Subject: [PATCH 4/6] update: change the api to /version --- api/internal/handler/tool/tool.go | 4 ++-- api/internal/handler/tool/tool_test.go | 2 +- api/test/e2e/info_test.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/api/internal/handler/tool/tool.go b/api/internal/handler/tool/tool.go index 0d90e096b3..def441b1ff 100644 --- a/api/internal/handler/tool/tool.go +++ b/api/internal/handler/tool/tool.go @@ -38,10 +38,10 @@ func NewHandler() (handler.RouteRegister, error) { } func (h *Handler) ApplyRoute(r *gin.Engine) { - r.GET("/info", wgin.Wraps(h.Info)) + r.GET("/version", wgin.Wraps(h.Version)) } -func (h *Handler) Info(c droplet.Context) (interface{}, error) { +func (h *Handler) Version(_ droplet.Context) (interface{}, error) { hash, version := utils.GetHashAndVersion() return &InfoOutput{ Hash: hash, diff --git a/api/internal/handler/tool/tool_test.go b/api/internal/handler/tool/tool_test.go index dc1cf0a1bd..9754cca4b7 100644 --- a/api/internal/handler/tool/tool_test.go +++ b/api/internal/handler/tool/tool_test.go @@ -31,7 +31,7 @@ func TestInfo_Get(t *testing.T) { hash, version := utils.GetHashAndVersion() - ret, err := h.Info(ctx) + ret, err := h.Version(ctx) assert.Nil(t, err) assert.Equal(t, &InfoOutput{ Hash: hash, diff --git a/api/test/e2e/info_test.go b/api/test/e2e/info_test.go index a3bd887f88..7139c945c5 100644 --- a/api/test/e2e/info_test.go +++ b/api/test/e2e/info_test.go @@ -27,7 +27,7 @@ func TestInfo(t *testing.T) { Desc: "get info", Object: ManagerApiExpect(t), Method: http.MethodGet, - Path: "/info", + Path: "/version", ExpectStatus: http.StatusOK, ExpectBody: []string{"commit_hash", "\"version\":\"master\""}, }, From b4b2d8f504ab3d27f584110342af59a3667c7100 Mon Sep 17 00:00:00 2001 From: starsz Date: Wed, 3 Feb 2021 22:25:34 +0800 Subject: [PATCH 5/6] update: dockerfile --- api/test/docker/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/test/docker/Dockerfile b/api/test/docker/Dockerfile index 6374393852..fdfa880e37 100644 --- a/api/test/docker/Dockerfile +++ b/api/test/docker/Dockerfile @@ -23,7 +23,7 @@ COPY ./ ./ RUN mkdir -p /go/manager-api/conf \ && mkdir -p /go/manager-api/build-tools \ - && go test -c -cover -covermode=atomic -o /go/manager-api/manager-api -coverpkg "./..." ./cmd/manager \ + && go test -c -cover -covermode=atomic -ldflags "-X internal/utils.version=master -X internal/utils.gitHash=hash_test" -o /go/manager-api/manager-api -coverpkg "./..." ./cmd/manager \ && mv /go/src/github.com/apisix/manager-api/entry.sh /go/manager-api/ \ && mv /go/src/github.com/apisix/manager-api/build-tools/* /go/manager-api/build-tools/ \ && mv /go/src/github.com/apisix/manager-api/conf/conf.yaml /go/manager-api/conf/conf.yaml \ From 5f3b9d1773aff96c16e8c94685a58ab08e1c0374 Mon Sep 17 00:00:00 2001 From: starsz Date: Thu, 4 Feb 2021 09:25:22 +0800 Subject: [PATCH 6/6] update: ci --- api/test/docker/Dockerfile | 2 +- api/test/e2e/{info_test.go => version_test.go} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename api/test/e2e/{info_test.go => version_test.go} (94%) diff --git a/api/test/docker/Dockerfile b/api/test/docker/Dockerfile index fdfa880e37..6374393852 100644 --- a/api/test/docker/Dockerfile +++ b/api/test/docker/Dockerfile @@ -23,7 +23,7 @@ COPY ./ ./ RUN mkdir -p /go/manager-api/conf \ && mkdir -p /go/manager-api/build-tools \ - && go test -c -cover -covermode=atomic -ldflags "-X internal/utils.version=master -X internal/utils.gitHash=hash_test" -o /go/manager-api/manager-api -coverpkg "./..." ./cmd/manager \ + && go test -c -cover -covermode=atomic -o /go/manager-api/manager-api -coverpkg "./..." ./cmd/manager \ && mv /go/src/github.com/apisix/manager-api/entry.sh /go/manager-api/ \ && mv /go/src/github.com/apisix/manager-api/build-tools/* /go/manager-api/build-tools/ \ && mv /go/src/github.com/apisix/manager-api/conf/conf.yaml /go/manager-api/conf/conf.yaml \ diff --git a/api/test/e2e/info_test.go b/api/test/e2e/version_test.go similarity index 94% rename from api/test/e2e/info_test.go rename to api/test/e2e/version_test.go index 7139c945c5..f486e3ddfd 100644 --- a/api/test/e2e/info_test.go +++ b/api/test/e2e/version_test.go @@ -29,7 +29,7 @@ func TestInfo(t *testing.T) { Method: http.MethodGet, Path: "/version", ExpectStatus: http.StatusOK, - ExpectBody: []string{"commit_hash", "\"version\":\"master\""}, + ExpectBody: []string{"commit_hash", "\"version\""}, }, }