From 5dc6fc0d22b7c73d51386f0e806922debf574b72 Mon Sep 17 00:00:00 2001 From: Charly Molter Date: Tue, 9 Jan 2024 19:04:32 +0100 Subject: [PATCH] add possibility to setup a custom logger for kubectl configuration This enables users to not log messages like: "Configuring Kubernetes client using config file" Fix #1383 Signed-off-by: Charly Molter --- modules/k8s/client.go | 10 +++++----- modules/k8s/kubectl_options.go | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/modules/k8s/client.go b/modules/k8s/client.go index 34b202593..17726ec10 100644 --- a/modules/k8s/client.go +++ b/modules/k8s/client.go @@ -32,25 +32,25 @@ func GetKubernetesClientFromOptionsE(t testing.TestingT, options *KubectlOptions if err != nil { return nil, err } - options.Logger.Logf(t, "Configuring Kubernetes client to use the in-cluster serviceaccount token") + options.ConfigLogger.Logf(t, "Configuring Kubernetes client to use the in-cluster serviceaccount token") } else if options.RestConfig != nil { config = options.RestConfig - options.Logger.Logf(t, "Configuring Kubernetes client to use provided rest config object set with API server address: %s", config.Host) + options.ConfigLogger.Logf(t, "Configuring Kubernetes client to use provided rest config object set with API server address: %s", config.Host) } else { kubeConfigPath, err := options.GetConfigPath(t) if err != nil { return nil, err } - options.Logger.Logf(t, "Configuring Kubernetes client using config file %s with context %s", kubeConfigPath, options.ContextName) + options.ConfigLogger.Logf(t, "Configuring Kubernetes client using config file %s with context %s", kubeConfigPath, options.ContextName) // Load API config (instead of more low level ClientConfig) config, err = LoadApiClientConfigE(kubeConfigPath, options.ContextName) if err != nil { - options.Logger.Logf(t, "Error loading api client config, falling back to in-cluster authentication via serviceaccount token: %s", err) + options.ConfigLogger.Logf(t, "Error loading api client config, falling back to in-cluster authentication via serviceaccount token: %s", err) config, err = rest.InClusterConfig() if err != nil { return nil, err } - options.Logger.Logf(t, "Configuring Kubernetes client to use the in-cluster serviceaccount token") + options.ConfigLogger.Logf(t, "Configuring Kubernetes client to use the in-cluster serviceaccount token") } } diff --git a/modules/k8s/kubectl_options.go b/modules/k8s/kubectl_options.go index 731a455bf..215ef723e 100644 --- a/modules/k8s/kubectl_options.go +++ b/modules/k8s/kubectl_options.go @@ -15,6 +15,8 @@ type KubectlOptions struct { InClusterAuth bool RestConfig *rest.Config Logger *logger.Logger + // ConfigLogger is useful to set a different logger when configuring kubectl with a config. This can significantly reduce verbosity of tests + ConfigLogger *logger.Logger } // NewKubectlOptions will return a pointer to new instance of KubectlOptions with the configured options