-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix (crc/machine) : KubeContext left in invalid state after crc stop
At the moment, we are only cleaning up crc context from kubeconfig during `crc delete`. This can be problematic if user tries to run any cluster related command after running `crc stop` as kubeconfig still points to CRC cluster that is not active. I checked minikube's behavior and noticed it's cleaning up kube config in case of both stop and delete commands. Make crc behavior consistent with minikube and perform kubeconfig cleanup in both sub commands. Signed-off-by: Rohan Kumar <[email protected]>
- Loading branch information
1 parent
f4ccc58
commit 2f2def2
Showing
7 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package machine | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
crcConfig "github.com/crc-org/crc/v2/pkg/crc/config" | ||
"github.com/crc-org/crc/v2/pkg/crc/machine/state" | ||
crcOs "github.com/crc-org/crc/v2/pkg/os" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestClient_WhenStopInvokedWithNonExistentVM_ThenThrowError(t *testing.T) { | ||
// Given | ||
dir := t.TempDir() | ||
oldKubeConfigEnvVarValue := os.Getenv("KUBECONFIG") | ||
kubeConfigPath := filepath.Join(dir, "kubeconfig") | ||
err := crcOs.CopyFile(filepath.Join("testdata", "kubeconfig.in"), kubeConfigPath) | ||
assert.NoError(t, err) | ||
err = os.Setenv("KUBECONFIG", kubeConfigPath) | ||
assert.NoError(t, err) | ||
crcConfigStorage := crcConfig.New(crcConfig.NewEmptyInMemoryStorage(), crcConfig.NewEmptyInMemorySecretStorage()) | ||
client := NewClient("i-dont-exist", false, crcConfigStorage) | ||
|
||
// When | ||
clusterState, stopErr := client.Stop() | ||
|
||
// Then | ||
assert.EqualError(t, stopErr, "Instance is already stopped") | ||
assert.Equal(t, clusterState, state.Error) | ||
err = os.Setenv("KUBECONFIG", oldKubeConfigEnvVarValue) | ||
assert.NoError(t, err) | ||
} | ||
|
||
var testArguments = map[string]struct { | ||
inputKubeConfigPath string | ||
expectedKubeConfigPath string | ||
}{ | ||
"When KubeConfig contains crc context, then cleanup KubeConfig": { | ||
"kubeconfig.in", "kubeconfig.out", | ||
}, | ||
"When KubeConfig does not contain crc context, then KubeConfig remains unchanged": { | ||
"kubeconfig.out", "kubeconfig.out", | ||
}, | ||
} | ||
|
||
func TestClient_WhenStopInvoked_ThenKubeConfigUpdatedIfRequired(t *testing.T) { | ||
for name, test := range testArguments { | ||
t.Run(name, func(t *testing.T) { | ||
// Given | ||
dir := t.TempDir() | ||
oldKubeConfigEnvVarValue := os.Getenv("KUBECONFIG") | ||
kubeConfigPath := filepath.Join(dir, "kubeconfig") | ||
err := crcOs.CopyFile(filepath.Join("testdata", test.inputKubeConfigPath), kubeConfigPath) | ||
assert.NoError(t, err) | ||
err = os.Setenv("KUBECONFIG", kubeConfigPath) | ||
assert.NoError(t, err) | ||
crcConfigStorage := crcConfig.New(crcConfig.NewEmptyInMemoryStorage(), crcConfig.NewEmptyInMemorySecretStorage()) | ||
client := NewClient("test-client", false, crcConfigStorage) | ||
|
||
// When | ||
clusterState, _ := client.Stop() | ||
|
||
// Then | ||
actualKubeConfigFile, err := os.ReadFile(kubeConfigPath) | ||
assert.NoError(t, err) | ||
expectedKubeConfigPath, err := os.ReadFile(filepath.Join("testdata", test.expectedKubeConfigPath)) | ||
assert.NoError(t, err) | ||
assert.YAMLEq(t, string(expectedKubeConfigPath), string(actualKubeConfigFile)) | ||
assert.Equal(t, clusterState, state.Error) | ||
err = os.Setenv("KUBECONFIG", oldKubeConfigEnvVarValue) | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
pkg/crc/machine/testdata/kubeconfig-without-api-crc-testing-cluster-domain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
apiVersion: v1 | ||
clusters: | ||
- cluster: | ||
server: https://api.unknown.testing:6443 | ||
name: api-crc-testing:6443 | ||
contexts: | ||
- context: | ||
cluster: api-crc-testing:6443 | ||
namespace: default | ||
user: kubeadmin/api-crc-testing:6443 | ||
name: default/api-crc-testing:6443/kubeadmin | ||
current-context: default/api-crc-testing:6443/kubeadmin | ||
kind: Config | ||
preferences: {} | ||
users: | ||
- name: kubeadmin/api-crc-testing:6443 | ||
user: | ||
token: sha256~secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters