kubernetes: Job may get stuck repeatedly failing with Duplicate value message for uncountedTerminatedPods.failed

What happened?

We have encountered a situation when a Job controller is stuck constantly failing on the syncJob. It fails with the validation message like this job_controller.go:600] "Unhandled Error" err="syncing job: tracking status: adding uncounted pods to status: Job.batch \"pi\" is invalid: status.uncountedTerminatedPods.failed[0]: Duplicate value: \"ca8e72a0-735a-45c9-925d-d82c91256b86\"" logger="UnhandledError".

The Job:

apiVersion: batch/v1
kind: Job
metadata:
  generation: 1
  namespace: default
spec:
  backoffLimit: 6
  completionMode: NonIndexed
  completions: 1
  parallelism: 1
  suspend: false
  ...
status:
  failed: 1
  ready: 0
  ...
  uncountedTerminatedPods:
    succeeded:
    - xyz

Pod:

apiVersion: v1
kind: Pod
metadata:
  ...
  finalizers:
  - batch.kubernetes.io/job-tracking
  ...
  uid: xyz
spec:
  affinity:
    nodeAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - preference:
          matchExpressions:
          - ...
        weight: 100
...
status:
  conditions:
  - message: 'Taint manager: deleting due to NoExecute taint'
    reason: DeletionByTaintManager
    status: "True"
    type: DisruptionTarget
  - status: "True"
    type: Initialized
  - reason: PodFailed
    status: "False"
    type: Ready
  - reason: PodFailed
    status: "False"
    type: ContainersReady
  - status: "True"
    type: PodScheduled
  containerStatuses:
  - state:
      terminated:
        exitCode: 137
        finishedAt: null
        message: The container could not be located when the pod was terminated
        reason: ContainerStatusUnknown
        startedAt: null
  message: 'Pod was rejected: Predicate NodeAffinity failed'
  phase: Failed
  qosClass: Guaranteed
  reason: NodeAffinity

What did you expect to happen?

Job controller does not get stuck. It might be that the issue originated from user actions, or kubelet bug which transitioned the Pod first to Succeeded, then to Failed. However, Job controller should be resilient to a situation like this.

How can we reproduce it (as minimally and precisely as possible)?

It remains unclear what happened exactly - if the pod was transitioned to Succeeded, and after to Failed. See below an artificial scenario which reproduces the Job controller getting stuck.

Anything else we need to know?

This scenario renders the Job controller stuck in v1.30.0. It involves a pod transitioning first to Succeeded. then to Failed. We had a similar issue in the past with pods being categorized first as Failed, then Succeeded: https://github.com/kubernetes/kubernetes/pull/111646/

  1. Create the Job as follows:
apiVersion: batch/v1
kind: Job
metadata:
  name: pi
  labels:
    environment: test
spec:
  completions: 2
  parallelism: 2
  template:
    metadata:
      labels:
        environment: test
    spec:
      terminationGracePeriodSeconds: 1
      containers:
        - name: busybox
          image: busybox
          command: ["sleep", "5m"]
      restartPolicy: Never
  backoffLimit: 1
  1. Create CEL validation kubectl create -f cel.yaml to make sure the Pod finalizer removal request fails:
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: "demo-policy.example.com"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:   [""]
      apiVersions: ["v1"]
      operations:  ["CREATE", "UPDATE"]
      resources:   ["pods"]
  validations:
    - expression: "size(object.metadata.finalizers) > 0"
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicyBinding
metadata:
  name: "demo-binding-test.example.com"
spec:
  policyName: "demo-policy.example.com"
  validationActions: [Deny]
  1. Set pod status as Succeeded with kubectl edit pods/pi-766q7 --subresource=status

You can observe that the Job status looks like this:

  status:
    active: 2
    ready: 2
    uncountedTerminatedPods:
      succeeded:
      - 56f6d408-7949-4c66-8256-9a9c57595dab
  1. Kill the pod that was mark succeeded: kubectl delete pods/pi-766q7 - it makes the Pod to transition to Failed
  2. Delete the CEL rule kubectl delete -f cel.yaml

Issue: Job controller does not remove the Job finalizer from the pi-766q7 pod. It perdiodically fails with errors like this: E0610 16:05:18.089533 1 job_controller.go:600] "Unhandled Error" err="syncing job: tracking status: adding uncounted pods to status: Job.batch \"pi\" is invalid: status.uncountedTerminatedPods.failed[0]: Duplicate value: \"56f6d408-7949-4c66-8256-9a9c57595dab\"" logger="UnhandledError"

Kubernetes version

1.27 ```console $ kubectl version # paste output here ```

Cloud provider

GKE

OS version

# On Linux:
$ cat /etc/os-release
# paste output here
$ uname -a
# paste output here

# On Windows:
C:\> wmic os get Caption, Version, BuildNumber, OSArchitecture
# paste output here

Install tools

Container runtime (CRI) and version (if applicable)

Related plugins (CNI, CSI, …) and versions (if applicable)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 27 (27 by maintainers)

Most upvoted comments

I found that it was introduced in 1.22: https://github.com/kubernetes/kubernetes/pull/102344/files#diff-67dd9e8f3ee257072765326cb4f242852554a2c0753563fa51e292c0a63a7b94L2112 It may be more visible because of some finalizers are cleaning up slower of something. But I think the bug is critical enough to justify the cherry-picks

I looked into this some today and it looks like there was a check added in the last month here

From this PR by @SergeyKanzhelev . From what I understand of the problem, this fix should close this issue as well.