Unify command line namespace resolution
This change allows the namespace in kubeconfig to be overridden by specifying the namespace in the spec file. If namespace is explicitly provided in the command line flags and the spec file has a different namespace, this will cause an error.
This commit is contained in:
@@ -47,8 +47,10 @@ type ClientConfig interface {
|
||||
RawConfig() (clientcmdapi.Config, error)
|
||||
// ClientConfig returns a complete client config
|
||||
ClientConfig() (*client.Config, error)
|
||||
// Namespace returns the namespace resulting from the merged result of all overrides
|
||||
Namespace() (string, error)
|
||||
// Namespace returns the namespace resulting from the merged
|
||||
// result of all overrides and a boolean indicating if it was
|
||||
// overridden
|
||||
Namespace() (string, bool, error)
|
||||
}
|
||||
|
||||
// DirectClientConfig is a ClientConfig interface that is backed by a clientcmdapi.Config, options overrides, and an optional fallbackReader for auth information
|
||||
@@ -207,17 +209,22 @@ func canIdentifyUser(config client.Config) bool {
|
||||
}
|
||||
|
||||
// Namespace implements KubeConfig
|
||||
func (config DirectClientConfig) Namespace() (string, error) {
|
||||
func (config DirectClientConfig) Namespace() (string, bool, error) {
|
||||
if err := config.ConfirmUsable(); err != nil {
|
||||
return "", err
|
||||
return "", false, err
|
||||
}
|
||||
|
||||
configContext := config.getContext()
|
||||
|
||||
if len(configContext.Namespace) == 0 {
|
||||
return api.NamespaceDefault, nil
|
||||
return api.NamespaceDefault, false, nil
|
||||
}
|
||||
return configContext.Namespace, nil
|
||||
|
||||
overridden := false
|
||||
if config.overrides != nil && config.overrides.Context.Namespace != "" {
|
||||
overridden = true
|
||||
}
|
||||
return configContext.Namespace, overridden, nil
|
||||
}
|
||||
|
||||
// ConfirmUsable looks a particular context and determines if that particular part of the config is useable. There might still be errors in the config,
|
||||
|
@@ -52,17 +52,32 @@ func TestMergeContext(t *testing.T) {
|
||||
const namespace = "overriden-namespace"
|
||||
|
||||
config := createValidTestConfig()
|
||||
clientBuilder := NewNonInteractiveClientConfig(*config, "clean", &ConfigOverrides{
|
||||
clientBuilder := NewNonInteractiveClientConfig(*config, "clean", &ConfigOverrides{})
|
||||
|
||||
_, overridden, err := clientBuilder.Namespace()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if overridden {
|
||||
t.Error("Expected namespace to not be overridden")
|
||||
}
|
||||
|
||||
clientBuilder = NewNonInteractiveClientConfig(*config, "clean", &ConfigOverrides{
|
||||
Context: clientcmdapi.Context{
|
||||
Namespace: namespace,
|
||||
},
|
||||
})
|
||||
|
||||
actual, err := clientBuilder.Namespace()
|
||||
actual, overridden, err := clientBuilder.Namespace()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if !overridden {
|
||||
t.Error("Expected namespace to be overridden")
|
||||
}
|
||||
|
||||
matchStringArg(namespace, actual, t)
|
||||
}
|
||||
|
||||
|
@@ -94,10 +94,10 @@ func (config DeferredLoadingClientConfig) ClientConfig() (*client.Config, error)
|
||||
}
|
||||
|
||||
// Namespace implements KubeConfig
|
||||
func (config DeferredLoadingClientConfig) Namespace() (string, error) {
|
||||
func (config DeferredLoadingClientConfig) Namespace() (string, bool, error) {
|
||||
mergedKubeConfig, err := config.createClientConfig()
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", false, err
|
||||
}
|
||||
|
||||
return mergedKubeConfig.Namespace()
|
||||
|
Reference in New Issue
Block a user