Skip to content

Commit

Permalink
Improve stop and kubectl context handling
Browse files Browse the repository at this point in the history
  • Loading branch information
medyagh committed Jun 28, 2019
1 parent 8f8b1d2 commit a3c8359
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
17 changes: 9 additions & 8 deletions cmd/minikube/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/docker/machine/libmachine/mcnerror"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
cmdUtil "k8s.io/minikube/cmd/util"
"k8s.io/minikube/pkg/minikube/cluster"
pkg_config "k8s.io/minikube/pkg/minikube/config"
Expand All @@ -44,7 +43,8 @@ itself, leaving all files intact. The cluster can be started again with the "sta

// runStop handles the executes the flow of "minikube stop"
func runStop(cmd *cobra.Command, args []string) {
profile := viper.GetString(pkg_config.MachineProfile)
profile := pkg_config.GetMachineName()

api, err := machine.NewAPIClient()
if err != nil {
exit.WithError("Error getting client", err)
Expand All @@ -64,9 +64,15 @@ func runStop(cmd *cobra.Command, args []string) {
return err
}
}
if err := pkgutil.RetryAfter(5, stop, 5*time.Second); err != nil {
err = pkgutil.UnsetCurrentContext(constants.KubeconfigPath, profile)
if err != nil {
exit.WithError("Failed to unset the kubernetes current-context", err)
}

if err := pkgutil.RetryAfter(3, stop, 5*time.Second); err != nil {
exit.WithError("Unable to stop VM", err)
}

if !nonexistent {
console.OutStyle(console.Stopped, "%q stopped.", profile)
}
Expand All @@ -75,11 +81,6 @@ func runStop(cmd *cobra.Command, args []string) {
console.OutStyle(console.WarningType, "Unable to kill mount process: %s", err)
}

machineName := pkg_config.GetMachineName()
err = pkgutil.UnsetCurrentContext(constants.KubeconfigPath, machineName)
if err != nil {
exit.WithError("update config", err)
}
}
func init() {
RootCmd.AddCommand(stopCmd)
Expand Down
18 changes: 9 additions & 9 deletions pkg/util/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,22 +313,22 @@ func GetPortFromKubeConfig(filename, machineName string) (int, error) {
return port, err
}

//UnsetCurrentContext unsets the current-context from minikube to "" on minikube stop
func UnsetCurrentContext(filename, machineName string) error {
confg, err := ReadConfigOrNew(filename)
//UnsetCurrentContext unsets the current-context to ""
func UnsetCurrentContext(kubeCfgPath, machineName string) error {
kcfg, err := ReadConfigOrNew(kubeCfgPath)
if err != nil {
return errors.Wrap(err, "Error getting kubeconfig status")
}

c := kcfg.CurrentContext
glog.Infof("Will unset current-context for profile %s, current-context is %s", machineName, c)
// Unset current-context only if profile is the current-context
if confg.CurrentContext == machineName {
confg.CurrentContext = ""
if err := WriteConfig(confg, filename); err != nil {
if c == machineName {
kcfg.CurrentContext = ""
if err := WriteConfig(kcfg, kubeCfgPath); err != nil {
return errors.Wrap(err, "writing kubeconfig")
}
return nil
}

glog.Infof("Successfully unset the kubectl current-context from %s to \"\" ", c)
return nil
}

Expand Down
21 changes: 12 additions & 9 deletions test/integration/start_stop_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,20 @@ func TestStartStop(t *testing.T) {
}

checkStop := func() error {
r.RunCommand("stop", true)
return r.CheckStatusNoFail(state.Stopped.String())
r.RunCommand("stop", false)
err := r.CheckStatusNoFail(state.Stopped.String())
if err == nil {
// kubecctl's current-context after minikube stop
afterCtx := kctlR.CurrentContext()
if afterCtx != "" {
return fmt.Errorf("got current-context - %q, want %q", afterCtx, "")
}
}
return err
}

if err := util.Retry(t, checkStop, 13*time.Second, 3); err != nil {
t.Fatalf("timed out while checking stopped status: %v", err)
}
// kubecctl's current-context after minikube stop
afterCtx := kctlR.CurrentContext()
if afterCtx != "" {
t.Fatalf("got current-context - %q, want %q", currCtx, "")
if err := util.Retry(t, checkStop, 7*time.Second, 3); err != nil {
t.Fatalf("Timed out checking stopped status: %v", err)
}

r.Start(test.args...)
Expand Down

0 comments on commit a3c8359

Please sign in to comment.