diff --git a/cmd/minikube/cmd/config/profile.go b/cmd/minikube/cmd/config/profile.go index 918b015902e0..770e5f6bf25d 100644 --- a/cmd/minikube/cmd/config/profile.go +++ b/cmd/minikube/cmd/config/profile.go @@ -23,7 +23,9 @@ import ( "github.com/spf13/viper" pkgConfig "k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/console" + "k8s.io/minikube/pkg/minikube/constants" "k8s.io/minikube/pkg/minikube/exit" + pkgutil "k8s.io/minikube/pkg/util" ) // ProfileCmd represents the profile command @@ -48,9 +50,24 @@ var ProfileCmd = &cobra.Command{ } err := Set(pkgConfig.MachineProfile, profile) if err != nil { - exit.WithError("set failed", err) - } else { - console.Success("minikube profile was successfully set to %s", profile) + exit.WithError("Setting profile failed", err) } + cc, err := pkgConfig.Load() + // might err when loading older version of cfg file that doesn't have KeepContext field + if err != nil && !os.IsNotExist(err) { + console.ErrLn("Error loading profile config: %v", err) + } + if err == nil { + if cc.MachineConfig.KeepContext { + console.Success("Skipped switching kubectl context for %s , because --keep-context", profile) + console.Success("To connect to this cluster, use: kubectl --context=%s", profile) + } else { + err := pkgutil.SetCurrentContext(constants.KubeconfigPath, profile) + if err != nil { + console.ErrLn("Error while setting kubectl current context : %v", err) + } + } + } + console.Success("minikube profile was successfully set to %s", profile) }, } diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index 28d73025faaa..41c60e69c58a 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -446,6 +446,7 @@ func generateConfig(cmd *cobra.Command, k8sVersion string) (cfg.Config, error) { cfg := cfg.Config{ MachineConfig: cfg.MachineConfig{ + KeepContext: viper.GetBool(keepContext), MinikubeISO: viper.GetString(isoURL), Memory: viper.GetInt(memory), CPUs: viper.GetInt(cpus), diff --git a/pkg/minikube/config/types.go b/pkg/minikube/config/types.go index 2537ebbd2fd8..0aa61d402a7a 100644 --- a/pkg/minikube/config/types.go +++ b/pkg/minikube/config/types.go @@ -30,6 +30,7 @@ type Config struct { // MachineConfig contains the parameters used to start a cluster. type MachineConfig struct { + KeepContext bool // used by start and profile command to or not to switch kubectl's current context MinikubeISO string Memory int CPUs int diff --git a/pkg/util/kubeconfig.go b/pkg/util/kubeconfig.go index 887c7a884111..3c881c58f69e 100644 --- a/pkg/util/kubeconfig.go +++ b/pkg/util/kubeconfig.go @@ -325,3 +325,16 @@ func UnsetCurrentContext(filename, machineName string) error { } return nil } + +//SetCurrentContext sets the kubectl's current-context +func SetCurrentContext(kubeCfgPath, name string) error { + kcfg, err := ReadConfigOrNew(kubeCfgPath) + if err != nil { + return errors.Wrap(err, "Error getting kubeconfig status") + } + kcfg.CurrentContext = name + if err := WriteConfig(kcfg, kubeCfgPath); err != nil { + return errors.Wrap(err, "writing kubeconfig") + } + return nil +}