forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.go
79 lines (64 loc) · 1.89 KB
/
endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package kube_inventory
import (
"context"
"strings"
"github.com/influxdata/telegraf"
corev1 "k8s.io/api/core/v1"
)
func collectEndpoints(ctx context.Context, acc telegraf.Accumulator, ki *KubernetesInventory) {
list, err := ki.client.getEndpoints(ctx)
if err != nil {
acc.AddError(err)
return
}
for _, i := range list.Items {
ki.gatherEndpoint(i, acc)
}
}
func (ki *KubernetesInventory) gatherEndpoint(e corev1.Endpoints, acc telegraf.Accumulator) {
if e.GetCreationTimestamp().Second() == 0 && e.GetCreationTimestamp().Nanosecond() == 0 {
return
}
fields := map[string]interface{}{
"created": e.GetCreationTimestamp().UnixNano(),
"generation": e.Generation,
}
tags := map[string]string{
"endpoint_name": e.Name,
"namespace": e.Namespace,
}
for _, endpoint := range e.Subsets {
for _, readyAddr := range endpoint.Addresses {
fields["ready"] = true
tags["hostname"] = readyAddr.Hostname
if readyAddr.NodeName != nil {
tags["node_name"] = *readyAddr.NodeName
}
if readyAddr.TargetRef != nil {
tags[strings.ToLower(readyAddr.TargetRef.Kind)] = readyAddr.TargetRef.Name
}
for _, port := range endpoint.Ports {
fields["port"] = port.Port
tags["port_name"] = port.Name
tags["port_protocol"] = string(port.Protocol)
acc.AddFields(endpointMeasurement, fields, tags)
}
}
for _, notReadyAddr := range endpoint.NotReadyAddresses {
fields["ready"] = false
tags["hostname"] = notReadyAddr.Hostname
if notReadyAddr.NodeName != nil {
tags["node_name"] = *notReadyAddr.NodeName
}
if notReadyAddr.TargetRef != nil {
tags[strings.ToLower(notReadyAddr.TargetRef.Kind)] = notReadyAddr.TargetRef.Name
}
for _, port := range endpoint.Ports {
fields["port"] = port.Port
tags["port_name"] = port.Name
tags["port_protocol"] = string(port.Protocol)
acc.AddFields(endpointMeasurement, fields, tags)
}
}
}
}