From 8931b6a19bc8e0de4957116ada6fa5091b0424ff Mon Sep 17 00:00:00 2001 From: Ashish Tiwari Date: Tue, 16 Apr 2024 12:23:41 +0530 Subject: [PATCH 1/2] fix: deletion of references (#2213) --- pkg/apisix/cache/cache.go | 2 ++ pkg/apisix/cache/memdb.go | 8 ++++---- pkg/apisix/cache/noop_db.go | 8 ++++++++ pkg/apisix/nonexistentclient.go | 2 ++ pkg/apisix/pluginconfig.go | 6 +++++- pkg/apisix/upstream.go | 6 +++++- 6 files changed, 26 insertions(+), 6 deletions(-) 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 } From 8699eb16e3e9a70501d945b2ceedffc87cf12de5 Mon Sep 17 00:00:00 2001 From: Ashish Tiwari Date: Tue, 16 Apr 2024 12:35:15 +0530 Subject: [PATCH 2/2] chore: add fix commit in release note Signed-off-by: Ashish Tiwari --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73d30b1600..5de1ab043b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ # Table of Contents +- [1.8.1](#181) - [1.8.0](#180) - [1.7.0](#170) - [1.6.0](#160) @@ -43,6 +44,7 @@ ## What's New +- fix: deletion of references @Revolyssup (#2213) - chore: remove redundant logs and improve logs for users @Revolyssup (#2206) - fix: use force=true to hard delete the apisix resource @Revolyssup (#2210) - chore: upgrade etcd-adapter to latest version @Revolyssup (#2205)