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

Switch kubectl current-context on profile change #4504

Merged
merged 5 commits into from
Jun 18, 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
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
}