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

4
go.mod
View File

@ -24,7 +24,7 @@ require (
golang.org/x/time v0.3.0
google.golang.org/protobuf v1.30.0
k8s.io/api v0.0.0-20230515170019-2f9553831ec2
k8s.io/apimachinery v0.0.0-20230516234833-8d8634786e1c
k8s.io/apimachinery v0.0.0-20230522181456-f5e92bb27cb2
k8s.io/klog/v2 v2.100.1
k8s.io/kube-openapi v0.0.0-20230515203736-54b630e78af5
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
@ -61,5 +61,5 @@ require (
replace (
k8s.io/api => k8s.io/api v0.0.0-20230515170019-2f9553831ec2
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20230516234833-8d8634786e1c
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20230522181456-f5e92bb27cb2
)

4
go.sum
View File

@ -481,8 +481,8 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/api v0.0.0-20230515170019-2f9553831ec2 h1:ZHyoWIXElhnLI03GkOsebYT1uqYowGjuVCbxBqcjjGw=
k8s.io/api v0.0.0-20230515170019-2f9553831ec2/go.mod h1:qZNz3vKcyZb5wFaBlHqUjHk7D5XhIaXu/d/df6PjNKk=
k8s.io/apimachinery v0.0.0-20230516234833-8d8634786e1c h1:AlsdVCajPXlBLR+F/9NTM6ATjiGKrcy0xGZdwN7dF5U=
k8s.io/apimachinery v0.0.0-20230516234833-8d8634786e1c/go.mod h1:duqOQ9WfGiD/x7zVH/k6rR0s2h9fq5UwPoY53R7iLQ4=
k8s.io/apimachinery v0.0.0-20230522181456-f5e92bb27cb2 h1:Rz67ooRjdqtkkM/i9q65H6x+ZKb5Kcxwvkb1EZ7A8TY=
k8s.io/apimachinery v0.0.0-20230522181456-f5e92bb27cb2/go.mod h1:duqOQ9WfGiD/x7zVH/k6rR0s2h9fq5UwPoY53R7iLQ4=
k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20230515203736-54b630e78af5 h1:azYPdzztXxPSa8wb+hksEKayiz0o+PPisO/d+QhWnoo=

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

View File

@ -18,6 +18,7 @@ package clientcmd
import (
"bytes"
"flag"
"fmt"
"os"
"path"
@ -32,6 +33,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest"
"k8s.io/klog/v2"
)
var (
@ -120,14 +122,77 @@ func TestNonExistentCommandLineFile(t *testing.T) {
}
func TestToleratingMissingFiles(t *testing.T) {
envVarValue := "bogus"
loadingRules := ClientConfigLoadingRules{
Precedence: []string{"bogus1", "bogus2", "bogus3"},
WarnIfAllMissing: true,
Warner: func(err error) { klog.Warning(err) },
}
buffer := &bytes.Buffer{}
klog.LogToStderr(false)
klog.SetOutput(buffer)
_, err := loadingRules.Load()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
klog.Flush()
expectedLog := fmt.Sprintf("Config not found: %s", envVarValue)
if !strings.Contains(buffer.String(), expectedLog) {
t.Fatalf("expected log: \"%s\"", expectedLog)
}
}
func TestWarningMissingFiles(t *testing.T) {
envVarValue := "bogus"
os.Setenv(RecommendedConfigPathEnvVar, envVarValue)
loadingRules := NewDefaultClientConfigLoadingRules()
buffer := &bytes.Buffer{}
flags := &flag.FlagSet{}
klog.InitFlags(flags)
flags.Set("v", "1")
klog.LogToStderr(false)
klog.SetOutput(buffer)
_, err := loadingRules.Load()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
klog.Flush()
expectedLog := fmt.Sprintf("Config not found: %s", envVarValue)
if !strings.Contains(buffer.String(), expectedLog) {
t.Fatalf("expected log: \"%s\"", expectedLog)
}
}
func TestNoWarningMissingFiles(t *testing.T) {
envVarValue := "bogus"
os.Setenv(RecommendedConfigPathEnvVar, envVarValue)
loadingRules := NewDefaultClientConfigLoadingRules()
buffer := &bytes.Buffer{}
flags := &flag.FlagSet{}
klog.InitFlags(flags)
flags.Set("v", "0")
klog.LogToStderr(false)
klog.SetOutput(buffer)
_, err := loadingRules.Load()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
klog.Flush()
logNotExpected := fmt.Sprintf("Config not found: %s", envVarValue)
if strings.Contains(buffer.String(), logNotExpected) {
t.Fatalf("log not expected: \"%s\"", logNotExpected)
}
}
func TestErrorReadingFile(t *testing.T) {