Add warning log callback in client-go loading rules (#117233)

* Add warning log callback in client-go loading rules

This provides a way to consumers use their own custom warning
mechanisms instead default klog warning.

* Use typed error instead plain string

* Fix interface change in unit test

Kubernetes-commit: 2ea6896f90c8b757d8a247d393b9a13fff2dab58
This commit is contained in:
Arda Güçlü
2023-05-24 19:08:50 +03:00
committed by Kubernetes Publisher
parent 2a5f18df73
commit 4cb373f7ca
4 changed files with 93 additions and 6 deletions

View File

@@ -128,6 +128,28 @@ type ClientConfigLoadingRules struct {
// WarnIfAllMissing indicates whether the configuration files pointed by KUBECONFIG environment variable are present or not.
// In case of missing files, it warns the user about the missing files.
WarnIfAllMissing bool
// Warner is the warning log callback to use in case of missing files.
Warner WarningHandler
}
// WarningHandler allows to set the logging function to use
type WarningHandler func(error)
func (handler WarningHandler) Warn(err error) {
if handler == nil {
klog.V(1).Info(err)
} else {
handler(err)
}
}
type MissingConfigError struct {
Missing []string
}
func (c MissingConfigError) Error() string {
return fmt.Sprintf("Config not found: %s", strings.Join(c.Missing, ", "))
}
// ClientConfigLoadingRules implements the ClientConfigLoader interface.
@@ -219,7 +241,7 @@ func (rules *ClientConfigLoadingRules) Load() (*clientcmdapi.Config, error) {
}
if rules.WarnIfAllMissing && len(missingList) > 0 && len(kubeconfigs) == 0 {
klog.Warningf("Config not found: %s", strings.Join(missingList, ", "))
rules.Warner.Warn(MissingConfigError{Missing: missingList})
}
// first merge all of our maps