Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add possibility to setup a custom logger for kubectl configuration #1384

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions modules/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, what will happen if the logger will not be provided?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logger falls back to the Default logger if it's nil. This also enables backward compatibility

// methods can be called on (typed) nil pointers. In this case, use the Default function to log. This enables the
// caller to do `var l *Logger` and then use the logger already.
if l == nil || l.l == nil {
Default.Logf(t, format, args...)
return
}
l.l.Logf(t, format, args...)

}

// NewKubectlOptions will return a pointer to new instance of KubectlOptions with the configured options
Expand Down