Skip to content

Commit

Permalink
Adding the active kubecontext to minikube profile list command
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Skalador committed Dec 5, 2023
1 parent c56894f commit d6d536c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
9 changes: 6 additions & 3 deletions cmd/minikube/cmd/config/profile_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("|")
Expand All @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/minikube/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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...)
Expand All @@ -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
}
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 @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions pkg/minikube/kubeconfig/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit d6d536c

Please sign in to comment.