diff --git a/pkg/registry/service/rest.go b/pkg/registry/service/rest.go index 3ee91f16b4c..075d3d84ac7 100644 --- a/pkg/registry/service/rest.go +++ b/pkg/registry/service/rest.go @@ -295,6 +295,9 @@ func (rs *REST) ResourceLocation(ctx api.Context, id string) (*url.URL, http.Rou // Find a Subset that has the port. for ssi := 0; ssi < len(eps.Subsets); ssi++ { ss := &eps.Subsets[(ssSeed+ssi)%len(eps.Subsets)] + if len(ss.Addresses) == 0 { + continue + } for i := range ss.Ports { if ss.Ports[i].Name == portStr { // Pick a random address. diff --git a/pkg/registry/service/rest_test.go b/pkg/registry/service/rest_test.go index 1271e3285b0..2f7e76e1619 100644 --- a/pkg/registry/service/rest_test.go +++ b/pkg/registry/service/rest_test.go @@ -440,6 +440,22 @@ func TestServiceRegistryResourceLocation(t *testing.T) { Ports: []api.EndpointPort{{Name: "", Port: 80}, {Name: "p", Port: 93}}, }}, }, + { + ObjectMeta: api.ObjectMeta{ + Name: "foo", + Namespace: api.NamespaceDefault, + }, + Subsets: []api.EndpointSubset{{ + Addresses: []api.EndpointAddress{}, + Ports: []api.EndpointPort{{Name: "", Port: 80}, {Name: "p", Port: 93}}, + }, { + Addresses: []api.EndpointAddress{{IP: "1.2.3.4"}}, + Ports: []api.EndpointPort{{Name: "", Port: 80}, {Name: "p", Port: 93}}, + }, { + Addresses: []api.EndpointAddress{{IP: "1.2.3.5"}}, + Ports: []api.EndpointPort{}, + }}, + }, }, } storage, registry := NewTestREST(t, endpoints)