diff --git a/pkg/apisix/cache/cache.go b/pkg/apisix/cache/cache.go index 7fcdc4aaa0..91e68b2214 100644 --- a/pkg/apisix/cache/cache.go +++ b/pkg/apisix/cache/cache.go @@ -97,5 +97,7 @@ type Cache interface { // DeletePluginConfig deletes the specified plugin_config in cache. DeletePluginConfig(*v1.PluginConfig) error + CheckUpstreamReference(*v1.Upstream) error + CheckPluginConfigReference(*v1.PluginConfig) error DeleteUpstreamServiceRelation(*v1.UpstreamServiceRelation) error } diff --git a/pkg/apisix/cache/memdb.go b/pkg/apisix/cache/memdb.go index 4a45a1a9d3..3dc039351c 100644 --- a/pkg/apisix/cache/memdb.go +++ b/pkg/apisix/cache/memdb.go @@ -311,7 +311,7 @@ func (c *dbCache) DeleteSSL(ssl *v1.Ssl) error { } func (c *dbCache) DeleteUpstream(u *v1.Upstream) error { - if err := c.checkUpstreamReference(u); err != nil { + if err := c.CheckUpstreamReference(u); err != nil { return err } return c.delete("upstream", u) @@ -334,7 +334,7 @@ func (c *dbCache) DeleteSchema(schema *v1.Schema) error { } func (c *dbCache) DeletePluginConfig(pc *v1.PluginConfig) error { - if err := c.checkPluginConfigReference(pc); err != nil { + if err := c.CheckPluginConfigReference(pc); err != nil { return err } return c.delete("plugin_config", pc) @@ -357,7 +357,7 @@ func (c *dbCache) delete(table string, obj interface{}) error { return nil } -func (c *dbCache) checkUpstreamReference(u *v1.Upstream) error { +func (c *dbCache) CheckUpstreamReference(u *v1.Upstream) error { // Upstream is referenced by Route. txn := c.db.Txn(false) defer txn.Abort() @@ -379,7 +379,7 @@ func (c *dbCache) checkUpstreamReference(u *v1.Upstream) error { return nil } -func (c *dbCache) checkPluginConfigReference(u *v1.PluginConfig) error { +func (c *dbCache) CheckPluginConfigReference(u *v1.PluginConfig) error { // PluginConfig is referenced by Route. txn := c.db.Txn(false) defer txn.Abort() diff --git a/pkg/apisix/cache/noop_db.go b/pkg/apisix/cache/noop_db.go index 6fce1ec6ba..d3e7b770d8 100644 --- a/pkg/apisix/cache/noop_db.go +++ b/pkg/apisix/cache/noop_db.go @@ -172,3 +172,11 @@ func (c *noopCache) DeletePluginConfig(pc *v1.PluginConfig) error { func (c *noopCache) DeleteUpstreamServiceRelation(us *v1.UpstreamServiceRelation) error { return nil } + +func (c *noopCache) CheckUpstreamReference(u *v1.Upstream) error { + return nil +} + +func (c *noopCache) CheckPluginConfigReference(pc *v1.PluginConfig) error { + return nil +} diff --git a/pkg/apisix/nonexistentclient.go b/pkg/apisix/nonexistentclient.go index ac7e49862f..237cbbc054 100644 --- a/pkg/apisix/nonexistentclient.go +++ b/pkg/apisix/nonexistentclient.go @@ -400,3 +400,5 @@ func (c *dummyCache) DeleteConsumer(_ *v1.Consumer) error func (c *dummyCache) DeleteSchema(_ *v1.Schema) error { return nil } func (c *dummyCache) DeletePluginConfig(_ *v1.PluginConfig) error { return nil } func (c *dummyCache) DeleteUpstreamServiceRelation(_ *v1.UpstreamServiceRelation) error { return nil } +func (c *dummyCache) CheckUpstreamReference(_ *v1.Upstream) error { return nil } +func (c *dummyCache) CheckPluginConfigReference(_ *v1.PluginConfig) error { return nil } diff --git a/pkg/apisix/pluginconfig.go b/pkg/apisix/pluginconfig.go index 0886e4507b..6a2d4f0f48 100644 --- a/pkg/apisix/pluginconfig.go +++ b/pkg/apisix/pluginconfig.go @@ -162,7 +162,11 @@ func (pc *pluginConfigClient) Delete(ctx context.Context, obj *v1.PluginConfig) zap.String("cluster", pc.cluster.name), zap.String("url", pc.url), ) - + err := pc.cluster.cache.CheckPluginConfigReference(obj) + if err != nil { + log.Warnw("deletion for plugin config: " + obj.Name + " aborted as it is still in use.") + return err + } if err := pc.cluster.HasSynced(ctx); err != nil { return err } diff --git a/pkg/apisix/upstream.go b/pkg/apisix/upstream.go index 2a0f394ca7..6b53b396e5 100644 --- a/pkg/apisix/upstream.go +++ b/pkg/apisix/upstream.go @@ -158,7 +158,11 @@ func (u *upstreamClient) Delete(ctx context.Context, obj *v1.Upstream) error { zap.String("cluster", u.cluster.name), zap.String("url", u.url), ) - + err := u.cluster.cache.CheckUpstreamReference(obj) + if err != nil { + log.Warnw("deletion for upstream: " + obj.Name + " aborted as it is still in use.") + return err + } if err := u.cluster.HasSynced(ctx); err != nil { return err }