提交 5176806e 编写于 作者: M Medya Gh

set context on profile

上级 63982e7f
......@@ -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,18 @@ 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()
if err != nil && !os.IsNotExist(err) {
console.ErrLn("Error loading profile config: %v", err)
}
// don't change context is user has started the minikube with --keep-context
if cc.MachineConfig.KeepContext == false {
err = pkgutil.SetCurrentContext(constants.KubeconfigPath, profile)
console.ErrLn("error while setting kubectl current context : %v", err)
}
console.Success("minikube profile was successfully set to %s", profile)
},
}
......@@ -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),
......
......@@ -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 current context
MinikubeISO string
Memory int
CPUs int
......
......@@ -325,3 +325,16 @@ func UnsetCurrentContext(filename, machineName string) error {
}
return nil
}
//SetCurrentContext sets the kubectl's current-context
func SetCurrentContext(filename, name string) error {
kcfg, err := ReadConfigOrNew(filename)
if err != nil {
return errors.Wrap(err, "Error getting kubeconfig status")
}
kcfg.CurrentContext = name
if err := WriteConfig(kcfg, filename); err != nil {
return errors.Wrap(err, "writing kubeconfig")
}
return nil
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册