diff --git a/discovery/kubernetes/endpoints.go b/discovery/kubernetes/endpoints.go index 3078cefde..7200d52dd 100644 --- a/discovery/kubernetes/endpoints.go +++ b/discovery/kubernetes/endpoints.go @@ -29,7 +29,6 @@ import ( "k8s.io/client-go/util/workqueue" "github.com/prometheus/prometheus/discovery/targetgroup" - "github.com/prometheus/prometheus/util/strutil" ) var ( @@ -248,9 +247,6 @@ func endpointsSourceFromNamespaceAndName(namespace, name string) string { } const ( - endpointsLabelPrefix = metaLabelPrefix + "endpoints_label_" - endpointsLabelPresentPrefix = metaLabelPrefix + "endpoints_labelpresent_" - endpointsNameLabel = metaLabelPrefix + "endpoints_name" endpointNodeName = metaLabelPrefix + "endpoint_node_name" endpointHostname = metaLabelPrefix + "endpoint_hostname" endpointReadyLabel = metaLabelPrefix + "endpoint_ready" @@ -265,16 +261,11 @@ func (e *Endpoints) buildEndpoints(eps *apiv1.Endpoints) *targetgroup.Group { Source: endpointsSource(eps), } tg.Labels = model.LabelSet{ - namespaceLabel: lv(eps.Namespace), - endpointsNameLabel: lv(eps.Name), + namespaceLabel: lv(eps.Namespace), } e.addServiceLabels(eps.Namespace, eps.Name, tg) // Add endpoints labels metadata. - for k, v := range eps.Labels { - ln := strutil.SanitizeLabelName(k) - tg.Labels[model.LabelName(endpointsLabelPrefix+ln)] = lv(v) - tg.Labels[model.LabelName(endpointsLabelPresentPrefix+ln)] = presentValue - } + addObjectMetaLabels(tg.Labels, eps.ObjectMeta, RoleEndpoint) type podEntry struct { pod *apiv1.Pod @@ -465,14 +456,7 @@ func addNodeLabels(tg model.LabelSet, nodeInf cache.SharedInformer, logger log.L node := obj.(*apiv1.Node) // Allocate one target label for the node name, - // and two target labels for each node label. - nodeLabelset := make(model.LabelSet, 1+2*len(node.GetLabels())) - nodeLabelset[nodeNameLabel] = lv(*nodeName) - for k, v := range node.GetLabels() { - ln := strutil.SanitizeLabelName(k) - nodeLabelset[model.LabelName(nodeLabelPrefix+ln)] = lv(v) - nodeLabelset[model.LabelName(nodeLabelPresentPrefix+ln)] = presentValue - } - + nodeLabelset := make(model.LabelSet) + addObjectMetaLabels(nodeLabelset, node.ObjectMeta, RoleNode) return tg.Merge(nodeLabelset) } diff --git a/discovery/kubernetes/endpoints_test.go b/discovery/kubernetes/endpoints_test.go index 256ffce1b..cf7fd9aee 100644 --- a/discovery/kubernetes/endpoints_test.go +++ b/discovery/kubernetes/endpoints_test.go @@ -32,6 +32,9 @@ func makeEndpoints() *v1.Endpoints { ObjectMeta: metav1.ObjectMeta{ Name: "testendpoints", Namespace: "default", + Annotations: map[string]string{ + "test.annotation": "test", + }, }, Subsets: []v1.EndpointSubset{ { @@ -134,8 +137,10 @@ func TestEndpointsDiscoveryBeforeRun(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_namespace": "default", - "__meta_kubernetes_endpoints_name": "testendpoints", + "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_endpoints_name": "testendpoints", + "__meta_kubernetes_endpoints_annotation_test_annotation": "test", + "__meta_kubernetes_endpoints_annotationpresent_test_annotation": "true", }, Source: "endpoints/default/testendpoints", }, @@ -434,11 +439,13 @@ func TestEndpointsDiscoveryWithService(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_namespace": "default", - "__meta_kubernetes_endpoints_name": "testendpoints", - "__meta_kubernetes_service_label_app_name": "test", - "__meta_kubernetes_service_labelpresent_app_name": "true", - "__meta_kubernetes_service_name": "testendpoints", + "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_endpoints_name": "testendpoints", + "__meta_kubernetes_service_label_app_name": "test", + "__meta_kubernetes_service_labelpresent_app_name": "true", + "__meta_kubernetes_service_name": "testendpoints", + "__meta_kubernetes_endpoints_annotation_test_annotation": "test", + "__meta_kubernetes_endpoints_annotationpresent_test_annotation": "true", }, Source: "endpoints/default/testendpoints", }, @@ -510,13 +517,15 @@ func TestEndpointsDiscoveryWithServiceUpdate(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_namespace": "default", - "__meta_kubernetes_endpoints_name": "testendpoints", - "__meta_kubernetes_service_label_app_name": "svc", - "__meta_kubernetes_service_labelpresent_app_name": "true", - "__meta_kubernetes_service_name": "testendpoints", - "__meta_kubernetes_service_label_component": "testing", - "__meta_kubernetes_service_labelpresent_component": "true", + "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_endpoints_name": "testendpoints", + "__meta_kubernetes_service_label_app_name": "svc", + "__meta_kubernetes_service_labelpresent_app_name": "true", + "__meta_kubernetes_service_name": "testendpoints", + "__meta_kubernetes_service_label_component": "testing", + "__meta_kubernetes_service_labelpresent_component": "true", + "__meta_kubernetes_endpoints_annotation_test_annotation": "test", + "__meta_kubernetes_endpoints_annotationpresent_test_annotation": "true", }, Source: "endpoints/default/testendpoints", }, @@ -583,11 +592,13 @@ func TestEndpointsDiscoveryWithNodeMetadata(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_namespace": "default", - "__meta_kubernetes_endpoints_name": "testendpoints", - "__meta_kubernetes_service_label_app_name": "test", - "__meta_kubernetes_service_labelpresent_app_name": "true", - "__meta_kubernetes_service_name": "testendpoints", + "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_endpoints_name": "testendpoints", + "__meta_kubernetes_service_label_app_name": "test", + "__meta_kubernetes_service_labelpresent_app_name": "true", + "__meta_kubernetes_service_name": "testendpoints", + "__meta_kubernetes_endpoints_annotation_test_annotation": "test", + "__meta_kubernetes_endpoints_annotationpresent_test_annotation": "true", }, Source: "endpoints/default/testendpoints", }, @@ -658,11 +669,13 @@ func TestEndpointsDiscoveryWithUpdatedNodeMetadata(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_namespace": "default", - "__meta_kubernetes_endpoints_name": "testendpoints", - "__meta_kubernetes_service_label_app_name": "test", - "__meta_kubernetes_service_labelpresent_app_name": "true", - "__meta_kubernetes_service_name": "testendpoints", + "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_endpoints_name": "testendpoints", + "__meta_kubernetes_service_label_app_name": "test", + "__meta_kubernetes_service_labelpresent_app_name": "true", + "__meta_kubernetes_service_name": "testendpoints", + "__meta_kubernetes_endpoints_annotation_test_annotation": "test", + "__meta_kubernetes_endpoints_annotationpresent_test_annotation": "true", }, Source: "endpoints/default/testendpoints", }, @@ -777,11 +790,13 @@ func TestEndpointsDiscoveryNamespaces(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_namespace": "ns1", - "__meta_kubernetes_endpoints_name": "testendpoints", - "__meta_kubernetes_service_label_app": "app1", - "__meta_kubernetes_service_labelpresent_app": "true", - "__meta_kubernetes_service_name": "testendpoints", + "__meta_kubernetes_namespace": "ns1", + "__meta_kubernetes_endpoints_name": "testendpoints", + "__meta_kubernetes_endpoints_annotation_test_annotation": "test", + "__meta_kubernetes_endpoints_annotationpresent_test_annotation": "true", + "__meta_kubernetes_service_label_app": "app1", + "__meta_kubernetes_service_labelpresent_app": "true", + "__meta_kubernetes_service_name": "testendpoints", }, Source: "endpoints/ns1/testendpoints", }, @@ -901,8 +916,10 @@ func TestEndpointsDiscoveryOwnNamespace(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_namespace": "own-ns", - "__meta_kubernetes_endpoints_name": "testendpoints", + "__meta_kubernetes_namespace": "own-ns", + "__meta_kubernetes_endpoints_name": "testendpoints", + "__meta_kubernetes_endpoints_annotation_test_annotation": "test", + "__meta_kubernetes_endpoints_annotationpresent_test_annotation": "true", }, Source: "endpoints/own-ns/testendpoints", }, diff --git a/discovery/kubernetes/endpointslice.go b/discovery/kubernetes/endpointslice.go index 1eb3fc180..e241c758b 100644 --- a/discovery/kubernetes/endpointslice.go +++ b/discovery/kubernetes/endpointslice.go @@ -252,7 +252,6 @@ func endpointSliceSourceFromNamespaceAndName(namespace, name string) string { } const ( - endpointSliceNameLabel = metaLabelPrefix + "endpointslice_name" endpointSliceAddressTypeLabel = metaLabelPrefix + "endpointslice_address_type" endpointSlicePortNameLabel = metaLabelPrefix + "endpointslice_port_name" endpointSlicePortProtocolLabel = metaLabelPrefix + "endpointslice_port_protocol" @@ -274,9 +273,11 @@ func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgrou } tg.Labels = model.LabelSet{ namespaceLabel: lv(eps.namespace()), - endpointSliceNameLabel: lv(eps.name()), endpointSliceAddressTypeLabel: lv(eps.addressType()), } + + addObjectMetaLabels(tg.Labels, eps.getObjectMeta(), RoleEndpointSlice) + e.addServiceLabels(eps, tg) type podEntry struct { diff --git a/discovery/kubernetes/endpointslice_adaptor.go b/discovery/kubernetes/endpointslice_adaptor.go index 5a21f1b89..46fa708c1 100644 --- a/discovery/kubernetes/endpointslice_adaptor.go +++ b/discovery/kubernetes/endpointslice_adaptor.go @@ -17,11 +17,13 @@ import ( corev1 "k8s.io/api/core/v1" v1 "k8s.io/api/discovery/v1" "k8s.io/api/discovery/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // endpointSliceAdaptor is an adaptor for the different EndpointSlice versions type endpointSliceAdaptor interface { get() interface{} + getObjectMeta() metav1.ObjectMeta name() string namespace() string addressType() string @@ -66,6 +68,10 @@ func (e *endpointSliceAdaptorV1) get() interface{} { return e.endpointSlice } +func (e *endpointSliceAdaptorV1) getObjectMeta() metav1.ObjectMeta { + return e.endpointSlice.ObjectMeta +} + func (e *endpointSliceAdaptorV1) name() string { return e.endpointSlice.ObjectMeta.Name } @@ -115,6 +121,10 @@ func (e *endpointSliceAdaptorV1Beta1) get() interface{} { return e.endpointSlice } +func (e *endpointSliceAdaptorV1Beta1) getObjectMeta() metav1.ObjectMeta { + return e.endpointSlice.ObjectMeta +} + func (e *endpointSliceAdaptorV1Beta1) name() string { return e.endpointSlice.Name } diff --git a/discovery/kubernetes/endpointslice_test.go b/discovery/kubernetes/endpointslice_test.go index 0c31147a9..f23c9e655 100644 --- a/discovery/kubernetes/endpointslice_test.go +++ b/discovery/kubernetes/endpointslice_test.go @@ -52,6 +52,9 @@ func makeEndpointSliceV1() *v1.EndpointSlice { Labels: map[string]string{ v1.LabelServiceName: "testendpoints", }, + Annotations: map[string]string{ + "test.annotation": "test", + }, }, AddressType: v1.AddressTypeIPv4, Ports: []v1.EndpointPort{ @@ -114,6 +117,9 @@ func makeEndpointSliceV1beta1() *v1beta1.EndpointSlice { Labels: map[string]string{ v1beta1.LabelServiceName: "testendpoints", }, + Annotations: map[string]string{ + "test.annotation": "test", + }, }, AddressType: v1beta1.AddressTypeIPv4, Ports: []v1beta1.EndpointPort{ @@ -219,9 +225,13 @@ func TestEndpointSliceDiscoveryBeforeRun(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_endpointslice_address_type": "IPv4", - "__meta_kubernetes_namespace": "default", - "__meta_kubernetes_endpointslice_name": "testendpoints", + "__meta_kubernetes_endpointslice_address_type": "IPv4", + "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_endpointslice_name": "testendpoints", + "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true", + "__meta_kubernetes_endpointslice_annotation_test_annotation": "test", + "__meta_kubernetes_endpointslice_annotationpresent_test_annotation": "true", }, Source: "endpointslice/default/testendpoints", }, @@ -280,9 +290,13 @@ func TestEndpointSliceDiscoveryBeforeRunV1beta1(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_endpointslice_address_type": "IPv4", - "__meta_kubernetes_namespace": "default", - "__meta_kubernetes_endpointslice_name": "testendpoints", + "__meta_kubernetes_endpointslice_address_type": "IPv4", + "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_endpointslice_name": "testendpoints", + "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true", + "__meta_kubernetes_endpointslice_annotation_test_annotation": "test", + "__meta_kubernetes_endpointslice_annotationpresent_test_annotation": "true", }, Source: "endpointslice/default/testendpoints", }, @@ -478,9 +492,13 @@ func TestEndpointSliceDiscoveryDelete(t *testing.T) { }, }, Labels: map[model.LabelName]model.LabelValue{ - "__meta_kubernetes_endpointslice_address_type": "IPv4", - "__meta_kubernetes_endpointslice_name": "testendpoints", - "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_endpointslice_address_type": "IPv4", + "__meta_kubernetes_endpointslice_name": "testendpoints", + "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true", + "__meta_kubernetes_endpointslice_annotation_test_annotation": "test", + "__meta_kubernetes_endpointslice_annotationpresent_test_annotation": "true", + "__meta_kubernetes_namespace": "default", }, }, }, @@ -574,9 +592,13 @@ func TestEndpointSliceDiscoveryUpdate(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_endpointslice_address_type": "IPv4", - "__meta_kubernetes_endpointslice_name": "testendpoints", - "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_endpointslice_address_type": "IPv4", + "__meta_kubernetes_endpointslice_name": "testendpoints", + "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true", + "__meta_kubernetes_endpointslice_annotation_test_annotation": "test", + "__meta_kubernetes_endpointslice_annotationpresent_test_annotation": "true", + "__meta_kubernetes_namespace": "default", }, }, }, @@ -659,9 +681,13 @@ func TestEndpointSliceDiscoveryEmptyEndpoints(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_endpointslice_address_type": "IPv4", - "__meta_kubernetes_endpointslice_name": "testendpoints", - "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_endpointslice_address_type": "IPv4", + "__meta_kubernetes_endpointslice_name": "testendpoints", + "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true", + "__meta_kubernetes_endpointslice_annotation_test_annotation": "test", + "__meta_kubernetes_endpointslice_annotationpresent_test_annotation": "true", + "__meta_kubernetes_namespace": "default", }, Source: "endpointslice/default/testendpoints", }, @@ -739,12 +765,16 @@ func TestEndpointSliceDiscoveryWithService(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_endpointslice_address_type": "IPv4", - "__meta_kubernetes_endpointslice_name": "testendpoints", - "__meta_kubernetes_namespace": "default", - "__meta_kubernetes_service_label_app_name": "test", - "__meta_kubernetes_service_labelpresent_app_name": "true", - "__meta_kubernetes_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_address_type": "IPv4", + "__meta_kubernetes_endpointslice_name": "testendpoints", + "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true", + "__meta_kubernetes_endpointslice_annotation_test_annotation": "test", + "__meta_kubernetes_endpointslice_annotationpresent_test_annotation": "true", + "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_service_label_app_name": "test", + "__meta_kubernetes_service_labelpresent_app_name": "true", + "__meta_kubernetes_service_name": "testendpoints", }, Source: "endpointslice/default/testendpoints", }, @@ -835,14 +865,18 @@ func TestEndpointSliceDiscoveryWithServiceUpdate(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_endpointslice_address_type": "IPv4", - "__meta_kubernetes_endpointslice_name": "testendpoints", - "__meta_kubernetes_namespace": "default", - "__meta_kubernetes_service_label_app_name": "svc", - "__meta_kubernetes_service_label_component": "testing", - "__meta_kubernetes_service_labelpresent_app_name": "true", - "__meta_kubernetes_service_labelpresent_component": "true", - "__meta_kubernetes_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_address_type": "IPv4", + "__meta_kubernetes_endpointslice_name": "testendpoints", + "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true", + "__meta_kubernetes_endpointslice_annotation_test_annotation": "test", + "__meta_kubernetes_endpointslice_annotationpresent_test_annotation": "true", + "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_service_label_app_name": "svc", + "__meta_kubernetes_service_label_component": "testing", + "__meta_kubernetes_service_labelpresent_app_name": "true", + "__meta_kubernetes_service_labelpresent_component": "true", + "__meta_kubernetes_service_name": "testendpoints", }, Source: "endpointslice/default/testendpoints", }, @@ -927,12 +961,16 @@ func TestEndpointsSlicesDiscoveryWithNodeMetadata(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_endpointslice_address_type": "IPv4", - "__meta_kubernetes_endpointslice_name": "testendpoints", - "__meta_kubernetes_namespace": "default", - "__meta_kubernetes_service_label_app_name": "test", - "__meta_kubernetes_service_labelpresent_app_name": "true", - "__meta_kubernetes_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_address_type": "IPv4", + "__meta_kubernetes_endpointslice_name": "testendpoints", + "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true", + "__meta_kubernetes_endpointslice_annotation_test_annotation": "test", + "__meta_kubernetes_endpointslice_annotationpresent_test_annotation": "true", + "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_service_label_app_name": "test", + "__meta_kubernetes_service_labelpresent_app_name": "true", + "__meta_kubernetes_service_name": "testendpoints", }, Source: "endpointslice/default/testendpoints", }, @@ -1023,12 +1061,16 @@ func TestEndpointsSlicesDiscoveryWithUpdatedNodeMetadata(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_endpointslice_address_type": "IPv4", - "__meta_kubernetes_endpointslice_name": "testendpoints", - "__meta_kubernetes_namespace": "default", - "__meta_kubernetes_service_label_app_name": "test", - "__meta_kubernetes_service_labelpresent_app_name": "true", - "__meta_kubernetes_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_address_type": "IPv4", + "__meta_kubernetes_endpointslice_name": "testendpoints", + "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true", + "__meta_kubernetes_endpointslice_annotation_test_annotation": "test", + "__meta_kubernetes_endpointslice_annotationpresent_test_annotation": "true", + "__meta_kubernetes_namespace": "default", + "__meta_kubernetes_service_label_app_name": "test", + "__meta_kubernetes_service_labelpresent_app_name": "true", + "__meta_kubernetes_service_name": "testendpoints", }, Source: "endpointslice/default/testendpoints", }, @@ -1159,12 +1201,16 @@ func TestEndpointSliceDiscoveryNamespaces(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_endpointslice_address_type": "IPv4", - "__meta_kubernetes_endpointslice_name": "testendpoints", - "__meta_kubernetes_namespace": "ns1", - "__meta_kubernetes_service_label_app": "app1", - "__meta_kubernetes_service_labelpresent_app": "true", - "__meta_kubernetes_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_address_type": "IPv4", + "__meta_kubernetes_endpointslice_name": "testendpoints", + "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true", + "__meta_kubernetes_endpointslice_annotation_test_annotation": "test", + "__meta_kubernetes_endpointslice_annotationpresent_test_annotation": "true", + "__meta_kubernetes_namespace": "ns1", + "__meta_kubernetes_service_label_app": "app1", + "__meta_kubernetes_service_labelpresent_app": "true", + "__meta_kubernetes_service_name": "testendpoints", }, Source: "endpointslice/ns1/testendpoints", }, @@ -1303,9 +1349,13 @@ func TestEndpointSliceDiscoveryOwnNamespace(t *testing.T) { }, }, Labels: model.LabelSet{ - "__meta_kubernetes_endpointslice_address_type": "IPv4", - "__meta_kubernetes_endpointslice_name": "testendpoints", - "__meta_kubernetes_namespace": "own-ns", + "__meta_kubernetes_endpointslice_address_type": "IPv4", + "__meta_kubernetes_endpointslice_name": "testendpoints", + "__meta_kubernetes_namespace": "own-ns", + "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints", + "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true", + "__meta_kubernetes_endpointslice_annotation_test_annotation": "test", + "__meta_kubernetes_endpointslice_annotationpresent_test_annotation": "true", }, Source: "endpointslice/own-ns/testendpoints", }, diff --git a/discovery/kubernetes/ingress.go b/discovery/kubernetes/ingress.go index ad47c341a..697b6f519 100644 --- a/discovery/kubernetes/ingress.go +++ b/discovery/kubernetes/ingress.go @@ -28,7 +28,6 @@ import ( "k8s.io/client-go/util/workqueue" "github.com/prometheus/prometheus/discovery/targetgroup" - "github.com/prometheus/prometheus/util/strutil" ) var ( @@ -143,37 +142,22 @@ func ingressSourceFromNamespaceAndName(namespace, name string) string { } const ( - ingressNameLabel = metaLabelPrefix + "ingress_name" - ingressLabelPrefix = metaLabelPrefix + "ingress_label_" - ingressLabelPresentPrefix = metaLabelPrefix + "ingress_labelpresent_" - ingressAnnotationPrefix = metaLabelPrefix + "ingress_annotation_" - ingressAnnotationPresentPrefix = metaLabelPrefix + "ingress_annotationpresent_" - ingressSchemeLabel = metaLabelPrefix + "ingress_scheme" - ingressHostLabel = metaLabelPrefix + "ingress_host" - ingressPathLabel = metaLabelPrefix + "ingress_path" - ingressClassNameLabel = metaLabelPrefix + "ingress_class_name" + ingressSchemeLabel = metaLabelPrefix + "ingress_scheme" + ingressHostLabel = metaLabelPrefix + "ingress_host" + ingressPathLabel = metaLabelPrefix + "ingress_path" + ingressClassNameLabel = metaLabelPrefix + "ingress_class_name" ) func ingressLabels(ingress ingressAdaptor) model.LabelSet { // Each label and annotation will create two key-value pairs in the map. - ls := make(model.LabelSet, 2*(len(ingress.labels())+len(ingress.annotations()))+2) - ls[ingressNameLabel] = lv(ingress.name()) + ls := make(model.LabelSet) ls[namespaceLabel] = lv(ingress.namespace()) if cls := ingress.ingressClassName(); cls != nil { ls[ingressClassNameLabel] = lv(*cls) } - for k, v := range ingress.labels() { - ln := strutil.SanitizeLabelName(k) - ls[model.LabelName(ingressLabelPrefix+ln)] = lv(v) - ls[model.LabelName(ingressLabelPresentPrefix+ln)] = presentValue - } + addObjectMetaLabels(ls, ingress.getObjectMeta(), RoleIngress) - for k, v := range ingress.annotations() { - ln := strutil.SanitizeLabelName(k) - ls[model.LabelName(ingressAnnotationPrefix+ln)] = lv(v) - ls[model.LabelName(ingressAnnotationPresentPrefix+ln)] = presentValue - } return ls } diff --git a/discovery/kubernetes/ingress_adaptor.go b/discovery/kubernetes/ingress_adaptor.go index 113a067ca..7be8538b5 100644 --- a/discovery/kubernetes/ingress_adaptor.go +++ b/discovery/kubernetes/ingress_adaptor.go @@ -16,10 +16,12 @@ package kubernetes import ( v1 "k8s.io/api/networking/v1" "k8s.io/api/networking/v1beta1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // ingressAdaptor is an adaptor for the different Ingress versions type ingressAdaptor interface { + getObjectMeta() metav1.ObjectMeta name() string namespace() string labels() map[string]string @@ -43,11 +45,12 @@ func newIngressAdaptorFromV1(ingress *v1.Ingress) ingressAdaptor { return &ingressAdaptorV1{ingress: ingress} } -func (i *ingressAdaptorV1) name() string { return i.ingress.Name } -func (i *ingressAdaptorV1) namespace() string { return i.ingress.Namespace } -func (i *ingressAdaptorV1) labels() map[string]string { return i.ingress.Labels } -func (i *ingressAdaptorV1) annotations() map[string]string { return i.ingress.Annotations } -func (i *ingressAdaptorV1) ingressClassName() *string { return i.ingress.Spec.IngressClassName } +func (i *ingressAdaptorV1) getObjectMeta() metav1.ObjectMeta { return i.ingress.ObjectMeta } +func (i *ingressAdaptorV1) name() string { return i.ingress.Name } +func (i *ingressAdaptorV1) namespace() string { return i.ingress.Namespace } +func (i *ingressAdaptorV1) labels() map[string]string { return i.ingress.Labels } +func (i *ingressAdaptorV1) annotations() map[string]string { return i.ingress.Annotations } +func (i *ingressAdaptorV1) ingressClassName() *string { return i.ingress.Spec.IngressClassName } func (i *ingressAdaptorV1) tlsHosts() []string { var hosts []string @@ -95,12 +98,12 @@ type ingressAdaptorV1Beta1 struct { func newIngressAdaptorFromV1beta1(ingress *v1beta1.Ingress) ingressAdaptor { return &ingressAdaptorV1Beta1{ingress: ingress} } - -func (i *ingressAdaptorV1Beta1) name() string { return i.ingress.Name } -func (i *ingressAdaptorV1Beta1) namespace() string { return i.ingress.Namespace } -func (i *ingressAdaptorV1Beta1) labels() map[string]string { return i.ingress.Labels } -func (i *ingressAdaptorV1Beta1) annotations() map[string]string { return i.ingress.Annotations } -func (i *ingressAdaptorV1Beta1) ingressClassName() *string { return i.ingress.Spec.IngressClassName } +func (i *ingressAdaptorV1Beta1) getObjectMeta() metav1.ObjectMeta { return i.ingress.ObjectMeta } +func (i *ingressAdaptorV1Beta1) name() string { return i.ingress.Name } +func (i *ingressAdaptorV1Beta1) namespace() string { return i.ingress.Namespace } +func (i *ingressAdaptorV1Beta1) labels() map[string]string { return i.ingress.Labels } +func (i *ingressAdaptorV1Beta1) annotations() map[string]string { return i.ingress.Annotations } +func (i *ingressAdaptorV1Beta1) ingressClassName() *string { return i.ingress.Spec.IngressClassName } func (i *ingressAdaptorV1Beta1) tlsHosts() []string { var hosts []string diff --git a/discovery/kubernetes/kubernetes.go b/discovery/kubernetes/kubernetes.go index e87a1c9b2..ca5ee49e2 100644 --- a/discovery/kubernetes/kubernetes.go +++ b/discovery/kubernetes/kubernetes.go @@ -23,6 +23,8 @@ import ( "sync" "time" + "github.com/prometheus/prometheus/util/strutil" + disv1beta1 "k8s.io/api/discovery/v1beta1" "github.com/go-kit/log" @@ -843,3 +845,19 @@ func checkDiscoveryV1Supported(client kubernetes.Interface) (bool, error) { // https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-25 return semVer.Major() >= 1 && semVer.Minor() >= 21, nil } + +func addObjectMetaLabels(labelSet model.LabelSet, objectMeta metav1.ObjectMeta, role Role) { + labelSet[model.LabelName(metaLabelPrefix+string(role)+"_name")] = lv(objectMeta.Name) + + for k, v := range objectMeta.Labels { + ln := strutil.SanitizeLabelName(k) + labelSet[model.LabelName(metaLabelPrefix+string(role)+"_label_"+ln)] = lv(v) + labelSet[model.LabelName(metaLabelPrefix+string(role)+"_labelpresent_"+ln)] = presentValue + } + + for k, v := range objectMeta.Annotations { + ln := strutil.SanitizeLabelName(k) + labelSet[model.LabelName(metaLabelPrefix+string(role)+"_annotation_"+ln)] = lv(v) + labelSet[model.LabelName(metaLabelPrefix+string(role)+"_annotationpresent_"+ln)] = presentValue + } +} diff --git a/discovery/kubernetes/node.go b/discovery/kubernetes/node.go index d0a6d2780..6a20e7b1f 100644 --- a/discovery/kubernetes/node.go +++ b/discovery/kubernetes/node.go @@ -152,33 +152,18 @@ func nodeSourceFromName(name string) string { } const ( - nodeNameLabel = metaLabelPrefix + "node_name" - nodeProviderIDLabel = metaLabelPrefix + "node_provider_id" - nodeLabelPrefix = metaLabelPrefix + "node_label_" - nodeLabelPresentPrefix = metaLabelPrefix + "node_labelpresent_" - nodeAnnotationPrefix = metaLabelPrefix + "node_annotation_" - nodeAnnotationPresentPrefix = metaLabelPrefix + "node_annotationpresent_" - nodeAddressPrefix = metaLabelPrefix + "node_address_" + nodeProviderIDLabel = metaLabelPrefix + "node_provider_id" + nodeAddressPrefix = metaLabelPrefix + "node_address_" ) func nodeLabels(n *apiv1.Node) model.LabelSet { // Each label and annotation will create two key-value pairs in the map. - ls := make(model.LabelSet, 2*(len(n.Labels)+len(n.Annotations))+1) + ls := make(model.LabelSet) - ls[nodeNameLabel] = lv(n.Name) ls[nodeProviderIDLabel] = lv(n.Spec.ProviderID) - for k, v := range n.Labels { - ln := strutil.SanitizeLabelName(k) - ls[model.LabelName(nodeLabelPrefix+ln)] = lv(v) - ls[model.LabelName(nodeLabelPresentPrefix+ln)] = presentValue - } + addObjectMetaLabels(ls, n.ObjectMeta, RoleNode) - for k, v := range n.Annotations { - ln := strutil.SanitizeLabelName(k) - ls[model.LabelName(nodeAnnotationPrefix+ln)] = lv(v) - ls[model.LabelName(nodeAnnotationPresentPrefix+ln)] = presentValue - } return ls } diff --git a/discovery/kubernetes/pod.go b/discovery/kubernetes/pod.go index 732cf52ad..74f74c1f7 100644 --- a/discovery/kubernetes/pod.go +++ b/discovery/kubernetes/pod.go @@ -30,7 +30,6 @@ import ( "k8s.io/client-go/util/workqueue" "github.com/prometheus/prometheus/discovery/targetgroup" - "github.com/prometheus/prometheus/util/strutil" ) const nodeIndex = "node" @@ -180,7 +179,6 @@ func convertToPod(o interface{}) (*apiv1.Pod, error) { } const ( - podNameLabel = metaLabelPrefix + "pod_name" podIPLabel = metaLabelPrefix + "pod_ip" podContainerNameLabel = metaLabelPrefix + "pod_container_name" podContainerIDLabel = metaLabelPrefix + "pod_container_id" @@ -191,10 +189,6 @@ const ( podContainerIsInit = metaLabelPrefix + "pod_container_init" podReadyLabel = metaLabelPrefix + "pod_ready" podPhaseLabel = metaLabelPrefix + "pod_phase" - podLabelPrefix = metaLabelPrefix + "pod_label_" - podLabelPresentPrefix = metaLabelPrefix + "pod_labelpresent_" - podAnnotationPrefix = metaLabelPrefix + "pod_annotation_" - podAnnotationPresentPrefix = metaLabelPrefix + "pod_annotationpresent_" podNodeNameLabel = metaLabelPrefix + "pod_node_name" podHostIPLabel = metaLabelPrefix + "pod_host_ip" podUID = metaLabelPrefix + "pod_uid" @@ -215,7 +209,6 @@ func GetControllerOf(controllee metav1.Object) *metav1.OwnerReference { func podLabels(pod *apiv1.Pod) model.LabelSet { ls := model.LabelSet{ - podNameLabel: lv(pod.ObjectMeta.Name), podIPLabel: lv(pod.Status.PodIP), podReadyLabel: podReady(pod), podPhaseLabel: lv(string(pod.Status.Phase)), @@ -224,6 +217,8 @@ func podLabels(pod *apiv1.Pod) model.LabelSet { podUID: lv(string(pod.ObjectMeta.UID)), } + addObjectMetaLabels(ls, pod.ObjectMeta, RolePod) + createdBy := GetControllerOf(pod) if createdBy != nil { if createdBy.Kind != "" { @@ -234,18 +229,6 @@ func podLabels(pod *apiv1.Pod) model.LabelSet { } } - for k, v := range pod.Labels { - ln := strutil.SanitizeLabelName(k) - ls[model.LabelName(podLabelPrefix+ln)] = lv(v) - ls[model.LabelName(podLabelPresentPrefix+ln)] = presentValue - } - - for k, v := range pod.Annotations { - ln := strutil.SanitizeLabelName(k) - ls[model.LabelName(podAnnotationPrefix+ln)] = lv(v) - ls[model.LabelName(podAnnotationPresentPrefix+ln)] = presentValue - } - return ls } diff --git a/discovery/kubernetes/service.go b/discovery/kubernetes/service.go index 40e17679e..7addf0054 100644 --- a/discovery/kubernetes/service.go +++ b/discovery/kubernetes/service.go @@ -28,7 +28,6 @@ import ( "k8s.io/client-go/util/workqueue" "github.com/prometheus/prometheus/discovery/targetgroup" - "github.com/prometheus/prometheus/util/strutil" ) var ( @@ -147,38 +146,20 @@ func serviceSourceFromNamespaceAndName(namespace, name string) string { } const ( - serviceNameLabel = metaLabelPrefix + "service_name" - serviceLabelPrefix = metaLabelPrefix + "service_label_" - serviceLabelPresentPrefix = metaLabelPrefix + "service_labelpresent_" - serviceAnnotationPrefix = metaLabelPrefix + "service_annotation_" - serviceAnnotationPresentPrefix = metaLabelPrefix + "service_annotationpresent_" - servicePortNameLabel = metaLabelPrefix + "service_port_name" - servicePortNumberLabel = metaLabelPrefix + "service_port_number" - servicePortProtocolLabel = metaLabelPrefix + "service_port_protocol" - serviceClusterIPLabel = metaLabelPrefix + "service_cluster_ip" - serviceLoadBalancerIP = metaLabelPrefix + "service_loadbalancer_ip" - serviceExternalNameLabel = metaLabelPrefix + "service_external_name" - serviceType = metaLabelPrefix + "service_type" + servicePortNameLabel = metaLabelPrefix + "service_port_name" + servicePortNumberLabel = metaLabelPrefix + "service_port_number" + servicePortProtocolLabel = metaLabelPrefix + "service_port_protocol" + serviceClusterIPLabel = metaLabelPrefix + "service_cluster_ip" + serviceLoadBalancerIP = metaLabelPrefix + "service_loadbalancer_ip" + serviceExternalNameLabel = metaLabelPrefix + "service_external_name" + serviceType = metaLabelPrefix + "service_type" ) func serviceLabels(svc *apiv1.Service) model.LabelSet { - // Each label and annotation will create two key-value pairs in the map. - ls := make(model.LabelSet, 2*(len(svc.Labels)+len(svc.Annotations))+2) - - ls[serviceNameLabel] = lv(svc.Name) + ls := make(model.LabelSet) ls[namespaceLabel] = lv(svc.Namespace) + addObjectMetaLabels(ls, svc.ObjectMeta, RoleService) - for k, v := range svc.Labels { - ln := strutil.SanitizeLabelName(k) - ls[model.LabelName(serviceLabelPrefix+ln)] = lv(v) - ls[model.LabelName(serviceLabelPresentPrefix+ln)] = presentValue - } - - for k, v := range svc.Annotations { - ln := strutil.SanitizeLabelName(k) - ls[model.LabelName(serviceAnnotationPrefix+ln)] = lv(v) - ls[model.LabelName(serviceAnnotationPresentPrefix+ln)] = presentValue - } return ls } diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index 30bb07a8c..669190257 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -2010,6 +2010,8 @@ Available meta labels: * `__meta_kubernetes_endpoints_name`: The names of the endpoints object. * `__meta_kubernetes_endpoints_label_`: Each label from the endpoints object. * `__meta_kubernetes_endpoints_labelpresent_`: `true` for each label from the endpoints object. +* `__meta_kubernetes_endpoints_annotation_`: Each annotation from the endpoints object. +* `__meta_kubernetes_endpoints_annotationpresent_`: `true` for each annotation from the endpoints object. * For all targets discovered directly from the endpoints list (those not additionally inferred from underlying pods), the following labels are attached: * `__meta_kubernetes_endpoint_hostname`: Hostname of the endpoint. @@ -2032,6 +2034,10 @@ Available meta labels: * `__meta_kubernetes_namespace`: The namespace of the endpoints object. * `__meta_kubernetes_endpointslice_name`: The name of endpointslice object. +* `__meta_kubernetes_endpointslice_label_`: Each label from the endpointslice object. +* `__meta_kubernetes_endpointslice_labelpresent_`: `true` for each label from the endpointslice object. +* `__meta_kubernetes_endpointslice_annotation_`: Each annotation from the endpointslice object. +* `__meta_kubernetes_endpointslice_annotationpresent_`: `true` for each annotation from the endpointslice object. * For all targets discovered directly from the endpointslice list (those not additionally inferred from underlying pods), the following labels are attached: * `__meta_kubernetes_endpointslice_address_target_kind`: Kind of the referenced object.