Skip to content

Commit

Permalink
feat: support plugin config (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
nic-chen authored Mar 3, 2021
1 parent 845cf99 commit 2de2c72
Show file tree
Hide file tree
Showing 11 changed files with 1,420 additions and 25 deletions.
14 changes: 14 additions & 0 deletions api/conf/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@
"type": "integer"
}]
},
"labels": {
"description": "key/value pairs to specify attributes",
"maxProperties": 16,
"patternProperties": {
".*": {
"description": "value of label",
"maxLength": 64,
"minLength": 1,
"pattern": "^\\S+$",
"type": "string"
}
},
"type": "object"
},
"plugins": {
"type": "object"
},
Expand Down
9 changes: 9 additions & 0 deletions api/internal/core/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type Route struct {
Script interface{} `json:"script,omitempty"`
ScriptID interface{} `json:"script_id,omitempty"` // For debug and optimization(cache), currently same as Route's ID
Plugins map[string]interface{} `json:"plugins,omitempty"`
PluginConfigID interface{} `json:"plugin_config_id,omitempty"`
Upstream *UpstreamDef `json:"upstream,omitempty"`
ServiceID interface{} `json:"service_id,omitempty"`
UpstreamID interface{} `json:"upstream_id,omitempty"`
Expand Down Expand Up @@ -257,3 +258,11 @@ type ServerInfo struct {
Hostname string `json:"hostname,omitempty"`
Version string `json:"version,omitempty"`
}

// swagger:model GlobalPlugins
type PluginConfig struct {
BaseInfo
Desc string `json:"desc,omitempty" validate:"max=256"`
Plugins map[string]interface{} `json:"plugins"`
Labels map[string]string `json:"labels,omitempty"`
}
29 changes: 21 additions & 8 deletions api/internal/core/store/storehub.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ import (
type HubKey string

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

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

err = InitStore(HubKeyPluginConfig, GenericStoreOption{
BasePath: "/apisix/plugin_configs",
ObjType: reflect.TypeOf(entity.PluginConfig{}),
KeyFunc: func(obj interface{}) string {
r := obj.(*entity.PluginConfig)
return utils.InterfaceToString(r.ID)
},
})
if err != nil {
return err
}

return nil
}
28 changes: 17 additions & 11 deletions api/internal/handler/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ import (
)

type Handler struct {
routeStore store.Interface
serviceStore store.Interface
upstreamStore store.Interface
sslStore store.Interface
consumerStore store.Interface
routeStore store.Interface
serviceStore store.Interface
upstreamStore store.Interface
sslStore store.Interface
consumerStore store.Interface
pluginConfigStore store.Interface
}

var _ json.Marshaler = Pair{}
Expand All @@ -59,11 +60,12 @@ func (p Pair) MarshalJSON() ([]byte, error) {

func NewHandler() (handler.RouteRegister, error) {
return &Handler{
routeStore: store.GetStore(store.HubKeyRoute),
serviceStore: store.GetStore(store.HubKeyService),
upstreamStore: store.GetStore(store.HubKeyUpstream),
sslStore: store.GetStore(store.HubKeySsl),
consumerStore: store.GetStore(store.HubKeyConsumer),
routeStore: store.GetStore(store.HubKeyRoute),
serviceStore: store.GetStore(store.HubKeyService),
upstreamStore: store.GetStore(store.HubKeyUpstream),
sslStore: store.GetStore(store.HubKeySsl),
consumerStore: store.GetStore(store.HubKeyConsumer),
pluginConfigStore: store.GetStore(store.HubKeyPluginConfig),
}, nil
}

Expand Down Expand Up @@ -154,9 +156,11 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) {
items = append(items, h.sslStore)
case "upstream":
items = append(items, h.upstreamStore)
case "plugin_config":
items = append(items, h.pluginConfigStore)
case "all":
items = append(items, h.routeStore, h.serviceStore, h.upstreamStore,
h.sslStore, h.consumerStore)
h.sslStore, h.consumerStore, h.pluginConfigStore)
}

predicate := func(obj interface{}) bool {
Expand All @@ -173,6 +177,8 @@ func (h *Handler) List(c droplet.Context) (interface{}, error) {
ls = obj.Labels
case *entity.Upstream:
ls = obj.Labels
case *entity.PluginConfig:
ls = obj.Labels
default:
return false
}
Expand Down
32 changes: 26 additions & 6 deletions api/internal/handler/label/label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ func genConsumer(labels map[string]string) *entity.Consumer {
return &r
}

func genPluginConfig(labels map[string]string) *entity.PluginConfig {
r := entity.PluginConfig{
BaseInfo: entity.BaseInfo{
ID: rand.Int(),
CreateTime: rand.Int63(),
},
Labels: labels,
}

return &r
}

func TestLabel(t *testing.T) {
m1 := map[string]string{
"label1": "value1",
Expand All @@ -189,7 +201,7 @@ func TestLabel(t *testing.T) {
}

// TODO: Test SSL after the ssl config bug fixed
types := []string{"route", "service", "upstream", "consumer"}
types := []string{"route", "service", "upstream", "consumer", "plugin_config"}

var giveData []interface{}
for _, typ := range types {
Expand Down Expand Up @@ -219,6 +231,11 @@ func TestLabel(t *testing.T) {
genConsumer(m1),
genConsumer(m2),
}
case "plugin_config":
giveData = []interface{}{
genPluginConfig(m1),
genPluginConfig(m2),
}
}

var testCases []*testCase
Expand Down Expand Up @@ -271,6 +288,8 @@ func TestLabel(t *testing.T) {
handler.upstreamStore = genMockStore(t, tc.giveData)
case "consumer":
handler.consumerStore = genMockStore(t, tc.giveData)
case "plugin_config":
handler.pluginConfigStore = genMockStore(t, tc.giveData)
}

ctx := droplet.NewContext()
Expand All @@ -296,11 +315,12 @@ func TestLabel(t *testing.T) {
}

handler := Handler{
routeStore: genMockStore(t, []interface{}{genRoute(m1)}),
sslStore: genMockStore(t, []interface{}{genSSL(m2)}),
upstreamStore: genMockStore(t, []interface{}{genUpstream(m3)}),
consumerStore: genMockStore(t, []interface{}{genConsumer(m4)}),
serviceStore: genMockStore(t, []interface{}{genService(m5)}),
routeStore: genMockStore(t, []interface{}{genRoute(m1)}),
sslStore: genMockStore(t, []interface{}{genSSL(m2)}),
upstreamStore: genMockStore(t, []interface{}{genUpstream(m3)}),
consumerStore: genMockStore(t, []interface{}{genConsumer(m4)}),
serviceStore: genMockStore(t, []interface{}{genService(m5)}),
pluginConfigStore: genMockStore(t, []interface{}{genPluginConfig(m5)}),
}

var testCases []*testCase
Expand Down
Loading

0 comments on commit 2de2c72

Please sign in to comment.