Skip to content

Commit

Permalink
Merge pull request kubernetes#17735 from Skalador/profile_list_kubectx
Browse files Browse the repository at this point in the history
Adding the active kubecontext to minikube profile list command
  • Loading branch information
spowelljr committed Mar 28, 2024
2 parents 68ed09b + 92ccbd1 commit c5dd90e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 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 @@ -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 Profile", "Active Kubecontext"})
table.SetAutoFormatHeaders(false)
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetCenterSeparator("|")
Expand All @@ -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
}
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 @@ -27,6 +27,7 @@ import (
"github.com/spf13/viper"
"k8s.io/klog/v2"
"k8s.io/minikube/pkg/drivers/kic/oci"
"k8s.io/minikube/pkg/minikube/kubeconfig"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/util/lock"
)
Expand Down Expand Up @@ -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...)
Expand All @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/minikube/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ 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
}

// 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 c5dd90e

Please sign in to comment.