From 8990648956dd2397eebbd41c1e781904957d261e Mon Sep 17 00:00:00 2001 From: Skalador Date: Tue, 5 Dec 2023 21:16:29 +0100 Subject: [PATCH 1/2] 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 55304240d9a6..7ab7e42e89d5 100644 --- a/cmd/minikube/cmd/config/profile_list.go +++ b/cmd/minikube/cmd/config/profile_list.go @@ -190,7 +190,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("|") @@ -217,11 +217,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, cpIP, strconv.Itoa(cpPort), k8sVersion, p.Status, strconv.Itoa(len(p.Config.Nodes)), c}) + if p.ActiveKubeContext { + k = "*" + } + data = append(data, []string{p.Name, p.Config.Driver, p.Config.KubernetesConfig.ContainerRuntime, cpIP, strconv.Itoa(cpPort), k8sVersion, p.Status, strconv.Itoa(len(p.Config.Nodes)), c, k}) } return data } diff --git a/pkg/minikube/config/profile.go b/pkg/minikube/config/profile.go index 50111ca5bd83..9316a5928ef9 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" ) @@ -200,6 +201,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...) @@ -215,6 +220,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 ee4cba6a5782..bcd5ccc30e2d 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() From 92ccbd1049dad7c606832f9da24cf8bb40191acf Mon Sep 17 00:00:00 2001 From: Skalador Date: Sat, 23 Dec 2023 17:23:41 +0100 Subject: [PATCH 2/2] Updating minikube profile list cmd table representation Updated the command line output of minikube profile list -o table to have two columns, i.e. Active Profile and Active Kubecontext. This makes the output more intuitive. Formatting issues are fixed as well. --- cmd/minikube/cmd/config/profile_list.go | 2 +- pkg/minikube/config/profile.go | 2 +- pkg/minikube/config/types.go | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/minikube/cmd/config/profile_list.go b/cmd/minikube/cmd/config/profile_list.go index 7ab7e42e89d5..2c9faac95c64 100644 --- a/cmd/minikube/cmd/config/profile_list.go +++ b/cmd/minikube/cmd/config/profile_list.go @@ -190,7 +190,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 Kubecontext"}) + table.SetHeader([]string{"Profile", "VM Driver", "Runtime", "IP", "Port", "Version", "Status", "Nodes", "Active Profile", "Active Kubecontext"}) table.SetAutoFormatHeaders(false) table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true}) table.SetCenterSeparator("|") diff --git a/pkg/minikube/config/profile.go b/pkg/minikube/config/profile.go index 9316a5928ef9..65ac95d5128b 100644 --- a/pkg/minikube/config/profile.go +++ b/pkg/minikube/config/profile.go @@ -27,8 +27,8 @@ import ( "github.com/spf13/viper" "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/minikube/localpath" "k8s.io/minikube/pkg/util/lock" ) diff --git a/pkg/minikube/config/types.go b/pkg/minikube/config/types.go index bcd5ccc30e2d..d8dc05281e78 100644 --- a/pkg/minikube/config/types.go +++ b/pkg/minikube/config/types.go @@ -25,10 +25,10 @@ import ( // Profile represents a minikube profile type Profile struct { - Name string - Status string // running, stopped, paused, unknown - Config *ClusterConfig - Active bool + Name string + Status string // running, stopped, paused, unknown + Config *ClusterConfig + Active bool ActiveKubeContext bool }