Skip to content

Commit 6210f84

Browse files
authored
Merge pull request #3494 from erikgb/improve-kind-source-logging
✨ Improve logging for dynamic type kind source
2 parents 885e77d + 6f89e1d commit 6210f84

3 files changed

Lines changed: 37 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: 23 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,28 @@ 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{
301+
"apiVersion": "apps/v1",
302+
"kind": "Deployment",
303+
},
304+
},
305+
}
306+
Expect(kind.String()).Should(Equal("kind source: *unstructured.Unstructured[apps/v1 Deployment]"))
307+
})
308+
It("should return kind source underlying type for PartialObjectMetadata", func() {
309+
kind := internal.Kind[*metav1.PartialObjectMetadata, reconcile.Request]{
310+
Type: &metav1.PartialObjectMetadata{
311+
TypeMeta: metav1.TypeMeta{
312+
APIVersion: "apps/v1",
313+
Kind: "Deployment",
314+
},
315+
},
316+
}
317+
Expect(kind.String()).Should(Equal("kind source: *v1.PartialObjectMetadata[apps/v1 Deployment]"))
318+
})
296319
})
297320
})
298321

pkg/internal/source/kind.go

Lines changed: 13 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,18 @@ 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+
gv, kind := gvk.ToAPIVersionAndKind()
129+
return fmt.Sprintf("kind source: %T[%s %s]", v, gv, kind)
130+
default:
120131
return fmt.Sprintf("kind source: %T", ks.Type)
121132
}
122-
return "kind source: unknown type"
123133
}
124134

125135
// WaitForSync implements SyncingSource to allow controllers to wait with starting
@@ -133,7 +143,7 @@ func (ks *Kind[object, request]) WaitForSync(ctx context.Context) error {
133143
if errors.Is(ctx.Err(), context.Canceled) {
134144
return nil
135145
}
136-
return fmt.Errorf("timed out waiting for cache to be synced for Kind %T", ks.Type)
146+
return fmt.Errorf("timed out waiting for cache to be synced for %s", ks.String())
137147
}
138148
}
139149

0 commit comments

Comments
 (0)