Skip to content

Commit 93d4146

Browse files
authored
fix log value dedupe in reconciler.go (#57)
Issue #, if available: aws-controllers-k8s/community#1001 Description of changes: * "Kind" value in logs was empty because `Get` from K8s typed client does not unmarshall GVK fields. We need to use dynamic client OR deduce GVK from `scheme` object. However to set "Kind" field in logs, resource-descriptor can be used and easily fix this bug. * Other resource fields(name, namespace, generation) were getting duped in logs because resourceLogger constructor was deducing them and setting them in `r.log` values. When INFO or DEBUG method was called, they were getting deduced again and added as additional values. To fix this, during construction, we do not deduce fields from resource. Additional changes: * Fields like name, namespace, kind do not change during reconciliation of the object so we can set them once during construction of resource logger * Only generation field is calculated on INFO/DEBUG methods * Adoption reconciler logger did not have these bugs. Adoption reconciler finds Kind from "adoptedResource.Spec" and does not use constructor for resource logger. Tested locally and validated the logs. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 2991a6b commit 93d4146

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

pkg/runtime/log/resource.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func NewResourceLogger(
112112
additionalValues ...interface{},
113113
) *ResourceLogger {
114114
return &ResourceLogger{
115-
log: AdaptResource(log, res, additionalValues...),
115+
log: log.WithValues(additionalValues...),
116116
res: res,
117117
blockDepth: 0,
118118
}
@@ -158,15 +158,8 @@ func expandResourceFields(
158158
additionalValues ...interface{},
159159
) []interface{} {
160160
metaObj := res.MetaObject()
161-
ns := metaObj.GetNamespace()
162-
resName := metaObj.GetName()
163161
generation := metaObj.GetGeneration()
164-
rtObj := res.RuntimeObject()
165-
kind := rtObj.GetObjectKind().GroupVersionKind().Kind
166162
vals := []interface{}{
167-
"kind", kind,
168-
"namespace", ns,
169-
"name", resName,
170163
"generation", generation,
171164
}
172165
if len(additionalValues) > 0 {

pkg/runtime/reconciler.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ func (r *resourceReconciler) Reconcile(req ctrlrt.Request) (ctrlrt.Result, error
155155
"account", acctID,
156156
"role", roleARN,
157157
"region", region,
158+
// All the fields for a resource that do not change during reconciliation
159+
// can be initialized during resourceLogger creation
160+
"kind", r.rd.GroupKind().Kind,
161+
"namespace", req.Namespace,
162+
"name", req.Name,
158163
)
159164
ctx = context.WithValue(ctx, ackrtlog.ContextKey, rlog)
160165

0 commit comments

Comments
 (0)