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/04 12:06:05 UTC

[apisix-dashboard] branch master updated: feat: support return the manager api's git hash and version (#1408)

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


The following commit(s) were added to refs/heads/master by this push:
     new 597b046  feat: support return the manager api's git hash and version (#1408)
597b046 is described below

commit 597b046199e1820ac6bcde281c315705b4237483
Author: Peter Zhu <st...@gmail.com>
AuthorDate: Thu Feb 4 20:05:59 2021 +0800

    feat: support return the manager api's git hash and version (#1408)
---
 api/build.sh                           |  2 +-
 api/cmd/managerapi.go                  |  5 +++-
 api/internal/handler/tool/tool.go      | 50 ++++++++++++++++++++++++++++++++++
 api/internal/handler/tool/tool_test.go | 40 +++++++++++++++++++++++++++
 api/internal/route.go                  |  2 ++
 api/internal/utils/version.go          | 27 ++++++++++++++++++
 api/test/e2e/version_test.go           | 39 ++++++++++++++++++++++++++
 api/test/shell/cli_test.sh             |  2 +-
 8 files changed, 164 insertions(+), 3 deletions(-)

diff --git a/api/build.sh b/api/build.sh
index 70ed743..6cd249a 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 d0dc1d0..419dc87 100644
--- a/api/cmd/managerapi.go
+++ b/api/cmd/managerapi.go
@@ -73,6 +73,8 @@ func NewManagerAPICommand() *cobra.Command {
 				return nil
 			})
 
+			GitHash, Version = utils.GetHashAndVersion()
+
 			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 0000000..def441b
--- /dev/null
+++ b/api/internal/handler/tool/tool.go
@@ -0,0 +1,50 @@
+/*
+ * 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/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 {
+}
+
+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("/version", wgin.Wraps(h.Version))
+}
+
+func (h *Handler) Version(_ 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 0000000..9754cca
--- /dev/null
+++ b/api/internal/handler/tool/tool_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 tool
+
+import (
+	"testing"
+
+	"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()
+
+	hash, version := utils.GetHashAndVersion()
+
+	ret, err := h.Version(ctx)
+	assert.Nil(t, err)
+	assert.Equal(t, &InfoOutput{
+		Hash:    hash,
+		Version: version,
+	}, ret)
+}
diff --git a/api/internal/route.go b/api/internal/route.go
index ac78a36..26fe1a4 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/version.go b/api/internal/utils/version.go
new file mode 100644
index 0000000..d4bec4a
--- /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
+}
diff --git a/api/test/e2e/version_test.go b/api/test/e2e/version_test.go
new file mode 100644
index 0000000..f486e3d
--- /dev/null
+++ b/api/test/e2e/version_test.go
@@ -0,0 +1,39 @@
+/*
+ * 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:         "/version",
+			ExpectStatus: http.StatusOK,
+			ExpectBody:   []string{"commit_hash", "\"version\""},
+		},
+	}
+
+	for _, tc := range tests {
+		testCaseCheck(tc, t)
+	}
+}
diff --git a/api/test/shell/cli_test.sh b/api/test/shell/cli_test.sh
index c272c36..5ec96cc 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