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

minikube service list: fix table format & hide URLs #15911

Merged
merged 2 commits into from
Feb 23, 2023
Merged
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
56 changes: 24 additions & 32 deletions cmd/minikube/cmd/service_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ import (
"encoding/json"
"fmt"
"os"
"runtime"
"strings"

"github.com/spf13/cobra"
core "k8s.io/api/core/v1"
"k8s.io/minikube/pkg/drivers/kic/oci"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/mustload"
"k8s.io/minikube/pkg/minikube/out"
Expand All @@ -52,53 +51,46 @@ var serviceListCmd = &cobra.Command{
out.ErrT(style.Notice, "Check that minikube is running and that you have specified the correct namespace (-n flag) if required.")
os.Exit(reason.ExSvcUnavailable)
}
serviceURLs = updatePortsAndURLs(serviceURLs, co)

switch output {
case "table":
printServicesTable(serviceURLs, co)
printServicesTable(serviceURLs)
case "json":
printServicesJSON(serviceURLs, co)
printServicesJSON(serviceURLs)
default:
exit.Message(reason.Usage, fmt.Sprintf("invalid output format: %s. Valid values: 'table', 'json'", output))
}
},
}

func printServicesTable(serviceURLs service.URLs, co mustload.ClusterController) {
// updatePortsAndURLs sets the port name to "No node port" if a service has no URLs and removes the URLs
// if the driver needs port forwarding as the user won't be able to hit the listed URLs which could confuse them
func updatePortsAndURLs(serviceURLs service.URLs, co mustload.ClusterController) service.URLs {
needsPortForward := driver.NeedsPortForward(co.Config.Driver)
for i := range serviceURLs {
if len(serviceURLs[i].URLs) == 0 {
serviceURLs[i].PortNames = []string{"No node port"}
} else if needsPortForward {
serviceURLs[i].URLs = []string{}
}
}
return serviceURLs
}

func printServicesTable(serviceURLs service.URLs) {
var data [][]string
for _, serviceURL := range serviceURLs {
if len(serviceURL.URLs) == 0 {
data = append(data, []string{serviceURL.Namespace, serviceURL.Name, "No node port"})
} else {
servicePortNames := strings.Join(serviceURL.PortNames, "\n")
serviceURLs := strings.Join(serviceURL.URLs, "\n")

// if we are running Docker on OSX we empty the internal service URLs
if runtime.GOOS == "darwin" && co.Config.Driver == oci.Docker {
serviceURLs = ""
}

data = append(data, []string{serviceURL.Namespace, serviceURL.Name, servicePortNames, serviceURLs})
}
portNames := strings.Join(serviceURL.PortNames, "\n")
urls := strings.Join(serviceURL.URLs, "\n")
data = append(data, []string{serviceURL.Namespace, serviceURL.Name, portNames, urls})
}

service.PrintServiceList(os.Stdout, data)
}

func printServicesJSON(serviceURLs service.URLs, co mustload.ClusterController) {
processedServiceURLs := serviceURLs

if runtime.GOOS == "darwin" && co.Config.Driver == oci.Docker {
// To ensure we don't modify the original serviceURLs
processedServiceURLs = make(service.URLs, len(serviceURLs))
copy(processedServiceURLs, serviceURLs)

for idx := range processedServiceURLs {
processedServiceURLs[idx].URLs = make([]string, 0)
}
}

jsonString, _ := json.Marshal(processedServiceURLs)
func printServicesJSON(serviceURLs service.URLs) {
jsonString, _ := json.Marshal(serviceURLs)
os.Stdout.Write(jsonString)
}

Expand Down