From 40584958b6a856e49b5a300470ece659273fbc09 Mon Sep 17 00:00:00 2001 From: Marco Nenciarini Date: Wed, 3 Sep 2025 13:08:42 +0200 Subject: [PATCH] chore: simplify using reflection Signed-off-by: Marco Nenciarini --- .../cnpgi/instance/internal/client/client.go | 39 +------------------ .../instance/internal/client/client_test.go | 2 - 2 files changed, 2 insertions(+), 39 deletions(-) diff --git a/internal/cnpgi/instance/internal/client/client.go b/internal/cnpgi/instance/internal/client/client.go index 5beb487..290e132 100644 --- a/internal/cnpgi/instance/internal/client/client.go +++ b/internal/cnpgi/instance/internal/client/client.go @@ -9,7 +9,6 @@ import ( "github.com/cloudnative-pg/machinery/pkg/log" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/client" pluginBarman "github.com/cloudnative-pg/plugin-barman-cloud/api/v1" @@ -71,28 +70,6 @@ func (e *ExtendedClient) Get( return e.Client.Get(ctx, key, obj, opts...) } -// addTypeInformationToObject adds TypeMeta information to a client.Object based upon the client Scheme -// inspired by: https://github.com/kubernetes/cli-runtime/blob/v0.19.2/pkg/printers/typesetter.go#L41 -func (e *ExtendedClient) addTypeInformationToObject(obj client.Object) error { - gvks, _, err := e.Client.Scheme().ObjectKinds(obj) - if err != nil { - return fmt.Errorf("missing apiVersion or kind and cannot assign it; %w", err) - } - - for _, gvk := range gvks { - if len(gvk.Kind) == 0 { - continue - } - if len(gvk.Version) == 0 || gvk.Version == runtime.APIVersionInternal { - continue - } - obj.GetObjectKind().SetGroupVersionKind(gvk) - break - } - - return nil -} - func (e *ExtendedClient) getCachedObject( ctx context.Context, key client.ObjectKey, @@ -103,12 +80,6 @@ func (e *ExtendedClient) getCachedObject( WithName("extended_client"). WithValues("name", key.Name, "namespace", key.Namespace) - // Make sure the object has GVK information - // This is needed to compare the object type with the cached one - if err := e.addTypeInformationToObject(obj); err != nil { - return fmt.Errorf("cannot add type metadata to object of type %T: %w", obj, err) - } - contextLogger.Trace("locking the cache") e.mux.Lock() defer e.mux.Unlock() @@ -119,7 +90,7 @@ func (e *ExtendedClient) getCachedObject( if cacheEntry.entry.GetNamespace() != key.Namespace || cacheEntry.entry.GetName() != key.Name { continue } - if cacheEntry.entry.GetObjectKind().GroupVersionKind() != obj.GetObjectKind().GroupVersionKind() { + if reflect.TypeOf(cacheEntry.entry) != reflect.TypeOf(obj) { continue } if cacheEntry.isExpired() { @@ -148,12 +119,6 @@ func (e *ExtendedClient) getCachedObject( return err } - // Populate the GKV information again, as the client.Get() may have - // returned an object without this information set - if err := e.addTypeInformationToObject(obj); err != nil { - return fmt.Errorf("cannot add type metadata to object of type %T: %w", obj, err) - } - cs := cachedEntry{ entry: obj.DeepCopyObject().(client.Object), fetchUnixTime: time.Now().Unix(), @@ -178,7 +143,7 @@ func (e *ExtendedClient) removeObject(object client.Object) { for i, cache := range e.cachedObjects { if cache.entry.GetNamespace() == object.GetNamespace() && cache.entry.GetName() == object.GetName() && - cache.entry.GetObjectKind().GroupVersionKind() == object.GetObjectKind().GroupVersionKind() { + reflect.TypeOf(cache.entry) == reflect.TypeOf(object) { e.cachedObjects = append(e.cachedObjects[:i], e.cachedObjects[i+1:]...) return } diff --git a/internal/cnpgi/instance/internal/client/client_test.go b/internal/cnpgi/instance/internal/client/client_test.go index 729f64a..6764c07 100644 --- a/internal/cnpgi/instance/internal/client/client_test.go +++ b/internal/cnpgi/instance/internal/client/client_test.go @@ -32,8 +32,6 @@ func addToCache(c *ExtendedClient, obj client.Object, fetchUnixTime int64) { ttlSeconds: DefaultTTLSeconds, } ce.entry.SetResourceVersion("from cache") - err := c.addTypeInformationToObject(ce.entry) - Expect(err).ToNot(HaveOccurred()) c.cachedObjects = append(c.cachedObjects, ce) }