From 11bcb24e40348dabf83e3641988ee1d6c37dc596 Mon Sep 17 00:00:00 2001 From: Thomas Ferrandiz Date: Fri, 21 Apr 2023 12:41:48 +0000 Subject: [PATCH] ensure that klog verbosity is set to the same level as logrus by repeatedly settting it every second during k3s startup Signed-off-by: Thomas Ferrandiz --- go.mod | 2 +- pkg/daemons/executor/embed.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index d35c2b067d3e..52533b606e98 100644 --- a/go.mod +++ b/go.mod @@ -135,6 +135,7 @@ require ( k8s.io/component-helpers v0.25.9 k8s.io/cri-api v0.26.0-alpha.3 k8s.io/klog v1.0.0 + k8s.io/klog/v2 v2.80.1 k8s.io/kubectl v0.25.9 k8s.io/kubernetes v1.25.9 k8s.io/utils v0.0.0-20221107191617-1a15be271d1d @@ -374,7 +375,6 @@ require ( k8s.io/controller-manager v0.25.9 // indirect k8s.io/csi-translation-lib v0.0.0 // indirect k8s.io/gengo v0.0.0-20211129171323-c02415ce4185 // indirect - k8s.io/klog/v2 v2.80.1 // indirect k8s.io/kube-aggregator v0.25.4 // indirect k8s.io/kube-controller-manager v0.0.0 // indirect k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1 // indirect diff --git a/pkg/daemons/executor/embed.go b/pkg/daemons/executor/embed.go index c09dd8277bae..2db37591e788 100644 --- a/pkg/daemons/executor/embed.go +++ b/pkg/daemons/executor/embed.go @@ -5,9 +5,12 @@ package executor import ( "context" + "flag" "net/http" "runtime" "runtime/debug" + "strconv" + "time" "github.com/k3s-io/k3s/pkg/cli/cmds" daemonconfig "github.com/k3s-io/k3s/pkg/daemons/config" @@ -31,6 +34,7 @@ import ( cloudcontrollerconfig "k8s.io/cloud-provider/app/config" ccmopt "k8s.io/cloud-provider/options" cliflag "k8s.io/component-base/cli/flag" + "k8s.io/klog/v2" "k8s.io/kubernetes/cmd/kube-apiserver/app" cmapp "k8s.io/kubernetes/cmd/kube-controller-manager/app" proxy "k8s.io/kubernetes/cmd/kube-proxy/app" @@ -47,6 +51,26 @@ func init() { func (e *Embedded) Bootstrap(ctx context.Context, nodeConfig *daemonconfig.Node, cfg cmds.Agent) error { e.nodeConfig = nodeConfig + + go func() { + // Ensure that the log verbosity remains set to the configured level by resetting it at 1-second intervals + // for the first 2 minutes that K3s is starting up. This is necessary because each of the Kubernetes + // components will initialize klog and reset the verbosity flag when they are starting. + logCtx, cancel := context.WithTimeout(ctx, time.Second*120) + defer cancel() + + klog.InitFlags(nil) + for { + flag.Set("v", strconv.Itoa(cmds.LogConfig.VLevel)) + + select { + case <-time.After(time.Second): + case <-logCtx.Done(): + return + } + } + }() + return nil }