Refactor multiple service ports handling after service list command added

pull/729/head
Jimmi Dyson 2016-10-27 16:39:03 +01:00
parent b8cdbd28f5
commit c057dede1f
No known key found for this signature in database
GPG Key ID: 978CD4AF4C1E87F5
4 changed files with 17 additions and 9 deletions

View File

@ -53,7 +53,7 @@ var dashboardCmd = &cobra.Command{
os.Exit(1)
}
urls, err := cluster.GetServiceURLs(api, namespace, service, nil)
urls, err := cluster.GetServiceURLsForService(api, namespace, service, nil)
if err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, "Check that minikube is running.")

View File

@ -76,7 +76,7 @@ var serviceCmd = &cobra.Command{
os.Exit(1)
}
urls, err := cluster.GetServiceURLs(api, namespace, service, serviceURLTemplate)
urls, err := cluster.GetServiceURLsForService(api, namespace, service, serviceURLTemplate)
if err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, "Check that minikube is running and that you have specified the correct namespace (-n flag).")

View File

@ -19,6 +19,7 @@ package cmd
import (
"fmt"
"os"
"strings"
"github.com/docker/machine/libmachine"
"github.com/olekukonko/tablewriter"
@ -48,7 +49,12 @@ var serviceListCmd = &cobra.Command{
var data [][]string
for _, serviceURL := range serviceURLs {
data = append(data, []string{serviceURL.Namespace, serviceURL.Name, serviceURL.URL})
if len(serviceURL.URLs) == 0 {
data = append(data, []string{serviceURL.Namespace, serviceURL.Name, "No node port"})
} else {
data = append(data, []string{serviceURL.Namespace, serviceURL.Name, strings.Join(serviceURL.URLs, "\n")})
}
}
table := tablewriter.NewWriter(os.Stdout)

View File

@ -515,7 +515,7 @@ type ipPort struct {
Port int32
}
func GetServiceURLs(api libmachine.API, namespace, service string, t *template.Template) ([]string, error) {
func GetServiceURLsForService(api libmachine.API, namespace, service string, t *template.Template) ([]string, error) {
host, err := CheckIfApiExistsAndLoad(api)
if err != nil {
return nil, errors.Wrap(err, "Error checking if api exist and loading it")
@ -596,7 +596,9 @@ func getServicePortsFromServiceGetter(services serviceGetter, service string) ([
var nodePorts []int32
if len(svc.Spec.Ports) > 0 {
for _, port := range svc.Spec.Ports {
nodePorts = append(nodePorts, port.NodePort)
if port.NodePort > 0 {
nodePorts = append(nodePorts, port.NodePort)
}
}
}
if len(nodePorts) == 0 {
@ -637,7 +639,7 @@ func EnsureMinikubeRunningOrExit(api libmachine.API, exitStatus int) {
type ServiceURL struct {
Namespace string
Name string
URL string
URLs []string
}
type ServiceURLs []ServiceURL
@ -668,15 +670,15 @@ func GetServiceURLs(api libmachine.API, namespace string, t *template.Template)
var serviceURLs []ServiceURL
for _, svc := range svcs.Items {
url, err := getServiceURLWithClient(client, ip, svc.Namespace, svc.Name, t)
urls, err := getServiceURLsWithClient(client, ip, svc.Namespace, svc.Name, t)
if err != nil {
if _, ok := err.(MissingNodePortError); ok {
serviceURLs = append(serviceURLs, ServiceURL{Namespace: svc.Namespace, Name: svc.Name, URL: "No node port"})
serviceURLs = append(serviceURLs, ServiceURL{Namespace: svc.Namespace, Name: svc.Name})
continue
}
return nil, err
}
serviceURLs = append(serviceURLs, ServiceURL{Namespace: svc.Namespace, Name: svc.Name, URL: url})
serviceURLs = append(serviceURLs, ServiceURL{Namespace: svc.Namespace, Name: svc.Name, URLs: urls})
}
return serviceURLs, nil