mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 19:01:49 +00:00
enhance and fix log calls
Some of these changes are cosmetic (repeatedly calling klog.V instead of reusing the result), others address real issues: - Logging a message only above a certain verbosity threshold without recording that verbosity level (if klog.V().Enabled() { klog.Info... }): this matters when using a logging backend which records the verbosity level. - Passing a format string with parameters to a logging function that doesn't do string formatting. All of these locations where found by the enhanced logcheck tool from https://github.com/kubernetes/klog/pull/297. In some cases it reports false positives, but those can be suppressed with source code comments.
This commit is contained in:
parent
0f4d0660a7
commit
edffc700a4
@ -149,11 +149,11 @@ func addUsersAndGroupsImpl(pathLoginDef, pathUsers, pathGroups string) (*UsersAn
|
|||||||
var loginDef string
|
var loginDef string
|
||||||
f, close, err := openFileWithLock(pathLoginDef)
|
f, close, err := openFileWithLock(pathLoginDef)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.V(1).Info("Could not open %q, using default system limits: %v", pathLoginDef, err)
|
klog.V(1).Infof("Could not open %q, using default system limits: %v", pathLoginDef, err)
|
||||||
} else {
|
} else {
|
||||||
loginDef, err = readFile(f)
|
loginDef, err = readFile(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.V(1).Info("Could not read %q, using default system limits: %v", pathLoginDef, err)
|
klog.V(1).Infof("Could not read %q, using default system limits: %v", pathLoginDef, err)
|
||||||
}
|
}
|
||||||
close()
|
close()
|
||||||
}
|
}
|
||||||
|
@ -406,6 +406,9 @@ func (g *GenericPLEG) updateCache(pod *kubecontainer.Pod, pid types.UID) error {
|
|||||||
// all containers again.
|
// all containers again.
|
||||||
status, err := g.runtime.GetPodStatus(pod.ID, pod.Name, pod.Namespace)
|
status, err := g.runtime.GetPodStatus(pod.ID, pod.Name, pod.Namespace)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// nolint:logcheck // Not using the result of klog.V inside the
|
||||||
|
// if branch is okay, we just use it to determine whether the
|
||||||
|
// additional "podStatus" key and its value should be added.
|
||||||
if klog.V(6).Enabled() {
|
if klog.V(6).Enabled() {
|
||||||
klog.ErrorS(err, "PLEG: Write status", "pod", klog.KRef(pod.Namespace, pod.Name), "podStatus", status)
|
klog.ErrorS(err, "PLEG: Write status", "pod", klog.KRef(pod.Namespace, pod.Name), "podStatus", status)
|
||||||
} else {
|
} else {
|
||||||
|
@ -399,7 +399,7 @@ func (i *scaleUpdatedObjectInfo) UpdatedObject(ctx context.Context, oldObj runti
|
|||||||
if _, ok := replicasPathInDeployment[requestGroupVersion.String()]; ok {
|
if _, ok := replicasPathInDeployment[requestGroupVersion.String()]; ok {
|
||||||
groupVersion = requestGroupVersion
|
groupVersion = requestGroupVersion
|
||||||
} else {
|
} else {
|
||||||
klog.Fatal("Unrecognized group/version in request info %q", requestGroupVersion.String())
|
klog.Fatalf("Unrecognized group/version in request info %q", requestGroupVersion.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -300,7 +300,7 @@ func (i *scaleUpdatedObjectInfo) UpdatedObject(ctx context.Context, oldObj runti
|
|||||||
if _, ok := replicasPathInReplicaSet[requestGroupVersion.String()]; ok {
|
if _, ok := replicasPathInReplicaSet[requestGroupVersion.String()]; ok {
|
||||||
groupVersion = requestGroupVersion
|
groupVersion = requestGroupVersion
|
||||||
} else {
|
} else {
|
||||||
klog.Fatal("Unrecognized group/version in request info %q", requestGroupVersion.String())
|
klog.Fatalf("Unrecognized group/version in request info %q", requestGroupVersion.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -294,7 +294,7 @@ func (i *scaleUpdatedObjectInfo) UpdatedObject(ctx context.Context, oldObj runti
|
|||||||
if _, ok := replicasPathInStatefulSet[requestGroupVersion.String()]; ok {
|
if _, ok := replicasPathInStatefulSet[requestGroupVersion.String()]; ok {
|
||||||
groupVersion = requestGroupVersion
|
groupVersion = requestGroupVersion
|
||||||
} else {
|
} else {
|
||||||
klog.Fatal("Unrecognized group/version in request info %q", requestGroupVersion.String())
|
klog.Fatalf("Unrecognized group/version in request info %q", requestGroupVersion.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -266,7 +266,7 @@ func (i *scaleUpdatedObjectInfo) UpdatedObject(ctx context.Context, oldObj runti
|
|||||||
if _, ok := replicasPathInReplicationController[requestGroupVersion.String()]; ok {
|
if _, ok := replicasPathInReplicationController[requestGroupVersion.String()]; ok {
|
||||||
groupVersion = requestGroupVersion
|
groupVersion = requestGroupVersion
|
||||||
} else {
|
} else {
|
||||||
klog.Fatal("Unrecognized group/version in request info %q", requestGroupVersion.String())
|
klog.Fatalf("Unrecognized group/version in request info %q", requestGroupVersion.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -971,10 +971,10 @@ func ignoreExitCodes(err error, ignoredExitCodes ...int) error {
|
|||||||
func execWithLog(b iscsiDiskMounter, cmd string, args ...string) (string, error) {
|
func execWithLog(b iscsiDiskMounter, cmd string, args ...string) (string, error) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
out, err := b.exec.Command(cmd, args...).CombinedOutput()
|
out, err := b.exec.Command(cmd, args...).CombinedOutput()
|
||||||
if klog.V(5).Enabled() {
|
if klogV := klog.V(5); klogV.Enabled() {
|
||||||
d := time.Since(start)
|
d := time.Since(start)
|
||||||
klog.V(5).Infof("Executed %s %v in %v, err: %v", cmd, args, d, err)
|
klogV.Infof("Executed %s %v in %v, err: %v", cmd, args, d, err)
|
||||||
klog.V(5).Infof("Output: %s", string(out))
|
klogV.Infof("Output: %s", string(out))
|
||||||
}
|
}
|
||||||
return string(out), err
|
return string(out), err
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,7 @@ func (r *RBACAuthorizer) Authorize(ctx context.Context, requestAttributes author
|
|||||||
|
|
||||||
// Build a detailed log of the denial.
|
// Build a detailed log of the denial.
|
||||||
// Make the whole block conditional so we don't do a lot of string-building we won't use.
|
// Make the whole block conditional so we don't do a lot of string-building we won't use.
|
||||||
if klog.V(5).Enabled() {
|
if klogV := klog.V(5); klogV.Enabled() {
|
||||||
var operation string
|
var operation string
|
||||||
if requestAttributes.IsResourceRequest() {
|
if requestAttributes.IsResourceRequest() {
|
||||||
b := &bytes.Buffer{}
|
b := &bytes.Buffer{}
|
||||||
@ -116,7 +116,7 @@ func (r *RBACAuthorizer) Authorize(ctx context.Context, requestAttributes author
|
|||||||
scope = "cluster-wide"
|
scope = "cluster-wide"
|
||||||
}
|
}
|
||||||
|
|
||||||
klog.Infof("RBAC: no rules authorize user %q with groups %q to %s %s", requestAttributes.GetUser().GetName(), requestAttributes.GetUser().GetGroups(), operation, scope)
|
klogV.Infof("RBAC: no rules authorize user %q with groups %q to %s %s", requestAttributes.GetUser().GetName(), requestAttributes.GetUser().GetGroups(), operation, scope)
|
||||||
}
|
}
|
||||||
|
|
||||||
reason := ""
|
reason := ""
|
||||||
|
@ -49,8 +49,9 @@ func StorageWithCacher() generic.StorageDecorator {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return s, d, err
|
return s, d, err
|
||||||
}
|
}
|
||||||
if klog.V(5).Enabled() {
|
if klogV := klog.V(5); klogV.Enabled() {
|
||||||
klog.V(5).InfoS("Storage caching is enabled", objectTypeToArgs(newFunc())...)
|
//nolint:logcheck // It complains about the key/value pairs because it cannot check them.
|
||||||
|
klogV.InfoS("Storage caching is enabled", objectTypeToArgs(newFunc())...)
|
||||||
}
|
}
|
||||||
|
|
||||||
cacherConfig := cacherstorage.Config{
|
cacherConfig := cacherstorage.Config{
|
||||||
|
@ -125,6 +125,10 @@ func WithPriorityAndFairness(
|
|||||||
workEstimate := workEstimator(r, classification.FlowSchemaName, classification.PriorityLevelName)
|
workEstimate := workEstimator(r, classification.FlowSchemaName, classification.PriorityLevelName)
|
||||||
|
|
||||||
fcmetrics.ObserveWorkEstimatedSeats(classification.PriorityLevelName, classification.FlowSchemaName, workEstimate.MaxSeats())
|
fcmetrics.ObserveWorkEstimatedSeats(classification.PriorityLevelName, classification.FlowSchemaName, workEstimate.MaxSeats())
|
||||||
|
// nolint:logcheck // Not using the result of klog.V
|
||||||
|
// inside the if branch is okay, we just use it to
|
||||||
|
// determine whether the additional information should
|
||||||
|
// be added.
|
||||||
if klog.V(4).Enabled() {
|
if klog.V(4).Enabled() {
|
||||||
httplog.AddKeyValue(ctx, "apf_iseats", workEstimate.InitialSeats)
|
httplog.AddKeyValue(ctx, "apf_iseats", workEstimate.InitialSeats)
|
||||||
httplog.AddKeyValue(ctx, "apf_fseats", workEstimate.FinalSeats)
|
httplog.AddKeyValue(ctx, "apf_fseats", workEstimate.FinalSeats)
|
||||||
|
@ -32,19 +32,19 @@ type klogWrapper struct{}
|
|||||||
const klogWrapperDepth = 4
|
const klogWrapperDepth = 4
|
||||||
|
|
||||||
func (klogWrapper) Info(args ...interface{}) {
|
func (klogWrapper) Info(args ...interface{}) {
|
||||||
if klog.V(5).Enabled() {
|
if klogV := klog.V(5); klogV.Enabled() {
|
||||||
klog.V(5).InfoSDepth(klogWrapperDepth, fmt.Sprint(args...))
|
klogV.InfoSDepth(klogWrapperDepth, fmt.Sprint(args...))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (klogWrapper) Infoln(args ...interface{}) {
|
func (klogWrapper) Infoln(args ...interface{}) {
|
||||||
if klog.V(5).Enabled() {
|
if klogV := klog.V(5); klogV.Enabled() {
|
||||||
klog.V(5).InfoSDepth(klogWrapperDepth, fmt.Sprintln(args...))
|
klogV.InfoSDepth(klogWrapperDepth, fmt.Sprintln(args...))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (klogWrapper) Infof(format string, args ...interface{}) {
|
func (klogWrapper) Infof(format string, args ...interface{}) {
|
||||||
if klog.V(5).Enabled() {
|
if klogV := klog.V(5); klogV.Enabled() {
|
||||||
klog.V(5).InfoSDepth(klogWrapperDepth, fmt.Sprintf(format, args...))
|
klog.V(5).InfoSDepth(klogWrapperDepth, fmt.Sprintf(format, args...))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -434,8 +434,8 @@ func (cfgCtlr *configController) digestConfigObjects(newPLs []*flowcontrol.Prior
|
|||||||
|
|
||||||
// if we are going to issue an update, be sure we track every name we update so we know if we update it too often.
|
// if we are going to issue an update, be sure we track every name we update so we know if we update it too often.
|
||||||
currResult.updatedItems.Insert(fsu.flowSchema.Name)
|
currResult.updatedItems.Insert(fsu.flowSchema.Name)
|
||||||
if klog.V(4).Enabled() {
|
if klogV := klog.V(4); klogV.Enabled() {
|
||||||
klog.V(4).Infof("%s writing Condition %s to FlowSchema %s, which had ResourceVersion=%s, because its previous value was %s, diff: %s",
|
klogV.Infof("%s writing Condition %s to FlowSchema %s, which had ResourceVersion=%s, because its previous value was %s, diff: %s",
|
||||||
cfgCtlr.name, fsu.condition, fsu.flowSchema.Name, fsu.flowSchema.ResourceVersion, fcfmt.Fmt(fsu.oldValue), cmp.Diff(fsu.oldValue, fsu.condition))
|
cfgCtlr.name, fsu.condition, fsu.flowSchema.Name, fsu.flowSchema.ResourceVersion, fcfmt.Fmt(fsu.oldValue), cmp.Diff(fsu.oldValue, fsu.condition))
|
||||||
}
|
}
|
||||||
fsIfc := cfgCtlr.flowcontrolClient.FlowSchemas()
|
fsIfc := cfgCtlr.flowcontrolClient.FlowSchemas()
|
||||||
|
@ -573,9 +573,9 @@ func (qs *queueSet) shuffleShardLocked(hashValue uint64, descr1, descr2 interfac
|
|||||||
bestQueueIdx = queueIdx
|
bestQueueIdx = queueIdx
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if klog.V(6).Enabled() {
|
if klogV := klog.V(6); klogV.Enabled() {
|
||||||
chosenQueue := qs.queues[bestQueueIdx]
|
chosenQueue := qs.queues[bestQueueIdx]
|
||||||
klog.V(6).Infof("QS(%s) at t=%s R=%v: For request %#+v %#+v chose queue %d, with sum: %#v & %d seats in use & nextDispatchR=%v", qs.qCfg.Name, qs.clock.Now().Format(nsTimeFmt), qs.currentR, descr1, descr2, bestQueueIdx, chosenQueue.requests.QueueSum(), chosenQueue.seatsInUse, chosenQueue.nextDispatchR)
|
klogV.Infof("QS(%s) at t=%s R=%v: For request %#+v %#+v chose queue %d, with sum: %#v & %d seats in use & nextDispatchR=%v", qs.qCfg.Name, qs.clock.Now().Format(nsTimeFmt), qs.currentR, descr1, descr2, bestQueueIdx, chosenQueue.requests.QueueSum(), chosenQueue.seatsInUse, chosenQueue.nextDispatchR)
|
||||||
}
|
}
|
||||||
return bestQueueIdx
|
return bestQueueIdx
|
||||||
}
|
}
|
||||||
|
@ -1051,13 +1051,13 @@ func truncateBody(body string) string {
|
|||||||
// allocating a new string for the body output unless necessary. Uses a simple heuristic to determine
|
// allocating a new string for the body output unless necessary. Uses a simple heuristic to determine
|
||||||
// whether the body is printable.
|
// whether the body is printable.
|
||||||
func glogBody(prefix string, body []byte) {
|
func glogBody(prefix string, body []byte) {
|
||||||
if klog.V(8).Enabled() {
|
if klogV := klog.V(8); klogV.Enabled() {
|
||||||
if bytes.IndexFunc(body, func(r rune) bool {
|
if bytes.IndexFunc(body, func(r rune) bool {
|
||||||
return r < 0x0a
|
return r < 0x0a
|
||||||
}) != -1 {
|
}) != -1 {
|
||||||
klog.Infof("%s:\n%s", prefix, truncateBody(hex.Dump(body)))
|
klogV.Infof("%s:\n%s", prefix, truncateBody(hex.Dump(body)))
|
||||||
} else {
|
} else {
|
||||||
klog.Infof("%s: %s", prefix, truncateBody(string(body)))
|
klogV.Infof("%s: %s", prefix, truncateBody(string(body)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,6 +65,7 @@ func TestZapLoggerInfo(t *testing.T) {
|
|||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
writer := zapcore.AddSync(&buffer)
|
writer := zapcore.AddSync(&buffer)
|
||||||
sampleInfoLogger, _ := NewJSONLogger(writer, nil, nil)
|
sampleInfoLogger, _ := NewJSONLogger(writer, nil, nil)
|
||||||
|
// nolint:logcheck // The linter cannot and doesn't need to check the key/value pairs.
|
||||||
sampleInfoLogger.Info(data.msg, data.keysValues...)
|
sampleInfoLogger.Info(data.msg, data.keysValues...)
|
||||||
logStr := buffer.String()
|
logStr := buffer.String()
|
||||||
|
|
||||||
|
@ -93,6 +93,8 @@ func DefaultBehaviorOnFatal() {
|
|||||||
// klog.Fatal is invoked for extended information. This is intended for maintainer
|
// klog.Fatal is invoked for extended information. This is intended for maintainer
|
||||||
// debugging and out of a reasonable range for users.
|
// debugging and out of a reasonable range for users.
|
||||||
func fatal(msg string, code int) {
|
func fatal(msg string, code int) {
|
||||||
|
// nolint:logcheck // Not using the result of klog.V(99) inside the if
|
||||||
|
// branch is okay, we just use it to determine how to terminate.
|
||||||
if klog.V(99).Enabled() {
|
if klog.V(99).Enabled() {
|
||||||
klog.FatalDepth(2, msg)
|
klog.FatalDepth(2, msg)
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ import (
|
|||||||
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
|
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
compute "google.golang.org/api/compute/v1"
|
compute "google.golang.org/api/compute/v1"
|
||||||
"k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
cloudprovider "k8s.io/cloud-provider"
|
cloudprovider "k8s.io/cloud-provider"
|
||||||
@ -202,9 +202,9 @@ func (g *Cloud) ensureInternalLoadBalancer(clusterName, clusterID string, svc *v
|
|||||||
// Delete existing forwarding rule before making changes to the backend service. For example - changing protocol
|
// Delete existing forwarding rule before making changes to the backend service. For example - changing protocol
|
||||||
// of backend service without first deleting forwarding rule will throw an error since the linked forwarding
|
// of backend service without first deleting forwarding rule will throw an error since the linked forwarding
|
||||||
// rule would show the old protocol.
|
// rule would show the old protocol.
|
||||||
if klog.V(2).Enabled() {
|
if klogV := klog.V(2); klogV.Enabled() {
|
||||||
frDiff := cmp.Diff(existingFwdRule, newFwdRule)
|
frDiff := cmp.Diff(existingFwdRule, newFwdRule)
|
||||||
klog.V(2).Infof("ensureInternalLoadBalancer(%v): forwarding rule changed - Existing - %+v\n, New - %+v\n, Diff(-existing, +new) - %s\n. Deleting existing forwarding rule.", loadBalancerName, existingFwdRule, newFwdRule, frDiff)
|
klogV.Infof("ensureInternalLoadBalancer(%v): forwarding rule changed - Existing - %+v\n, New - %+v\n, Diff(-existing, +new) - %s\n. Deleting existing forwarding rule.", loadBalancerName, existingFwdRule, newFwdRule, frDiff)
|
||||||
}
|
}
|
||||||
if err = ignoreNotFound(g.DeleteRegionForwardingRule(loadBalancerName, g.region)); err != nil {
|
if err = ignoreNotFound(g.DeleteRegionForwardingRule(loadBalancerName, g.region)); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
Reference in New Issue
Block a user