Skip to content

Commit a5eedcd

Browse files
Himanshu Pandeyk8s-ci-robot
authored andcommitted
Added custom error message when wrong file is provided with KUBECONFIG (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
1 parent 8a1c9e2 commit a5eedcd

File tree

1 file changed

+18
-2
lines changed
  • staging/src/k8s.io/client-go/tools/clientcmd

1 file changed

+18
-2
lines changed

staging/src/k8s.io/client-go/tools/clientcmd/loader.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ type ClientConfigLoadingRules struct {
127127
// DefaultClientConfig is an optional field indicating what rules to use to calculate a default configuration.
128128
// This should match the overrides passed in to ClientConfig loader.
129129
DefaultClientConfig ClientConfig
130+
131+
// WarnIfAllMissing indicates whether the configuration files pointed by KUBECONFIG environment variable are present or not.
132+
// In case of missing files, it warns the user about the missing files.
133+
WarnIfAllMissing bool
130134
}
131135

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

140145
envVarFiles := os.Getenv(RecommendedConfigPathEnvVar)
141146
if len(envVarFiles) != 0 {
142147
fileList := filepath.SplitList(envVarFiles)
143148
// prevent the same path load multiple times
144149
chain = append(chain, deduplicate(fileList)...)
150+
warnIfAllMissing = true
145151

146152
} else {
147153
chain = append(chain, RecommendedHomeFile)
148154
}
149155

150156
return &ClientConfigLoadingRules{
151-
Precedence: chain,
152-
MigrationRules: currentMigrationRules(),
157+
Precedence: chain,
158+
MigrationRules: currentMigrationRules(),
159+
WarnIfAllMissing: warnIfAllMissing,
153160
}
154161
}
155162

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

174181
errlist := []error{}
182+
missingList := []string{}
175183

176184
kubeConfigFiles := []string{}
177185

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

197205
config, err := LoadFromFile(filename)
206+
198207
if os.IsNotExist(err) {
199208
// skip missing files
209+
// Add to the missing list to produce a warning
210+
missingList = append(missingList, filename)
200211
continue
201212
}
213+
202214
if err != nil {
203215
errlist = append(errlist, fmt.Errorf("Error loading config file \"%s\": %v", filename, err))
204216
continue
@@ -207,6 +219,10 @@ func (rules *ClientConfigLoadingRules) Load() (*clientcmdapi.Config, error) {
207219
kubeconfigs = append(kubeconfigs, config)
208220
}
209221

222+
if rules.WarnIfAllMissing && len(missingList) > 0 && len(kubeconfigs) == 0 {
223+
klog.Warningf("Config not found: %s", strings.Join(missingList, ", "))
224+
}
225+
210226
// first merge all of our maps
211227
mapConfig := clientcmdapi.NewConfig()
212228

0 commit comments

Comments
 (0)