From d6d536c634a5fc278cfd0037f02409b3339eb6c4 Mon Sep 17 00:00:00 2001 From: Skalador Date: Tue, 5 Dec 2023 21:16:29 +0100 Subject: [PATCH] Adding the active kubecontext to minikube profile list command The minikube profile list does not show the active kubecontext. The active kubecontext was added to the profiles as ActiveKubeContext similar to the Active property of the existing implementation. This allows older version, which rely on the json field Active, to still work and additionally obtain the information about ActiveKubeContext. The table representation for the minikube profile list command was updated as well. Active minikube profiles are highlighted in the Profile column. The Active column was replaced with an Active Kubecontext column. --- cmd/minikube/cmd/config/profile_list.go | 9 ++++++--- pkg/minikube/config/profile.go | 8 ++++++++ pkg/minikube/config/types.go | 1 + pkg/minikube/kubeconfig/context.go | 14 ++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/cmd/minikube/cmd/config/profile_list.go b/cmd/minikube/cmd/config/profile_list.go index 2cd2f1432f6e..5af7442739d6 100644 --- a/cmd/minikube/cmd/config/profile_list.go +++ b/cmd/minikube/cmd/config/profile_list.go @@ -154,7 +154,7 @@ func profileStatus(p *config.Profile, api libmachine.API) string { func renderProfilesTable(ps [][]string) { table := tablewriter.NewWriter(os.Stdout) - table.SetHeader([]string{"Profile", "VM Driver", "Runtime", "IP", "Port", "Version", "Status", "Nodes", "Active"}) + table.SetHeader([]string{"Profile", "VM Driver", "Runtime", "IP", "Port", "Version", "Status", "Nodes", "Active Kubecontext"}) table.SetAutoFormatHeaders(false) table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true}) table.SetCenterSeparator("|") @@ -175,11 +175,14 @@ func profilesToTableData(profiles []*config.Profile) [][]string { if k8sVersion == constants.NoKubernetesVersion { // for --no-kubernetes flag k8sVersion = "N/A" } - var c string + var c,k string if p.Name == currentProfile { c = "*" } - data = append(data, []string{p.Name, p.Config.Driver, p.Config.KubernetesConfig.ContainerRuntime, cp.IP, strconv.Itoa(cp.Port), k8sVersion, p.Status, strconv.Itoa(len(p.Config.Nodes)), c}) + if p.ActiveKubeContext { + k = "*" + } + data = append(data, []string{c+p.Name, p.Config.Driver, p.Config.KubernetesConfig.ContainerRuntime, cp.IP, strconv.Itoa(cp.Port), k8sVersion, p.Status, strconv.Itoa(len(p.Config.Nodes)), k}) } return data } diff --git a/pkg/minikube/config/profile.go b/pkg/minikube/config/profile.go index 7c1a46c7161a..17550ec25edd 100644 --- a/pkg/minikube/config/profile.go +++ b/pkg/minikube/config/profile.go @@ -28,6 +28,7 @@ import ( "k8s.io/klog/v2" "k8s.io/minikube/pkg/drivers/kic/oci" "k8s.io/minikube/pkg/minikube/localpath" + "k8s.io/minikube/pkg/minikube/kubeconfig" "k8s.io/minikube/pkg/util/lock" ) @@ -206,6 +207,10 @@ func ListProfiles(miniHome ...string) (validPs []*Profile, inValidPs []*Profile, pDirs = append(pDirs, cs...) } + activeKubeContext, err := kubeconfig.GetCurrentContext(kubeconfig.PathFromEnv()) + if err != nil { + return nil, nil, err + } nodeNames := map[string]bool{} for _, n := range removeDupes(pDirs) { p, err := LoadProfile(n, miniHome...) @@ -221,6 +226,9 @@ func ListProfiles(miniHome ...string) (validPs []*Profile, inValidPs []*Profile, if p.Name == activeP { p.Active = true } + if p.Name == activeKubeContext { + p.ActiveKubeContext = true + } for _, child := range p.Config.Nodes { nodeNames[MachineName(*p.Config, child)] = true } diff --git a/pkg/minikube/config/types.go b/pkg/minikube/config/types.go index 245f5c10e7e3..e80d68f1a718 100644 --- a/pkg/minikube/config/types.go +++ b/pkg/minikube/config/types.go @@ -29,6 +29,7 @@ type Profile struct { Status string // running, stopped, paused, unknown Config *ClusterConfig Active bool + ActiveKubeContext bool } // ClusterConfig contains the parameters used to start a cluster. diff --git a/pkg/minikube/kubeconfig/context.go b/pkg/minikube/kubeconfig/context.go index 8b3d584643fc..9436aab4385c 100644 --- a/pkg/minikube/kubeconfig/context.go +++ b/pkg/minikube/kubeconfig/context.go @@ -45,6 +45,20 @@ func UnsetCurrentContext(machineName string, configPath ...string) error { return nil } +// GetCurrentContext gets the kubectl's current-context +func GetCurrentContext(configPath ...string) (string, error) { + fPath := PathFromEnv() + if configPath != nil { + fPath = configPath[0] + } + kcfg, err := readOrNew(fPath) + if err != nil { + return "", errors.Wrap(err, "Error getting kubeconfig status") + } + + return kcfg.CurrentContext, err +} + // SetCurrentContext sets the kubectl's current-context func SetCurrentContext(name string, configPath ...string) error { fPath := PathFromEnv()