diff --git a/.gitignore b/.gitignore index 2ddc5a8b87..31d2570309 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ # editor and IDE paraphernalia .idea +*.iml *.swp *.swo *~ diff --git a/pkg/internal/source/internal_test.go b/pkg/internal/source/internal_test.go index 73eb1a1d28..bebdc63fef 100644 --- a/pkg/internal/source/internal_test.go +++ b/pkg/internal/source/internal_test.go @@ -21,6 +21,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/client-go/tools/cache" "k8s.io/client-go/util/workqueue" "sigs.k8s.io/controller-runtime/pkg/client" @@ -293,6 +294,28 @@ var _ = Describe("Internal", func() { } Expect(kind.String()).Should(Equal("kind source: *v1.Pod")) }) + It("should return kind source underlying type for Unstructured", func() { + kind := internal.Kind[*unstructured.Unstructured, reconcile.Request]{ + Type: &unstructured.Unstructured{ + Object: map[string]any{ + "apiVersion": "apps/v1", + "kind": "Deployment", + }, + }, + } + Expect(kind.String()).Should(Equal("kind source: *unstructured.Unstructured[apps/v1 Deployment]")) + }) + It("should return kind source underlying type for PartialObjectMetadata", func() { + kind := internal.Kind[*metav1.PartialObjectMetadata, reconcile.Request]{ + Type: &metav1.PartialObjectMetadata{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "apps/v1", + Kind: "Deployment", + }, + }, + } + Expect(kind.String()).Should(Equal("kind source: *v1.PartialObjectMetadata[apps/v1 Deployment]")) + }) }) }) diff --git a/pkg/internal/source/kind.go b/pkg/internal/source/kind.go index a28aeb177e..4eb82f9825 100644 --- a/pkg/internal/source/kind.go +++ b/pkg/internal/source/kind.go @@ -8,6 +8,8 @@ import ( "time" "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/wait" toolscache "k8s.io/client-go/tools/cache" @@ -116,10 +118,18 @@ func (ks *Kind[object, request]) Start(ctx context.Context, queue workqueue.Type } func (ks *Kind[object, request]) String() string { - if !isNil(ks.Type) { + if isNil(ks.Type) { + return "kind source: unknown type" + } + + switch v := any(ks.Type).(type) { + case *unstructured.Unstructured, *metav1.PartialObjectMetadata: + gvk := v.(client.Object).GetObjectKind().GroupVersionKind() + gv, kind := gvk.ToAPIVersionAndKind() + return fmt.Sprintf("kind source: %T[%s %s]", v, gv, kind) + default: return fmt.Sprintf("kind source: %T", ks.Type) } - return "kind source: unknown type" } // WaitForSync implements SyncingSource to allow controllers to wait with starting @@ -133,7 +143,7 @@ func (ks *Kind[object, request]) WaitForSync(ctx context.Context) error { if errors.Is(ctx.Err(), context.Canceled) { return nil } - return fmt.Errorf("timed out waiting for cache to be synced for Kind %T", ks.Type) + return fmt.Errorf("timed out waiting for cache to be synced for %s", ks.String()) } }