Skip to content

Commit

Permalink
feat: implate API to get apisix instances status (apache#849)
Browse files Browse the repository at this point in the history
  • Loading branch information
starsz committed Dec 2, 2020
1 parent 4bb353d commit 746b855
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 8 deletions.
13 changes: 11 additions & 2 deletions api/internal/core/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (

type BaseInfo struct {
ID string `json:"id"`
CreateTime int64 `json:"create_time"`
UpdateTime int64 `json:"update_time"`
CreateTime int64 `json:"create_time,omitempty"`
UpdateTime int64 `json:"update_time,omitempty"`
}

func (info *BaseInfo) GetBaseInfo() *BaseInfo {
Expand Down Expand Up @@ -214,3 +214,12 @@ type Script struct {
ID string `json:"id"`
Script interface{} `json:"script,omitempty"`
}

type ServerInfo struct {
BaseInfo
LastReportTime int `json:"last_report_time,omitempty"`
UpTime int `json:"up_time,omitempty"`
EtcdVersion string `json:"etcd_version,omitempty"`
Hostname string `json:"hostname,omitempty"`
Version string `json:"version,omitempty"`
}
25 changes: 19 additions & 6 deletions api/internal/core/store/storehub.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import (
type HubKey string

const (
HubKeyConsumer HubKey = "consumer"
HubKeyRoute HubKey = "route"
HubKeyService HubKey = "service"
HubKeySsl HubKey = "ssl"
HubKeyUpstream HubKey = "upstream"
HubKeyScript HubKey = "script"
HubKeyConsumer HubKey = "consumer"
HubKeyRoute HubKey = "route"
HubKeyService HubKey = "service"
HubKeySsl HubKey = "ssl"
HubKeyUpstream HubKey = "upstream"
HubKeyScript HubKey = "script"
HubKeyServerInfo HubKey = `server_info`
)

var (
Expand Down Expand Up @@ -144,5 +145,17 @@ func InitStores() error {
return err
}

err = InitStore(HubKeyServerInfo, GenericStoreOption{
BasePath: "/apisix/data_plane/server_info",
ObjType: reflect.TypeOf(entity.ServerInfo{}),
KeyFunc: func(obj interface{}) string {
r := obj.(*entity.ServerInfo)
return r.ID
},
})
if err != nil {
return err
}

return nil
}
89 changes: 89 additions & 0 deletions api/internal/handler/server_info/server_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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 server_info

import (
"reflect"
"strings"

"github.com/gin-gonic/gin"
"github.com/shiningrush/droplet"
"github.com/shiningrush/droplet/wrapper"
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"
)

type Handler struct {
serverInfoStore store.Interface
}

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

func (h *Handler) ApplyRoute(r *gin.Engine) {
r.GET("/apisix/server_info/:id", wgin.Wraps(h.Get,
wrapper.InputType(reflect.TypeOf(GetInput{}))))
r.GET("/apisix/server_info", wgin.Wraps(h.List,
wrapper.InputType(reflect.TypeOf(ListInput{}))))
}

type GetInput struct {
ID string `auto_read:"id,path" validate:"required"`
}

func (h *Handler) Get(c droplet.Context) (interface{}, error) {
input := c.Input().(*GetInput)

r, err := h.serverInfoStore.Get(input.ID)
if err != nil {
return handler.SpecCodeResponse(err), err
}

return r, nil
}

type ListInput struct {
Hostname string `auto_read:"hostname,query"`
store.Pagination
}

func (h *Handler) List(c droplet.Context) (interface{}, error) {
input := c.Input().(*ListInput)

ret, err := h.serverInfoStore.List(store.ListInput{
Predicate: func(obj interface{}) bool {
if input.Hostname != "" {
return strings.Contains(obj.(*entity.ServerInfo).Hostname, input.Hostname)
}
return true
},
PageSize: input.PageSize,
PageNumber: input.PageNumber,
})

if err != nil {
return nil, err
}

return ret, nil
}
155 changes: 155 additions & 0 deletions api/internal/handler/server_info/server_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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 server_info

import (
"strings"
"testing"
"time"

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

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

const (
SleepTime = 100 * time.Millisecond
)

var serverInfoHandler *Handler

func init() {
err := storage.InitETCDClient([]string{"127.0.0.1:2379"})

if err != nil {
panic(err)
}
err = store.InitStores()
if err != nil {
panic(err)
}

serverInfoHandler = &Handler{
serverInfoStore: store.GetStore(store.HubKeyServerInfo),
}
}

func create(t *testing.T, ctx droplet.Context, info *entity.ServerInfo) {
err := serverInfoHandler.serverInfoStore.Create(ctx.Context(), info)
assert.Nil(t, err)
}

func delete(t *testing.T, ctx droplet.Context, ids []string) {
err := serverInfoHandler.serverInfoStore.BatchDelete(ctx.Context(), ids)
assert.Nil(t, err)
}

func testGet(t *testing.T, ctx droplet.Context, id string) {
input := &GetInput{ID: id}

ctx.SetInput(input)
res, err := serverInfoHandler.Get(ctx)
assert.Nil(t, err)

stored := res.(*entity.ServerInfo)
assert.Equal(t, stored.ID, input.ID)
}

func testList(t *testing.T, ctx droplet.Context, ids []string, hostname string) {
input := &ListInput{Hostname: hostname, Pagination: store.Pagination{PageSize: 10, PageNumber: 1}}

ctx.SetInput(input)
res, err := serverInfoHandler.List(ctx)
assert.Nil(t, err)

data := res.(*store.ListOutput)
assert.Equal(t, len(data.Rows), len(ids))

var find bool
for _, id := range ids {
find = false
for _, row := range data.Rows {
si := row.(*entity.ServerInfo)
if si.ID == id {

if hostname == "" {
find = true
break
}

if strings.Contains(si.Hostname, hostname) {
find = true
break
}
}
}

assert.Equal(t, find, true)
}
}

func TestServerInfo(t *testing.T) {
ctx := droplet.NewContext()

serverInfo := &entity.ServerInfo{
BaseInfo: entity.BaseInfo{ID: "1"},
UpTime: 10,
LastReportTime: 1606892686,
EtcdVersion: "3.5.0",
Hostname: "gentoo",
Version: "2.0",
}

create(t, ctx, serverInfo)
time.Sleep(SleepTime)
testGet(t, ctx, "1")
testList(t, ctx, []string{"1"}, "")
delete(t, ctx, []string{"1"})
}

func TestServerInfo_Pass_Host(t *testing.T) {
ctx := droplet.NewContext()

serverInfo := &entity.ServerInfo{
BaseInfo: entity.BaseInfo{ID: "4"},
UpTime: 10,
LastReportTime: 1606892686,
EtcdVersion: "3.5.0",
Hostname: "gentoo",
Version: "2.0",
}

create(t, ctx, serverInfo)

serverInfo.ID = "5"
serverInfo.Hostname = "gentoo2"

create(t, ctx, serverInfo)

serverInfo.ID = "6"
serverInfo.Hostname = "ubuntu"

create(t, ctx, serverInfo)
time.Sleep(SleepTime)

testList(t, ctx, []string{"4", "5"}, "gentoo")
delete(t, ctx, []string{"4", "5", "6"})
}
3 changes: 3 additions & 0 deletions api/internal/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package internal

import (
"fmt"

"github.com/gin-contrib/pprof"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
Expand All @@ -32,6 +33,7 @@ import (
"github.com/apisix/manager-api/internal/handler/healthz"
"github.com/apisix/manager-api/internal/handler/plugin"
"github.com/apisix/manager-api/internal/handler/route"
"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/upstream"
Expand Down Expand Up @@ -61,6 +63,7 @@ func SetUpRouter() *gin.Engine {
plugin.NewHandler,
healthz.NewHandler,
authentication.NewHandler,
server_info.NewHandler,
}

for i := range factories {
Expand Down

0 comments on commit 746b855

Please sign in to comment.