Add endpoint labels for pod targets discovered form endpoint but has different ports (#4253)

Signed-off-by: Vasilchenko Anton <vasilchenko-as@yandex.ru>
This commit is contained in:
Vasilchenko Anton 2023-05-05 18:46:07 +07:00 committed by Aliaksandr Valialkin
parent 348693ff84
commit 866dbee4e3
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
3 changed files with 20 additions and 12 deletions

View file

@ -114,10 +114,6 @@ func (eps *Endpoints) getTargetLabels(gw *groupWatcher) []*promutils.Labels {
logger.Warnf(`the number of targets for "role: endpoints" %q exceeds 1000 and will be truncated in the next k8s releases; please use "role: endpointslice" instead`, eps.Metadata.key()) logger.Warnf(`the number of targets for "role: endpoints" %q exceeds 1000 and will be truncated in the next k8s releases; please use "role: endpointslice" instead`, eps.Metadata.key())
} }
// Prometheus sets endpoints_name and namespace labels for all endpoints
// Even if port is not matching service port.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4154
commonEpLabels := eps.getCommonLabels()
// Append labels for skipped ports on seen pods. // Append labels for skipped ports on seen pods.
portSeen := func(port int, ports []int) bool { portSeen := func(port int, ports []int) bool {
for _, p := range ports { for _, p := range ports {
@ -138,7 +134,11 @@ func (eps *Endpoints) getTargetLabels(gw *groupWatcher) []*promutils.Labels {
m.Add("__address__", addr) m.Add("__address__", addr)
p.appendCommonLabels(m, gw) p.appendCommonLabels(m, gw)
p.appendContainerLabels(m, c, &cp) p.appendContainerLabels(m, c, &cp)
m.AddFrom(commonEpLabels)
// Prometheus sets endpoints_name and namespace labels for all endpoints
// Even if port is not matching service port.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4154
p.appendEndpointLabels(m, eps)
if svc != nil { if svc != nil {
svc.appendCommonLabels(m) svc.appendCommonLabels(m)
} }
@ -151,13 +151,6 @@ func (eps *Endpoints) getTargetLabels(gw *groupWatcher) []*promutils.Labels {
return ms return ms
} }
func (eps *Endpoints) getCommonLabels() *promutils.Labels {
m := promutils.GetLabels()
m.Add("__meta_kubernetes_namespace", eps.Metadata.Namespace)
m.Add("__meta_kubernetes_endpoints_name", eps.Metadata.Name)
return m
}
func appendEndpointLabelsForAddresses(ms []*promutils.Labels, gw *groupWatcher, podPortsSeen map[*Pod][]int, eps *Endpoints, func appendEndpointLabelsForAddresses(ms []*promutils.Labels, gw *groupWatcher, podPortsSeen map[*Pod][]int, eps *Endpoints,
eas []EndpointAddress, epp EndpointPort, svc *Service, ready string) []*promutils.Labels { eas []EndpointAddress, epp EndpointPort, svc *Service, ready string) []*promutils.Labels {
for _, ea := range eas { for _, ea := range eas {

View file

@ -84,6 +84,11 @@ func (eps *EndpointSlice) getTargetLabels(gw *groupWatcher) []*promutils.Labels
m.Add("__address__", addr) m.Add("__address__", addr)
p.appendCommonLabels(m, gw) p.appendCommonLabels(m, gw)
p.appendContainerLabels(m, c, &cp) p.appendContainerLabels(m, c, &cp)
// Prometheus sets endpoints_name and namespace labels for all endpoints
// Even if port is not matching service port.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4154
p.appendEndpointSliceLabels(m, eps)
if svc != nil { if svc != nil {
svc.appendCommonLabels(m) svc.appendCommonLabels(m)
} }

View file

@ -178,6 +178,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)
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)
eps.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_endpointslice", m)
}
func (p *Pod) appendCommonLabels(m *promutils.Labels, gw *groupWatcher) { func (p *Pod) appendCommonLabels(m *promutils.Labels, gw *groupWatcher) {
if gw.attachNodeMetadata { if gw.attachNodeMetadata {
m.Add("__meta_kubernetes_node_name", p.Spec.NodeName) m.Add("__meta_kubernetes_node_name", p.Spec.NodeName)