kubernetes_sd role: endpointslice: pod metadata (__meta_kubernetes_pod_phase) not refreshed on pod updates
What did you do?
We scrape Services through kubernetes_sd_configs with role: endpointslice and, as usual, drop targets whose pod is not running:
relabel_configs:
- action: drop
source_labels: [__meta_kubernetes_pod_phase]
regex: (Failed|Pending|Succeeded)Some Services select the pods of a CronJob. When a Job pod completes, the pod goes to phase Succeeded but stays in the Service's EndpointSlice, with all conditions set to false:
endpoints:
- addresses:
- 10.233.133.240
conditions:
ready: false
serving: false
terminating: false
targetRef:
kind: Pod
name: my-cronjob-29838420-8jj49What did you expect to see?
The target should be dropped once the pod is Succeeded, because __meta_kubernetes_pod_phase reflects the current pod phase.
What did you see instead? Under which circumstances?
The target stays active, and is reported down (dial tcp 10.233.133.240:8081: connect: no route to host), for hours. It stays that way until the EndpointSlice changes again, usually on the next CronJob run. /api/v1/targets shows stale pod metadata:
"__meta_kubernetes_endpointslice_endpoint_conditions_ready": "false",
"__meta_kubernetes_endpointslice_endpoint_conditions_serving"
"__meta_kubernetes_pod_phase": "Running",
"__meta_kubernetes_pod_ready": "false"kubectl get pod reports status.phase: Succeeded for that same pod at the same moment. As a result, the phase-based drop never fires and alerts such as
up == 0 keep firing on pods that no longer exist.
Likely cause: in discovery/kubernetes/endpointslice.go, NewEndpointSlice registers event handlers on the EndpointSlice, Service, and (optionally) Node and Namespace informers, but none on the Pod informer. Pod metadata is only resolved (resolvePodRef → podStore) when an EndpointSlice or Service event is processed. The EndpointSlice update (endpoint turning not-ready) is typically processed while the pod store still holds the pod as Running. The later pod update to Succeeded does not re-enqueue the slice, so the target stays frozen with stale labels.
The same problem was reported and fixed for role: endpoints in #11305 / #13337. endpoints.go now re-enqueues endpoints on pod phase changes:
// the Pod's phase may change without triggering an update on
// https://github.com/prometheus/prometheus/issues/11305.
if curPod.Status.Phase != oldPod.Status.Phase {
e.enqueuePod(namespacedName(curPod.Namespace, curPod.Name
}That fix was never ported to endpointslice.go. This is still the case on main / v3.15.0. It is a problem because endpointslice is now the recommended role, since the Endpoints API is deprecated in Kubernetes.
Suggested fix: port the pod UpdateFunc handler from endpoints.go to endpointslice.go: on a pod phase change, re-enqueue the EndpointSlices referencing that pod, for example through an index on targetRef.
Current workaround: we drop not-ready endpoints of Job-owned pods, using only labels that cannot be stale. controller_kind never changes for a pod, and endpoint conditions come from the slice itself:
- action: drop
source_labels: [__meta_kubernetes_pod_controller_kind, __meta_kubernetes_endpointslice_endpoint_conditions_ready]
regex: Job;falseSteps to reproduce
apiVersion: batch/v1
kind: CronJob
metadata:
name: repro
spec:
schedule: "*/10 * * * *"
jobTemplate:
spec:
template:
metadata:
labels: {app: repro}
spec:
restartPolicy: Never
containers:
- name: repro
image: busybox
command: ["sh", "-c", "httpd -f -p 8081 & sleep 120"]
ports: [{containerPort: 8081, name: metrics}]
---
apiVersion: v1
kind: Service
metadata:
name: repro
spec:
selector: {app: repro}
ports: [{name: metrics, port: 8081}]Scrape it with role: endpointslice and the phase drop above. Once the Job pod completes, the target remains in /api/v1/targets with __meta_kubernetes_pod_phase="Running" and health: down until the next Job run.
System information
Linux, AKS (Kubernetes 1.3x)
Prometheus version
Prometheus v3.10.0 (agent mode).Prometheus configuration file
scrape_configs:
- job_name: service-monitor
kubernetes_sd_configs:
- role: endpointslice
relabel_configs:
- source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
regex: "true"
action: keep
- action: drop
source_labels: [__meta_kubernetes_pod_phase]
regex: (Failed|Pending|Succeeded)Alertmanager version
Alertmanager configuration file
Logs
Source: prometheus/prometheus