2020-04-13 20:02:27 +02:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-03-11 15:41:09 +01:00
|
|
|
"io"
|
2020-04-13 20:02:27 +02:00
|
|
|
"strings"
|
2020-04-23 10:34:04 +02:00
|
|
|
|
2022-11-30 06:22:12 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2020-04-23 10:34:04 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2022-11-30 06:22:12 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2020-04-13 20:02:27 +02:00
|
|
|
)
|
|
|
|
|
2021-04-02 13:45:08 +02:00
|
|
|
func (p *Pod) key() string {
|
|
|
|
return p.Metadata.key()
|
2021-02-26 15:54:03 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 15:41:09 +01:00
|
|
|
func parsePodList(r io.Reader) (map[string]object, ListMeta, error) {
|
2021-02-26 15:54:03 +01:00
|
|
|
var pl PodList
|
2021-03-11 15:41:09 +01:00
|
|
|
d := json.NewDecoder(r)
|
|
|
|
if err := d.Decode(&pl); err != nil {
|
|
|
|
return nil, pl.Metadata, fmt.Errorf("cannot unmarshal PodList: %w", err)
|
2021-02-26 15:54:03 +01:00
|
|
|
}
|
2021-04-02 13:45:08 +02:00
|
|
|
objectsByKey := make(map[string]object)
|
2021-02-26 15:54:03 +01:00
|
|
|
for _, p := range pl.Items {
|
2021-04-02 13:45:08 +02:00
|
|
|
objectsByKey[p.key()] = p
|
2021-02-26 15:54:03 +01:00
|
|
|
}
|
2021-04-02 13:45:08 +02:00
|
|
|
return objectsByKey, pl.Metadata, nil
|
2021-02-26 15:54:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func parsePod(data []byte) (object, error) {
|
|
|
|
var p Pod
|
|
|
|
if err := json.Unmarshal(data, &p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &p, nil
|
|
|
|
}
|
|
|
|
|
2020-04-13 20:02:27 +02:00
|
|
|
// PodList implements k8s pod list.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#podlist-v1-core
|
|
|
|
type PodList struct {
|
2021-02-26 15:54:03 +01:00
|
|
|
Metadata ListMeta
|
|
|
|
Items []*Pod
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pod implements k8s pod.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#pod-v1-core
|
|
|
|
type Pod struct {
|
|
|
|
Metadata ObjectMeta
|
|
|
|
Spec PodSpec
|
|
|
|
Status PodStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
// PodSpec implements k8s pod spec.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#podspec-v1-core
|
|
|
|
type PodSpec struct {
|
|
|
|
NodeName string
|
|
|
|
Containers []Container
|
|
|
|
InitContainers []Container
|
|
|
|
}
|
|
|
|
|
|
|
|
// Container implements k8s container.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#container-v1-core
|
|
|
|
type Container struct {
|
|
|
|
Name string
|
2022-08-15 00:18:21 +02:00
|
|
|
Image string
|
2020-04-13 20:02:27 +02:00
|
|
|
Ports []ContainerPort
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerPort implements k8s container port.
|
|
|
|
type ContainerPort struct {
|
|
|
|
Name string
|
|
|
|
ContainerPort int
|
|
|
|
Protocol string
|
|
|
|
}
|
|
|
|
|
|
|
|
// PodStatus implements k8s pod status.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#podstatus-v1-core
|
|
|
|
type PodStatus struct {
|
2023-01-28 01:33:02 +01:00
|
|
|
Phase string
|
|
|
|
PodIP string
|
|
|
|
HostIP string
|
|
|
|
Conditions []PodCondition
|
|
|
|
ContainerStatuses []ContainerStatus
|
|
|
|
InitContainerStatuses []ContainerStatus
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PodCondition implements k8s pod condition.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#podcondition-v1-core
|
|
|
|
type PodCondition struct {
|
|
|
|
Type string
|
|
|
|
Status string
|
|
|
|
}
|
|
|
|
|
2023-01-28 01:33:02 +01:00
|
|
|
// ContainerStatus implements k8s container status.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#containerstatus-v1-core
|
|
|
|
type ContainerStatus struct {
|
|
|
|
Name string
|
|
|
|
ContainerID string
|
2024-01-24 13:52:12 +01:00
|
|
|
State ContainerState
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerState implements k8s container state.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#containerstatus-v1-core
|
|
|
|
type ContainerState struct {
|
|
|
|
Terminated *ContainerStateTerminated
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerState implements k8s terminated container state.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#containerstatus-v1-core
|
|
|
|
type ContainerStateTerminated struct {
|
|
|
|
ExitCode int
|
2023-01-28 01:33:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func getContainerID(p *Pod, containerName string, isInit bool) string {
|
2024-01-24 13:52:12 +01:00
|
|
|
cs := p.getContainerStatus(containerName, isInit)
|
|
|
|
if cs == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return cs.ContainerID
|
|
|
|
}
|
|
|
|
|
|
|
|
func isContainerTerminated(p *Pod, containerName string, isInit bool) bool {
|
|
|
|
cs := p.getContainerStatus(containerName, isInit)
|
|
|
|
if cs == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return cs.State.Terminated != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pod) getContainerStatus(containerName string, isInit bool) *ContainerStatus {
|
2023-01-28 01:33:02 +01:00
|
|
|
css := p.Status.ContainerStatuses
|
|
|
|
if isInit {
|
|
|
|
css = p.Status.InitContainerStatuses
|
|
|
|
}
|
2024-01-24 13:52:12 +01:00
|
|
|
for i := range css {
|
|
|
|
cs := &css[i]
|
2023-01-28 01:33:02 +01:00
|
|
|
if cs.Name == containerName {
|
2024-01-24 13:52:12 +01:00
|
|
|
return cs
|
2023-01-28 01:33:02 +01:00
|
|
|
}
|
|
|
|
}
|
2024-01-24 13:52:12 +01:00
|
|
|
return nil
|
2023-01-28 01:33:02 +01:00
|
|
|
}
|
|
|
|
|
2021-02-26 19:21:27 +01:00
|
|
|
// getTargetLabels returns labels for each port of the given p.
|
2020-04-13 20:02:27 +02:00
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#pod
|
2022-11-30 06:22:12 +01:00
|
|
|
func (p *Pod) getTargetLabels(gw *groupWatcher) []*promutils.Labels {
|
2020-04-13 20:02:27 +02:00
|
|
|
if len(p.Status.PodIP) == 0 {
|
2024-01-24 13:52:12 +01:00
|
|
|
// Skip pod without IP, since such pods cannnot be scraped.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if isPodPhaseFinished(p.Status.Phase) {
|
|
|
|
// Skip already stopped pod, since it cannot be scraped.
|
2021-02-26 19:21:27 +01:00
|
|
|
return nil
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
2024-01-24 13:52:12 +01:00
|
|
|
|
2022-11-30 06:22:12 +01:00
|
|
|
var ms []*promutils.Labels
|
2023-01-28 01:33:02 +01:00
|
|
|
ms = appendPodLabels(ms, gw, p, p.Spec.Containers, false)
|
|
|
|
ms = appendPodLabels(ms, gw, p, p.Spec.InitContainers, true)
|
2020-04-13 20:02:27 +02:00
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
2024-01-24 13:52:12 +01:00
|
|
|
func isPodPhaseFinished(phase string) bool {
|
|
|
|
// See https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase
|
|
|
|
return phase == "Succeeded" || phase == "Failed"
|
|
|
|
|
|
|
|
}
|
2023-01-28 01:33:02 +01:00
|
|
|
func appendPodLabels(ms []*promutils.Labels, gw *groupWatcher, p *Pod, cs []Container, isInit bool) []*promutils.Labels {
|
2020-04-13 20:02:27 +02:00
|
|
|
for _, c := range cs {
|
2024-01-24 13:52:12 +01:00
|
|
|
if isContainerTerminated(p, c.Name, isInit) {
|
|
|
|
// Skip terminated containers
|
|
|
|
continue
|
|
|
|
}
|
2020-04-13 20:02:27 +02:00
|
|
|
for _, cp := range c.Ports {
|
2024-01-24 13:52:12 +01:00
|
|
|
ms = appendPodLabelsInternal(ms, gw, p, &c, &cp, isInit)
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
|
|
|
if len(c.Ports) == 0 {
|
2024-01-24 13:52:12 +01:00
|
|
|
ms = appendPodLabelsInternal(ms, gw, p, &c, nil, isInit)
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
2024-01-24 13:52:12 +01:00
|
|
|
func appendPodLabelsInternal(ms []*promutils.Labels, gw *groupWatcher, p *Pod, c *Container, cp *ContainerPort, isInit bool) []*promutils.Labels {
|
2020-04-13 20:02:27 +02:00
|
|
|
addr := p.Status.PodIP
|
|
|
|
if cp != nil {
|
2020-04-23 10:34:04 +02:00
|
|
|
addr = discoveryutils.JoinHostPort(addr, cp.ContainerPort)
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
2022-11-30 06:22:12 +01:00
|
|
|
m := promutils.GetLabels()
|
|
|
|
m.Add("__address__", addr)
|
2023-01-28 01:33:02 +01:00
|
|
|
isInitStr := "false"
|
|
|
|
if isInit {
|
|
|
|
isInitStr = "true"
|
|
|
|
}
|
|
|
|
m.Add("__meta_kubernetes_pod_container_init", isInitStr)
|
|
|
|
|
|
|
|
containerID := getContainerID(p, c.Name, isInit)
|
|
|
|
if containerID != "" {
|
|
|
|
m.Add("__meta_kubernetes_pod_container_id", containerID)
|
|
|
|
}
|
|
|
|
|
2022-04-26 14:25:58 +02:00
|
|
|
p.appendCommonLabels(m, gw)
|
2020-04-13 20:02:27 +02:00
|
|
|
p.appendContainerLabels(m, c, cp)
|
2022-04-22 18:39:34 +02:00
|
|
|
return append(ms, m)
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
|
|
|
|
2024-01-24 13:52:12 +01:00
|
|
|
func (p *Pod) appendContainerLabels(m *promutils.Labels, c *Container, cp *ContainerPort) {
|
2022-11-30 06:22:12 +01:00
|
|
|
m.Add("__meta_kubernetes_pod_container_image", c.Image)
|
|
|
|
m.Add("__meta_kubernetes_pod_container_name", c.Name)
|
2020-04-13 20:02:27 +02:00
|
|
|
if cp != nil {
|
2022-11-30 06:22:12 +01:00
|
|
|
m.Add("__meta_kubernetes_pod_container_port_name", cp.Name)
|
|
|
|
m.Add("__meta_kubernetes_pod_container_port_number", bytesutil.Itoa(cp.ContainerPort))
|
|
|
|
m.Add("__meta_kubernetes_pod_container_port_protocol", cp.Protocol)
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 13:46:07 +02:00
|
|
|
func (p *Pod) appendEndpointLabels(m *promutils.Labels, eps *Endpoints) {
|
|
|
|
m.Add("__meta_kubernetes_endpoints_name", eps.Metadata.Name)
|
2023-05-05 14:41:17 +02:00
|
|
|
m.Add("__meta_kubernetes_endpoint_address_target_kind", "Pod")
|
|
|
|
m.Add("__meta_kubernetes_endpoint_address_target_name", p.Metadata.Name)
|
2023-05-05 13:46:07 +02:00
|
|
|
eps.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_endpoints", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pod) appendEndpointSliceLabels(m *promutils.Labels, eps *EndpointSlice) {
|
|
|
|
m.Add("__meta_kubernetes_endpointslice_name", eps.Metadata.Name)
|
2023-05-05 14:41:17 +02:00
|
|
|
m.Add("__meta_kubernetes_endpointslice_address_target_kind", "Pod")
|
|
|
|
m.Add("__meta_kubernetes_endpointslice_address_target_name", p.Metadata.Name)
|
|
|
|
m.Add("__meta_kubernetes_endpointslice_address_type", eps.AddressType)
|
2023-05-05 13:46:07 +02:00
|
|
|
eps.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_endpointslice", m)
|
|
|
|
}
|
|
|
|
|
2022-11-30 06:22:12 +01:00
|
|
|
func (p *Pod) appendCommonLabels(m *promutils.Labels, gw *groupWatcher) {
|
2022-04-22 18:39:34 +02:00
|
|
|
if gw.attachNodeMetadata {
|
2022-11-30 06:22:12 +01:00
|
|
|
m.Add("__meta_kubernetes_node_name", p.Spec.NodeName)
|
2022-04-22 18:39:34 +02:00
|
|
|
o := gw.getObjectByRoleLocked("node", p.Metadata.Namespace, p.Spec.NodeName)
|
2022-04-26 14:25:58 +02:00
|
|
|
if o != nil {
|
|
|
|
n := o.(*Node)
|
|
|
|
n.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_node", m)
|
2022-04-22 18:39:34 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-30 06:22:12 +01:00
|
|
|
m.Add("__meta_kubernetes_pod_name", p.Metadata.Name)
|
|
|
|
m.Add("__meta_kubernetes_pod_ip", p.Status.PodIP)
|
|
|
|
m.Add("__meta_kubernetes_pod_ready", getPodReadyStatus(p.Status.Conditions))
|
|
|
|
m.Add("__meta_kubernetes_pod_phase", p.Status.Phase)
|
|
|
|
m.Add("__meta_kubernetes_pod_node_name", p.Spec.NodeName)
|
|
|
|
m.Add("__meta_kubernetes_pod_host_ip", p.Status.HostIP)
|
|
|
|
m.Add("__meta_kubernetes_pod_uid", p.Metadata.UID)
|
|
|
|
m.Add("__meta_kubernetes_namespace", p.Metadata.Namespace)
|
2020-04-13 20:02:27 +02:00
|
|
|
if pc := getPodController(p.Metadata.OwnerReferences); pc != nil {
|
|
|
|
if pc.Kind != "" {
|
2022-11-30 06:22:12 +01:00
|
|
|
m.Add("__meta_kubernetes_pod_controller_kind", pc.Kind)
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
|
|
|
if pc.Name != "" {
|
2022-11-30 06:22:12 +01:00
|
|
|
m.Add("__meta_kubernetes_pod_controller_name", pc.Name)
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_pod", m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPodController(ors []OwnerReference) *OwnerReference {
|
|
|
|
for _, or := range ors {
|
|
|
|
if or.Controller {
|
|
|
|
return &or
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPodReadyStatus(conds []PodCondition) string {
|
|
|
|
for _, c := range conds {
|
|
|
|
if c.Type == "Ready" {
|
2022-11-30 06:22:12 +01:00
|
|
|
return toLowerConverter.Transform(c.Status)
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "unknown"
|
|
|
|
}
|
2022-11-30 06:22:12 +01:00
|
|
|
|
|
|
|
var toLowerConverter = bytesutil.NewFastStringTransformer(strings.ToLower)
|