From 84e99eed619a193843b8984e42bf947eaa5b1bbd Mon Sep 17 00:00:00 2001 From: Iceber Gu Date: Mon, 25 Dec 2023 14:35:43 +0800 Subject: [PATCH] add ConvertDeletedObject method for ResourceStorage Signed-off-by: Iceber Gu --- .../internalstorage/resource_storage.go | 17 ++++++++++++++++ .../memorystorage/memory_resource_storage.go | 20 +++++++++++++++++++ pkg/storage/storage.go | 2 ++ .../clustersynchro/resource_synchro.go | 15 +++++--------- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/pkg/storage/internalstorage/resource_storage.go b/pkg/storage/internalstorage/resource_storage.go index ff2692b05..9900d93c9 100644 --- a/pkg/storage/internalstorage/resource_storage.go +++ b/pkg/storage/internalstorage/resource_storage.go @@ -20,6 +20,7 @@ import ( "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/watch" genericstorage "k8s.io/apiserver/pkg/storage" + "k8s.io/client-go/tools/cache" internal "github.com/clusterpedia-io/api/clusterpedia" "github.com/clusterpedia-io/clusterpedia/pkg/storage" @@ -125,6 +126,22 @@ func (s *ResourceStorage) Update(ctx context.Context, cluster string, obj runtim return InterpretResourceDBError(cluster, metaobj.GetName(), result.Error) } +func (c *ResourceStorage) ConvertDeletedObject(obj interface{}) (runtime.Object, error) { + key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj) + if err != nil { + return nil, err + } + + namespace, name, err := cache.SplitMetaNamespaceKey(key) + if err != nil { + return nil, err + } + + // Since it is not necessary to save the complete deleted object to the queue, + // we convert the object to `PartialObjectMetadata` + return &metav1.PartialObjectMetadata{ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: name}}, nil +} + func (s *ResourceStorage) deleteObject(cluster, namespace, name string) *gorm.DB { return s.db.Model(&Resource{}).Where(map[string]interface{}{ "cluster": cluster, diff --git a/pkg/storage/memorystorage/memory_resource_storage.go b/pkg/storage/memorystorage/memory_resource_storage.go index 4a8c75eba..055cda5ff 100644 --- a/pkg/storage/memorystorage/memory_resource_storage.go +++ b/pkg/storage/memorystorage/memory_resource_storage.go @@ -8,11 +8,13 @@ import ( "sync" "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/conversion" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apimachinery/pkg/watch" + kubecache "k8s.io/client-go/tools/cache" internal "github.com/clusterpedia-io/api/clusterpedia" "github.com/clusterpedia-io/clusterpedia/pkg/storage" @@ -82,6 +84,24 @@ func (s *ResourceStorage) Update(ctx context.Context, cluster string, obj runtim return nil } +func (s *ResourceStorage) ConvertDeletedObject(obj interface{}) (runobj runtime.Object, _ error) { + if d, ok := obj.(kubecache.DeletedFinalStateUnknown); ok { + if obj, ok := d.Obj.(runtime.Object); ok { + return obj, nil + } + namespace, name, err := kubecache.SplitMetaNamespaceKey(d.Key) + if err != nil { + return nil, err + } + return &metav1.PartialObjectMetadata{ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: name}}, nil + } + + if obj, ok := obj.(runtime.Object); ok { + return obj, nil + } + return nil, fmt.Errorf("Invalid Type(%T): couldn't convert deleted object", obj) +} + func (s *ResourceStorage) Delete(ctx context.Context, cluster string, obj runtime.Object) error { resourceVersion, err := s.CrvSynchro.UpdateClusterResourceVersion(obj, cluster) if err != nil { diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index 97543c8d5..a8df23fcf 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -36,6 +36,8 @@ type ResourceStorage interface { Create(ctx context.Context, cluster string, obj runtime.Object) error Update(ctx context.Context, cluster string, obj runtime.Object) error + + ConvertDeletedObject(obj interface{}) (runtime.Object, error) Delete(ctx context.Context, cluster string, obj runtime.Object) error } diff --git a/pkg/synchromanager/clustersynchro/resource_synchro.go b/pkg/synchromanager/clustersynchro/resource_synchro.go index a7820a705..dbf0a37a6 100644 --- a/pkg/synchromanager/clustersynchro/resource_synchro.go +++ b/pkg/synchromanager/clustersynchro/resource_synchro.go @@ -346,19 +346,14 @@ func (synchro *ResourceSynchro) OnDelete(obj interface{}) { if !synchro.isRunnableForStorage.Load() { return } - - if d, ok := obj.(cache.DeletedFinalStateUnknown); ok { - if obj, ok = d.Obj.(*unstructured.Unstructured); !ok { - namespace, name, err := cache.SplitMetaNamespaceKey(d.Key) - if err != nil { - return - } - obj = &metav1.PartialObjectMetadata{ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: name}} - } - } if o, ok := obj.(*unstructured.Unstructured); ok { synchro.pruneObject(o) } + + obj, err := synchro.storage.ConvertDeletedObject(obj) + if err != nil { + return + } _ = synchro.queue.Delete(obj) }