mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 19:31:44 +00:00
Merge pull request #85953 from St0rmingBr4in/verify-context-exists
kubeadm: Throw an error if the currentContext does not exists
This commit is contained in:
commit
3994f52ee6
@ -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)
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user