Parcourir la source

update uid getter

Signed-off-by: r2k1 <yokree@gmail.com>
r2k1 il y a 2 ans
Parent
commit
ade0be4ce5
1 fichiers modifiés avec 14 ajouts et 15 suppressions
  1. 14 15
      pkg/clustercache/store.go

+ 14 - 15
pkg/clustercache/store.go

@@ -5,7 +5,6 @@ import (
 	"sync"
 	"sync"
 
 
 	v1 "k8s.io/api/core/v1"
 	v1 "k8s.io/api/core/v1"
-	"k8s.io/apimachinery/pkg/api/meta"
 	"k8s.io/apimachinery/pkg/fields"
 	"k8s.io/apimachinery/pkg/fields"
 	"k8s.io/apimachinery/pkg/types"
 	"k8s.io/apimachinery/pkg/types"
 	"k8s.io/client-go/rest"
 	"k8s.io/client-go/rest"
@@ -28,6 +27,18 @@ func NewGenericStore[Input any, Output any](transformFunc func(input Input) Outp
 	}
 	}
 }
 }
 
 
+type uidGetter interface {
+	GetUID() types.UID
+}
+
+func getUid(obj any) types.UID {
+	if o, ok := obj.(uidGetter); ok {
+		return o.GetUID()
+	}
+	return ""
+
+}
+
 func CreateStoreAndWatch[Input any, Output any](
 func CreateStoreAndWatch[Input any, Output any](
 	ctx context.Context,
 	ctx context.Context,
 	restClient rest.Interface,
 	restClient rest.Interface,
@@ -52,32 +63,20 @@ func (s *GenericStore[Input, Output]) Add(obj any) error {
 
 
 // Update updates the existing entry in the store.
 // Update updates the existing entry in the store.
 func (s *GenericStore[Input, Output]) Update(obj any) error {
 func (s *GenericStore[Input, Output]) Update(obj any) error {
-	// The 'meta.Accessor' function can be used for types implementing 'metav1.Object'.
-	o, err := meta.Accessor(obj)
-	if err != nil {
-		return err
-	}
-	uid := o.GetUID()
-
 	s.mutex.Lock()
 	s.mutex.Lock()
 	defer s.mutex.Unlock()
 	defer s.mutex.Unlock()
 
 
-	s.items[uid] = s.transformFunc(obj.(Input))
+	s.items[getUid(obj)] = s.transformFunc(obj.(Input))
 
 
 	return nil
 	return nil
 }
 }
 
 
 // Delete removes an object from the store.
 // Delete removes an object from the store.
 func (s *GenericStore[Input, Output]) Delete(obj any) error {
 func (s *GenericStore[Input, Output]) Delete(obj any) error {
-	o, err := meta.Accessor(obj)
-	if err != nil {
-		return err
-	}
-
 	s.mutex.Lock()
 	s.mutex.Lock()
 	defer s.mutex.Unlock()
 	defer s.mutex.Unlock()
 
 
-	delete(s.items, o.GetUID())
+	delete(s.items, getUid(obj))
 
 
 	return nil
 	return nil
 }
 }