From 1a00c9ef03dad0a82c5f3e8e887be6ea36e7ff4e Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Mon, 15 Aug 2022 01:18:21 +0300 Subject: [PATCH] lib/promscrape/discovery/kubernetes: add `__meta_kubernetes_pod_container_image` label in the same way as Prometheus 2.38 does See https://github.com/prometheus/prometheus/pull/11034 --- docs/CHANGELOG.md | 1 + docs/sd_configs.md | 1 + lib/promscrape/discovery/kubernetes/endpoints_test.go | 8 +++++++- lib/promscrape/discovery/kubernetes/endpointslice_test.go | 8 +++++++- lib/promscrape/discovery/kubernetes/pod.go | 2 ++ lib/promscrape/discovery/kubernetes/pod_test.go | 1 + 6 files changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index ac5bc17d7c..bfb5f47b41 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -20,6 +20,7 @@ The following tip changes can be tested by building VictoriaMetrics components f * FEATURE: [VictoriaMetrics cluster](https://docs.victoriametrics.com/Cluster-VictoriaMetrics.html): improve performance for heavy queries on systems with many CPU cores. * FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): add support for MX record types in [dns_sd_configs](https://docs.victoriametrics.com/sd_configs.html#dns_sd_configs) in the same way as Prometheus 2.38 [does](https://github.com/prometheus/prometheus/pull/10099). * FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): add `__meta_kubernetes_service_port_number` meta-label for `role: service` in [kubernetes_sd_configs](https://docs.victoriametrics.com/sd_configs.html#kubernetes_sd_configs) in the same way as Prometheus 2.38 [does](https://github.com/prometheus/prometheus/pull/11002). +* FEATURE: [vmagent](https://docs.victoriametrics.com/vmagent.html): add `__meta_kubernetes_pod_container_image` meta-label for `role: pod` in [kubernetes_sd_configs](https://docs.victoriametrics.com/sd_configs.html#kubernetes_sd_configs) in the same way as Prometheus 2.38 [does](https://github.com/prometheus/prometheus/pull/11034). * FEATURE: [vmalert](https://docs.victoriametrics.com/vmalert.html): add `toTime()` template function in the same way as Prometheus 2.38 [does](https://github.com/prometheus/prometheus/pull/10993). See [these docs](https://prometheus.io/docs/prometheus/latest/configuration/template_reference/#numbers). * BUGFIX: prevent from excess CPU usage when the storage enters [read-only mode](https://docs.victoriametrics.com/Cluster-VictoriaMetrics.html#readonly-mode). diff --git a/docs/sd_configs.md b/docs/sd_configs.md index 7844517f74..aa219c6a97 100644 --- a/docs/sd_configs.md +++ b/docs/sd_configs.md @@ -776,6 +776,7 @@ One of the following `role` types can be configured to discover targets: * `__meta_kubernetes_pod_annotation_`: Each annotation from the pod object. * `__meta_kubernetes_pod_annotationpresent_`: "true" for each annotation from the pod object. * `__meta_kubernetes_pod_container_init`: "true" if the container is an InitContainer + * `__meta_kubernetes_pod_container_image`: Container image the target address points to. * `__meta_kubernetes_pod_container_name`: Name of the container the target address points to. * `__meta_kubernetes_pod_container_port_name`: Name of the container port. * `__meta_kubernetes_pod_container_port_number`: Number of the container port. diff --git a/lib/promscrape/discovery/kubernetes/endpoints_test.go b/lib/promscrape/discovery/kubernetes/endpoints_test.go index bbd7c296b2..c238de6c43 100644 --- a/lib/promscrape/discovery/kubernetes/endpoints_test.go +++ b/lib/promscrape/discovery/kubernetes/endpoints_test.go @@ -184,7 +184,11 @@ func TestGetEndpointsLabels(t *testing.T) { }, } for cn, ports := range args.containerPorts { - pod.Spec.Containers = append(pod.Spec.Containers, Container{Name: cn, Ports: ports}) + pod.Spec.Containers = append(pod.Spec.Containers, Container{ + Name: cn, + Image: "test-image", + Ports: ports, + }) } var gw groupWatcher gw.m = map[string]*urlWatcher{ @@ -298,6 +302,7 @@ func TestGetEndpointsLabels(t *testing.T) { "__meta_kubernetes_node_label_node_label": "xyz", "__meta_kubernetes_node_labelpresent_node_label": "true", "__meta_kubernetes_node_name": "test-node", + "__meta_kubernetes_pod_container_image": "test-image", "__meta_kubernetes_pod_container_name": "metrics", "__meta_kubernetes_pod_container_port_name": "http-metrics", "__meta_kubernetes_pod_container_port_number": "8428", @@ -343,6 +348,7 @@ func TestGetEndpointsLabels(t *testing.T) { "__meta_kubernetes_node_label_node_label": "xyz", "__meta_kubernetes_node_labelpresent_node_label": "true", "__meta_kubernetes_node_name": "test-node", + "__meta_kubernetes_pod_container_image": "test-image", "__meta_kubernetes_pod_container_name": "metrics", "__meta_kubernetes_pod_container_port_name": "web", "__meta_kubernetes_pod_container_port_number": "8428", diff --git a/lib/promscrape/discovery/kubernetes/endpointslice_test.go b/lib/promscrape/discovery/kubernetes/endpointslice_test.go index f6c65a0945..e789843f46 100644 --- a/lib/promscrape/discovery/kubernetes/endpointslice_test.go +++ b/lib/promscrape/discovery/kubernetes/endpointslice_test.go @@ -304,7 +304,11 @@ func TestGetEndpointsliceLabels(t *testing.T) { }, } for cn, ports := range args.containerPorts { - pod.Spec.Containers = append(pod.Spec.Containers, Container{Name: cn, Ports: ports}) + pod.Spec.Containers = append(pod.Spec.Containers, Container{ + Name: cn, + Image: "test-image", + Ports: ports, + }) } var gw groupWatcher gw.m = map[string]*urlWatcher{ @@ -432,6 +436,7 @@ func TestGetEndpointsliceLabels(t *testing.T) { "__meta_kubernetes_node_label_node_label": "xyz", "__meta_kubernetes_node_labelpresent_node_label": "true", "__meta_kubernetes_node_name": "test-node", + "__meta_kubernetes_pod_container_image": "test-image", "__meta_kubernetes_pod_container_name": "metrics", "__meta_kubernetes_pod_container_port_name": "http-metrics", "__meta_kubernetes_pod_container_port_number": "8428", @@ -484,6 +489,7 @@ func TestGetEndpointsliceLabels(t *testing.T) { "__meta_kubernetes_node_label_node_label": "xyz", "__meta_kubernetes_node_labelpresent_node_label": "true", "__meta_kubernetes_node_name": "test-node", + "__meta_kubernetes_pod_container_image": "test-image", "__meta_kubernetes_pod_container_name": "metrics", "__meta_kubernetes_pod_container_port_name": "web", "__meta_kubernetes_pod_container_port_number": "8428", diff --git a/lib/promscrape/discovery/kubernetes/pod.go b/lib/promscrape/discovery/kubernetes/pod.go index 6a3059c1f3..02f821a861 100644 --- a/lib/promscrape/discovery/kubernetes/pod.go +++ b/lib/promscrape/discovery/kubernetes/pod.go @@ -66,6 +66,7 @@ type PodSpec struct { // See https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#container-v1-core type Container struct { Name string + Image string Ports []ContainerPort } @@ -135,6 +136,7 @@ func appendPodLabelsInternal(ms []map[string]string, gw *groupWatcher, p *Pod, c } func (p *Pod) appendContainerLabels(m map[string]string, c Container, cp *ContainerPort) { + m["__meta_kubernetes_pod_container_image"] = c.Image m["__meta_kubernetes_pod_container_name"] = c.Name if cp != nil { m["__meta_kubernetes_pod_container_port_name"] = cp.Name diff --git a/lib/promscrape/discovery/kubernetes/pod_test.go b/lib/promscrape/discovery/kubernetes/pod_test.go index f6b7681c1e..a3c7997af0 100644 --- a/lib/promscrape/discovery/kubernetes/pod_test.go +++ b/lib/promscrape/discovery/kubernetes/pod_test.go @@ -249,6 +249,7 @@ func TestParsePodListSuccess(t *testing.T) { "__meta_kubernetes_node_name": "test-node", "__meta_kubernetes_pod_name": "etcd-m01", "__meta_kubernetes_pod_ip": "172.17.0.2", + "__meta_kubernetes_pod_container_image": "k8s.gcr.io/etcd:3.4.3-0", "__meta_kubernetes_pod_container_name": "etcd", "__meta_kubernetes_pod_container_port_name": "foobar", "__meta_kubernetes_pod_container_port_number": "1234",