From 892a116dc33975d616dd467a80f819f883d71bf5 Mon Sep 17 00:00:00 2001 From: Jimmi Dyson Date: Fri, 8 Jul 2016 18:46:35 +0100 Subject: [PATCH] Better error message for services with no NodePort --- pkg/minikube/cluster/cluster.go | 9 ++++++++- pkg/minikube/cluster/cluster_test.go | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/minikube/cluster/cluster.go b/pkg/minikube/cluster/cluster.go index a1839c5aaf..dcd39261d6 100644 --- a/pkg/minikube/cluster/cluster.go +++ b/pkg/minikube/cluster/cluster.go @@ -424,7 +424,14 @@ func getServicePortFromServiceGetter(services serviceGetter, service string) (in if err != nil { return 0, fmt.Errorf("Error getting %s service: %s", service, err) } - return int(svc.Spec.Ports[0].NodePort), nil + nodePort := 0 + if len(svc.Spec.Ports) > 0 { + nodePort = int(svc.Spec.Ports[0].NodePort) + } + if nodePort == 0 { + return 0, fmt.Errorf("Service %s does not have a node port. To have one assigned automatically, the service type must be NodePort or LoadBalancer, but this service is of type %s.", service, svc.Spec.Type) + } + return nodePort, nil } func getKubernetesServicesWithNamespace(namespace string) (serviceGetter, error) { diff --git a/pkg/minikube/cluster/cluster_test.go b/pkg/minikube/cluster/cluster_test.go index 900be0e646..99143562e1 100644 --- a/pkg/minikube/cluster/cluster_test.go +++ b/pkg/minikube/cluster/cluster_test.go @@ -464,6 +464,17 @@ func TestGetDashboardURL(t *testing.T) { } +func TestGetServiceURLWithoutNodePort(t *testing.T) { + mockServiceGetter := NewMockServiceGetter() + mockDashboardService := api.Service{} + mockServiceGetter.services["mock-service"] = mockDashboardService + + _, err := getServicePortFromServiceGetter(mockServiceGetter, "mock-service") + if err == nil { + t.Fatalf("Expected error getting service with no node port") + } +} + func TestUpdate(t *testing.T) { s, _ := tests.NewSSHServer() port, err := s.Start()