Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the active kubecontext to minikube profile list command #17735

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR, I think for sake of more clarity what active minikube profile is, in addition to "ActiveKubeContext" Column
we should have "Active miniikube" column,
by the way one idea for a follow up PR,
we could Later get rid of some of the column by default such as "Port" or K8S version, and make it available only in a --detailed or --long format

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for your fast answer!
Do I understand you correctly, that you want the table output to change from:

minikube profile list -o table
|-----------|-----------|---------|--------------|------|---------|---------|-------|--------------------|
|  Profile  | VM Driver | Runtime |      IP      | Port | Version | Status  | Nodes | Active Kubecontext |
|-----------|-----------|---------|--------------|------|---------|---------|-------|--------------------|
| *minikube | docker    | docker  | 192.168.49.2 | 8443 | v1.28.4 | Running |     1 |                    |
| minikube2 | docker    | docker  | 192.168.58.2 | 8443 | v1.28.4 | Running |     1 | *                  |
|-----------|-----------|---------|--------------|------|---------|---------|-------|--------------------|

to

minikube profile list -o table
|-----------|-----------|---------|--------------|------|---------|---------|-------|--------------------|-------------------|
|  Profile  | VM Driver | Runtime |      IP      | Port | Version | Status  | Nodes | Active Profile     | Active Kubecontext|
|-----------|-----------|---------|--------------|------|---------|---------|-------|--------------------|-------------------|
| minikube  | docker    | docker  | 192.168.49.2 | 8443 | v1.28.4 | Running |     1 |  *                 |                   |
| minikube2 | docker    | docker  | 192.168.58.2 | 8443 | v1.28.4 | Running |     1 |                    | *                 |
|-----------|-----------|---------|--------------|------|---------|---------|-------|--------------------|-------------------|

The JSON output shall be the same due to compatibility reasons, correct?
When this PR is merged, I can create an issue (or PR) addressing the follow up issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @medyagh, I have updated this PR with my above comment.

minikube profile list -o table
|-----------|-----------|---------|--------------|------|---------|---------|-------|----------------|--------------------|
|  Profile  | VM Driver | Runtime |      IP      | Port | Version | Status  | Nodes | Active Profile | Active Kubecontext |
|-----------|-----------|---------|--------------|------|---------|---------|-------|----------------|--------------------|
| minikube  | docker    | docker  | 192.168.49.2 | 8443 | v1.28.4 | Running |     1 | *              |                    |
| minikube2 | docker    | docker  | 192.168.58.2 | 8443 | v1.28.4 | Running |     1 |                | *                  |
|-----------|-----------|---------|--------------|------|---------|---------|-------|----------------|--------------------|

The JSON output is the same as before due to compatibility reasons. I thought about adding ActiveProfile, which has the same data as Active with a deprecation note. The deprecation note shall point out, that Active will be removed in a future release and ActiveProfile shall be used instead.

minikube profile list -o json | jq | grep -i active
      "Active": true,
      "ActiveKubeContext": false
      "Active": false,
      "ActiveKubeContext": true

Please let me know, what you think.

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
Loading