Skip to content

Commit 4058495

Browse files
mnencialeonardoce
authored andcommitted
chore: simplify using reflection
Signed-off-by: Marco Nenciarini <[email protected]>
1 parent 0f4b8a7 commit 4058495

File tree

2 files changed

+2
-39
lines changed

2 files changed

+2
-39
lines changed

internal/cnpgi/instance/internal/client/client.go

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/cloudnative-pg/machinery/pkg/log"
1111
corev1 "k8s.io/api/core/v1"
12-
"k8s.io/apimachinery/pkg/runtime"
1312
"sigs.k8s.io/controller-runtime/pkg/client"
1413

1514
pluginBarman "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
@@ -71,28 +70,6 @@ func (e *ExtendedClient) Get(
7170
return e.Client.Get(ctx, key, obj, opts...)
7271
}
7372

74-
// addTypeInformationToObject adds TypeMeta information to a client.Object based upon the client Scheme
75-
// inspired by: https://github.com/kubernetes/cli-runtime/blob/v0.19.2/pkg/printers/typesetter.go#L41
76-
func (e *ExtendedClient) addTypeInformationToObject(obj client.Object) error {
77-
gvks, _, err := e.Client.Scheme().ObjectKinds(obj)
78-
if err != nil {
79-
return fmt.Errorf("missing apiVersion or kind and cannot assign it; %w", err)
80-
}
81-
82-
for _, gvk := range gvks {
83-
if len(gvk.Kind) == 0 {
84-
continue
85-
}
86-
if len(gvk.Version) == 0 || gvk.Version == runtime.APIVersionInternal {
87-
continue
88-
}
89-
obj.GetObjectKind().SetGroupVersionKind(gvk)
90-
break
91-
}
92-
93-
return nil
94-
}
95-
9673
func (e *ExtendedClient) getCachedObject(
9774
ctx context.Context,
9875
key client.ObjectKey,
@@ -103,12 +80,6 @@ func (e *ExtendedClient) getCachedObject(
10380
WithName("extended_client").
10481
WithValues("name", key.Name, "namespace", key.Namespace)
10582

106-
// Make sure the object has GVK information
107-
// This is needed to compare the object type with the cached one
108-
if err := e.addTypeInformationToObject(obj); err != nil {
109-
return fmt.Errorf("cannot add type metadata to object of type %T: %w", obj, err)
110-
}
111-
11283
contextLogger.Trace("locking the cache")
11384
e.mux.Lock()
11485
defer e.mux.Unlock()
@@ -119,7 +90,7 @@ func (e *ExtendedClient) getCachedObject(
11990
if cacheEntry.entry.GetNamespace() != key.Namespace || cacheEntry.entry.GetName() != key.Name {
12091
continue
12192
}
122-
if cacheEntry.entry.GetObjectKind().GroupVersionKind() != obj.GetObjectKind().GroupVersionKind() {
93+
if reflect.TypeOf(cacheEntry.entry) != reflect.TypeOf(obj) {
12394
continue
12495
}
12596
if cacheEntry.isExpired() {
@@ -148,12 +119,6 @@ func (e *ExtendedClient) getCachedObject(
148119
return err
149120
}
150121

151-
// Populate the GKV information again, as the client.Get() may have
152-
// returned an object without this information set
153-
if err := e.addTypeInformationToObject(obj); err != nil {
154-
return fmt.Errorf("cannot add type metadata to object of type %T: %w", obj, err)
155-
}
156-
157122
cs := cachedEntry{
158123
entry: obj.DeepCopyObject().(client.Object),
159124
fetchUnixTime: time.Now().Unix(),
@@ -178,7 +143,7 @@ func (e *ExtendedClient) removeObject(object client.Object) {
178143
for i, cache := range e.cachedObjects {
179144
if cache.entry.GetNamespace() == object.GetNamespace() &&
180145
cache.entry.GetName() == object.GetName() &&
181-
cache.entry.GetObjectKind().GroupVersionKind() == object.GetObjectKind().GroupVersionKind() {
146+
reflect.TypeOf(cache.entry) == reflect.TypeOf(object) {
182147
e.cachedObjects = append(e.cachedObjects[:i], e.cachedObjects[i+1:]...)
183148
return
184149
}

internal/cnpgi/instance/internal/client/client_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ func addToCache(c *ExtendedClient, obj client.Object, fetchUnixTime int64) {
3232
ttlSeconds: DefaultTTLSeconds,
3333
}
3434
ce.entry.SetResourceVersion("from cache")
35-
err := c.addTypeInformationToObject(ce.entry)
36-
Expect(err).ToNot(HaveOccurred())
3735
c.cachedObjects = append(c.cachedObjects, ce)
3836
}
3937

0 commit comments

Comments
 (0)