diff --git a/api/internal/handler/tool/tool.go b/api/internal/handler/tool/tool.go index e7bf4d665b..c3f9e996f1 100644 --- a/api/internal/handler/tool/tool.go +++ b/api/internal/handler/tool/tool.go @@ -17,22 +17,15 @@ 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 { @@ -40,26 +33,12 @@ type InfoOutput struct { 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) { @@ -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 -} diff --git a/api/internal/handler/tool/tool_test.go b/api/internal/handler/tool/tool_test.go index e1ed54d7f0..304249fd36 100644 --- a/api/internal/handler/tool/tool_test.go +++ b/api/internal/handler/tool/tool_test.go @@ -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" ) @@ -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) - }) - } -} diff --git a/api/internal/utils/consts/versionMap.go b/api/internal/utils/consts/versionMap.go deleted file mode 100644 index d2448feec0..0000000000 --- a/api/internal/utils/consts/versionMap.go +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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 consts - -/* - Key is dashboard version - Val is apisix version -*/ -var VersionMap = map[string]string{ - "2.3": "2.2", - "2.4": "2.3", - "2.5": "2.4", - "2.6": "2.5", - "2.7": "2.6", -} diff --git a/api/internal/utils/utils.go b/api/internal/utils/utils.go index 16cdc50419..4d8150b9a4 100644 --- a/api/internal/utils/utils.go +++ b/api/internal/utils/utils.go @@ -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 @@ -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 "" -} diff --git a/api/test/e2enew/version/version_test.go b/api/test/e2enew/version/version_test.go index 13794e6f84..931284026e 100644 --- a/api/test/e2enew/version/version_test.go +++ b/api/test/e2enew/version/version_test.go @@ -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"}, - }), ) })