Commit 60b63b7c authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #31767 from asalkeld/bad-context-error

Automatic merge from submit-queue Add a check in ConfirmUsable() to validate the contextName **What this PR does / why we need it**: When a context name is provided, but can't be found (miss spelling), it currently uses the defaults. This PR will cause the command to fail, to prevent unexpected side effects of using the wrong configuration. **Which issue this PR fixes** fixes #21062 **Special notes for your reviewer**: None **Release note**: ```release-note Error if a contextName is provided but not found in the kubeconfig. ```
parents e0d7425b e9cad12e
...@@ -265,6 +265,21 @@ func (config *DirectClientConfig) ConfigAccess() ConfigAccess { ...@@ -265,6 +265,21 @@ func (config *DirectClientConfig) ConfigAccess() ConfigAccess {
// but no errors in the sections requested or referenced. It does not return early so that it can find as many errors as possible. // but no errors in the sections requested or referenced. It does not return early so that it can find as many errors as possible.
func (config *DirectClientConfig) ConfirmUsable() error { func (config *DirectClientConfig) ConfirmUsable() error {
validationErrors := make([]error, 0) validationErrors := make([]error, 0)
var contextName string
if len(config.contextName) != 0 {
contextName = config.contextName
} else {
contextName = config.config.CurrentContext
}
if len(contextName) > 0 {
_, exists := config.config.Contexts[contextName]
if !exists {
validationErrors = append(validationErrors, &errContextNotFound{contextName})
}
}
validationErrors = append(validationErrors, validateAuthInfo(config.getAuthInfoName(), config.getAuthInfo())...) validationErrors = append(validationErrors, validateAuthInfo(config.getAuthInfoName(), config.getAuthInfo())...)
validationErrors = append(validationErrors, validateClusterInfo(config.getClusterName(), config.getCluster())...) validationErrors = append(validationErrors, validateClusterInfo(config.getClusterName(), config.getCluster())...)
// when direct client config is specified, and our only error is that no server is defined, we should // when direct client config is specified, and our only error is that no server is defined, we should
......
...@@ -20,10 +20,10 @@ import ( ...@@ -20,10 +20,10 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"reflect" "reflect"
"strings"
"testing" "testing"
"github.com/imdario/mergo" "github.com/imdario/mergo"
"k8s.io/kubernetes/pkg/client/restclient"
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api" clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
) )
...@@ -357,22 +357,20 @@ func TestCreateMissingContextNoDefault(t *testing.T) { ...@@ -357,22 +357,20 @@ func TestCreateMissingContextNoDefault(t *testing.T) {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Unexpected error: %v", err)
} }
} }
func TestCreateMissingContext(t *testing.T) { func TestCreateMissingContext(t *testing.T) {
const expectedErrorContains = "Context was not found for specified context" const expectedErrorContains = "context was not found for specified context: not-present"
config := createValidTestConfig() config := createValidTestConfig()
clientBuilder := NewNonInteractiveClientConfig(*config, "not-present", &ConfigOverrides{ clientBuilder := NewNonInteractiveClientConfig(*config, "not-present", &ConfigOverrides{
ClusterDefaults: DefaultCluster, ClusterDefaults: DefaultCluster,
}, nil) }, nil)
clientConfig, err := clientBuilder.ClientConfig() _, err := clientBuilder.ClientConfig()
if err != nil { if err == nil {
t.Fatalf("Unexpected error: %v", err) t.Fatalf("Expected error: %v", expectedErrorContains)
} }
if !strings.Contains(err.Error(), expectedErrorContains) {
expectedConfig := &restclient.Config{Host: clientConfig.Host} t.Fatalf("Expected error: %v, but got %v", expectedErrorContains, err)
if !reflect.DeepEqual(expectedConfig, clientConfig) {
t.Errorf("Expected %#v, got %#v", expectedConfig, clientConfig)
} }
} }
......
...@@ -49,12 +49,13 @@ func TestKubectlValidation(t *testing.T) { ...@@ -49,12 +49,13 @@ func TestKubectlValidation(t *testing.T) {
// Enable swagger api on master. // Enable swagger api on master.
components.KubeMaster.InstallSwaggerAPI() components.KubeMaster.InstallSwaggerAPI()
cluster := clientcmdapi.NewCluster() cluster := clientcmdapi.NewCluster()
cluster.Server = components.ApiServer.URL cluster.Server = components.ApiServer.URL
cluster.InsecureSkipTLSVerify = true cluster.InsecureSkipTLSVerify = true
cfg.Contexts = map[string]*clientcmdapi.Context{"test": ctx}
cfg.CurrentContext = "test"
overrides := clientcmd.ConfigOverrides{ overrides := clientcmd.ConfigOverrides{
ClusterInfo: *cluster, ClusterInfo: *cluster,
Context: *ctx,
CurrentContext: "test",
} }
cmdConfig := clientcmd.NewNonInteractiveClientConfig(*cfg, "test", &overrides, nil) cmdConfig := clientcmd.NewNonInteractiveClientConfig(*cfg, "test", &overrides, nil)
factory := util.NewFactory(cmdConfig) factory := util.NewFactory(cmdConfig)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment