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.
This commit is contained in:
Patrick Ohly 2022-04-01 15:22:37 +02:00
parent 19c8a21271
commit 4033e64bf1

View File

@ -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). // 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. // This is the default "last-known-good" config for dynamic config, and must always remain valid.
if err := kubeletconfigvalidation.ValidateKubeletConfiguration(kubeletConfig); err != nil { 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) 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 // construct a KubeletServer from kubeletFlags and kubeletConfig
kubeletServer := &options.KubeletServer{ kubeletServer := &options.KubeletServer{
KubeletFlags: *kubeletFlags, KubeletFlags: *kubeletFlags,