Merge pull request #85953 from St0rmingBr4in/verify-context-exists

kubeadm: Throw an error if the currentContext does not exists
This commit is contained in:
Kubernetes Prow Robot 2019-12-06 05:35:03 -08:00 committed by GitHub
commit 3994f52ee6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 5 deletions

View File

@ -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)
}

View File

@ -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,