forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
70 lines (56 loc) · 1.44 KB
/
service.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
package kube_inventory
import (
"context"
corev1 "k8s.io/api/core/v1"
"github.com/influxdata/telegraf"
)
func collectServices(ctx context.Context, acc telegraf.Accumulator, ki *KubernetesInventory) {
list, err := ki.client.getServices(ctx)
if err != nil {
acc.AddError(err)
return
}
for _, i := range list.Items {
ki.gatherService(i, acc)
}
}
func (ki *KubernetesInventory) gatherService(s corev1.Service, acc telegraf.Accumulator) {
if s.GetCreationTimestamp().Second() == 0 && s.GetCreationTimestamp().Nanosecond() == 0 {
return
}
fields := map[string]interface{}{
"created": s.GetCreationTimestamp().UnixNano(),
"generation": s.Generation,
}
tags := map[string]string{
"service_name": s.Name,
"namespace": s.Namespace,
}
for key, val := range s.Spec.Selector {
if ki.selectorFilter.Match(key) {
tags["selector_"+key] = val
}
}
var getPorts = func() {
for _, port := range s.Spec.Ports {
fields["port"] = port.Port
fields["target_port"] = port.TargetPort.IntVal
tags["port_name"] = port.Name
tags["port_protocol"] = string(port.Protocol)
if s.Spec.Type == "ExternalName" {
tags["external_name"] = s.Spec.ExternalName
} else {
tags["cluster_ip"] = s.Spec.ClusterIP
}
acc.AddFields(serviceMeasurement, fields, tags)
}
}
if externIPs := s.Spec.ExternalIPs; externIPs != nil {
for _, ip := range externIPs {
tags["ip"] = ip
getPorts()
}
} else {
getPorts()
}
}