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

Unset the current-context after minikube stop #4177

Merged
merged 9 commits into from
May 15, 2019
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
7 changes: 7 additions & 0 deletions cmd/minikube/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"k8s.io/minikube/pkg/minikube/cluster"
pkg_config "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/console"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
pkgutil "k8s.io/minikube/pkg/util"
Expand Down Expand Up @@ -69,6 +70,12 @@ itself, leaving all files intact. The cluster can be started again with the "sta
if err := cmdUtil.KillMountProcess(); err != nil {
exit.WithError("Unable to kill mount process", err)
}

machineName := pkg_config.GetMachineName()
err = pkgutil.UnsetCurrentContext(constants.KubeconfigPath, machineName)
if err != nil {
exit.WithError("update config", err)
}
},
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/util/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,16 @@ func GetPortFromKubeConfig(filename, machineName string) (int, error) {
port, err := strconv.Atoi(kport)
return port, err
}

//UnsetCurrentContext unsets the current-context from minikube to "" on minikube stop
func UnsetCurrentContext(filename, machineName string) error {
confg, err := ReadConfigOrNew(filename)
if err != nil {
return errors.Wrap(err, "Error getting kubeconfig status")
}
confg.CurrentContext = ""
if err := WriteConfig(confg, filename); err != nil {
return errors.Wrap(err, "writing kubeconfig")
}
return nil
}
15 changes: 15 additions & 0 deletions test/integration/start_stop_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ func TestStartStop(t *testing.T) {
t.Fatalf("IP command returned an invalid address: %s", ip)
}

// check for the current-context before and after the stop
kubectlRunner := util.NewKubectlRunner(t)
currentContext, err := kubectlRunner.RunCommand([]string{"config", "current-context"})
if err != nil {
t.Fatalf("Failed to fetch current-context")
}
if strings.TrimRight(string(currentContext), "\n") != "minikube" {
t.Fatalf("got current-context - %q, want current-context %q", string(currentContext), "minikube")
}

checkStop := func() error {
r.RunCommand("stop", true)
return r.CheckStatusNoFail(state.Stopped.String())
Expand All @@ -86,6 +96,11 @@ func TestStartStop(t *testing.T) {
t.Fatalf("timed out while checking stopped status: %v", err)
}

// running this command results in error when the current-context is not set
if err := r.Run("config current-context"); err != nil {
t.Logf("current-context is not set to minikube")
}

r.Start(test.args...)
r.CheckStatus(state.Running.String())

Expand Down