From 3c538f400af1efc5a75672fc97f7730837ae2216 Mon Sep 17 00:00:00 2001 From: Steven Powell Date: Wed, 22 Feb 2023 13:50:47 -0800 Subject: [PATCH 1/2] minikube service list: fix table format & hide URLs --- cmd/minikube/cmd/service_list.go | 55 +++++++++++++------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/cmd/minikube/cmd/service_list.go b/cmd/minikube/cmd/service_list.go index b8fcfb7e6f4e..62dede3aa4e5 100644 --- a/cmd/minikube/cmd/service_list.go +++ b/cmd/minikube/cmd/service_list.go @@ -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" @@ -52,53 +51,45 @@ 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 users +func updatePortsAndURLs(serviceURLs service.URLs, co mustload.ClusterController) service.URLs { + for i := range serviceURLs { + if len(serviceURLs[i].URLs) == 0 { + serviceURLs[i].PortNames = []string{"No node port"} + } else if driver.NeedsPortForward(co.Config.Driver) { + 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) } From ca3bcae87acd3702298892962d62ca8c7e276dd2 Mon Sep 17 00:00:00 2001 From: Steven Powell Date: Wed, 22 Feb 2023 13:59:48 -0800 Subject: [PATCH 2/2] remove unnecessary duplicated calls to NeedsPortForward --- cmd/minikube/cmd/service_list.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/minikube/cmd/service_list.go b/cmd/minikube/cmd/service_list.go index 62dede3aa4e5..99fd13b7d93e 100644 --- a/cmd/minikube/cmd/service_list.go +++ b/cmd/minikube/cmd/service_list.go @@ -65,12 +65,13 @@ var serviceListCmd = &cobra.Command{ } // 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 users +// 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 driver.NeedsPortForward(co.Config.Driver) { + } else if needsPortForward { serviceURLs[i].URLs = []string{} } }