From 4033e64bf1878386a0c5ea4420b7555adebda4a4 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Fri, 1 Apr 2022 15:22:37 +0200 Subject: [PATCH] kubelet: initialize logging even sooner, return error After the removal of the dynamic kubelet configuration feature it became possible to initialize logging directly after configuration parsing. The advantage is that logs emitted by kubeletconfigvalidation.ValidateKubeletConfiguration and `klog.InfoS("unsupported configuration ...` already use the intended log output. After the code was originally added, Run was replaced by RunE. Taking advantage of that and returning an error is cleaner. --- cmd/kubelet/app/server.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/cmd/kubelet/app/server.go b/cmd/kubelet/app/server.go index 8713f6e822a..faa83d469f9 100644 --- a/cmd/kubelet/app/server.go +++ b/cmd/kubelet/app/server.go @@ -211,6 +211,14 @@ HTTP server: The kubelet can also listen for HTTP and respond to a simple API } } + // Config and flags parsed, now we can initialize logging. + logs.InitLogs() + logOption := &logs.Options{Config: kubeletConfig.Logging} + if err := logOption.ValidateAndApply(utilfeature.DefaultFeatureGate); err != nil { + return fmt.Errorf("failed to initialize logging: %v", err) + } + cliflag.PrintFlags(cleanFlagSet) + // We always validate the local configuration (command line + config file). // This is the default "last-known-good" config for dynamic config, and must always remain valid. if err := kubeletconfigvalidation.ValidateKubeletConfiguration(kubeletConfig); err != nil { @@ -228,15 +236,6 @@ HTTP server: The kubelet can also listen for HTTP and respond to a simple API return fmt.Errorf("cannot set feature gate %v to %v, feature is locked to %v", features.DynamicKubeletConfig, true, false) } - // Config and flags parsed, now we can initialize logging. - logs.InitLogs() - logOption := &logs.Options{Config: kubeletConfig.Logging} - if err := logOption.ValidateAndApply(utilfeature.DefaultFeatureGate); err != nil { - klog.ErrorS(err, "Failed to initialize logging") - os.Exit(1) - } - cliflag.PrintFlags(cleanFlagSet) - // construct a KubeletServer from kubeletFlags and kubeletConfig kubeletServer := &options.KubeletServer{ KubeletFlags: *kubeletFlags,