avoid klog Info calls without verbosity

In the following code pattern, the log message will get logged with v=0 in JSON
output although conceptually it has a higher verbosity:

   if klog.V(5).Enabled() {
       klog.Info("hello world")
   }

Having the actual verbosity in the JSON output is relevant, for example for
filtering out only the important info messages. The solution is to use
klog.V(5).Info or something similar.

Whether the outer if is necessary at all depends on how complex the parameters
are. The return value of klog.V can be captured in a variable and be used
multiple times to avoid the overhead for that function call and to avoid
repeating the verbosity level.
This commit is contained in:
Patrick Ohly
2021-12-11 12:10:21 +01:00
parent 935052185c
commit 9eaa2dc554
26 changed files with 184 additions and 130 deletions

View File

@@ -69,12 +69,10 @@ func (r *resourceAllocationScorer) score(
score := r.scorer(requested, allocatable)
if klog.V(10).Enabled() {
klog.InfoS("Listing internal info for allocatable resources, requested resources and score", "pod",
klog.KObj(pod), "node", klog.KObj(node), "resourceAllocationScorer", r.Name,
"allocatableResource", allocatable, "requestedResource", requested, "resourceScore", score,
)
}
klog.V(10).InfoS("Listing internal info for allocatable resources, requested resources and score", "pod",
klog.KObj(pod), "node", klog.KObj(node), "resourceAllocationScorer", r.Name,
"allocatableResource", allocatable, "requestedResource", requested, "resourceScore", score,
)
return score, nil
}
@@ -107,9 +105,7 @@ func (r *resourceAllocationScorer) calculateResourceAllocatableRequest(nodeInfo
return nodeInfo.Allocatable.ScalarResources[resource], (nodeInfo.Requested.ScalarResources[resource] + podRequest)
}
}
if klog.V(10).Enabled() {
klog.InfoS("Requested resource is omitted for node score calculation", "resourceName", resource)
}
klog.V(10).InfoS("Requested resource is omitted for node score calculation", "resourceName", resource)
return 0, 0
}