add label and labelpresent for endpointslice role in k8s discovery

Signed-off-by: renzheng.wang <wangrzneu@gmail.com>
This commit is contained in:
renzheng.wang 2023-05-30 20:13:00 +08:00
parent 92d6980360
commit 866fa25b20
10 changed files with 134 additions and 158 deletions

View file

@ -29,7 +29,6 @@ import (
"k8s.io/client-go/util/workqueue" "k8s.io/client-go/util/workqueue"
"github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/util/strutil"
) )
var ( var (
@ -248,9 +247,6 @@ func endpointsSourceFromNamespaceAndName(namespace, name string) string {
} }
const ( const (
endpointsLabelPrefix = metaLabelPrefix + "endpoints_label_"
endpointsLabelPresentPrefix = metaLabelPrefix + "endpoints_labelpresent_"
endpointsNameLabel = metaLabelPrefix + "endpoints_name"
endpointNodeName = metaLabelPrefix + "endpoint_node_name" endpointNodeName = metaLabelPrefix + "endpoint_node_name"
endpointHostname = metaLabelPrefix + "endpoint_hostname" endpointHostname = metaLabelPrefix + "endpoint_hostname"
endpointReadyLabel = metaLabelPrefix + "endpoint_ready" endpointReadyLabel = metaLabelPrefix + "endpoint_ready"
@ -265,16 +261,11 @@ func (e *Endpoints) buildEndpoints(eps *apiv1.Endpoints) *targetgroup.Group {
Source: endpointsSource(eps), Source: endpointsSource(eps),
} }
tg.Labels = model.LabelSet{ tg.Labels = model.LabelSet{
namespaceLabel: lv(eps.Namespace), namespaceLabel: lv(eps.Namespace),
endpointsNameLabel: lv(eps.Name),
} }
e.addServiceLabels(eps.Namespace, eps.Name, tg) e.addServiceLabels(eps.Namespace, eps.Name, tg)
// Add endpoints labels metadata. // Add endpoints labels metadata.
for k, v := range eps.Labels { addObjectMetaLabels(tg.Labels, eps.ObjectMeta, RoleEndpoint)
ln := strutil.SanitizeLabelName(k)
tg.Labels[model.LabelName(endpointsLabelPrefix+ln)] = lv(v)
tg.Labels[model.LabelName(endpointsLabelPresentPrefix+ln)] = presentValue
}
type podEntry struct { type podEntry struct {
pod *apiv1.Pod pod *apiv1.Pod
@ -462,14 +453,7 @@ func addNodeLabels(tg model.LabelSet, nodeInf cache.SharedInformer, logger log.L
node := obj.(*apiv1.Node) node := obj.(*apiv1.Node)
// Allocate one target label for the node name, // Allocate one target label for the node name,
// and two target labels for each node label. nodeLabelset := make(model.LabelSet)
nodeLabelset := make(model.LabelSet, 1+2*len(node.GetLabels())) addObjectMetaLabels(nodeLabelset, node.ObjectMeta, RoleNode)
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
}
return tg.Merge(nodeLabelset) return tg.Merge(nodeLabelset)
} }

View file

@ -274,9 +274,11 @@ func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgrou
} }
tg.Labels = model.LabelSet{ tg.Labels = model.LabelSet{
namespaceLabel: lv(eps.namespace()), namespaceLabel: lv(eps.namespace()),
endpointSliceNameLabel: lv(eps.name()),
endpointSliceAddressTypeLabel: lv(eps.addressType()), endpointSliceAddressTypeLabel: lv(eps.addressType()),
} }
addObjectMetaLabels(tg.Labels, eps.getObjectMeta(), RoleEndpointSlice)
e.addServiceLabels(eps, tg) e.addServiceLabels(eps, tg)
type podEntry struct { type podEntry struct {

View file

@ -17,11 +17,13 @@ import (
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/discovery/v1" v1 "k8s.io/api/discovery/v1"
"k8s.io/api/discovery/v1beta1" "k8s.io/api/discovery/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
) )
// endpointSliceAdaptor is an adaptor for the different EndpointSlice versions // endpointSliceAdaptor is an adaptor for the different EndpointSlice versions
type endpointSliceAdaptor interface { type endpointSliceAdaptor interface {
get() interface{} get() interface{}
getObjectMeta() metav1.ObjectMeta
name() string name() string
namespace() string namespace() string
addressType() string addressType() string
@ -66,6 +68,10 @@ func (e *endpointSliceAdaptorV1) get() interface{} {
return e.endpointSlice return e.endpointSlice
} }
func (e *endpointSliceAdaptorV1) getObjectMeta() metav1.ObjectMeta {
return e.endpointSlice.ObjectMeta
}
func (e *endpointSliceAdaptorV1) name() string { func (e *endpointSliceAdaptorV1) name() string {
return e.endpointSlice.ObjectMeta.Name return e.endpointSlice.ObjectMeta.Name
} }
@ -115,6 +121,10 @@ func (e *endpointSliceAdaptorV1Beta1) get() interface{} {
return e.endpointSlice return e.endpointSlice
} }
func (e *endpointSliceAdaptorV1Beta1) getObjectMeta() metav1.ObjectMeta {
return e.endpointSlice.ObjectMeta
}
func (e *endpointSliceAdaptorV1Beta1) name() string { func (e *endpointSliceAdaptorV1Beta1) name() string {
return e.endpointSlice.Name return e.endpointSlice.Name
} }

View file

@ -219,9 +219,11 @@ func TestEndpointSliceDiscoveryBeforeRun(t *testing.T) {
}, },
}, },
Labels: model.LabelSet{ Labels: model.LabelSet{
"__meta_kubernetes_endpointslice_address_type": "IPv4", "__meta_kubernetes_endpointslice_address_type": "IPv4",
"__meta_kubernetes_namespace": "default", "__meta_kubernetes_namespace": "default",
"__meta_kubernetes_endpointslice_name": "testendpoints", "__meta_kubernetes_endpointslice_name": "testendpoints",
"__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints",
"__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true",
}, },
Source: "endpointslice/default/testendpoints", Source: "endpointslice/default/testendpoints",
}, },
@ -280,9 +282,11 @@ func TestEndpointSliceDiscoveryBeforeRunV1beta1(t *testing.T) {
}, },
}, },
Labels: model.LabelSet{ Labels: model.LabelSet{
"__meta_kubernetes_endpointslice_address_type": "IPv4", "__meta_kubernetes_endpointslice_address_type": "IPv4",
"__meta_kubernetes_namespace": "default", "__meta_kubernetes_namespace": "default",
"__meta_kubernetes_endpointslice_name": "testendpoints", "__meta_kubernetes_endpointslice_name": "testendpoints",
"__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints",
"__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true",
}, },
Source: "endpointslice/default/testendpoints", Source: "endpointslice/default/testendpoints",
}, },
@ -478,9 +482,11 @@ func TestEndpointSliceDiscoveryDelete(t *testing.T) {
}, },
}, },
Labels: map[model.LabelName]model.LabelValue{ Labels: map[model.LabelName]model.LabelValue{
"__meta_kubernetes_endpointslice_address_type": "IPv4", "__meta_kubernetes_endpointslice_address_type": "IPv4",
"__meta_kubernetes_endpointslice_name": "testendpoints", "__meta_kubernetes_endpointslice_name": "testendpoints",
"__meta_kubernetes_namespace": "default", "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints",
"__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true",
"__meta_kubernetes_namespace": "default",
}, },
}, },
}, },
@ -574,9 +580,11 @@ func TestEndpointSliceDiscoveryUpdate(t *testing.T) {
}, },
}, },
Labels: model.LabelSet{ Labels: model.LabelSet{
"__meta_kubernetes_endpointslice_address_type": "IPv4", "__meta_kubernetes_endpointslice_address_type": "IPv4",
"__meta_kubernetes_endpointslice_name": "testendpoints", "__meta_kubernetes_endpointslice_name": "testendpoints",
"__meta_kubernetes_namespace": "default", "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints",
"__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true",
"__meta_kubernetes_namespace": "default",
}, },
}, },
}, },
@ -659,9 +667,11 @@ func TestEndpointSliceDiscoveryEmptyEndpoints(t *testing.T) {
}, },
}, },
Labels: model.LabelSet{ Labels: model.LabelSet{
"__meta_kubernetes_endpointslice_address_type": "IPv4", "__meta_kubernetes_endpointslice_address_type": "IPv4",
"__meta_kubernetes_endpointslice_name": "testendpoints", "__meta_kubernetes_endpointslice_name": "testendpoints",
"__meta_kubernetes_namespace": "default", "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints",
"__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true",
"__meta_kubernetes_namespace": "default",
}, },
Source: "endpointslice/default/testendpoints", Source: "endpointslice/default/testendpoints",
}, },
@ -739,12 +749,14 @@ func TestEndpointSliceDiscoveryWithService(t *testing.T) {
}, },
}, },
Labels: model.LabelSet{ Labels: model.LabelSet{
"__meta_kubernetes_endpointslice_address_type": "IPv4", "__meta_kubernetes_endpointslice_address_type": "IPv4",
"__meta_kubernetes_endpointslice_name": "testendpoints", "__meta_kubernetes_endpointslice_name": "testendpoints",
"__meta_kubernetes_namespace": "default", "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints",
"__meta_kubernetes_service_label_app_name": "test", "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true",
"__meta_kubernetes_service_labelpresent_app_name": "true", "__meta_kubernetes_namespace": "default",
"__meta_kubernetes_service_name": "testendpoints", "__meta_kubernetes_service_label_app_name": "test",
"__meta_kubernetes_service_labelpresent_app_name": "true",
"__meta_kubernetes_service_name": "testendpoints",
}, },
Source: "endpointslice/default/testendpoints", Source: "endpointslice/default/testendpoints",
}, },
@ -835,14 +847,16 @@ func TestEndpointSliceDiscoveryWithServiceUpdate(t *testing.T) {
}, },
}, },
Labels: model.LabelSet{ Labels: model.LabelSet{
"__meta_kubernetes_endpointslice_address_type": "IPv4", "__meta_kubernetes_endpointslice_address_type": "IPv4",
"__meta_kubernetes_endpointslice_name": "testendpoints", "__meta_kubernetes_endpointslice_name": "testendpoints",
"__meta_kubernetes_namespace": "default", "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints",
"__meta_kubernetes_service_label_app_name": "svc", "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true",
"__meta_kubernetes_service_label_component": "testing", "__meta_kubernetes_namespace": "default",
"__meta_kubernetes_service_labelpresent_app_name": "true", "__meta_kubernetes_service_label_app_name": "svc",
"__meta_kubernetes_service_labelpresent_component": "true", "__meta_kubernetes_service_label_component": "testing",
"__meta_kubernetes_service_name": "testendpoints", "__meta_kubernetes_service_labelpresent_app_name": "true",
"__meta_kubernetes_service_labelpresent_component": "true",
"__meta_kubernetes_service_name": "testendpoints",
}, },
Source: "endpointslice/default/testendpoints", Source: "endpointslice/default/testendpoints",
}, },
@ -927,12 +941,14 @@ func TestEndpointsSlicesDiscoveryWithNodeMetadata(t *testing.T) {
}, },
}, },
Labels: model.LabelSet{ Labels: model.LabelSet{
"__meta_kubernetes_endpointslice_address_type": "IPv4", "__meta_kubernetes_endpointslice_address_type": "IPv4",
"__meta_kubernetes_endpointslice_name": "testendpoints", "__meta_kubernetes_endpointslice_name": "testendpoints",
"__meta_kubernetes_namespace": "default", "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints",
"__meta_kubernetes_service_label_app_name": "test", "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true",
"__meta_kubernetes_service_labelpresent_app_name": "true", "__meta_kubernetes_namespace": "default",
"__meta_kubernetes_service_name": "testendpoints", "__meta_kubernetes_service_label_app_name": "test",
"__meta_kubernetes_service_labelpresent_app_name": "true",
"__meta_kubernetes_service_name": "testendpoints",
}, },
Source: "endpointslice/default/testendpoints", Source: "endpointslice/default/testendpoints",
}, },
@ -1023,12 +1039,14 @@ func TestEndpointsSlicesDiscoveryWithUpdatedNodeMetadata(t *testing.T) {
}, },
}, },
Labels: model.LabelSet{ Labels: model.LabelSet{
"__meta_kubernetes_endpointslice_address_type": "IPv4", "__meta_kubernetes_endpointslice_address_type": "IPv4",
"__meta_kubernetes_endpointslice_name": "testendpoints", "__meta_kubernetes_endpointslice_name": "testendpoints",
"__meta_kubernetes_namespace": "default", "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints",
"__meta_kubernetes_service_label_app_name": "test", "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true",
"__meta_kubernetes_service_labelpresent_app_name": "true", "__meta_kubernetes_namespace": "default",
"__meta_kubernetes_service_name": "testendpoints", "__meta_kubernetes_service_label_app_name": "test",
"__meta_kubernetes_service_labelpresent_app_name": "true",
"__meta_kubernetes_service_name": "testendpoints",
}, },
Source: "endpointslice/default/testendpoints", Source: "endpointslice/default/testendpoints",
}, },
@ -1159,12 +1177,14 @@ func TestEndpointSliceDiscoveryNamespaces(t *testing.T) {
}, },
}, },
Labels: model.LabelSet{ Labels: model.LabelSet{
"__meta_kubernetes_endpointslice_address_type": "IPv4", "__meta_kubernetes_endpointslice_address_type": "IPv4",
"__meta_kubernetes_endpointslice_name": "testendpoints", "__meta_kubernetes_endpointslice_name": "testendpoints",
"__meta_kubernetes_namespace": "ns1", "__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints",
"__meta_kubernetes_service_label_app": "app1", "__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true",
"__meta_kubernetes_service_labelpresent_app": "true", "__meta_kubernetes_namespace": "ns1",
"__meta_kubernetes_service_name": "testendpoints", "__meta_kubernetes_service_label_app": "app1",
"__meta_kubernetes_service_labelpresent_app": "true",
"__meta_kubernetes_service_name": "testendpoints",
}, },
Source: "endpointslice/ns1/testendpoints", Source: "endpointslice/ns1/testendpoints",
}, },
@ -1303,9 +1323,11 @@ func TestEndpointSliceDiscoveryOwnNamespace(t *testing.T) {
}, },
}, },
Labels: model.LabelSet{ Labels: model.LabelSet{
"__meta_kubernetes_endpointslice_address_type": "IPv4", "__meta_kubernetes_endpointslice_address_type": "IPv4",
"__meta_kubernetes_endpointslice_name": "testendpoints", "__meta_kubernetes_endpointslice_name": "testendpoints",
"__meta_kubernetes_namespace": "own-ns", "__meta_kubernetes_namespace": "own-ns",
"__meta_kubernetes_endpointslice_label_kubernetes_io_service_name": "testendpoints",
"__meta_kubernetes_endpointslice_labelpresent_kubernetes_io_service_name": "true",
}, },
Source: "endpointslice/own-ns/testendpoints", Source: "endpointslice/own-ns/testendpoints",
}, },

