From 3e19201ec2fffc52248ac0fa5a9cb7592ee5b23f Mon Sep 17 00:00:00 2001 From: Julien DOCHE Date: Thu, 5 Dec 2019 15:31:38 +0100 Subject: [PATCH] kubeadm: Throw an error if the currentContext does not exists Check if the CurrentContext exists in validateKubeConfig Signed-off-by: Julien DOCHE --- cmd/kubeadm/app/phases/kubeconfig/kubeconfig.go | 13 ++++++++----- .../app/phases/kubeconfig/kubeconfig_test.go | 8 ++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cmd/kubeadm/app/phases/kubeconfig/kubeconfig.go b/cmd/kubeadm/app/phases/kubeconfig/kubeconfig.go index 90128f472d7..b22e06bcfa0 100644 --- a/cmd/kubeadm/app/phases/kubeconfig/kubeconfig.go +++ b/cmd/kubeadm/app/phases/kubeconfig/kubeconfig.go @@ -221,13 +221,16 @@ func validateKubeConfig(outDir, filename string, config *clientcmdapi.Config) er return errors.Wrapf(err, "failed to load kubeconfig file %s that already exists on disk", kubeConfigFilePath) } - expectedCtx := config.CurrentContext - expectedCluster := config.Contexts[expectedCtx].Cluster - currentCtx := currentConfig.CurrentContext - if currentConfig.Contexts[currentCtx] == nil { + expectedCtx, exists := config.Contexts[config.CurrentContext] + if !exists { + return errors.Errorf("failed to find expected context %s", config.CurrentContext) + } + expectedCluster := expectedCtx.Cluster + currentCtx, exists := currentConfig.Contexts[currentConfig.CurrentContext] + if !exists { return errors.Errorf("failed to find CurrentContext in Contexts of the kubeconfig file %s", kubeConfigFilePath) } - currentCluster := currentConfig.Contexts[currentCtx].Cluster + currentCluster := currentCtx.Cluster if currentConfig.Clusters[currentCluster] == nil { return errors.Errorf("failed to find the given CurrentContext Cluster in Clusters of the kubeconfig file %s", kubeConfigFilePath) } diff --git a/cmd/kubeadm/app/phases/kubeconfig/kubeconfig_test.go b/cmd/kubeadm/app/phases/kubeconfig/kubeconfig_test.go index 06a0cff382f..48060b35fa7 100644 --- a/cmd/kubeadm/app/phases/kubeconfig/kubeconfig_test.go +++ b/cmd/kubeadm/app/phases/kubeconfig/kubeconfig_test.go @@ -217,6 +217,8 @@ func TestCreateKubeConfigFileIfNotExists(t *testing.T) { config := setupdKubeConfigWithClientAuth(t, caCert, caKey, "https://1.2.3.4:1234", "test-cluster", "myOrg1", "myOrg2") configWithAnotherClusterCa := setupdKubeConfigWithClientAuth(t, anotherCaCert, anotherCaKey, "https://1.2.3.4:1234", "test-cluster", "myOrg1", "myOrg2") configWithAnotherClusterAddress := setupdKubeConfigWithClientAuth(t, caCert, caKey, "https://3.4.5.6:3456", "myOrg1", "test-cluster", "myOrg2") + invalidConfig := setupdKubeConfigWithClientAuth(t, caCert, caKey, "https://1.2.3.4:1234", "test-cluster", "myOrg1", "myOrg2") + invalidConfig.CurrentContext = "invalid context" var tests = []struct { name string @@ -228,6 +230,12 @@ func TestCreateKubeConfigFileIfNotExists(t *testing.T) { name: "KubeConfig doesn't exist", kubeConfig: config, }, + { // if KubeConfig is invalid raise error + name: "KubeConfig is invalid", + existingKubeConfig: invalidConfig, + kubeConfig: invalidConfig, + expectedError: true, + }, { // if KubeConfig is equal to the existingKubeConfig - refers to the same cluster -, use the existing (Test idempotency) name: "KubeConfig refers to the same cluster", existingKubeConfig: config,