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 warning log callback in client-go loading rules #117233

Merged
merged 3 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion staging/src/k8s.io/client-go/tools/clientcmd/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ 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(...interface{})
ardaguclu marked this conversation as resolved.
Show resolved Hide resolved

func (handler WarningHandler) Warn(message string) {
if handler == nil {
klog.V(1).Info(message)
Copy link
Member

Choose a reason for hiding this comment

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

what is the difference in output between klog.Warningf and klog.V(1).Info?

Copy link
Member Author

Choose a reason for hiding this comment

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

klog.Warning prints;

W0524 09:11:16.915498   42167 loader.go:142] Config not found:

klog.V(1).Info prints(only after --verbosity >= 1);

I0524 08:48:15.896816   40664 loader.go:142] Config not found:

} else {
handler(message)
}
}

// ClientConfigLoadingRules implements the ClientConfigLoader interface.
Expand Down Expand Up @@ -219,7 +233,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(fmt.Sprintf("Config not found: %s", strings.Join(missingList, ", ")))
ardaguclu marked this conversation as resolved.
Show resolved Hide resolved
}

// first merge all of our maps
Expand Down
67 changes: 66 additions & 1 deletion staging/src/k8s.io/client-go/tools/clientcmd/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package clientcmd

import (
"bytes"
"flag"
"fmt"
"os"
"path"
Expand All @@ -32,6 +33,7 @@ import (
"k8s.io/apimachinery/pkg/util/diff"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
clientcmdlatest "k8s.io/client-go/tools/clientcmd/api/latest"
"k8s.io/klog/v2"
)

var (
Expand Down Expand Up @@ -120,14 +122,77 @@ func TestNonExistentCommandLineFile(t *testing.T) {
}

func TestToleratingMissingFiles(t *testing.T) {
envVarValue := "bogus"
loadingRules := ClientConfigLoadingRules{
Precedence: []string{"bogus1", "bogus2", "bogus3"},
Precedence: []string{"bogus1", "bogus2", "bogus3"},
WarnIfAllMissing: true,
Warner: klog.Warning,
}

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) {
Expand Down