2020-04-13 18:02:27 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-03-11 14:41:09 +00:00
|
|
|
"io"
|
2020-04-13 18:02:27 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-04-23 08:34:04 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2020-04-13 18:02:27 +00:00
|
|
|
)
|
|
|
|
|
2021-04-02 11:45:08 +00:00
|
|
|
func (p *Pod) key() string {
|
|
|
|
return p.Metadata.key()
|
2021-02-26 14:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 14:41:09 +00:00
|
|
|
func parsePodList(r io.Reader) (map[string]object, ListMeta, error) {
|
2021-02-26 14:54:03 +00:00
|
|
|
var pl PodList
|
2021-03-11 14:41:09 +00: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 14:54:03 +00:00
|
|
|
}
|
2021-04-02 11:45:08 +00:00
|
|
|
objectsByKey := make(map[string]object)
|
2021-02-26 14:54:03 +00:00
|
|
|
for _, p := range pl.Items {
|
2021-04-02 11:45:08 +00:00
|
|
|
objectsByKey[p.key()] = p
|
2021-02-26 14:54:03 +00:00
|
|
|
}
|
2021-04-02 11:45:08 +00:00
|
|
|
return objectsByKey, pl.Metadata, nil
|
2021-02-26 14:54:03 +00: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 18:02:27 +00: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 14:54:03 +00:00
|
|
|
Metadata ListMeta
|
|
|
|
Items []*Pod
|
2020-04-13 18:02:27 +00: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-14 22:18:21 +00:00
|
|
|
Image string
|
2020-04-13 18:02:27 +00: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 {
|
|
|
|
Phase string
|
|
|
|
PodIP string
|
|
|
|
HostIP string
|
|
|
|
Conditions []PodCondition
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-02-26 18:21:27 +00:00
|
|
|
// getTargetLabels returns labels for each port of the given p.
|
2020-04-13 18:02:27 +00:00
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#pod
|
2021-03-14 19:10:35 +00:00
|
|
|
func (p *Pod) getTargetLabels(gw *groupWatcher) []map[string]string {
|
2020-04-13 18:02:27 +00:00
|
|
|
if len(p.Status.PodIP) == 0 {
|
|
|
|
// Skip pod without IP
|
2021-02-26 18:21:27 +00:00
|
|
|
return nil
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
2021-02-26 18:21:27 +00:00
|
|
|
var ms []map[string]string
|
2022-04-22 16:39:34 +00:00
|
|
|
ms = appendPodLabels(ms, gw, p, p.Spec.Containers, "false")
|
|
|
|
ms = appendPodLabels(ms, gw, p, p.Spec.InitContainers, "true")
|
2020-04-13 18:02:27 +00:00
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
2022-04-22 16:39:34 +00:00
|
|
|
func appendPodLabels(ms []map[string]string, gw *groupWatcher, p *Pod, cs []Container, isInit string) []map[string]string {
|
2020-04-13 18:02:27 +00:00
|
|
|
for _, c := range cs {
|
|
|
|
for _, cp := range c.Ports {
|
2022-04-22 16:39:34 +00:00
|
|
|
ms = appendPodLabelsInternal(ms, gw, p, c, &cp, isInit)
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
|
|
|
if len(c.Ports) == 0 {
|
2022-04-22 16:39:34 +00:00
|
|
|
ms = appendPodLabelsInternal(ms, gw, p, c, nil, isInit)
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
2022-04-22 16:39:34 +00:00
|
|
|
func appendPodLabelsInternal(ms []map[string]string, gw *groupWatcher, p *Pod, c Container, cp *ContainerPort, isInit string) []map[string]string {
|
2020-04-13 18:02:27 +00:00
|
|
|
addr := p.Status.PodIP
|
|
|
|
if cp != nil {
|
2020-04-23 08:34:04 +00:00
|
|
|
addr = discoveryutils.JoinHostPort(addr, cp.ContainerPort)
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
|
|
|
m := map[string]string{
|
|
|
|
"__address__": addr,
|
|
|
|
"__meta_kubernetes_pod_container_init": isInit,
|
|
|
|
}
|
2022-04-26 12:25:58 +00:00
|
|
|
p.appendCommonLabels(m, gw)
|
2020-04-13 18:02:27 +00:00
|
|
|
p.appendContainerLabels(m, c, cp)
|
2022-04-22 16:39:34 +00:00
|
|
|
return append(ms, m)
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pod) appendContainerLabels(m map[string]string, c Container, cp *ContainerPort) {
|
2022-08-14 22:18:21 +00:00
|
|
|
m["__meta_kubernetes_pod_container_image"] = c.Image
|
2020-04-13 18:02:27 +00:00
|
|
|
m["__meta_kubernetes_pod_container_name"] = c.Name
|
|
|
|
if cp != nil {
|
|
|
|
m["__meta_kubernetes_pod_container_port_name"] = cp.Name
|
|
|
|
m["__meta_kubernetes_pod_container_port_number"] = strconv.Itoa(cp.ContainerPort)
|
|
|
|
m["__meta_kubernetes_pod_container_port_protocol"] = cp.Protocol
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-26 12:25:58 +00:00
|
|
|
func (p *Pod) appendCommonLabels(m map[string]string, gw *groupWatcher) {
|
2022-04-22 16:39:34 +00:00
|
|
|
if gw.attachNodeMetadata {
|
2022-04-26 12:25:58 +00:00
|
|
|
m["__meta_kubernetes_node_name"] = p.Spec.NodeName
|
2022-04-22 16:39:34 +00:00
|
|
|
o := gw.getObjectByRoleLocked("node", p.Metadata.Namespace, p.Spec.NodeName)
|
2022-04-26 12:25:58 +00:00
|
|
|
if o != nil {
|
|
|
|
n := o.(*Node)
|
|
|
|
n.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_node", m)
|
2022-04-22 16:39:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-13 18:02:27 +00:00
|
|
|
m["__meta_kubernetes_pod_name"] = p.Metadata.Name
|
|
|
|
m["__meta_kubernetes_pod_ip"] = p.Status.PodIP
|
|
|
|
m["__meta_kubernetes_pod_ready"] = getPodReadyStatus(p.Status.Conditions)
|
|
|
|
m["__meta_kubernetes_pod_phase"] = p.Status.Phase
|
|
|
|
m["__meta_kubernetes_pod_node_name"] = p.Spec.NodeName
|
|
|
|
m["__meta_kubernetes_pod_host_ip"] = p.Status.HostIP
|
|
|
|
m["__meta_kubernetes_pod_uid"] = p.Metadata.UID
|
|
|
|
m["__meta_kubernetes_namespace"] = p.Metadata.Namespace
|
|
|
|
if pc := getPodController(p.Metadata.OwnerReferences); pc != nil {
|
|
|
|
if pc.Kind != "" {
|
|
|
|
m["__meta_kubernetes_pod_controller_kind"] = pc.Kind
|
|
|
|
}
|
|
|
|
if pc.Name != "" {
|
|
|
|
m["__meta_kubernetes_pod_controller_name"] = pc.Name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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" {
|
|
|
|
return strings.ToLower(c.Status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "unknown"
|
|
|
|
}
|