diff --git a/src/app/backend/replicationcontrollerdetail.go b/src/app/backend/replicationcontrollerdetail.go index bb53ffb6e7db..c0ba5dc6be07 100644 --- a/src/app/backend/replicationcontrollerdetail.go +++ b/src/app/backend/replicationcontrollerdetail.go @@ -330,9 +330,7 @@ func getExternalEndpoints(replicationController api.ReplicationController, pods if service.Spec.Type == api.ServiceTypeNodePort { externalEndpoints = getNodePortEndpoints(replicationControllerPods, service, getNodeFn) - } - - if service.Spec.Type == api.ServiceTypeLoadBalancer { + } else if service.Spec.Type == api.ServiceTypeLoadBalancer { for _, ingress := range service.Status.LoadBalancer.Ingress { externalEndpoints = append(externalEndpoints, getExternalEndpoint(ingress, service.Spec.Ports)) @@ -343,6 +341,28 @@ func getExternalEndpoints(replicationController api.ReplicationController, pods } } + if len(externalEndpoints) == 0 && (service.Spec.Type == api.ServiceTypeNodePort || + service.Spec.Type == api.ServiceTypeLoadBalancer) { + externalEndpoints = getLocalhostEndpoints(service) + } + + return externalEndpoints +} + +// Returns localhost endpoints for specified node port or load balancer service. +func getLocalhostEndpoints(service api.Service) []Endpoint { + var externalEndpoints []Endpoint + for _, port := range service.Spec.Ports { + externalEndpoints = append(externalEndpoints, Endpoint{ + Host: "localhost", + Ports: []ServicePort{ + { + Protocol: port.Protocol, + Port: port.NodePort, + }, + }, + }) + } return externalEndpoints } diff --git a/src/test/backend/replicationcontrollerdetail_test.go b/src/test/backend/replicationcontrollerdetail_test.go index 03db4ed1027d..165db78fdb73 100644 --- a/src/test/backend/replicationcontrollerdetail_test.go +++ b/src/test/backend/replicationcontrollerdetail_test.go @@ -493,6 +493,57 @@ func TestGetNodePortEndpoints(t *testing.T) { } } +func TestGetLocalhostEndpoints(t *testing.T) { + cases := []struct { + service api.Service + expected []Endpoint + }{ + { + api.Service{ + Spec: api.ServiceSpec{ + Ports: []api.ServicePort{ + { + Protocol: "TCP", + NodePort: 30100, + }, + { + Protocol: "TCP", + NodePort: 30101, + }, + }, + }, + }, + []Endpoint{ + { + Host: "localhost", + Ports: []ServicePort{ + { + Port: 30100, + Protocol: "TCP", + }, + }, + }, + { + Host: "localhost", + Ports: []ServicePort{ + { + Port: 30101, + Protocol: "TCP", + }, + }, + }, + }, + }, + } + for _, c := range cases { + actual := getLocalhostEndpoints(c.service) + if !reflect.DeepEqual(actual, c.expected) { + t.Errorf("getLocalhostEndpoints(%+v) == %+v, expected %+v", c.service, actual, + c.expected) + } + } +} + func TestGetInternalEndpoint(t *testing.T) { cases := []struct { serviceName, namespace string