From 24500c1c5071622a18e8641f22a0970300bcbe7e Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Wed, 4 Dec 2024 15:39:14 +0100 Subject: [PATCH] client-go clientcmd: exclude from structured, contextual logging The only log output is for error messages which should normally not occur. It's also likely that users expect to see exactly those messages, so it's better to not touch them. Kubernetes-commit: 8701f481ff6e24257021f07afcec67df2aef27dc --- tools/clientcmd/client_config.go | 2 ++ tools/clientcmd/config.go | 1 + tools/clientcmd/loader.go | 2 ++ tools/clientcmd/loader_test.go | 3 +++ tools/clientcmd/merged_client_builder.go | 2 ++ 5 files changed, 10 insertions(+) diff --git a/tools/clientcmd/client_config.go b/tools/clientcmd/client_config.go index ed35891e5..efac2830b 100644 --- a/tools/clientcmd/client_config.go +++ b/tools/clientcmd/client_config.go @@ -679,11 +679,13 @@ func (config *inClusterClientConfig) Possible() bool { // to the default config. func BuildConfigFromFlags(masterUrl, kubeconfigPath string) (*restclient.Config, error) { if kubeconfigPath == "" && masterUrl == "" { + //nolint:logcheck // A helper function like this should not log. But this is probably part of the the established client-go API and not worth changing. klog.Warning("Neither --kubeconfig nor --master was specified. Using the inClusterConfig. This might not work.") kubeconfig, err := restclient.InClusterConfig() if err == nil { return kubeconfig, nil } + //nolint:logcheck // A helper function like this should not log. But this is probably part of the the established client-go API and not worth changing. klog.Warning("error creating inClusterConfig, falling back to default config: ", err) } return NewNonInteractiveDeferredLoadingClientConfig( diff --git a/tools/clientcmd/config.go b/tools/clientcmd/config.go index 2cd213ccb..828ac974d 100644 --- a/tools/clientcmd/config.go +++ b/tools/clientcmd/config.go @@ -492,6 +492,7 @@ func getConfigFromFile(filename string) (*clientcmdapi.Config, error) { func GetConfigFromFileOrDie(filename string) *clientcmdapi.Config { config, err := getConfigFromFile(filename) if err != nil { + //nolint:logcheck // A helper function like this should not log. But this is probably part of the the established client-go API and not worth changing. klog.FatalDepth(1, err) } diff --git a/tools/clientcmd/loader.go b/tools/clientcmd/loader.go index b127e2e08..d1d0a8295 100644 --- a/tools/clientcmd/loader.go +++ b/tools/clientcmd/loader.go @@ -137,6 +137,7 @@ type WarningHandler func(error) func (handler WarningHandler) Warn(err error) { if handler == nil { + //nolint:logcheck // This is the fallback when logging is not initialized. With nothing provided, using the global logger is the only option. klog.V(1).Info(err) } else { handler(err) @@ -402,6 +403,7 @@ func LoadFromFile(filename string) (*clientcmdapi.Config, error) { if err != nil { return nil, err } + //nolint:logcheck // A helper function like this should not log. But this is probably part of the the established client-go API and not worth changing. klog.V(6).Infoln("Config loaded from file: ", filename) // set LocationOfOrigin on every Cluster, User, and Context diff --git a/tools/clientcmd/loader_test.go b/tools/clientcmd/loader_test.go index e9d78bbe8..22006aa6c 100644 --- a/tools/clientcmd/loader_test.go +++ b/tools/clientcmd/loader_test.go @@ -122,6 +122,7 @@ func TestNonExistentCommandLineFile(t *testing.T) { } } +//nolint:logcheck // Tests klog APIs. func TestToleratingMissingFiles(t *testing.T) { envVarValue := "bogus" loadingRules := ClientConfigLoadingRules{ @@ -146,6 +147,7 @@ func TestToleratingMissingFiles(t *testing.T) { } } +//nolint:logcheck // Tests klog APIs. func TestWarningMissingFiles(t *testing.T) { envVarValue := "bogus" t.Setenv(RecommendedConfigPathEnvVar, envVarValue) @@ -171,6 +173,7 @@ func TestWarningMissingFiles(t *testing.T) { } } +//nolint:logcheck // Tests klog APIs. func TestNoWarningMissingFiles(t *testing.T) { envVarValue := "bogus" t.Setenv(RecommendedConfigPathEnvVar, envVarValue) diff --git a/tools/clientcmd/merged_client_builder.go b/tools/clientcmd/merged_client_builder.go index 0fc2fd0a0..db7bf0a82 100644 --- a/tools/clientcmd/merged_client_builder.go +++ b/tools/clientcmd/merged_client_builder.go @@ -118,6 +118,7 @@ func (config *DeferredLoadingClientConfig) ClientConfig() (*restclient.Config, e // check for in-cluster configuration and use it if config.icc.Possible() { + //nolint:logcheck // A helper function like this should not log. But this is probably part of the the established client-go API and not worth changing. klog.V(4).Infof("Using in-cluster configuration") return config.icc.ClientConfig() } @@ -160,6 +161,7 @@ func (config *DeferredLoadingClientConfig) Namespace() (string, bool, error) { } } + //nolint:logcheck // A helper function like this should not log. But this is probably part of the the established client-go API and not worth changing. klog.V(4).Infof("Using in-cluster namespace") // allow the namespace from the service account token directory to be used.