Skip to content

Commit

Permalink
fix: deletion of references
Browse files Browse the repository at this point in the history
  • Loading branch information
Revolyssup committed Apr 15, 2024
1 parent 42b9e4d commit 75b7256
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions pkg/apisix/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 4 additions & 4 deletions pkg/apisix/cache/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions pkg/apisix/cache/noop_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions pkg/apisix/nonexistentclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
6 changes: 5 additions & 1 deletion pkg/apisix/pluginconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/apisix/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 75b7256

Please sign in to comment.