MCPcopy
hub / github.com/kubernetes/client-go / writeCurrentContext

Function writeCurrentContext

tools/clientcmd/config.go:372–428  ·  view source on GitHub ↗

writeCurrentContext takes three possible paths. If newCurrentContext is the same as the startingConfig's current context, then we exit. If newCurrentContext has a value, then that value is written into the default destination file. If newCurrentContext is empty, then we find the config file that is

(configAccess ConfigAccess, newCurrentContext string)

Source from the content-addressed store, hash-verified

370// If newCurrentContext has a value, then that value is written into the default destination file.
371// If newCurrentContext is empty, then we find the config file that is setting the CurrentContext and clear the value from that file
372func writeCurrentContext(configAccess ConfigAccess, newCurrentContext string) error {
373 if startingConfig, err := configAccess.GetStartingConfig(); err != nil {
374 return err
375 } else if startingConfig.CurrentContext == newCurrentContext {
376 return nil
377 }
378
379 if configAccess.IsExplicitFile() {
380 file := configAccess.GetExplicitFile()
381 currConfig, err := getConfigFromFile(file)
382 if err != nil {
383 return err
384 }
385 currConfig.CurrentContext = newCurrentContext
386 if err := WriteToFile(*currConfig, file); err != nil {
387 return err
388 }
389
390 return nil
391 }
392
393 if len(newCurrentContext) > 0 {
394 destinationFile := configAccess.GetDefaultFilename()
395 config, err := getConfigFromFile(destinationFile)
396 if err != nil {
397 return err
398 }
399 config.CurrentContext = newCurrentContext
400
401 if err := WriteToFile(*config, destinationFile); err != nil {
402 return err
403 }
404
405 return nil
406 }
407
408 // we're supposed to be clearing the current context. We need to find the first spot in the chain that is setting it and clear it
409 for _, file := range configAccess.GetLoadingPrecedence() {
410 if _, err := os.Stat(file); err == nil {
411 currConfig, err := getConfigFromFile(file)
412 if err != nil {
413 return err
414 }
415
416 if len(currConfig.CurrentContext) > 0 {
417 currConfig.CurrentContext = newCurrentContext
418 if err := WriteToFile(*currConfig, file); err != nil {
419 return err
420 }
421
422 return nil
423 }
424 }
425 }
426
427 return errors.New("no config found to write context")
428}
429

Callers 1

ModifyConfigFunction · 0.85

Calls 7

getConfigFromFileFunction · 0.85
WriteToFileFunction · 0.85
GetStartingConfigMethod · 0.65
IsExplicitFileMethod · 0.65
GetExplicitFileMethod · 0.65
GetDefaultFilenameMethod · 0.65
GetLoadingPrecedenceMethod · 0.65

Tested by

no test coverage detected