Skip to content

Commit 413e7ca

Browse files
authored
feat: implement pluginconfig clients (#638) (#772)
1 parent fe4a824 commit 413e7ca

14 files changed

+682
-30
lines changed

pkg/api/validation/apisix_route_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func (c fakeSchemaClient) GetSslSchema(_ context.Context) (*api.Schema, error) {
5757
return nil, nil
5858
}
5959

60+
func (c fakeSchemaClient) GetPluginConfigSchema(_ context.Context) (*api.Schema, error) {
61+
return nil, nil
62+
}
63+
6064
func newFakeSchemaClient() apisix.Schema {
6165
testData := map[string]string{
6266
"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"}}}`,

pkg/apisix/apisix.go

+11
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ type Schema interface {
133133
GetUpstreamSchema(context.Context) (*v1.Schema, error)
134134
GetConsumerSchema(context.Context) (*v1.Schema, error)
135135
GetSslSchema(context.Context) (*v1.Schema, error)
136+
GetPluginConfigSchema(ctx context.Context) (*v1.Schema, error)
137+
}
138+
139+
// PluginConfig is the specific client interface to take over the create, update,
140+
// list and delete for APISIX PluginConfig resource.
141+
type PluginConfig interface {
142+
Get(context.Context, string) (*v1.PluginConfig, error)
143+
List(context.Context) ([]*v1.PluginConfig, error)
144+
Create(context.Context, *v1.PluginConfig) (*v1.PluginConfig, error)
145+
Delete(context.Context, *v1.PluginConfig) error
146+
Update(context.Context, *v1.PluginConfig) (*v1.PluginConfig, error)
136147
}
137148

138149
type apisix struct {

pkg/apisix/cache/cache.go

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type Cache interface {
3737
InsertConsumer(*v1.Consumer) error
3838
// InsertSchema adds or updates schema to cache.
3939
InsertSchema(*v1.Schema) error
40+
// InsertPluginConfig adds or updates plugin_config to cache.
41+
InsertPluginConfig(*v1.PluginConfig) error
4042

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

5660
// ListRoutes lists all routes in cache.
5761
ListRoutes() ([]*v1.Route, error)
@@ -67,6 +71,8 @@ type Cache interface {
6771
ListConsumers() ([]*v1.Consumer, error)
6872
// ListSchema lists all schema in cache.
6973
ListSchema() ([]*v1.Schema, error)
74+
// ListPluginConfig lists all plugin_config in cache.
75+
ListPluginConfigs() ([]*v1.PluginConfig, error)
7076

7177
// DeleteRoute deletes the specified route in cache.
7278
DeleteRoute(*v1.Route) error
@@ -82,4 +88,6 @@ type Cache interface {
8288
DeleteConsumer(*v1.Consumer) error
8389
// DeleteSchema deletes the specified schema in cache.
8490
DeleteSchema(*v1.Schema) error
91+
// DeletePluginConfig deletes the specified plugin_config in cache.
92+
DeletePluginConfig(*v1.PluginConfig) error
8593
}

pkg/apisix/cache/memdb.go

+28
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ func (c *dbCache) InsertSchema(schema *v1.Schema) error {
7474
return c.insert("schema", schema.DeepCopy())
7575
}
7676

77+
func (c *dbCache) InsertPluginConfig(pc *v1.PluginConfig) error {
78+
return c.insert("plugin_config", pc.DeepCopy())
79+
}
80+
7781
func (c *dbCache) insert(table string, obj interface{}) error {
7882
txn := c.db.Txn(true)
7983
defer txn.Abort()
@@ -140,6 +144,14 @@ func (c *dbCache) GetSchema(name string) (*v1.Schema, error) {
140144
return obj.(*v1.Schema).DeepCopy(), nil
141145
}
142146

147+
func (c *dbCache) GetPluginConfig(name string) (*v1.PluginConfig, error) {
148+
obj, err := c.get("plugin_config", name)
149+
if err != nil {
150+
return nil, err
151+
}
152+
return obj.(*v1.PluginConfig).DeepCopy(), nil
153+
}
154+
143155
func (c *dbCache) get(table, id string) (interface{}, error) {
144156
txn := c.db.Txn(false)
145157
defer txn.Abort()
@@ -240,6 +252,18 @@ func (c *dbCache) ListSchema() ([]*v1.Schema, error) {
240252
return schemaList, nil
241253
}
242254

255+
func (c *dbCache) ListPluginConfigs() ([]*v1.PluginConfig, error) {
256+
raws, err := c.list("plugin_config")
257+
if err != nil {
258+
return nil, err
259+
}
260+
pluginConfigs := make([]*v1.PluginConfig, 0, len(raws))
261+
for _, raw := range raws {
262+
pluginConfigs = append(pluginConfigs, raw.(*v1.PluginConfig).DeepCopy())
263+
}
264+
return pluginConfigs, nil
265+
}
266+
243267
func (c *dbCache) list(table string) ([]interface{}, error) {
244268
txn := c.db.Txn(false)
245269
defer txn.Abort()
@@ -285,6 +309,10 @@ func (c *dbCache) DeleteSchema(schema *v1.Schema) error {
285309
return c.delete("schema", schema)
286310
}
287311

312+
func (c *dbCache) DeletePluginConfig(pc *v1.PluginConfig) error {
313+
return c.delete("plugin_config", pc)
314+
}
315+
288316
func (c *dbCache) delete(table string, obj interface{}) error {
289317
txn := c.db.Txn(true)
290318
defer txn.Abort()

pkg/apisix/cache/memdb_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,58 @@ func TestMemDBCacheSchema(t *testing.T) {
389389
}
390390
assert.Error(t, ErrNotFound, c.DeleteSchema(s4))
391391
}
392+
393+
func TestMemDBCachePluginConfig(t *testing.T) {
394+
c, err := NewMemDBCache()
395+
assert.Nil(t, err, "NewMemDBCache")
396+
397+
pc1 := &v1.PluginConfig{
398+
Metadata: v1.Metadata{
399+
ID: "1",
400+
Name: "name1",
401+
},
402+
}
403+
assert.Nil(t, c.InsertPluginConfig(pc1), "inserting plugin_config pc1")
404+
405+
pc11, err := c.GetPluginConfig("1")
406+
assert.Nil(t, err)
407+
assert.Equal(t, pc1, pc11)
408+
409+
pc2 := &v1.PluginConfig{
410+
Metadata: v1.Metadata{
411+
ID: "2",
412+
Name: "name2",
413+
},
414+
}
415+
pc3 := &v1.PluginConfig{
416+
Metadata: v1.Metadata{
417+
ID: "3",
418+
Name: "name3",
419+
},
420+
}
421+
assert.Nil(t, c.InsertPluginConfig(pc2), "inserting plugin_config pc2")
422+
assert.Nil(t, c.InsertPluginConfig(pc3), "inserting plugin_config pc3")
423+
424+
pc22, err := c.GetPluginConfig("2")
425+
assert.Nil(t, err)
426+
assert.Equal(t, pc2, pc22)
427+
428+
assert.Nil(t, c.DeletePluginConfig(pc3), "delete plugin_config pc3")
429+
430+
pcList, err := c.ListPluginConfigs()
431+
assert.Nil(t, err, "listing plugin_config")
432+
433+
if pcList[0].Name > pcList[1].Name {
434+
pcList[0], pcList[1] = pcList[1], pcList[0]
435+
}
436+
assert.Equal(t, pcList[0], pc1)
437+
assert.Equal(t, pcList[1], pc2)
438+
439+
pc4 := &v1.PluginConfig{
440+
Metadata: v1.Metadata{
441+
ID: "4",
442+
Name: "name4",
443+
},
444+
}
445+
assert.Error(t, ErrNotFound, c.DeletePluginConfig(pc4))
446+
}

pkg/apisix/cache/schema.go

+16
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ var (
116116
},
117117
},
118118
},
119+
"plugin_config": {
120+
Name: "plugin_config",
121+
Indexes: map[string]*memdb.IndexSchema{
122+
"id": {
123+
Name: "id",
124+
Unique: true,
125+
Indexer: &memdb.StringFieldIndex{Field: "ID"},
126+
},
127+
"name": {
128+
Name: "name",
129+
Unique: true,
130+
Indexer: &memdb.StringFieldIndex{Field: "Name"},
131+
AllowMissing: true,
132+
},
133+
},
134+
},
119135
},
120136
}
121137
)

pkg/apisix/cluster.go

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ type cluster struct {
9999
consumer Consumer
100100
plugin Plugin
101101
schema Schema
102+
pluginConfig PluginConfig
102103
metricsCollector metrics.Collector
103104
}
104105

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

144146
c.cache, err = cache.NewMemDBCache()
145147
if err != nil {

pkg/apisix/nonexistentclient.go

+64-30
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,29 @@ type nonExistentCluster struct {
2929
func newNonExistentCluster() *nonExistentCluster {
3030
return &nonExistentCluster{
3131
embedDummyResourceImplementer{
32-
route: &dummyRoute{},
33-
ssl: &dummySSL{},
34-
upstream: &dummyUpstream{},
35-
streamRoute: &dummyStreamRoute{},
36-
globalRule: &dummyGlobalRule{},
37-
consumer: &dummyConsumer{},
38-
plugin: &dummyPlugin{},
39-
schema: &dummySchema{},
32+
route: &dummyRoute{},
33+
ssl: &dummySSL{},
34+
upstream: &dummyUpstream{},
35+
streamRoute: &dummyStreamRoute{},
36+
globalRule: &dummyGlobalRule{},
37+
consumer: &dummyConsumer{},
38+
plugin: &dummyPlugin{},
39+
schema: &dummySchema{},
40+
pluginConfig: &dummyPluginConfig{},
4041
},
4142
}
4243
}
4344

4445
type embedDummyResourceImplementer struct {
45-
route Route
46-
ssl SSL
47-
upstream Upstream
48-
streamRoute StreamRoute
49-
globalRule GlobalRule
50-
consumer Consumer
51-
plugin Plugin
52-
schema Schema
46+
route Route
47+
ssl SSL
48+
upstream Upstream
49+
streamRoute StreamRoute
50+
globalRule GlobalRule
51+
consumer Consumer
52+
plugin Plugin
53+
schema Schema
54+
pluginConfig PluginConfig
5355
}
5456

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

217+
func (f *dummySchema) GetPluginConfigSchema(_ context.Context) (*v1.Schema, error) {
218+
return nil, ErrClusterNotExist
219+
}
220+
221+
type dummyPluginConfig struct{}
222+
223+
func (f *dummyPluginConfig) Get(_ context.Context, _ string) (*v1.PluginConfig, error) {
224+
return nil, ErrClusterNotExist
225+
}
226+
227+
func (f *dummyPluginConfig) List(_ context.Context) ([]*v1.PluginConfig, error) {
228+
return nil, ErrClusterNotExist
229+
}
230+
231+
func (f *dummyPluginConfig) Create(_ context.Context, _ *v1.PluginConfig) (*v1.PluginConfig, error) {
232+
return nil, ErrClusterNotExist
233+
}
234+
235+
func (f *dummyPluginConfig) Delete(_ context.Context, _ *v1.PluginConfig) error {
236+
return ErrClusterNotExist
237+
}
238+
239+
func (f *dummyPluginConfig) Update(_ context.Context, _ *v1.PluginConfig) (*v1.PluginConfig, error) {
240+
return nil, ErrClusterNotExist
241+
}
242+
215243
func (nc *nonExistentCluster) Route() Route {
216244
return nc.route
217245
}
@@ -267,24 +295,30 @@ func (c *dummyCache) InsertStreamRoute(_ *v1.StreamRoute) error { return
267295
func (c *dummyCache) InsertGlobalRule(_ *v1.GlobalRule) error { return nil }
268296
func (c *dummyCache) InsertConsumer(_ *v1.Consumer) error { return nil }
269297
func (c *dummyCache) InsertSchema(_ *v1.Schema) error { return nil }
298+
func (c *dummyCache) InsertPluginConfig(_ *v1.PluginConfig) error { return nil }
270299
func (c *dummyCache) GetRoute(_ string) (*v1.Route, error) { return nil, cache.ErrNotFound }
271300
func (c *dummyCache) GetSSL(_ string) (*v1.Ssl, error) { return nil, cache.ErrNotFound }
272301
func (c *dummyCache) GetUpstream(_ string) (*v1.Upstream, error) { return nil, cache.ErrNotFound }
273302
func (c *dummyCache) GetStreamRoute(_ string) (*v1.StreamRoute, error) { return nil, cache.ErrNotFound }
274303
func (c *dummyCache) GetGlobalRule(_ string) (*v1.GlobalRule, error) { return nil, cache.ErrNotFound }
275304
func (c *dummyCache) GetConsumer(_ string) (*v1.Consumer, error) { return nil, cache.ErrNotFound }
276305
func (c *dummyCache) GetSchema(_ string) (*v1.Schema, error) { return nil, cache.ErrNotFound }
277-
func (c *dummyCache) ListRoutes() ([]*v1.Route, error) { return nil, nil }
278-
func (c *dummyCache) ListSSL() ([]*v1.Ssl, error) { return nil, nil }
279-
func (c *dummyCache) ListUpstreams() ([]*v1.Upstream, error) { return nil, nil }
280-
func (c *dummyCache) ListStreamRoutes() ([]*v1.StreamRoute, error) { return nil, nil }
281-
func (c *dummyCache) ListGlobalRules() ([]*v1.GlobalRule, error) { return nil, nil }
282-
func (c *dummyCache) ListConsumers() ([]*v1.Consumer, error) { return nil, nil }
283-
func (c *dummyCache) ListSchema() ([]*v1.Schema, error) { return nil, nil }
284-
func (c *dummyCache) DeleteRoute(_ *v1.Route) error { return nil }
285-
func (c *dummyCache) DeleteSSL(_ *v1.Ssl) error { return nil }
286-
func (c *dummyCache) DeleteUpstream(_ *v1.Upstream) error { return nil }
287-
func (c *dummyCache) DeleteStreamRoute(_ *v1.StreamRoute) error { return nil }
288-
func (c *dummyCache) DeleteGlobalRule(_ *v1.GlobalRule) error { return nil }
289-
func (c *dummyCache) DeleteConsumer(_ *v1.Consumer) error { return nil }
290-
func (c *dummyCache) DeleteSchema(_ *v1.Schema) error { return nil }
306+
func (c *dummyCache) GetPluginConfig(_ string) (*v1.PluginConfig, error) {
307+
return nil, cache.ErrNotFound
308+
}
309+
func (c *dummyCache) ListRoutes() ([]*v1.Route, error) { return nil, nil }
310+
func (c *dummyCache) ListSSL() ([]*v1.Ssl, error) { return nil, nil }
311+
func (c *dummyCache) ListUpstreams() ([]*v1.Upstream, error) { return nil, nil }
312+
func (c *dummyCache) ListStreamRoutes() ([]*v1.StreamRoute, error) { return nil, nil }
313+
func (c *dummyCache) ListGlobalRules() ([]*v1.GlobalRule, error) { return nil, nil }
314+
func (c *dummyCache) ListConsumers() ([]*v1.Consumer, error) { return nil, nil }
315+
func (c *dummyCache) ListSchema() ([]*v1.Schema, error) { return nil, nil }
316+
func (c *dummyCache) ListPluginConfigs() ([]*v1.PluginConfig, error) { return nil, nil }
317+
func (c *dummyCache) DeleteRoute(_ *v1.Route) error { return nil }
318+
func (c *dummyCache) DeleteSSL(_ *v1.Ssl) error { return nil }
319+
func (c *dummyCache) DeleteUpstream(_ *v1.Upstream) error { return nil }
320+
func (c *dummyCache) DeleteStreamRoute(_ *v1.StreamRoute) error { return nil }
321+
func (c *dummyCache) DeleteGlobalRule(_ *v1.GlobalRule) error { return nil }
322+
func (c *dummyCache) DeleteConsumer(_ *v1.Consumer) error { return nil }
323+
func (c *dummyCache) DeleteSchema(_ *v1.Schema) error { return nil }
324+
func (c *dummyCache) DeletePluginConfig(_ *v1.PluginConfig) error { return nil }

0 commit comments

Comments
 (0)