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

Bug Fix: k8s --save doesn't update the Kubeconfig if cluster with same name is re-created #314

Merged
merged 1 commit into from
May 2, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmd/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ func init() {

kubernetesConfigCmd.Flags().BoolVarP(&saveConfig, "save", "s", false, "save the config")
kubernetesConfigCmd.Flags().BoolVarP(&switchConfig, "switch", "i", false, "switch context to newly-created cluster (needs --merge flag)")
kubernetesConfigCmd.Flags().BoolVarP(&mergeConfig, "merge", "m", false, "merge the config with existing kubeconfig if it already exists")
kubernetesConfigCmd.Flags().BoolVarP(&overwriteConfig, "overwrite", "w", false, "overwrite the kubeconfig file")
kubernetesConfigCmd.Flags().StringVarP(&localPathConfig, "local-path", "p", fmt.Sprintf("%s/.kube/config", home), "local path to save the kubeconfig file")

Expand Down
34 changes: 10 additions & 24 deletions cmd/kubernetes/kubernetes_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ import (

var kubernetesConfigCmdExample = `* To merge and save:
civo kubernetes config CLUSTER_NAME --save
civo kubernetes config CLUSTER_NAME --save --merge

* To merge and save, and switch to the new context:
civo kubernetes config CLUSTER_NAME --save --switch

* To overwrite and save:
civo kubernetes config CLUSTER_NAME --save --overwrite

Notes:
* By default, when --save is specified, we will merge your kubeconfig (unless --overwrite is specified).
* The --merge and --overwrite flags can't be used together.
* To auto-switch to new kubeconfig, --switch is required. Without it, your active context will remain unchanged.
* When --overwrite is specified, --switch is not required. Your context will be updated automatically.
`

var saveConfig, mergeConfig, switchConfig, overwriteConfig bool
var saveConfig, switchConfig, overwriteConfig bool
var localPathConfig string

var kubernetesConfigCmd = &cobra.Command{
Expand All @@ -50,23 +51,8 @@ If you wish to use a custom format, the available fields are:
os.Exit(1)
}

// default to merge strategy
if !mergeConfig && !overwriteConfig {
mergeConfig = true
}

if overwriteConfig && mergeConfig {
utility.Error("You can't use --merge and --overwrite flags together")
os.Exit(1)
}

if switchConfig && !mergeConfig {
if overwriteConfig {
utility.Info("--switch is not required when --overwrite is present")
} else {
utility.Error("The --switch flag can only be used together with --merge flag")
os.Exit(1)
}
if switchConfig && overwriteConfig {
utility.Info("--switch is not required when --overwrite is present")
}

kube, err := client.FindKubernetesCluster(args[0])
Expand All @@ -81,10 +67,10 @@ If you wish to use a custom format, the available fields are:
}

if saveConfig {
if !mergeConfig && strings.Contains(localPathConfig, ".kube") {
if overwriteConfig && strings.Contains(localPathConfig, ".kube") {
// overwrite and save
if overwriteConfig || utility.UserConfirmedOverwrite("kubernetes config", common.DefaultYes) {
err := utility.ObtainKubeConfig(localPathConfig, kube.KubeConfig, mergeConfig, switchConfig, kube.Name)
if utility.UserConfirmedOverwrite("kubernetes config", common.DefaultYes) {
err := utility.ObtainKubeConfig(localPathConfig, kube.KubeConfig, false, switchConfig, kube.Name)
if err != nil {
utility.Error("Saving the cluster config failed with %s", err)
os.Exit(1)
Expand All @@ -95,7 +81,7 @@ If you wish to use a custom format, the available fields are:
}
} else {
// merge and save
err := utility.ObtainKubeConfig(localPathConfig, kube.KubeConfig, mergeConfig, switchConfig, kube.Name)
err := utility.ObtainKubeConfig(localPathConfig, kube.KubeConfig, true, switchConfig, kube.Name)
if err != nil {
utility.Error("Saving the cluster config failed with %s", err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions utility/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ func mergeConfigs(localKubeconfigPath string, k3sconfig []byte, switchContext bo
var cmd *exec.Cmd

if osResult == "windows" {
os.Setenv("KUBECONFIG", fmt.Sprintf("%s;%s", localKubeconfigPath, file.Name()))
os.Setenv("KUBECONFIG", fmt.Sprintf("%s;%s", file.Name(), localKubeconfigPath))
cmd = exec.Command("powershell", "kubectl", "config", "view", "--merge", "--flatten")
} else {
// Append KUBECONFIGS in ENV Vars
appendKubeConfigENV := fmt.Sprintf("KUBECONFIG=%s:%s", localKubeconfigPath, file.Name())
appendKubeConfigENV := fmt.Sprintf("KUBECONFIG=%s:%s", file.Name(), localKubeconfigPath)
cmd = exec.Command("kubectl", "config", "view", "--merge", "--flatten")
cmd.Env = append(os.Environ(), appendKubeConfigENV)
}
Expand Down