2019-02-05 04:28:43 +08:00
|
|
|
package kube_inventory
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2023-05-19 21:54:09 +08:00
|
|
|
"strings"
|
2019-02-05 04:28:43 +08:00
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
corev1 "k8s.io/api/core/v1"
|
2019-02-05 04:28:43 +08:00
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func collectPods(ctx context.Context, acc telegraf.Accumulator, ki *KubernetesInventory) {
|
2023-10-04 21:26:41 +08:00
|
|
|
list, err := ki.client.getPods(ctx, ki.NodeName)
|
|
|
|
|
|
2019-02-05 04:28:43 +08:00
|
|
|
if err != nil {
|
|
|
|
|
acc.AddError(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for _, p := range list.Items {
|
2021-03-18 05:35:25 +08:00
|
|
|
ki.gatherPod(p, acc)
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
func (ki *KubernetesInventory) gatherPod(p corev1.Pod, acc telegraf.Accumulator) {
|
2021-10-26 05:01:35 +08:00
|
|
|
creationTs := p.GetCreationTimestamp()
|
|
|
|
|
if creationTs.IsZero() {
|
2021-03-18 05:35:25 +08:00
|
|
|
return
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
containerList := map[string]*corev1.ContainerStatus{}
|
|
|
|
|
for i := range p.Status.ContainerStatuses {
|
|
|
|
|
containerList[p.Status.ContainerStatuses[i].Name] = &p.Status.ContainerStatuses[i]
|
2020-12-11 22:08:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, c := range p.Spec.Containers {
|
2021-03-18 05:35:25 +08:00
|
|
|
cs, ok := containerList[c.Name]
|
2020-12-11 22:08:30 +08:00
|
|
|
if !ok {
|
2021-03-18 05:35:25 +08:00
|
|
|
cs = &corev1.ContainerStatus{}
|
2020-12-11 22:08:30 +08:00
|
|
|
}
|
2021-10-27 23:48:57 +08:00
|
|
|
ki.gatherPodContainer(p, *cs, c, acc)
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 23:48:57 +08:00
|
|
|
func (ki *KubernetesInventory) gatherPodContainer(p corev1.Pod, cs corev1.ContainerStatus, c corev1.Container, acc telegraf.Accumulator) {
|
2019-02-05 04:28:43 +08:00
|
|
|
stateCode := 3
|
2020-06-17 04:28:09 +08:00
|
|
|
stateReason := ""
|
2019-02-05 04:28:43 +08:00
|
|
|
state := "unknown"
|
2020-12-11 22:08:30 +08:00
|
|
|
readiness := "unready"
|
2020-06-17 04:28:09 +08:00
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
switch {
|
|
|
|
|
case cs.State.Running != nil:
|
|
|
|
|
stateCode = 0
|
|
|
|
|
state = "running"
|
|
|
|
|
case cs.State.Terminated != nil:
|
|
|
|
|
stateCode = 1
|
|
|
|
|
state = "terminated"
|
|
|
|
|
stateReason = cs.State.Terminated.Reason
|
|
|
|
|
case cs.State.Waiting != nil:
|
|
|
|
|
stateCode = 2
|
|
|
|
|
state = "waiting"
|
|
|
|
|
stateReason = cs.State.Waiting.Reason
|
2020-06-17 04:28:09 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
if cs.Ready {
|
2020-06-17 04:28:09 +08:00
|
|
|
readiness = "ready"
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fields := map[string]interface{}{
|
2021-03-18 05:35:25 +08:00
|
|
|
"restarts_total": cs.RestartCount,
|
2020-12-11 22:08:30 +08:00
|
|
|
"state_code": stateCode,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// deprecated in 1.15: use `state_reason` instead
|
|
|
|
|
if state == "terminated" {
|
|
|
|
|
fields["terminated_reason"] = stateReason
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
2020-06-17 04:28:09 +08:00
|
|
|
|
|
|
|
|
if stateReason != "" {
|
|
|
|
|
fields["state_reason"] = stateReason
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 05:35:25 +08:00
|
|
|
phaseReason := p.Status.Reason
|
2020-12-11 22:08:30 +08:00
|
|
|
if phaseReason != "" {
|
|
|
|
|
fields["phase_reason"] = phaseReason
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-05 04:28:43 +08:00
|
|
|
tags := map[string]string{
|
2021-03-18 05:35:25 +08:00
|
|
|
"container_name": c.Name,
|
|
|
|
|
"namespace": p.Namespace,
|
|
|
|
|
"node_name": p.Spec.NodeName,
|
|
|
|
|
"pod_name": p.Name,
|
|
|
|
|
"phase": string(p.Status.Phase),
|
2019-02-05 04:28:43 +08:00
|
|
|
"state": state,
|
2020-06-17 04:28:09 +08:00
|
|
|
"readiness": readiness,
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
2023-05-19 21:54:09 +08:00
|
|
|
splitImage := strings.Split(c.Image, ":")
|
|
|
|
|
if len(splitImage) == 2 {
|
|
|
|
|
tags["version"] = splitImage[1]
|
|
|
|
|
}
|
|
|
|
|
tags["image"] = splitImage[0]
|
2021-03-18 05:35:25 +08:00
|
|
|
for key, val := range p.Spec.NodeSelector {
|
2020-06-19 03:08:52 +08:00
|
|
|
if ki.selectorFilter.Match(key) {
|
|
|
|
|
tags["node_selector_"+key] = val
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-05 04:28:43 +08:00
|
|
|
|
|
|
|
|
req := c.Resources.Requests
|
|
|
|
|
lim := c.Resources.Limits
|
|
|
|
|
|
|
|
|
|
for resourceName, val := range req {
|
|
|
|
|
switch resourceName {
|
|
|
|
|
case "cpu":
|
2021-10-27 23:48:57 +08:00
|
|
|
fields["resource_requests_millicpu_units"] = ki.convertQuantity(val.String(), 1000)
|
2019-02-05 04:28:43 +08:00
|
|
|
case "memory":
|
2021-10-27 23:48:57 +08:00
|
|
|
fields["resource_requests_memory_bytes"] = ki.convertQuantity(val.String(), 1)
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for resourceName, val := range lim {
|
|
|
|
|
switch resourceName {
|
|
|
|
|
case "cpu":
|
2021-10-27 23:48:57 +08:00
|
|
|
fields["resource_limits_millicpu_units"] = ki.convertQuantity(val.String(), 1000)
|
2019-02-05 04:28:43 +08:00
|
|
|
case "memory":
|
2021-10-27 23:48:57 +08:00
|
|
|
fields["resource_limits_memory_bytes"] = ki.convertQuantity(val.String(), 1)
|
2019-02-05 04:28:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 21:54:09 +08:00
|
|
|
for _, val := range p.Status.Conditions {
|
|
|
|
|
conditionfields := map[string]interface{}{}
|
|
|
|
|
conditiontags := map[string]string{
|
|
|
|
|
"container_name": c.Name,
|
|
|
|
|
"image": splitImage[0],
|
|
|
|
|
"status": string(val.Status),
|
|
|
|
|
"namespace": p.Namespace,
|
|
|
|
|
"node_name": p.Spec.NodeName,
|
|
|
|
|
"pod_name": p.Name,
|
|
|
|
|
"condition": string(val.Type),
|
|
|
|
|
}
|
|
|
|
|
if len(splitImage) == 2 {
|
|
|
|
|
conditiontags["version"] = splitImage[1]
|
|
|
|
|
}
|
|
|
|
|
running := 0
|
|
|
|
|
podready := 0
|
|
|
|
|
if val.Status == "True" {
|
|
|
|
|
if val.Type == "Ready" {
|
|
|
|
|
podready = 1
|
|
|
|
|
}
|
|
|
|
|
running = 1
|
|
|
|
|
} else if val.Status == "Unknown" {
|
|
|
|
|
if val.Type == "Ready" {
|
|
|
|
|
podready = 0
|
|
|
|
|
}
|
|
|
|
|
running = 2
|
|
|
|
|
}
|
|
|
|
|
conditionfields["status_condition"] = running
|
|
|
|
|
conditionfields["ready"] = podready
|
|
|
|
|
acc.AddFields(podContainerMeasurement, conditionfields, conditiontags)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-05 04:28:43 +08:00
|
|
|
acc.AddFields(podContainerMeasurement, fields, tags)
|
|
|
|
|
}
|