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: remove version match on api #2038

Merged
merged 10 commits into from
Aug 6, 2021
66 changes: 1 addition & 65 deletions api/internal/handler/tool/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,28 @@
package tool

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/shiningrush/droplet"
"github.com/shiningrush/droplet/data"
wgin "github.com/shiningrush/droplet/wrapper/gin"

"github.com/apisix/manager-api/internal/core/entity"
"github.com/apisix/manager-api/internal/core/store"
"github.com/apisix/manager-api/internal/handler"
"github.com/apisix/manager-api/internal/utils"
)

type Handler struct {
serverInfoStore store.Interface
}

type InfoOutput struct {
Hash string `json:"commit_hash"`
Version string `json:"version"`
}

type nodes struct {
Hostname string `json:"hostname"`
Version string `json:"version"`
}

type VersionMatchOutput struct {
Matched bool `json:"matched"`
DashboardVersion string `json:"dashboard_version"`
MismatchedNodes []nodes `json:"mismatched_nodes"`
}

func NewHandler() (handler.RouteRegister, error) {
return &Handler{
serverInfoStore: store.GetStore(store.HubKeyServerInfo),
}, nil
return &Handler{}, nil
}

func (h *Handler) ApplyRoute(r *gin.Engine) {
r.GET("/apisix/admin/tool/version", wgin.Wraps(h.Version))
r.GET("/apisix/admin/tool/version_match", wgin.Wraps(h.VersionMatch))
}

func (h *Handler) Version(_ droplet.Context) (interface{}, error) {
Expand All @@ -69,46 +48,3 @@ func (h *Handler) Version(_ droplet.Context) (interface{}, error) {
Version: version,
}, nil
}

func (h *Handler) VersionMatch(c droplet.Context) (interface{}, error) {
_, version := utils.GetHashAndVersion()
var output VersionMatchOutput
output.DashboardVersion = version

matchedVersion := utils.GetMatchedVersion(version)

var mismatchedNodes = make([]nodes, 0)
_, err := h.serverInfoStore.List(c.Context(), store.ListInput{
Predicate: func(obj interface{}) bool {
serverInfo := obj.(*entity.ServerInfo)

if serverInfo.Version != matchedVersion {
mismatchedNodes = append(mismatchedNodes, nodes{
Hostname: serverInfo.Hostname,
Version: serverInfo.Version,
})
}
return false
},
})

if err != nil {
return nil, err
}

output.MismatchedNodes = mismatchedNodes
if len(output.MismatchedNodes) == 0 {
output.Matched = true
} else {
// TODO: move this to utils
return &data.SpecCodeResponse{StatusCode: http.StatusOK, Response: data.Response{
Data: &output,
Code: 2000001,
Message: fmt.Sprintf("The Manager API and Apache APISIX are mismatched. "+
"The version of Manager API is %s, and should be used with Apache APISIX %s.",
version, matchedVersion),
}}, nil
}

return &output, nil
}
116 changes: 0 additions & 116 deletions api/internal/handler/tool/tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@
package tool

import (
"net/http"
"testing"

"github.com/shiningrush/droplet"
"github.com/shiningrush/droplet/data"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/apisix/manager-api/internal/core/entity"
"github.com/apisix/manager-api/internal/core/store"
"github.com/apisix/manager-api/internal/utils"
)

Expand All @@ -43,114 +38,3 @@ func TestTool_Version(t *testing.T) {
Version: version,
}, ret)
}

