Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support return the manager api's git hash and version #1408

Merged
merged 10 commits into from
Feb 4, 2021
5 changes: 4 additions & 1 deletion api/cmd/managerapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
49 changes: 49 additions & 0 deletions api/internal/handler/tool/tool.go
Original file line number Diff line number Diff line change
@@ -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"
nic-chen marked this conversation as resolved.
Show resolved Hide resolved
"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))
juzhiyuan marked this conversation as resolved.
Show resolved Hide resolved
}

func (h *Handler) Info(c droplet.Context) (interface{}, error) {
hash, version := utils.GetHashAndVersion()
return &InfoOutput{
Hash: hash,
Version: version,
juzhiyuan marked this conversation as resolved.
Show resolved Hide resolved
}, nil
}
38 changes: 38 additions & 0 deletions api/internal/handler/tool/tool_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 2 additions & 0 deletions api/internal/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -76,6 +77,7 @@ func SetUpRouter() *gin.Engine {
label.NewHandler,
data_loader.NewHandler,
data_loader.NewImportHandler,
tool.NewHandler,
}

for i := range factories {
Expand Down
16 changes: 16 additions & 0 deletions api/internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
starsz marked this conversation as resolved.
Show resolved Hide resolved
GitHash = hash
Version = version
}

// get the hash and version
func GetHashAndVersion() (string, string) {
return GitHash, Version
}
40 changes: 40 additions & 0 deletions api/test/e2e/info_test.go
Original file line number Diff line number Diff line change
@@ -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\""},
juzhiyuan marked this conversation as resolved.
Show resolved Hide resolved
},
}

for _, tc := range tests {
testCaseCheck(tc, t)
}
}