Skip to content

Commit

Permalink
Merge pull request #4504 from medyagh/profile_context_4503
Browse files Browse the repository at this point in the history
Switch kubectl current-context on profile change
  • Loading branch information
sharifelgamal authored Jun 18, 2019
2 parents 79240b0 + c10878a commit 35bbace
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
23 changes: 20 additions & 3 deletions cmd/minikube/cmd/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
},
}
1 change: 1 addition & 0 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions pkg/minikube/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions pkg/util/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 35bbace

Please sign in to comment.