diff --git a/lib/promscrape/discovery/kubernetes/endpoints.go b/lib/promscrape/discovery/kubernetes/endpoints.go index 37bf270afc..a32e79e878 100644 --- a/lib/promscrape/discovery/kubernetes/endpoints.go +++ b/lib/promscrape/discovery/kubernetes/endpoints.go @@ -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 { diff --git a/lib/promscrape/discovery/kubernetes/endpointslice.go b/lib/promscrape/discovery/kubernetes/endpointslice.go index bf81928436..8e2244989d 100644 --- a/lib/promscrape/discovery/kubernetes/endpointslice.go +++ b/lib/promscrape/discovery/kubernetes/endpointslice.go @@ -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 { diff --git a/lib/promscrape/discovery/kubernetes/pod.go b/lib/promscrape/discovery/kubernetes/pod.go index 306116197a..6a3059c1f3 100644 --- a/lib/promscrape/discovery/kubernetes/pod.go +++ b/lib/promscrape/discovery/kubernetes/pod.go @@ -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 {