Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions typed/typedindexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,37 @@ func (t Indexer[K]) ListKeys() []string {
}

func (t Indexer[K]) Get(obj K) (item K, exists bool, err error) {
var typedObj *K
var zero K
gotItem, gotExists, gotErr := t.indexer.Get(obj)
if err != nil || !gotExists {
return *typedObj, gotExists, gotErr
if gotErr != nil || !gotExists {
return zero, gotExists, gotErr
}
gotRObj, ok := gotItem.(runtime.Object)
if !ok {
return *typedObj, gotExists, fmt.Errorf("%v is not a runtime.Object", gotItem)
return zero, gotExists, fmt.Errorf("%v is not a runtime.Object", gotItem)
}

gotTypedObj, err := UnstructuredObjToTypedObj[K](gotRObj)
if err != nil {
return *typedObj, gotExists, fmt.Errorf("could not convert %s to %T", gotItem, *typedObj)
return zero, gotExists, fmt.Errorf("could not convert %s to %T", gotItem, zero)
}
return gotTypedObj, gotExists, gotErr
}

func (t Indexer[K]) GetByKey(key string) (item K, exists bool, err error) {
var typedObj *K
var zero K
gotItem, gotExists, gotErr := t.indexer.GetByKey(key)
if err != nil || !gotExists {
return *typedObj, gotExists, gotErr
if gotErr != nil || !gotExists {
return zero, gotExists, gotErr
}
gotRObj, ok := gotItem.(runtime.Object)
if !ok {
return *typedObj, gotExists, fmt.Errorf("%v is not a runtime.Object", gotItem)
return zero, gotExists, fmt.Errorf("%v is not a runtime.Object", gotItem)
}

gotTypedObj, err := UnstructuredObjToTypedObj[K](gotRObj)
if err != nil {
return *typedObj, gotExists, fmt.Errorf("could not convert %s to %T", gotItem, *typedObj)
return zero, gotExists, fmt.Errorf("could not convert %s to %T", gotItem, zero)
}
return gotTypedObj, gotExists, gotErr
}
Expand Down
24 changes: 24 additions & 0 deletions typed/typedindexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package typed
import (
"context"
"fmt"
"testing"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -25,3 +26,26 @@ func ExampleIndexer() {
fmt.Printf("%T", secrets)
// Output: []*v1.Secret
}

func TestGetByKeyErrorHandling(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

client := fake.NewSimpleDynamicClient(runtime.NewScheme())
informerFactory := dynamicinformer.NewDynamicSharedInformerFactory(client, 0)
informerFactory.Start(ctx.Done())
informerFactory.WaitForCacheSync(ctx.Done())

indexer := NewIndexer[*corev1.Secret](informerFactory.ForResource(corev1.SchemeGroupVersion.WithResource("secrets")).Informer().GetIndexer())

secret, exists, err := indexer.GetByKey("nonexistent/key")
if secret != nil {
t.Errorf("Expected nil secret for non-existent key, got %v", secret)
}
if exists {
t.Errorf("Expected exists to be false for non-existent key")
}
if err != nil {
t.Errorf("Expected no error for non-existent key, got %v", err)
}
}
Loading