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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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
})

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
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
50 changes: 50 additions & 0 deletions api/internal/handler/tool/tool.go
Original file line number Diff line number Diff line change
@@ -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))
Copy link
Member

@juzhiyuan juzhiyuan Feb 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@starsz @nic-chen

This will lead to 2 kinds of APIs:

  1. /apisix/admin/xxx
  2. /version

When we deprecate admin-api in V3, we will have to omit that prefix apisix/admin either, to have ManagerAPIs.

Now FE meets one issue: because HTTP call is handled by UmiJS, which only supports one prefix, /apisix/admin or /, then FE has to make hack codes to be compatible with those 2 kinds of APIs.

I know it's wired to have something like /apisix/admin/version, but could we add a prefix for this API first? with some description about this hacking case. In V3, we may omit that prefix from all APIs.

cc @LiteSun

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.Got it.
I will add the prefix and ignore this API's auth checking in the next PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we need to omit the prefix, but unification is appropriate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!!!

}

func (h *Handler) Version(_ 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
}
40 changes: 40 additions & 0 deletions api/internal/handler/tool/tool_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 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)
}
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
27 changes: 27 additions & 0 deletions api/internal/utils/version.go
Original file line number Diff line number Diff line change
@@ -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
starsz marked this conversation as resolved.
Show resolved Hide resolved
version string
)

// get the hash and version
func GetHashAndVersion() (string, string) {
return gitHash, version
}
39 changes: 39 additions & 0 deletions api/test/e2e/version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
2 changes: 1 addition & 1 deletion api/test/shell/cli_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down