Added custom error message when wrong file is provided with KUBECONFIG (#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
This commit is contained in:
Himanshu Pandey 2019-07-10 20:24:54 -07:00 committed by Kubernetes Prow Robot
parent 8a1c9e2b41
commit a5eedcde61

View File

@ -127,6 +127,10 @@ type ClientConfigLoadingRules struct {
// DefaultClientConfig is an optional field indicating what rules to use to calculate a default configuration. // 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. // This should match the overrides passed in to ClientConfig loader.
DefaultClientConfig ClientConfig 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. // ClientConfigLoadingRules implements the ClientConfigLoader interface.
@ -136,20 +140,23 @@ var _ ClientConfigLoader = &ClientConfigLoadingRules{}
// use this constructor // use this constructor
func NewDefaultClientConfigLoadingRules() *ClientConfigLoadingRules { func NewDefaultClientConfigLoadingRules() *ClientConfigLoadingRules {
chain := []string{} chain := []string{}
warnIfAllMissing := false
envVarFiles := os.Getenv(RecommendedConfigPathEnvVar) envVarFiles := os.Getenv(RecommendedConfigPathEnvVar)
if len(envVarFiles) != 0 { if len(envVarFiles) != 0 {
fileList := filepath.SplitList(envVarFiles) fileList := filepath.SplitList(envVarFiles)
// prevent the same path load multiple times // prevent the same path load multiple times
chain = append(chain, deduplicate(fileList)...) chain = append(chain, deduplicate(fileList)...)
warnIfAllMissing = true
} else { } else {
chain = append(chain, RecommendedHomeFile) chain = append(chain, RecommendedHomeFile)
} }
return &ClientConfigLoadingRules{ return &ClientConfigLoadingRules{
Precedence: chain, Precedence: chain,
MigrationRules: currentMigrationRules(), MigrationRules: currentMigrationRules(),
WarnIfAllMissing: warnIfAllMissing,
} }
} }
@ -172,6 +179,7 @@ func (rules *ClientConfigLoadingRules) Load() (*clientcmdapi.Config, error) {
} }
errlist := []error{} errlist := []error{}
missingList := []string{}
kubeConfigFiles := []string{} kubeConfigFiles := []string{}
@ -195,10 +203,14 @@ func (rules *ClientConfigLoadingRules) Load() (*clientcmdapi.Config, error) {
} }
config, err := LoadFromFile(filename) config, err := LoadFromFile(filename)
if os.IsNotExist(err) { if os.IsNotExist(err) {
// skip missing files // skip missing files
// Add to the missing list to produce a warning
missingList = append(missingList, filename)
continue continue
} }
if err != nil { if err != nil {
errlist = append(errlist, fmt.Errorf("Error loading config file \"%s\": %v", filename, err)) errlist = append(errlist, fmt.Errorf("Error loading config file \"%s\": %v", filename, err))
continue continue
@ -207,6 +219,10 @@ func (rules *ClientConfigLoadingRules) Load() (*clientcmdapi.Config, error) {
kubeconfigs = append(kubeconfigs, config) 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 // first merge all of our maps
mapConfig := clientcmdapi.NewConfig() mapConfig := clientcmdapi.NewConfig()