Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Commit

Permalink
kubernetes/services: Return ingress hostname if IP is empty but hostn…
Browse files Browse the repository at this point in the history
…ame is not. (#431)

RDS client has already been changed to DNS-resolve resource's IP if it's not a valid IP address.

This is to support Kubernetes cases where ingress is given a hostname instead of an IP address, for example, AWS ELB. See #418 for more details.

PiperOrigin-RevId: 320145826
  • Loading branch information
manugarg authored Jul 8, 2020
1 parent 94bc577 commit 02c9fbb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
10 changes: 8 additions & 2 deletions rds/kubernetes/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ type serviceInfo struct {
Status struct {
LoadBalancer struct {
Ingress []struct {
IP string
IP string
Hostname string
}
}
}
Expand Down Expand Up @@ -154,7 +155,12 @@ func (si *serviceInfo) resources(portFilter *filter.RegexFilter, reqIPType pb.IP
if len(si.Status.LoadBalancer.Ingress) == 0 {
continue
}
res.Ip = proto.String(si.Status.LoadBalancer.Ingress[0].IP)
ingress := si.Status.LoadBalancer.Ingress[0]

res.Ip = proto.String(ingress.IP)
if ingress.IP == "" && ingress.Hostname != "" {
res.Ip = proto.String(ingress.Hostname)
}
} else {
res.Ip = proto.String(si.Spec.ClusterIP)
}
Expand Down
32 changes: 17 additions & 15 deletions rds/kubernetes/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
pb "github.com/google/cloudprober/rds/proto"
)

func testServiceInfo(name, ns, ip, publicIP string, labels map[string]string, ports []int) *serviceInfo {
func testServiceInfo(name, ns, ip, publicIP, hostname string, labels map[string]string, ports []int) *serviceInfo {
si := &serviceInfo{Metadata: kMetadata{Name: name, Namespace: ns, Labels: labels}}
si.Spec.ClusterIP = ip

Expand All @@ -37,10 +37,11 @@ func testServiceInfo(name, ns, ip, publicIP string, labels map[string]string, po
})
}

if publicIP != "" {
si.Status.LoadBalancer.Ingress = []struct{ IP string }{
if publicIP != "" || hostname != "" {
si.Status.LoadBalancer.Ingress = []struct{ IP, Hostname string }{
{
IP: publicIP,
IP: publicIP,
Hostname: hostname,
},
}
}
Expand All @@ -49,11 +50,12 @@ func testServiceInfo(name, ns, ip, publicIP string, labels map[string]string, po

func TestListSvcResources(t *testing.T) {
sl := &servicesLister{}
sl.names = []string{"serviceA", "serviceB", "serviceC"}
sl.names = []string{"serviceA", "serviceB", "serviceC", "serviceD"}
sl.cache = map[string]*serviceInfo{
"serviceA": testServiceInfo("serviceA", "nsAB", "10.1.1.1", "", map[string]string{"app": "appA"}, []int{9313, 9314}),
"serviceB": testServiceInfo("serviceB", "nsAB", "10.1.1.2", "192.16.16.199", map[string]string{"app": "appB"}, []int{443}),
"serviceC": testServiceInfo("serviceC", "nsC", "10.1.1.3", "192.16.16.200", map[string]string{"app": "appC", "func": "web"}, []int{3141}),
"serviceA": testServiceInfo("serviceA", "nsAB", "10.1.1.1", "", "", map[string]string{"app": "appA"}, []int{9313, 9314}),
"serviceB": testServiceInfo("serviceB", "nsAB", "10.1.1.2", "192.16.16.199", "", map[string]string{"app": "appB"}, []int{443}),
"serviceC": testServiceInfo("serviceC", "nsC", "10.1.1.3", "192.16.16.200", "serviceC.test.com", map[string]string{"app": "appC", "func": "web"}, []int{3141}),
"serviceD": testServiceInfo("serviceD", "nsD", "10.1.1.4", "", "serviceD.test.com", map[string]string{"app": "appD", "func": "web"}, []int{3141}),
}

tests := []struct {
Expand All @@ -80,11 +82,11 @@ func TestListSvcResources(t *testing.T) {
wantPorts: []int32{443, 3141},
},
{
desc: "only port filter for serviceA and serviceB's ports 9314 and 3141",
desc: "only port filter for ports 9314 and 3141",
filters: map[string]string{"port": "314"},
wantServices: []string{"serviceA", "serviceC"},
wantIPs: []string{"10.1.1.1", "10.1.1.3"},
wantPorts: []int32{9314, 3141},
wantServices: []string{"serviceA", "serviceC", "serviceD"},
wantIPs: []string{"10.1.1.1", "10.1.1.3", "10.1.1.4"},
wantPorts: []int32{9314, 3141, 3141},
},
{
desc: "name and namespace filter for serviceB",
Expand All @@ -102,9 +104,9 @@ func TestListSvcResources(t *testing.T) {
},
{
desc: "only services with public IPs",
wantServices: []string{"serviceB", "serviceC"},
wantPublicIPs: []string{"192.16.16.199", "192.16.16.200"},
wantPorts: []int32{443, 3141},
wantServices: []string{"serviceB", "serviceC", "serviceD"},
wantPublicIPs: []string{"192.16.16.199", "192.16.16.200", "serviceD.test.com"},
wantPorts: []int32{443, 3141, 3141},
},
}

Expand Down

0 comments on commit 02c9fbb

Please sign in to comment.