func TestTool_VersionMatch(t *testing.T) {
var (
tests = []struct {
caseDesc string
giveData []interface{}
giveErr error
wantErr error
wantRet interface{}
}{
{
caseDesc: "version matched",
giveData: []interface{}{
&entity.ServerInfo{
BaseInfo: entity.BaseInfo{ID: "server_1"},
UpTime: 10,
LastReportTime: 1608195454,
BootTime: 1608195454,
Hostname: "gentoo",
Version: "",
},
&entity.ServerInfo{
BaseInfo: entity.BaseInfo{ID: "server_2"},
UpTime: 10,
LastReportTime: 1608195454,
BootTime: 1608195454,
Hostname: "ubuntu",
Version: "",
},
},
wantRet: &VersionMatchOutput{
Matched: true,
DashboardVersion: "",
MismatchedNodes: make([]nodes, 0),
},
},
{
caseDesc: "version not matched",
giveData: []interface{}{
&entity.ServerInfo{
BaseInfo: entity.BaseInfo{ID: "server_1"},
UpTime: 10,
LastReportTime: 1608195454,
BootTime: 1608195454,
Hostname: "gentoo",
Version: "2.2",
},
&entity.ServerInfo{
BaseInfo: entity.BaseInfo{ID: "server_2"},
UpTime: 10,
LastReportTime: 1608195454,
BootTime: 1608195454,
Hostname: "ubuntu",
Version: "2.2",
},
},
wantRet: &data.SpecCodeResponse{StatusCode: http.StatusOK, Response: data.Response{
Data: &VersionMatchOutput{
Matched: false,
DashboardVersion: "",
MismatchedNodes: []nodes{
{
Hostname: "gentoo",
Version: "2.2",
},
{
Hostname: "ubuntu",
Version: "2.2",
},
},
},
Code: 2000001,
Message: "The Manager API and Apache APISIX are mismatched. The version of Manager API is , and should be used with Apache APISIX .",
}},
},
}
)

for _, tc := range tests {
t.Run(tc.caseDesc, func(t *testing.T) {
getCalled := false
mStore := &store.MockInterface{}
mStore.On("List", mock.Anything).Run(func(args mock.Arguments) {
getCalled = true
}).Return(func(input store.ListInput) *store.ListOutput {
var res []interface{}
for _, c := range tc.giveData {
if input.Predicate(c) {
if input.Format != nil {
res = append(res, input.Format(c))
} else {
res = append(res, c)
}
}
}

return &store.ListOutput{
Rows: res,
TotalSize: len(res),
}
}, tc.giveErr)

h := Handler{serverInfoStore: mStore}
ctx := droplet.NewContext()
ret, err := h.VersionMatch(ctx)
assert.True(t, getCalled)
assert.Equal(t, tc.wantErr, err)
assert.Equal(t, tc.wantRet, ret)
})
}
}
29 changes: 0 additions & 29 deletions api/internal/utils/consts/versionMap.go

This file was deleted.

10 changes: 0 additions & 10 deletions api/internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import (

"github.com/sony/sonyflake"
"github.com/yuin/gopher-lua/parse"

"github.com/apisix/manager-api/internal/utils/consts"
)

var _sf *sonyflake.Sonyflake
Expand Down Expand Up @@ -208,11 +206,3 @@ func ValueEqual(a interface{}, b interface{}) bool {
}
return bytes.Equal(aBytes, bBytes)
}

func GetMatchedVersion(dashboardVersion string) string {
if apisixVersion, exist := consts.VersionMap[dashboardVersion]; exist {
return apisixVersion
}

return ""
}
11 changes: 0 additions & 11 deletions api/test/e2enew/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,5 @@ var _ = ginkgo.Describe("Version", func() {
ExpectStatus: http.StatusOK,
ExpectBody: []string{"commit_hash", "\"version\""},
}),
table.Entry("check version matched (not matched)", base.HttpTestCase{
Object: base.ManagerApiExpect(),
Method: http.MethodGet,
Path: "/apisix/admin/tool/version_match",
Headers: map[string]string{"Authorization": base.GetToken()},
ExpectStatus: http.StatusOK,
ExpectBody: []string{"\"code\":2000001",
`"message":"The Manager API and Apache APISIX are mismatched. ` +
`The version of Manager API is , and should be used with Apache APISIX ."`,
`"matched":false`, "apisix_server1", "apisix_server2"},
}),
)
})