Skip to content

Commit 3b4164b

Browse files
committed
Improve logging for dynamic type kind source
1 parent 885e77d commit 3b4164b

3 files changed

Lines changed: 32 additions & 3 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
# editor and IDE paraphernalia
1515
.idea
16+
*.iml
1617
*.swp
1718
*.swo
1819
*~

pkg/internal/source/internal_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
. "github.com/onsi/ginkgo/v2"
2323
. "github.com/onsi/gomega"
24+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2425
"k8s.io/client-go/tools/cache"
2526
"k8s.io/client-go/util/workqueue"
2627
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -293,6 +294,24 @@ var _ = Describe("Internal", func() {
293294
}
294295
Expect(kind.String()).Should(Equal("kind source: *v1.Pod"))
295296
})
297+
It("should return kind source underlying type for Unstructured", func() {
298+
kind := internal.Kind[*unstructured.Unstructured, reconcile.Request]{
299+
Type: &unstructured.Unstructured{
300+
Object: map[string]any{"kind": "Pod"},
301+
},
302+
}
303+
Expect(kind.String()).Should(Equal("kind source: *unstructured.Unstructured[Pod]"))
304+
})
305+
It("should return kind source underlying type for PartialObjectMetadata", func() {
306+
kind := internal.Kind[*metav1.PartialObjectMetadata, reconcile.Request]{
307+
Type: &metav1.PartialObjectMetadata{
308+
TypeMeta: metav1.TypeMeta{
309+
Kind: "Pod",
310+
},
311+
},
312+
}
313+
Expect(kind.String()).Should(Equal("kind source: *v1.PartialObjectMetadata[Pod]"))
314+
})
296315
})
297316
})
298317

pkg/internal/source/kind.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"time"
99

1010
"k8s.io/apimachinery/pkg/api/meta"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1113
"k8s.io/apimachinery/pkg/runtime"
1214
"k8s.io/apimachinery/pkg/util/wait"
1315
toolscache "k8s.io/client-go/tools/cache"
@@ -116,10 +118,17 @@ func (ks *Kind[object, request]) Start(ctx context.Context, queue workqueue.Type
116118
}
117119

118120
func (ks *Kind[object, request]) String() string {
119-
if !isNil(ks.Type) {
121+
if isNil(ks.Type) {
122+
return "kind source: unknown type"
123+
}
124+
125+
switch v := any(ks.Type).(type) {
126+
case *unstructured.Unstructured, *metav1.PartialObjectMetadata:
127+
gvk := v.(client.Object).GetObjectKind().GroupVersionKind()
128+
return fmt.Sprintf("kind source: %T[%s]", v, gvk.Kind)
129+
default:
120130
return fmt.Sprintf("kind source: %T", ks.Type)
121131
}
122-
return "kind source: unknown type"
123132
}
124133

125134
// WaitForSync implements SyncingSource to allow controllers to wait with starting
@@ -133,7 +142,7 @@ func (ks *Kind[object, request]) WaitForSync(ctx context.Context) error {
133142
if errors.Is(ctx.Err(), context.Canceled) {
134143
return nil
135144
}
136-
return fmt.Errorf("timed out waiting for cache to be synced for Kind %T", ks.Type)
145+
return fmt.Errorf("timed out waiting for cache to be synced for %s", ks.String())
137146
}
138147
}
139148

0 commit comments

Comments
 (0)