From 4cd32bb0056c79b14f6bc886b74abed5a4ed064b Mon Sep 17 00:00:00 2001 From: GaoXiaodong <13814823514@163.com> Date: Thu, 3 Aug 2023 10:37:41 +0800 Subject: [PATCH] fix(platform): cluster status is Terminating, update will delete it (#2304) Co-authored-by: xdonggao --- pkg/platform/registry/cluster/storage/storage.go | 2 ++ pkg/platform/registry/cluster/strategy.go | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/pkg/platform/registry/cluster/storage/storage.go b/pkg/platform/registry/cluster/storage/storage.go index 66d9ecd1c0..7dc9a0199a 100644 --- a/pkg/platform/registry/cluster/storage/storage.go +++ b/pkg/platform/registry/cluster/storage/storage.go @@ -73,6 +73,8 @@ func NewStorage(optsGetter genericregistry.RESTOptionsGetter, platformClient pla UpdateStrategy: strategy, DeleteStrategy: strategy, + ShouldDeleteDuringUpdate: clusterstrategy.ShouldDeleteDuringUpdate, + TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(AddHandlers)}, } options := &genericregistry.StoreOptions{ diff --git a/pkg/platform/registry/cluster/strategy.go b/pkg/platform/registry/cluster/strategy.go index 787a326fdc..c844cb5f6b 100644 --- a/pkg/platform/registry/cluster/strategy.go +++ b/pkg/platform/registry/cluster/strategy.go @@ -30,6 +30,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/validation/field" genericregistry "k8s.io/apiserver/pkg/registry/generic" + "k8s.io/apiserver/pkg/registry/generic/registry" "k8s.io/apiserver/pkg/registry/rest" "k8s.io/apiserver/pkg/storage" "k8s.io/apiserver/pkg/storage/names" @@ -251,6 +252,15 @@ func MatchCluster(label labels.Selector, field fields.Selector) storage.Selectio } } +func ShouldDeleteDuringUpdate(ctx context.Context, key string, obj, existing runtime.Object) bool { + app, ok := obj.(*platform.Cluster) + if !ok { + log.Errorf("unexpected object, key:%s", key) + return false + } + return len(app.Spec.Finalizers) == 0 && registry.ShouldDeleteDuringUpdate(ctx, key, obj, existing) +} + // ToSelectableFields returns a field set that represents the object func ToSelectableFields(cluster *platform.Cluster) fields.Set { objectMetaFieldsSet := genericregistry.ObjectMetaFieldsSet(&cluster.ObjectMeta, false)