Skip to content

Commit

Permalink
add possibility to setup a custom logger for kubectl configuration
Browse files Browse the repository at this point in the history
This enables users to not log messages like: "Configuring Kubernetes
client using config file"

Fix gruntwork-io#1383

Signed-off-by: Charly Molter <[email protected]>
  • Loading branch information
lahabana committed Sep 20, 2024
1 parent fc19428 commit 79b7c38
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 5 additions & 6 deletions modules/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
// See: https://github.com/kubernetes/client-go/issues/242
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"

"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/testing"
)

Expand All @@ -33,25 +32,25 @@ func GetKubernetesClientFromOptionsE(t testing.TestingT, options *KubectlOptions
if err != nil {
return nil, err
}
logger.Log(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
logger.Log(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
}
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 {
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
}
logger.Log(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")
}
}

Expand Down
2 changes: 2 additions & 0 deletions modules/k8s/kubectl_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 79b7c38

Please sign in to comment.