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: implement pluginconfig clients (#638) #772

Merged
merged 6 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(config *v1.PluginConfig) error
tao12345666333 marked this conversation as resolved.
Show resolved Hide resolved
}
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
63 changes: 59 additions & 4 deletions pkg/apisix/cache/memdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func TestMemDBCacheSchema(t *testing.T) {
Name: "plugins/p1",
Content: "plugin schema",
}
assert.Nil(t, c.InsertSchema(s1), "inserting schema s1")
assert.Nil(t, c.InsertSchema(s1), "inserting schema pc1")
tao12345666333 marked this conversation as resolved.
Show resolved Hide resolved

s11, err := c.GetSchema("plugins/p1")
assert.Nil(t, err)
Expand All @@ -366,14 +366,14 @@ func TestMemDBCacheSchema(t *testing.T) {
s3 := &v1.Schema{
Name: "plugins/p3",
}
assert.Nil(t, c.InsertSchema(s2), "inserting schema s2")
assert.Nil(t, c.InsertSchema(s3), "inserting schema s3")
assert.Nil(t, c.InsertSchema(s2), "inserting schema pc2")
assert.Nil(t, c.InsertSchema(s3), "inserting schema pc3")

s22, err := c.GetSchema("plugins/p2")
assert.Nil(t, err)
assert.Equal(t, s2, s22)

assert.Nil(t, c.DeleteSchema(s3), "delete schema s3")
assert.Nil(t, c.DeleteSchema(s3), "delete schema pc3")

schemaList, err := c.ListSchema()
assert.Nil(t, err, "listing schema")
Expand All @@ -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 s1")
tao12345666333 marked this conversation as resolved.
Show resolved Hide resolved

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

pc2 := &v1.PluginConfig{
Metadata: v1.Metadata{
ID: "2",
Name: "name2",
},
}
s3 := &v1.PluginConfig{
Metadata: v1.Metadata{
ID: "3",
Name: "name3",
},
}
assert.Nil(t, c.InsertPluginConfig(pc2), "inserting plugin_config s2")
tao12345666333 marked this conversation as resolved.
Show resolved Hide resolved
assert.Nil(t, c.InsertPluginConfig(s3), "inserting plugin_config s3")

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

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

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)

s4 := &v1.PluginConfig{
Metadata: v1.Metadata{
ID: "4",
Name: "name4",
},
}
assert.Error(t, ErrNotFound, c.DeletePluginConfig(s4))
}
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 }
1 change: 1 addition & 0 deletions pkg/apisix/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"

"github.com/apache/apisix-ingress-controller/pkg/metrics"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we sort these imports?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My local golangci-lint would print these warnings below:

golangci-lint run
pkg/apisix/plugin_test.go:25: File is not `goimports`-ed with -local github.com/apache/apisix-ingress-controller (goimports)
	"github.com/apache/apisix-ingress-controller/pkg/metrics"
pkg/apisix/schema_test.go:24: File is not `goimports`-ed with -local github.com/apache/apisix-ingress-controller (goimports)
	"github.com/apache/apisix-ingress-controller/pkg/metrics"

Finally, I found that if I split the imports then there no warnings left.
Maybe I should ignore it in the next commits.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's caused by the lexicographical orders.

"github.com/stretchr/testify/assert"

"golang.org/x/net/nettest"
Expand Down
Loading