View file

@ -28,7 +28,6 @@ import (
"k8s.io/client-go/util/workqueue" "k8s.io/client-go/util/workqueue"
"github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/util/strutil"
) )
var ( var (
@ -143,37 +142,22 @@ func ingressSourceFromNamespaceAndName(namespace, name string) string {
} }
const ( const (
ingressNameLabel = metaLabelPrefix + "ingress_name" ingressSchemeLabel = metaLabelPrefix + "ingress_scheme"
ingressLabelPrefix = metaLabelPrefix + "ingress_label_" ingressHostLabel = metaLabelPrefix + "ingress_host"
ingressLabelPresentPrefix = metaLabelPrefix + "ingress_labelpresent_" ingressPathLabel = metaLabelPrefix + "ingress_path"
ingressAnnotationPrefix = metaLabelPrefix + "ingress_annotation_" ingressClassNameLabel = metaLabelPrefix + "ingress_class_name"
ingressAnnotationPresentPrefix = metaLabelPrefix + "ingress_annotationpresent_"
ingressSchemeLabel = metaLabelPrefix + "ingress_scheme"
ingressHostLabel = metaLabelPrefix + "ingress_host"
ingressPathLabel = metaLabelPrefix + "ingress_path"
ingressClassNameLabel = metaLabelPrefix + "ingress_class_name"
) )
func ingressLabels(ingress ingressAdaptor) model.LabelSet { func ingressLabels(ingress ingressAdaptor) model.LabelSet {
// Each label and annotation will create two key-value pairs in the map. // 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 := make(model.LabelSet)
ls[ingressNameLabel] = lv(ingress.name())
ls[namespaceLabel] = lv(ingress.namespace()) ls[namespaceLabel] = lv(ingress.namespace())
if cls := ingress.ingressClassName(); cls != nil { if cls := ingress.ingressClassName(); cls != nil {
ls[ingressClassNameLabel] = lv(*cls) ls[ingressClassNameLabel] = lv(*cls)
} }
for k, v := range ingress.labels() { addObjectMetaLabels(ls, ingress.getObjectMeta(), RoleIngress)
ln := strutil.SanitizeLabelName(k)
ls[model.LabelName(ingressLabelPrefix+ln)] = lv(v)
ls[model.LabelName(ingressLabelPresentPrefix+ln)] = presentValue
}
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 return ls
} }

View file

@ -16,10 +16,12 @@ package kubernetes
import ( import (
v1 "k8s.io/api/networking/v1" v1 "k8s.io/api/networking/v1"
"k8s.io/api/networking/v1beta1" "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
) )
// ingressAdaptor is an adaptor for the different Ingress versions // ingressAdaptor is an adaptor for the different Ingress versions
type ingressAdaptor interface { type ingressAdaptor interface {
getObjectMeta() metav1.ObjectMeta
name() string name() string
namespace() string namespace() string
labels() map[string]string labels() map[string]string
@ -43,11 +45,12 @@ func newIngressAdaptorFromV1(ingress *v1.Ingress) ingressAdaptor {
return &ingressAdaptorV1{ingress: ingress} return &ingressAdaptorV1{ingress: ingress}
} }
func (i *ingressAdaptorV1) name() string { return i.ingress.Name } func (i *ingressAdaptorV1) getObjectMeta() metav1.ObjectMeta { return i.ingress.ObjectMeta }
func (i *ingressAdaptorV1) namespace() string { return i.ingress.Namespace } func (i *ingressAdaptorV1) name() string { return i.ingress.Name }
func (i *ingressAdaptorV1) labels() map[string]string { return i.ingress.Labels } func (i *ingressAdaptorV1) namespace() string { return i.ingress.Namespace }
func (i *ingressAdaptorV1) annotations() map[string]string { return i.ingress.Annotations } func (i *ingressAdaptorV1) labels() map[string]string { return i.ingress.Labels }
func (i *ingressAdaptorV1) ingressClassName() *string { return i.ingress.Spec.IngressClassName } 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 { func (i *ingressAdaptorV1) tlsHosts() []string {
var hosts []string var hosts []string
@ -95,12 +98,12 @@ type ingressAdaptorV1Beta1 struct {
func newIngressAdaptorFromV1beta1(ingress *v1beta1.Ingress) ingressAdaptor { func newIngressAdaptorFromV1beta1(ingress *v1beta1.Ingress) ingressAdaptor {
return &ingressAdaptorV1Beta1{ingress: ingress} return &ingressAdaptorV1Beta1{ingress: ingress}
} }
func (i *ingressAdaptorV1Beta1) getObjectMeta() metav1.ObjectMeta { return i.ingress.ObjectMeta }
func (i *ingressAdaptorV1Beta1) name() string { return i.ingress.Name } func (i *ingressAdaptorV1Beta1) name() string { return i.ingress.Name }
func (i *ingressAdaptorV1Beta1) namespace() string { return i.ingress.Namespace } func (i *ingressAdaptorV1Beta1) namespace() string { return i.ingress.Namespace }
func (i *ingressAdaptorV1Beta1) labels() map[string]string { return i.ingress.Labels } 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) annotations() map[string]string { return i.ingress.Annotations }
func (i *ingressAdaptorV1Beta1) ingressClassName() *string { return i.ingress.Spec.IngressClassName } func (i *ingressAdaptorV1Beta1) ingressClassName() *string { return i.ingress.Spec.IngressClassName }
func (i *ingressAdaptorV1Beta1) tlsHosts() []string { func (i *ingressAdaptorV1Beta1) tlsHosts() []string {
var hosts []string var hosts []string

View file

@ -17,6 +17,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"github.com/prometheus/prometheus/util/strutil"
"os" "os"
"reflect" "reflect"
"strings" "strings"
@ -843,3 +844,19 @@ func checkDiscoveryV1Supported(client kubernetes.Interface) (bool, error) {
// https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-25 // https://kubernetes.io/docs/reference/using-api/deprecation-guide/#v1-25
return semVer.Major() >= 1 && semVer.Minor() >= 21, nil 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
}
}

View file

@ -152,33 +152,18 @@ func nodeSourceFromName(name string) string {
} }
const ( const (
nodeNameLabel = metaLabelPrefix + "node_name" nodeProviderIDLabel = metaLabelPrefix + "node_provider_id"
nodeProviderIDLabel = metaLabelPrefix + "node_provider_id" nodeAddressPrefix = metaLabelPrefix + "node_address_"
nodeLabelPrefix = metaLabelPrefix + "node_label_"
nodeLabelPresentPrefix = metaLabelPrefix + "node_labelpresent_"
nodeAnnotationPrefix = metaLabelPrefix + "node_annotation_"
nodeAnnotationPresentPrefix = metaLabelPrefix + "node_annotationpresent_"
nodeAddressPrefix = metaLabelPrefix + "node_address_"
) )
func nodeLabels(n *apiv1.Node) model.LabelSet { func nodeLabels(n *apiv1.Node) model.LabelSet {
// Each label and annotation will create two key-value pairs in the map. // 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) ls[nodeProviderIDLabel] = lv(n.Spec.ProviderID)
for k, v := range n.Labels { addObjectMetaLabels(ls, n.ObjectMeta, RoleNode)
ln := strutil.SanitizeLabelName(k)
ls[model.LabelName(nodeLabelPrefix+ln)] = lv(v)
ls[model.LabelName(nodeLabelPresentPrefix+ln)] = presentValue
}
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 return ls
} }

View file

@ -30,7 +30,6 @@ import (
"k8s.io/client-go/util/workqueue" "k8s.io/client-go/util/workqueue"
"github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/util/strutil"
) )
const nodeIndex = "node" const nodeIndex = "node"
@ -180,7 +179,6 @@ func convertToPod(o interface{}) (*apiv1.Pod, error) {
} }
const ( const (
podNameLabel = metaLabelPrefix + "pod_name"
podIPLabel = metaLabelPrefix + "pod_ip" podIPLabel = metaLabelPrefix + "pod_ip"
podContainerNameLabel = metaLabelPrefix + "pod_container_name" podContainerNameLabel = metaLabelPrefix + "pod_container_name"
podContainerIDLabel = metaLabelPrefix + "pod_container_id" podContainerIDLabel = metaLabelPrefix + "pod_container_id"
@ -191,10 +189,6 @@ const (
podContainerIsInit = metaLabelPrefix + "pod_container_init" podContainerIsInit = metaLabelPrefix + "pod_container_init"
podReadyLabel = metaLabelPrefix + "pod_ready" podReadyLabel = metaLabelPrefix + "pod_ready"
podPhaseLabel = metaLabelPrefix + "pod_phase" 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" podNodeNameLabel = metaLabelPrefix + "pod_node_name"
podHostIPLabel = metaLabelPrefix + "pod_host_ip" podHostIPLabel = metaLabelPrefix + "pod_host_ip"
podUID = metaLabelPrefix + "pod_uid" podUID = metaLabelPrefix + "pod_uid"
@ -215,7 +209,6 @@ func GetControllerOf(controllee metav1.Object) *metav1.OwnerReference {
func podLabels(pod *apiv1.Pod) model.LabelSet { func podLabels(pod *apiv1.Pod) model.LabelSet {
ls := model.LabelSet{ ls := model.LabelSet{
podNameLabel: lv(pod.ObjectMeta.Name),
podIPLabel: lv(pod.Status.PodIP), podIPLabel: lv(pod.Status.PodIP),
podReadyLabel: podReady(pod), podReadyLabel: podReady(pod),
podPhaseLabel: lv(string(pod.Status.Phase)), podPhaseLabel: lv(string(pod.Status.Phase)),
@ -224,6 +217,8 @@ func podLabels(pod *apiv1.Pod) model.LabelSet {
podUID: lv(string(pod.ObjectMeta.UID)), podUID: lv(string(pod.ObjectMeta.UID)),
} }
addObjectMetaLabels(ls, pod.ObjectMeta, RolePod)
createdBy := GetControllerOf(pod) createdBy := GetControllerOf(pod)
if createdBy != nil { if createdBy != nil {
if createdBy.Kind != "" { 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 return ls
} }

View file

@ -28,7 +28,6 @@ import (
"k8s.io/client-go/util/workqueue" "k8s.io/client-go/util/workqueue"
"github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/util/strutil"
) )
var ( var (
@ -162,23 +161,10 @@ const (
) )
func serviceLabels(svc *apiv1.Service) model.LabelSet { func serviceLabels(svc *apiv1.Service) model.LabelSet {
// Each label and annotation will create two key-value pairs in the map. ls := make(model.LabelSet)
ls := make(model.LabelSet, 2*(len(svc.Labels)+len(svc.Annotations))+2)
ls[serviceNameLabel] = lv(svc.Name)
ls[namespaceLabel] = lv(svc.Namespace) 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 return ls
} }