Skip to content
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

kubeadm: Throw an error if the currentContext does not exists #85953

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions cmd/kubeadm/app/phases/kubeconfig/kubeconfig.go
Expand Up @@ -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)
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/kubeadm/app/phases/kubeconfig/kubeconfig_test.go
Expand Up @@ -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
Expand All @@ -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,
Expand Down