Skip to content

Commit

Permalink
Added custom error message when wrong file is provided with KUBECONFIG (
Browse files Browse the repository at this point in the history
kubernetes#78185)

* Added custom error message when wrong file is provided with KUBECONFIG

* Modified test case

* Updated the code to warn the missing files

* Renamed the variable
  • Loading branch information
Himanshu Pandey authored and k8s-ci-robot committed Jul 11, 2019
1 parent 8a1c9e2 commit a5eedcd
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions staging/src/k8s.io/client-go/tools/clientcmd/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ type ClientConfigLoadingRules struct {
// DefaultClientConfig is an optional field indicating what rules to use to calculate a default configuration.
// This should match the overrides passed in to ClientConfig loader.
DefaultClientConfig ClientConfig

// 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
}

// ClientConfigLoadingRules implements the ClientConfigLoader interface.
Expand All @@ -136,20 +140,23 @@ var _ ClientConfigLoader = &ClientConfigLoadingRules{}
// use this constructor
func NewDefaultClientConfigLoadingRules() *ClientConfigLoadingRules {
chain := []string{}
warnIfAllMissing := false

envVarFiles := os.Getenv(RecommendedConfigPathEnvVar)
if len(envVarFiles) != 0 {
fileList := filepath.SplitList(envVarFiles)
// prevent the same path load multiple times
chain = append(chain, deduplicate(fileList)...)
warnIfAllMissing = true

} else {
chain = append(chain, RecommendedHomeFile)
}

return &ClientConfigLoadingRules{
Precedence: chain,
MigrationRules: currentMigrationRules(),
Precedence: chain,
MigrationRules: currentMigrationRules(),
WarnIfAllMissing: warnIfAllMissing,
}
}

Expand All @@ -172,6 +179,7 @@ func (rules *ClientConfigLoadingRules) Load() (*clientcmdapi.Config, error) {
}

errlist := []error{}
missingList := []string{}

kubeConfigFiles := []string{}

Expand All @@ -195,10 +203,14 @@ func (rules *ClientConfigLoadingRules) Load() (*clientcmdapi.Config, error) {
}

config, err := LoadFromFile(filename)

if os.IsNotExist(err) {
// skip missing files
// Add to the missing list to produce a warning
missingList = append(missingList, filename)
continue
}

if err != nil {
errlist = append(errlist, fmt.Errorf("Error loading config file \"%s\": %v", filename, err))
continue
Expand All @@ -207,6 +219,10 @@ func (rules *ClientConfigLoadingRules) Load() (*clientcmdapi.Config, error) {
kubeconfigs = append(kubeconfigs, config)
}

if rules.WarnIfAllMissing && len(missingList) > 0 && len(kubeconfigs) == 0 {
klog.Warningf("Config not found: %s", strings.Join(missingList, ", "))
}

// first merge all of our maps
mapConfig := clientcmdapi.NewConfig()

Expand Down

0 comments on commit a5eedcd

Please sign in to comment.