Skip to content

Commit

Permalink
feat: implement pluginconfig clients (apache#638) (apache#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
neverCase authored and Sindweller committed Dec 18, 2021
1 parent 4ee13e8 commit 7c0073d
Show file tree
Hide file tree
Showing 14 changed files with 682 additions and 30 deletions.
4 changes: 4 additions & 0 deletions pkg/api/validation/apisix_route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (c fakeSchemaClient) GetSslSchema(_ context.Context) (*api.Schema, error) {
return nil, nil
}

func (c fakeSchemaClient) GetPluginConfigSchema(_ context.Context) (*api.Schema, error) {
return nil, nil
}

func newFakeSchemaClient() apisix.Schema {
testData := map[string]string{
"api-breaker": `{"required":["break_response_code"],"$comment":"this is a mark for our injected plugin schema","type":"object","properties":{"healthy":{"properties":{"successes":{"minimum":1,"type":"integer","default":3},"http_statuses":{"items":{"minimum":200,"type":"integer","maximum":499},"uniqueItems":true,"type":"array","minItems":1,"default":[200]}},"type":"object","default":{"successes":3,"http_statuses":[200]}},"break_response_code":{"minimum":200,"type":"integer","maximum":599},"max_breaker_sec":{"minimum":3,"type":"integer","default":300},"unhealthy":{"properties":{"failures":{"minimum":1,"type":"integer","default":3},"http_statuses":{"items":{"minimum":500,"type":"integer","maximum":599},"uniqueItems":true,"type":"array","minItems":1,"default":[500]}},"type":"object","default":{"failures":3,"http_statuses":[500]}},"disable":{"type":"boolean"}}}`,
Expand Down
11 changes: 11 additions & 0 deletions pkg/apisix/apisix.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ type Schema interface {
GetUpstreamSchema(context.Context) (*v1.Schema, error)
GetConsumerSchema(context.Context) (*v1.Schema, error)
GetSslSchema(context.Context) (*v1.Schema, error)
GetPluginConfigSchema(ctx context.Context) (*v1.Schema, error)
}

// PluginConfig is the specific client interface to take over the create, update,
// list and delete for APISIX PluginConfig resource.
type PluginConfig interface {
Get(context.Context, string) (*v1.PluginConfig, error)
List(context.Context) ([]*v1.PluginConfig, error)
Create(context.Context, *v1.PluginConfig) (*v1.PluginConfig, error)
Delete(context.Context, *v1.PluginConfig) error
Update(context.Context, *v1.PluginConfig) (*v1.PluginConfig, error)
}

type apisix struct {
Expand Down
8 changes: 8 additions & 0 deletions pkg/apisix/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Cache interface {
InsertConsumer(*v1.Consumer) error
// InsertSchema adds or updates schema to cache.
InsertSchema(*v1.Schema) error
// InsertPluginConfig adds or updates plugin_config to cache.
InsertPluginConfig(*v1.PluginConfig) error

// GetRoute finds the route from cache according to the primary index (id).
GetRoute(string) (*v1.Route, error)
Expand All @@ -52,6 +54,8 @@ type Cache interface {
GetConsumer(string) (*v1.Consumer, error)
// GetSchema finds the scheme from cache according to the primary index (id).
GetSchema(string) (*v1.Schema, error)
// GetPluginConfig finds the plugin_config from cache according to the primary index (id).
GetPluginConfig(string) (*v1.PluginConfig, error)

// ListRoutes lists all routes in cache.
ListRoutes() ([]*v1.Route, error)
Expand All @@ -67,6 +71,8 @@ type Cache interface {
ListConsumers() ([]*v1.Consumer, error)
// ListSchema lists all schema in cache.
ListSchema() ([]*v1.Schema, error)
// ListPluginConfig lists all plugin_config in cache.
ListPluginConfigs() ([]*v1.PluginConfig, error)

// DeleteRoute deletes the specified route in cache.
DeleteRoute(*v1.Route) error
Expand All @@ -82,4 +88,6 @@ type Cache interface {
DeleteConsumer(*v1.Consumer) error
// DeleteSchema deletes the specified schema in cache.
DeleteSchema(*v1.Schema) error
// DeletePluginConfig deletes the specified plugin_config in cache.
DeletePluginConfig(*v1.PluginConfig) error
}
28 changes: 28 additions & 0 deletions pkg/apisix/cache/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func (c *dbCache) InsertSchema(schema *v1.Schema) error {
return c.insert("schema", schema.DeepCopy())
}

func (c *dbCache) InsertPluginConfig(pc *v1.PluginConfig) error {
return c.insert("plugin_config", pc.DeepCopy())
}

func (c *dbCache) insert(table string, obj interface{}) error {
txn := c.db.Txn(true)
defer txn.Abort()
Expand Down Expand Up @@ -140,6 +144,14 @@ func (c *dbCache) GetSchema(name string) (*v1.Schema, error) {
return obj.(*v1.Schema).DeepCopy(), nil
}

func (c *dbCache) GetPluginConfig(name string) (*v1.PluginConfig, error) {
obj, err := c.get("plugin_config", name)
if err != nil {
return nil, err
}
return obj.(*v1.PluginConfig).DeepCopy(), nil
}

func (c *dbCache) get(table, id string) (interface{}, error) {
txn := c.db.Txn(false)
defer txn.Abort()
Expand Down Expand Up @@ -240,6 +252,18 @@ func (c *dbCache) ListSchema() ([]*v1.Schema, error) {
return schemaList, nil
}

func (c *dbCache) ListPluginConfigs() ([]*v1.PluginConfig, error) {
raws, err := c.list("plugin_config")
if err != nil {
return nil, err
}
pluginConfigs := make([]*v1.PluginConfig, 0, len(raws))
for _, raw := range raws {
pluginConfigs = append(pluginConfigs, raw.(*v1.PluginConfig).DeepCopy())
}
return pluginConfigs, nil
}

func (c *dbCache) list(table string) ([]interface{}, error) {
txn := c.db.Txn(false)
defer txn.Abort()
Expand Down Expand Up @@ -285,6 +309,10 @@ func (c *dbCache) DeleteSchema(schema *v1.Schema) error {
return c.delete("schema", schema)
}

func (c *dbCache) DeletePluginConfig(pc *v1.PluginConfig) error {
return c.delete("plugin_config", pc)
}

func (c *dbCache) delete(table string, obj interface{}) error {
txn := c.db.Txn(true)
defer txn.Abort()
Expand Down
55 changes: 55 additions & 0 deletions pkg/apisix/cache/memdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,58 @@ func TestMemDBCacheSchema(t *testing.T) {
}
assert.Error(t, ErrNotFound, c.DeleteSchema(s4))
}

func TestMemDBCachePluginConfig(t *testing.T) {
c, err := NewMemDBCache()
assert.Nil(t, err, "NewMemDBCache")

pc1 := &v1.PluginConfig{
Metadata: v1.Metadata{
ID: "1",
Name: "name1",
},
}
assert.Nil(t, c.InsertPluginConfig(pc1), "inserting plugin_config pc1")

pc11, err := c.GetPluginConfig("1")
assert.Nil(t, err)
assert.Equal(t, pc1, pc11)

pc2 := &v1.PluginConfig{
Metadata: v1.Metadata{
ID: "2",
Name: "name2",
},
}
pc3 := &v1.PluginConfig{
Metadata: v1.Metadata{
ID: "3",
Name: "name3",
},
}
assert.Nil(t, c.InsertPluginConfig(pc2), "inserting plugin_config pc2")
assert.Nil(t, c.InsertPluginConfig(pc3), "inserting plugin_config pc3")

pc22, err := c.GetPluginConfig("2")
assert.Nil(t, err)
assert.Equal(t, pc2, pc22)

assert.Nil(t, c.DeletePluginConfig(pc3), "delete plugin_config pc3")

pcList, err := c.ListPluginConfigs()
assert.Nil(t, err, "listing plugin_config")

if pcList[0].Name > pcList[1].Name {
pcList[0], pcList[1] = pcList[1], pcList[0]
}
assert.Equal(t, pcList[0], pc1)
assert.Equal(t, pcList[1], pc2)

pc4 := &v1.PluginConfig{
Metadata: v1.Metadata{
ID: "4",
Name: "name4",
},
}
assert.Error(t, ErrNotFound, c.DeletePluginConfig(pc4))
}
16 changes: 16 additions & 0 deletions pkg/apisix/cache/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,22 @@ var (
},
},
},
"plugin_config": {
Name: "plugin_config",
Indexes: map[string]*memdb.IndexSchema{
"id": {
Name: "id",
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "ID"},
},
"name": {
Name: "name",
Unique: true,
Indexer: &memdb.StringFieldIndex{Field: "Name"},
AllowMissing: true,
},
},
},
},
}
)
2 changes: 2 additions & 0 deletions pkg/apisix/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type cluster struct {
consumer Consumer
plugin Plugin
schema Schema
pluginConfig PluginConfig
metricsCollector metrics.Collector
}

Expand Down Expand Up @@ -140,6 +141,7 @@ func newCluster(ctx context.Context, o *ClusterOptions) (Cluster, error) {
c.consumer = newConsumerClient(c)
c.plugin = newPluginClient(c)
c.schema = newSchemaClient(c)
c.pluginConfig = newPluginConfigClient(c)

c.cache, err = cache.NewMemDBCache()
if err != nil {
Expand Down
94 changes: 64 additions & 30 deletions pkg/apisix/nonexistentclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,29 @@ type nonExistentCluster struct {
func newNonExistentCluster() *nonExistentCluster {
return &nonExistentCluster{
embedDummyResourceImplementer{
route: &dummyRoute{},
ssl: &dummySSL{},
upstream: &dummyUpstream{},
streamRoute: &dummyStreamRoute{},
globalRule: &dummyGlobalRule{},
consumer: &dummyConsumer{},
plugin: &dummyPlugin{},
schema: &dummySchema{},
route: &dummyRoute{},
ssl: &dummySSL{},
upstream: &dummyUpstream{},
streamRoute: &dummyStreamRoute{},
globalRule: &dummyGlobalRule{},
consumer: &dummyConsumer{},
plugin: &dummyPlugin{},
schema: &dummySchema{},
pluginConfig: &dummyPluginConfig{},
},
}
}

type embedDummyResourceImplementer struct {
route Route
ssl SSL
upstream Upstream
streamRoute StreamRoute
globalRule GlobalRule
consumer Consumer
plugin Plugin
schema Schema
route Route
ssl SSL
upstream Upstream
streamRoute StreamRoute
globalRule GlobalRule
consumer Consumer
plugin Plugin
schema Schema
pluginConfig PluginConfig
}

type dummyRoute struct{}
Expand Down Expand Up @@ -212,6 +214,32 @@ func (f *dummySchema) GetSslSchema(_ context.Context) (*v1.Schema, error) {
return nil, ErrClusterNotExist
}

func (f *dummySchema) GetPluginConfigSchema(_ context.Context) (*v1.Schema, error) {
return nil, ErrClusterNotExist
}

type dummyPluginConfig struct{}

func (f *dummyPluginConfig) Get(_ context.Context, _ string) (*v1.PluginConfig, error) {
return nil, ErrClusterNotExist
}

func (f *dummyPluginConfig) List(_ context.Context) ([]*v1.PluginConfig, error) {
return nil, ErrClusterNotExist
}

func (f *dummyPluginConfig) Create(_ context.Context, _ *v1.PluginConfig) (*v1.PluginConfig, error) {
return nil, ErrClusterNotExist
}

func (f *dummyPluginConfig) Delete(_ context.Context, _ *v1.PluginConfig) error {
return ErrClusterNotExist
}

func (f *dummyPluginConfig) Update(_ context.Context, _ *v1.PluginConfig) (*v1.PluginConfig, error) {
return nil, ErrClusterNotExist
}

func (nc *nonExistentCluster) Route() Route {
return nc.route
}
Expand Down Expand Up @@ -267,24 +295,30 @@ func (c *dummyCache) InsertStreamRoute(_ *v1.StreamRoute) error { return
func (c *dummyCache) InsertGlobalRule(_ *v1.GlobalRule) error { return nil }
func (c *dummyCache) InsertConsumer(_ *v1.Consumer) error { return nil }
func (c *dummyCache) InsertSchema(_ *v1.Schema) error { return nil }
func (c *dummyCache) InsertPluginConfig(_ *v1.PluginConfig) error { return nil }
func (c *dummyCache) GetRoute(_ string) (*v1.Route, error) { return nil, cache.ErrNotFound }
func (c *dummyCache) GetSSL(_ string) (*v1.Ssl, error) { return nil, cache.ErrNotFound }
func (c *dummyCache) GetUpstream(_ string) (*v1.Upstream, error) { return nil, cache.ErrNotFound }
func (c *dummyCache) GetStreamRoute(_ string) (*v1.StreamRoute, error) { return nil, cache.ErrNotFound }
func (c *dummyCache) GetGlobalRule(_ string) (*v1.GlobalRule, error) { return nil, cache.ErrNotFound }
func (c *dummyCache) GetConsumer(_ string) (*v1.Consumer, error) { return nil, cache.ErrNotFound }
func (c *dummyCache) GetSchema(_ string) (*v1.Schema, error) { return nil, cache.ErrNotFound }
func (c *dummyCache) ListRoutes() ([]*v1.Route, error) { return nil, nil }
func (c *dummyCache) ListSSL() ([]*v1.Ssl, error) { return nil, nil }
func (c *dummyCache) ListUpstreams() ([]*v1.Upstream, error) { return nil, nil }
func (c *dummyCache) ListStreamRoutes() ([]*v1.StreamRoute, error) { return nil, nil }
func (c *dummyCache) ListGlobalRules() ([]*v1.GlobalRule, error) { return nil, nil }
func (c *dummyCache) ListConsumers() ([]*v1.Consumer, error) { return nil, nil }
func (c *dummyCache) ListSchema() ([]*v1.Schema, error) { return nil, nil }
func (c *dummyCache) DeleteRoute(_ *v1.Route) error { return nil }
func (c *dummyCache) DeleteSSL(_ *v1.Ssl) error { return nil }
func (c *dummyCache) DeleteUpstream(_ *v1.Upstream) error { return nil }
func (c *dummyCache) DeleteStreamRoute(_ *v1.StreamRoute) error { return nil }
func (c *dummyCache) DeleteGlobalRule(_ *v1.GlobalRule) error { return nil }
func (c *dummyCache) DeleteConsumer(_ *v1.Consumer) error { return nil }
func (c *dummyCache) DeleteSchema(_ *v1.Schema) error { return nil }
func (c *dummyCache) GetPluginConfig(_ string) (*v1.PluginConfig, error) {
return nil, cache.ErrNotFound
}
func (c *dummyCache) ListRoutes() ([]*v1.Route, error) { return nil, nil }
func (c *dummyCache) ListSSL() ([]*v1.Ssl, error) { return nil, nil }
func (c *dummyCache) ListUpstreams() ([]*v1.Upstream, error) { return nil, nil }
func (c *dummyCache) ListStreamRoutes() ([]*v1.StreamRoute, error) { return nil, nil }
func (c *dummyCache) ListGlobalRules() ([]*v1.GlobalRule, error) { return nil, nil }
func (c *dummyCache) ListConsumers() ([]*v1.Consumer, error) { return nil, nil }
func (c *dummyCache) ListSchema() ([]*v1.Schema, error) { return nil, nil }
func (c *dummyCache) ListPluginConfigs() ([]*v1.PluginConfig, error) { return nil, nil }
func (c *dummyCache) DeleteRoute(_ *v1.Route) error { return nil }
func (c *dummyCache) DeleteSSL(_ *v1.Ssl) error { return nil }
func (c *dummyCache) DeleteUpstream(_ *v1.Upstream) error { return nil }
func (c *dummyCache) DeleteStreamRoute(_ *v1.StreamRoute) error { return nil }
func (c *dummyCache) DeleteGlobalRule(_ *v1.GlobalRule) error { return nil }
func (c *dummyCache) DeleteConsumer(_ *v1.Consumer) error { return nil }
func (c *dummyCache) DeleteSchema(_ *v1.Schema) error { return nil }
func (c *dummyCache) DeletePluginConfig(_ *v1.PluginConfig) error { return nil }
Loading

0 comments on commit 7c0073d

Please sign in to comment.