2019-07-20 04:18:50 +08:00
|
|
|
package kube_inventory
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2025-02-25 09:37:12 +08:00
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2019-07-20 04:18:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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 {
|
2024-12-17 22:33:30 +08:00
|
|
|
gatherEndpoint(i, acc)
|
2019-07-20 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-17 22:33:30 +08:00
|
|
|
func gatherEndpoint(e corev1.Endpoints, acc telegraf.Accumulator) {
|
2025-04-25 04:23:04 +08:00
|
|
|
creationTS := e.GetCreationTimestamp()
|
|
|
|
|
if creationTS.IsZero() {
|
2021-03-18 05:35:25 +08:00
|
|
|
return
|
2019-07-20 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fields := map[string]interface{}{
|
2021-03-18 05:35:25 +08:00
|
|
|
"created": e.GetCreationTimestamp().UnixNano(),
|
|
|
|
|
"generation": e.Generation,
|
2019-07-20 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tags := map[string]string{
|
2021-03-18 05:35:25 +08:00
|
|
|
"endpoint_name": e.Name,
|
|
|
|
|
"namespace": e.Namespace,
|
2019-07-20 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
for _, endpoint := range e.Subsets {
|
|
|
|
|
for _, readyAddr := range endpoint.Addresses {
|
2019-07-20 04:18:50 +08:00
|
|
|
fields["ready"] = true
|
|
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
tags["hostname"] = readyAddr.Hostname
|
2021-07-07 03:57:52 +08:00
|
|
|
if readyAddr.NodeName != nil {
|
|
|
|
|
tags["node_name"] = *readyAddr.NodeName
|
|
|
|
|
}
|
2019-07-20 04:18:50 +08:00
|
|
|
if readyAddr.TargetRef != nil {
|
2021-03-18 05:35:25 +08:00
|
|
|
tags[strings.ToLower(readyAddr.TargetRef.Kind)] = readyAddr.TargetRef.Name
|
2019-07-20 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
for _, port := range endpoint.Ports {
|
|
|
|
|
fields["port"] = port.Port
|
2019-07-20 04:18:50 +08:00
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
tags["port_name"] = port.Name
|
|
|
|
|
tags["port_protocol"] = string(port.Protocol)
|
2019-07-20 04:18:50 +08:00
|
|
|
|
|
|
|
|
acc.AddFields(endpointMeasurement, fields, tags)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-18 05:35:25 +08:00
|
|
|
for _, notReadyAddr := range endpoint.NotReadyAddresses {
|
2019-07-20 04:18:50 +08:00
|
|
|
fields["ready"] = false
|
|
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
tags["hostname"] = notReadyAddr.Hostname
|
2021-07-07 03:57:52 +08:00
|
|
|
if notReadyAddr.NodeName != nil {
|
|
|
|
|
tags["node_name"] = *notReadyAddr.NodeName
|
|
|
|
|
}
|
2019-07-20 04:18:50 +08:00
|
|
|
if notReadyAddr.TargetRef != nil {
|
2021-03-18 05:35:25 +08:00
|
|
|
tags[strings.ToLower(notReadyAddr.TargetRef.Kind)] = notReadyAddr.TargetRef.Name
|
2019-07-20 04:18:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
for _, port := range endpoint.Ports {
|
|
|
|
|
fields["port"] = port.Port
|
2019-07-20 04:18:50 +08:00
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
tags["port_name"] = port.Name
|
|
|
|
|
tags["port_protocol"] = string(port.Protocol)
|
2019-07-20 04:18:50 +08:00
|
|
|
|
|
|
|
|
acc.AddFields(endpointMeasurement, fields, tags)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|