Skip to content

Commit

Permalink
Capture resourceType GVK in logs for duck types (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
scothis committed Sep 6, 2023
1 parent ce2ead6 commit d577e94
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions reconcilers/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (r *AggregateReconciler[T]) SetupWithManagerYieldingController(ctx context.
log := logr.FromContextOrDiscard(ctx).
WithName(r.Name).
WithValues(
"resourceType", gvk(r.Type, r.Config.Scheme()),
"resourceType", gvk(r.Config, r.Type),
"request", r.Request,
)
ctx = logr.NewContext(ctx, log)
Expand Down Expand Up @@ -210,7 +210,7 @@ func (r *AggregateReconciler[T]) Reconcile(ctx context.Context, req Request) (Re

log := logr.FromContextOrDiscard(ctx).
WithName(r.Name).
WithValues("resourceType", gvk(r.Type, c.Scheme()))
WithValues("resourceType", gvk(c, r.Type))
ctx = logr.NewContext(ctx, log)

ctx = rtime.StashNow(ctx, time.Now())
Expand Down
2 changes: 1 addition & 1 deletion reconcilers/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (r *CastResource[T, CT]) cast(ctx context.Context, resource T) (context.Con
if kind := castResource.GetObjectKind(); kind.GroupVersionKind().Empty() {
// default the apiVersion/kind with the real value from the resource if not already defined
c := RetrieveConfigOrDie(ctx)
kind.SetGroupVersionKind(gvk(resource, c.Scheme()))
kind.SetGroupVersionKind(gvk(c, resource))
}
ctx = StashResourceType(ctx, castResource)
return ctx, castResource, nil
Expand Down
4 changes: 2 additions & 2 deletions reconcilers/child.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (r *ChildReconciler[T, CT, CLT]) SetupWithManager(ctx context.Context, mgr

log := logr.FromContextOrDiscard(ctx).
WithName(r.Name).
WithValues("childType", gvk(r.ChildType, c.Scheme()))
WithValues("childType", gvk(c, r.ChildType))
ctx = logr.NewContext(ctx, log)

if err := r.validate(ctx); err != nil {
Expand Down Expand Up @@ -247,7 +247,7 @@ func (r *ChildReconciler[T, CT, CLT]) Reconcile(ctx context.Context, resource T)

log := logr.FromContextOrDiscard(ctx).
WithName(r.Name).
WithValues("childType", gvk(r.ChildType, c.Scheme()))
WithValues("childType", gvk(c, r.ChildType))
ctx = logr.NewContext(ctx, log)

child, err := r.reconcile(ctx, resource)
Expand Down
2 changes: 1 addition & 1 deletion reconcilers/childset.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (r *ChildSetReconciler[T, CT, CLT]) SetupWithManager(ctx context.Context, m

log := logr.FromContextOrDiscard(ctx).
WithName(r.Name).
WithValues("childType", gvk(r.ChildType, c.Scheme()))
WithValues("childType", gvk(c, r.ChildType))
ctx = logr.NewContext(ctx, log)

if err := r.validate(ctx); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions reconcilers/reconcilers.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ func typeName(i interface{}) string {
return t.Name()
}

func gvk(obj client.Object, scheme *runtime.Scheme) schema.GroupVersionKind {
gvks, _, err := scheme.ObjectKinds(obj)
func gvk(c client.Client, obj runtime.Object) schema.GroupVersionKind {
gvk, err := c.GroupVersionKindFor(obj)
if err != nil {
return schema.GroupVersionKind{}
}
return gvks[0]
return gvk
}

func namespaceName(obj client.Object) types.NamespacedName {
Expand Down
4 changes: 2 additions & 2 deletions reconcilers/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (r *ResourceReconciler[T]) SetupWithManagerYieldingController(ctx context.C

log := logr.FromContextOrDiscard(ctx).
WithName(r.Name).
WithValues("resourceType", gvk(r.Type, r.Config.Scheme()))
WithValues("resourceType", gvk(r.Config, r.Type))
ctx = logr.NewContext(ctx, log)

ctx = StashConfig(ctx, r.Config)
Expand Down Expand Up @@ -188,7 +188,7 @@ func (r *ResourceReconciler[T]) Reconcile(ctx context.Context, req Request) (Res

log := logr.FromContextOrDiscard(ctx).
WithName(r.Name).
WithValues("resourceType", gvk(r.Type, c.Scheme()))
WithValues("resourceType", gvk(c, r.Type))
ctx = logr.NewContext(ctx, log)

ctx = rtime.StashNow(ctx, time.Now())
Expand Down

0 comments on commit d577e94

Please sign in to comment.