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

Commit

Permalink
Add "node" and "pod" labels to endpoints resources.
Browse files Browse the repository at this point in the history
These labels can then be added to metrics, and be used for debugging.

Ref: #612
PiperOrigin-RevId: 382125535
  • Loading branch information
manugarg committed Jun 29, 2021
1 parent 933e248 commit 1027e15
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
20 changes: 18 additions & 2 deletions rds/kubernetes/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ func (lister *epLister) listResources(req *pb.ListResourcesRequest) ([]*pb.Resou

type epSubset struct {
Addresses []struct {
IP string
IP string
NodeName string
TargetRef struct {
Kind string
Name string
}
}
Ports []struct {
Name string
Expand Down Expand Up @@ -129,11 +134,22 @@ func (epi *epInfo) resources(portFilter *filter.RegexFilter, l *logger.Logger) (
for _, addr := range eps.Addresses {
// We name the resource as <endpoints_name>_<IP>_<port>
resName := fmt.Sprintf("%s_%s_%s", epi.Metadata.Name, addr.IP, portName)

labels := make(map[string]string)
for k, v := range epi.Metadata.Labels {
labels[k] = v
}
labels["node"] = addr.NodeName
// If adding labels, make a copy of the metadata labels.
if addr.TargetRef.Kind == "Pod" {
labels["pod"] = addr.TargetRef.Name
}

resources = append(resources, &pb.Resource{
Name: proto.String(resName),
Ip: proto.String(addr.IP),
Port: proto.Int(port.Port),
Labels: epi.Metadata.Labels,
Labels: labels,
})
}
}
Expand Down
54 changes: 50 additions & 4 deletions rds/kubernetes/endpoints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,31 @@ func TestParseEndpoints(t *testing.T) {
}

eps := epi.Subsets[0]
var ips []string
var ips, pods, nodes []string
for _, addr := range eps.Addresses {
ips = append(ips, addr.IP)
nodes = append(nodes, addr.NodeName)
if addr.TargetRef.Kind != "Pod" {
t.Errorf("Unexpected target ref kind for addr (%s): %s", addr.IP, addr.TargetRef.Kind)
}
pods = append(pods, addr.TargetRef.Name)
}

expectedIPs := []string{"10.28.0.3", "10.28.2.3", "10.28.2.6"}
if !reflect.DeepEqual(ips, expectedIPs) {
t.Errorf("cloudprober endpoints addresses: got=%v, want=%v", ips, expectedIPs)
}

expectedNodes := []string{"gke-cluster-1-default-pool-abd8ad35-ccr7", "gke-cluster-1-default-pool-abd8ad35-mzh9", "gke-cluster-1-default-pool-abd8ad35-mzh9"}
if !reflect.DeepEqual(nodes, expectedNodes) {
t.Errorf("cloudprober endpoints nodes: got=%v, want=%v", nodes, expectedNodes)
}

expectedPods := []string{"cloudprober-test-577cf7bbcc-c7l5p", "cloudprober-test-577cf7bbcc-qnrvg", "cloudprober-54778d95f5-vms2d"}
if !reflect.DeepEqual(pods, expectedPods) {
t.Errorf("cloudprober endpoints pods: got=%v, want=%v", pods, expectedPods)
}

if len(eps.Ports) != 1 {
t.Errorf("cloudprober endpoints len(eps.Ports)=%d, want=1", len(eps.Ports))
}
Expand All @@ -77,10 +93,27 @@ func TestEndpointsToResources(t *testing.T) {

epi.Subsets[0] = epSubset{
Addresses: []struct {
IP string
IP string
NodeName string
TargetRef struct {
Kind string
Name string
}
}{
{IP: ips[0]},
{IP: ips[1]},
{
IP: ips[0],
NodeName: "n1",
TargetRef: struct {
Kind, Name string
}{
Kind: "Pod",
Name: "test-pod",
},
},
{
IP: ips[1],
NodeName: "n2",
},
},
Ports: []struct {
Name string
Expand All @@ -104,10 +137,13 @@ func TestEndpointsToResources(t *testing.T) {
}

var names, resIPs []string
var labels []map[string]string
var ports []int32

for _, res := range resources {
t.Logf("name=%s, IP=%s", res.GetName(), res.GetIp())
names = append(names, res.GetName())
labels = append(labels, res.GetLabels())
resIPs = append(resIPs, res.GetIp())
ports = append(ports, res.GetPort())
}
Expand All @@ -117,6 +153,16 @@ func TestEndpointsToResources(t *testing.T) {
t.Errorf("Cloudprober endpoints resource names=%v, want=%v", names, expectedNames)
}

expectedLabels := []map[string]string{
{"app": "lCloudprober", "node": "n1", "pod": "test-pod"},
{"app": "lCloudprober", "node": "n2"},
{"app": "lCloudprober", "node": "n1", "pod": "test-pod"},
{"app": "lCloudprober", "node": "n2"},
}
if !reflect.DeepEqual(labels, expectedLabels) {
t.Errorf("Cloudprober endpoints resource labels=%v, want=%v", labels, expectedLabels)
}

expectedIPs := []string{"10.0.0.1", "10.0.0.2", "10.0.0.1", "10.0.0.2"}
if !reflect.DeepEqual(resIPs, expectedIPs) {
t.Errorf("Cloudprober endpoints resource IPs=%v, want=%v", resIPs, expectedIPs)
Expand Down

0 comments on commit 1027e15

Please sign in to comment.