Repository: VictoriaMetrics/VictoriaMetrics
Pull request #4255 (2023-05-05 12:31:14)

lib/promscrape/discovery/kubernetes: follow-up for d5e94721dbe0d0ab9f
- add changelog reference to an author
- fix tests
- add metadata to match Prometheus behavior


Patch:
diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 029d66090..3c40433ce 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -52,7 +52,7 @@ The following tip changes can be tested by building VictoriaMetrics components f
 * BUGFIX: [vmui](https://docs.victoriametrics.com/#vmui): fix issue where vmui would freeze when adding a query that returned regular rows alongside a heatmap query.
 * BUGFIX: [vmalert](https://docs.victoriametrics.com/vmalert.html): properly display an error when using `query` function for templating value of `-external.alert.source` flag. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4181).
 * BUGFIX: [vmalert](https://docs.victoriametrics.com/vmalert.html): properly return empty slices instead of nil for `/api/v1/rules` and `/api/v1/alerts` API handlers. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4221).
-* BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): add `__meta_kubernetes_endpoints_name` label for all ports discovered from endpoint. Previously, ports not matched by `Service` did not have this label. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4154) for details.
+* BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): add `__meta_kubernetes_endpoints_name` label for all ports discovered from endpoint. Previously, ports not matched by `Service` did not have this label. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4154) for details. Thanks to @thunderbird86 for discovering and [fixing](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/4253) the issue.
 * BUGFIX: fix indexdb rotation getting in infinite loop when using `retentionTimezoneOffset` and local timezone is not UTC. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4207) for details. Thanks to @faceair for the fix.
 * BUGFIX: max value for `memory.allowedPercent` changed from 200 to 100. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4171).
 
diff --git a/lib/promscrape/discovery/kubernetes/pod.go b/lib/promscrape/discovery/kubernetes/pod.go
index 475269d95..5c89a5a59 100644
--- a/lib/promscrape/discovery/kubernetes/pod.go
+++ b/lib/promscrape/discovery/kubernetes/pod.go
@@ -180,11 +180,16 @@ func (p *Pod) appendContainerLabels(m *promutils.Labels, c Container, cp *Contai
 
 func (p *Pod) appendEndpointLabels(m *promutils.Labels, eps *Endpoints) {
 	m.Add("__meta_kubernetes_endpoints_name", eps.Metadata.Name)
+	m.Add("__meta_kubernetes_endpoint_address_target_kind", "Pod")
+	m.Add("__meta_kubernetes_endpoint_address_target_name", p.Metadata.Name)
 	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)
+	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)
 	eps.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_endpointslice", m)
 }
 


Test patch:
diff --git a/lib/promscrape/discovery/kubernetes/endpoints_test.go b/lib/promscrape/discovery/kubernetes/endpoints_test.go
index bb6b9e8ac..36b4c5745 100644
--- a/lib/promscrape/discovery/kubernetes/endpoints_test.go
+++ b/lib/promscrape/discovery/kubernetes/endpoints_test.go
@@ -292,7 +292,9 @@ func TestGetEndpointsLabels(t *testing.T) {
 				"__meta_kubernetes_service_type":                 "service-type",
 			}),
 			promutils.NewLabelsFromMap(map[string]string{
-				"__address__":                                    "192.168.15.1:8428",
+				"__address__": "192.168.15.1:8428",
+				"__meta_kubernetes_endpoint_address_target_kind": "Pod",
+				"__meta_kubernetes_endpoint_address_target_name": "test-pod",
 				"__meta_kubernetes_endpoints_name":               "test-eps",
 				"__meta_kubernetes_namespace":                    "default",
 				"__meta_kubernetes_node_label_node_label":        "xyz",
diff --git a/lib/promscrape/discovery/kubernetes/endpointslice_test.go b/lib/promscrape/discovery/kubernetes/endpointslice_test.go
index ef5c51e6a..7a15c0b3b 100644
--- a/lib/promscrape/discovery/kubernetes/endpointslice_test.go
+++ b/lib/promscrape/discovery/kubernetes/endpointslice_test.go
@@ -426,26 +426,32 @@ func TestGetEndpointsliceLabels(t *testing.T) {
 				"__meta_kubernetes_service_type":                                          "service-type",
 			}),
 			promutils.NewLabelsFromMap(map[string]string{
-				"__address__":                                    "192.168.15.1:8428",
-				"__meta_kubernetes_namespace":                    "default",
-				"__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",
-				"__meta_kubernetes_pod_container_port_protocol":  "foobar",
-				"__meta_kubernetes_pod_host_ip":                  "4.5.6.7",
-				"__meta_kubernetes_pod_ip":                       "192.168.15.1",
-				"__meta_kubernetes_pod_name":                     "test-pod",
-				"__meta_kubernetes_pod_node_name":                "test-node",
-				"__meta_kubernetes_pod_phase":                    "abc",
-				"__meta_kubernetes_pod_ready":                    "unknown",
-				"__meta_kubernetes_pod_uid":                      "pod-uid",
-				"__meta_kubernetes_service_cluster_ip":           "1.2.3.4",
-				"__meta_kubernetes_service_name":                 "test-svc",
-				"__meta_kubernetes_service_type":                 "service-type",
+				"__address__": "192.168.15.1:8428",
+				"__meta_kubernetes_endpointslice_address_target_kind":                     "Pod",
+				"__meta_kubernetes_endpointslice_address_target_name":                     "test-pod",
+				"__meta_kubernetes_endpointslice_address_type":                            "foobar",
+				"__meta_kubernetes_endpointslice_label_kubernetes_io_service_name":        "test-svc",
+				"__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true",
+				"__meta_kubernetes_endpointslice_name":                                    "test-eps",
+				"__meta_kubernetes_namespace":                                             "default",
+				"__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",
+				"__meta_kubernetes_pod_container_port_protocol":                           "foobar",
+				"__meta_kubernetes_pod_host_ip":                                           "4.5.6.7",
+				"__meta_kubernetes_pod_ip":                                                "192.168.15.1",
+				"__meta_kubernetes_pod_name":                                              "test-pod",
+				"__meta_kubernetes_pod_node_name":                                         "test-node",
+				"__meta_kubernetes_pod_phase":                                             "abc",
+				"__meta_kubernetes_pod_ready":                                             "unknown",
+				"__meta_kubernetes_pod_uid":                                               "pod-uid",
+				"__meta_kubernetes_service_cluster_ip":                                    "1.2.3.4",
+				"__meta_kubernetes_service_name":                                          "test-svc",
+				"__meta_kubernetes_service_type":                                          "service-type",
 			}),
 		})
 	})