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
[WIP] Raise error if the user has multiple contexts but no current set #94905
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,9 @@ func (o *PathOptions) GetStartingConfig() (*clientcmdapi.Config, error) { | |
return clientcmdapi.NewConfig(), nil | ||
} | ||
if err != nil { | ||
if errors.Is(err, ErrNoContextSet) { | ||
return &rawConfig, err | ||
} | ||
return nil, err | ||
} | ||
|
||
|
@@ -164,7 +167,7 @@ func NewDefaultPathOptions() *PathOptions { | |
// (no nil strings), we're forced have separate handling for them. In the kubeconfig cases, newConfig should have at most one difference, | ||
// that means that this code will only write into a single file. If you want to relativizePaths, you must provide a fully qualified path in any | ||
// modified element. | ||
func ModifyConfig(configAccess ConfigAccess, newConfig clientcmdapi.Config, relativizePaths bool) error { | ||
func ModifyConfig(configAccess ConfigAccess, newConfig clientcmdapi.Config, relativizePaths bool, isSetContext bool) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is changing external API in a library we provide, you can't do that b/c you're breaking all the existing users. |
||
if UseModifyConfigLock { | ||
possibleSources := configAccess.GetLoadingPrecedence() | ||
// sort the possible kubeconfig files so we always "lock" in the same order | ||
|
@@ -179,8 +182,13 @@ func ModifyConfig(configAccess ConfigAccess, newConfig clientcmdapi.Config, rela | |
} | ||
|
||
startingConfig, err := configAccess.GetStartingConfig() | ||
|
||
if err != nil { | ||
return err | ||
if isSetContext && errors.Is(err, ErrNoContextSet) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to be careful with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My intent here was to keep the old behavior. Other errors should be reported as before. I added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I missed some detail why this is necessary. However, my main point is to discuss this change in an issue before investing more time into the implementation. It's possible that there is a reason for the current behavior that I don't know. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand that the change should be discussed in an issue first. This is why I created one and initially sent a very minimal patch. |
||
|
||
} else { | ||
return err | ||
} | ||
} | ||
|
||
// We need to find all differences, locate their original files, read a partial config to modify only that stanza and write out the file. | ||
|
@@ -191,8 +199,14 @@ func ModifyConfig(configAccess ConfigAccess, newConfig clientcmdapi.Config, rela | |
} | ||
|
||
if startingConfig.CurrentContext != newConfig.CurrentContext { | ||
if err := writeCurrentContext(configAccess, newConfig.CurrentContext); err != nil { | ||
return err | ||
if err := writeCurrentContext(configAccess, newConfig.CurrentContext, isSetContext); | ||
|
||
err != nil { | ||
if isSetContext && errors.Is(err, ErrNoContextSet) { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. empty if blocks are unidiomatic in Go. Try to refactor. |
||
} else { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
|
@@ -371,7 +385,7 @@ func (p *persister) Persist(config map[string]string) error { | |
authInfo, ok := newConfig.AuthInfos[p.user] | ||
if ok && authInfo.AuthProvider != nil { | ||
authInfo.AuthProvider.Config = config | ||
ModifyConfig(p.configAccess, *newConfig, false) | ||
ModifyConfig(p.configAccess, *newConfig, false, false) | ||
} | ||
return nil | ||
} | ||
|
@@ -380,9 +394,12 @@ func (p *persister) Persist(config map[string]string) error { | |
// 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 setting the CurrentContext and clear the value from that file | ||
func writeCurrentContext(configAccess ConfigAccess, newCurrentContext string) error { | ||
if startingConfig, err := configAccess.GetStartingConfig(); err != nil { | ||
return err | ||
func writeCurrentContext(configAccess ConfigAccess, newCurrentContext string, isSetContext bool) error { | ||
startingConfig, err := configAccess.GetStartingConfig() | ||
if err != nil { | ||
if !(isSetContext == true && errors.Is(err, ErrNoContextSet)){ | ||
return err | ||
} | ||
} else if startingConfig.CurrentContext == newCurrentContext { | ||
return nil | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -244,6 +244,9 @@ func (rules *ClientConfigLoadingRules) Load() (*clientcmdapi.Config, error) { | |||||||
mergo.MergeWithOverwrite(config, mapConfig) | ||||||||
mergo.MergeWithOverwrite(config, nonMapConfig) | ||||||||
|
||||||||
if (len(config.Contexts) > 1 && config.CurrentContext == "") { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This seems to be the wrong place to add this logic though. The I think the better place is to add it in
|
||||||||
errlist = append(errlist, ErrNoContextSet) | ||||||||
} | ||||||||
if rules.ResolvePaths() { | ||||||||
if err := ResolveLocalPaths(config); err != nil { | ||||||||
errlist = append(errlist, err) | ||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -29,8 +29,9 @@ import ( | |||||
) | ||||||
|
||||||
var ( | ||||||
ErrNoContext = errors.New("no context chosen") | ||||||
ErrEmptyConfig = NewEmptyConfigError("no configuration has been provided, try setting KUBERNETES_MASTER environment variable") | ||||||
ErrNoContext = errors.New("no context chosen") | ||||||
ErrNoContextSet = errors.New("Multiple contexts found. However, no current-context is set in the configuration") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Go errors should not be sentences, because it reads better if they are chained. Therefore also start in lower case. |
||||||
ErrEmptyConfig = NewEmptyConfigError("no configuration has been provided, try setting KUBERNETES_MASTER environment variable") | ||||||
// message is for consistency with old behavior | ||||||
ErrEmptyCluster = errors.New("cluster has no server defined") | ||||||
) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.