lib/promscrape/discovery/kubernetes: do not drop pod meta-labels even if the corresponding node objects are missing

This reflects the logic used in Prometheus.

See https://github.com/prometheus/prometheus/pull/10080
This commit is contained in:
Aliaksandr Valialkin 2022-04-26 15:25:58 +03:00
parent 155cd5d6e1
commit aa82987d70
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
3 changed files with 13 additions and 41 deletions

View file

@ -132,11 +132,7 @@ func (eps *Endpoints) getTargetLabels(gw *groupWatcher) []map[string]string {
m := map[string]string{
"__address__": addr,
}
if !p.appendCommonLabels(m, gw) {
// The corresponding node is filtered out with label or field selectors.
// Do not generate endpoint labels in this case.
continue
}
p.appendCommonLabels(m, gw)
p.appendContainerLabels(m, c, &cp)
if svc != nil {
svc.appendCommonLabels(m)
@ -158,9 +154,7 @@ func appendEndpointLabelsForAddresses(ms []map[string]string, gw *groupWatcher,
}
}
m := getEndpointLabelsForAddressAndPort(gw, podPortsSeen, eps, ea, epp, p, svc, ready)
if m != nil {
ms = append(ms, m)
}
ms = append(ms, m)
}
return ms
}
@ -176,11 +170,7 @@ func getEndpointLabelsForAddressAndPort(gw *groupWatcher, podPortsSeen map[*Pod]
if ea.TargetRef.Kind != "Pod" || p == nil {
return m
}
if !p.appendCommonLabels(m, gw) {
// The corresponding node is filtered out with label or field selectors.
// Do not generate endpoint labels in this case.
return nil
}
p.appendCommonLabels(m, gw)
// always add pod targetRef, even if epp port doesn't match container port
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2134
if _, ok := podPortsSeen[p]; !ok {

View file

@ -52,9 +52,7 @@ func (eps *EndpointSlice) getTargetLabels(gw *groupWatcher) []map[string]string
for _, epp := range eps.Ports {
for _, addr := range ess.Addresses {
m := getEndpointSliceLabelsForAddressAndPort(gw, podPortsSeen, addr, eps, ess, epp, p, svc)
if m != nil {
ms = append(ms, m)
}
ms = append(ms, m)
}
}
@ -79,11 +77,7 @@ func (eps *EndpointSlice) getTargetLabels(gw *groupWatcher) []map[string]string
m := map[string]string{
"__address__": addr,
}
if !p.appendCommonLabels(m, gw) {
// The corresponding node is filtered out with label or field selectors.
// Do not generate endpointslice labels in this case.
continue
}
p.appendCommonLabels(m, gw)
p.appendContainerLabels(m, c, &cp)
if svc != nil {
svc.appendCommonLabels(m)
@ -116,11 +110,7 @@ func getEndpointSliceLabelsForAddressAndPort(gw *groupWatcher, podPortsSeen map[
if _, ok := podPortsSeen[p]; !ok {
podPortsSeen[p] = []int{}
}
if !p.appendCommonLabels(m, gw) {
// The corresponding node is filtered out with label or field selectors.
// Do not generate endpointslice labels in this case.
return nil
}
p.appendCommonLabels(m, gw)
for _, c := range p.Spec.Containers {
for _, cp := range c.Ports {
if cp.ContainerPort == epp.Port {

View file

@ -129,11 +129,7 @@ func appendPodLabelsInternal(ms []map[string]string, gw *groupWatcher, p *Pod, c
"__address__": addr,
"__meta_kubernetes_pod_container_init": isInit,
}
if !p.appendCommonLabels(m, gw) {
// The corresponding node is filtered out with label or field selectors.
// Do not generate pod labels in this case.
return ms
}
p.appendCommonLabels(m, gw)
p.appendContainerLabels(m, c, cp)
return append(ms, m)
}
@ -147,17 +143,14 @@ func (p *Pod) appendContainerLabels(m map[string]string, c Container, cp *Contai
}
}
func (p *Pod) appendCommonLabels(m map[string]string, gw *groupWatcher) bool {
func (p *Pod) appendCommonLabels(m map[string]string, gw *groupWatcher) {
if gw.attachNodeMetadata {
o := gw.getObjectByRoleLocked("node", p.Metadata.Namespace, p.Spec.NodeName)
if o == nil {
// The node associated with the pod is filtered out with label or field selectors,
// so do not generate labels for the pod.
return false
}
n := o.(*Node)
m["__meta_kubernetes_node_name"] = p.Spec.NodeName
n.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_node", m)
o := gw.getObjectByRoleLocked("node", p.Metadata.Namespace, p.Spec.NodeName)
if o != nil {
n := o.(*Node)
n.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_node", m)
}
}
m["__meta_kubernetes_pod_name"] = p.Metadata.Name
m["__meta_kubernetes_pod_ip"] = p.Status.PodIP
@ -176,7 +169,6 @@ func (p *Pod) appendCommonLabels(m map[string]string, gw *groupWatcher) bool {
}
}
p.Metadata.registerLabelsAndAnnotations("__meta_kubernetes_pod", m)
return true
}
func getPodController(ors []OwnerReference) *OwnerReference {