-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(kubernetes): recover BatchSandbox after Pod restart #1523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,6 +125,7 @@ type podFailureSummary struct { | |
| failed int | ||
| primaryReason string | ||
| samplePod string | ||
| podUIDs []types.UID | ||
| } | ||
|
|
||
| func summarizePodFailures(pods []*corev1.Pod) (podFailureSummary, bool) { | ||
|
|
@@ -140,6 +141,9 @@ func summarizePodFailures(pods []*corev1.Pod) (podFailureSummary, bool) { | |
| } | ||
|
|
||
| summary.failed++ | ||
| if pod.UID != "" { | ||
| summary.podUIDs = append(summary.podUIDs, pod.UID) | ||
| } | ||
| if _, exists := firstPodByReason[reason]; !exists { | ||
| firstPodByReason[reason] = pod.Name | ||
| } | ||
|
|
@@ -203,28 +207,64 @@ func applyResumingRuntimePhase(status *sandboxv1alpha1.BatchSandboxStatus, pods | |
| if summary, hasFailures := summarizePodFailures(pods); hasFailures { | ||
| setConditionInStatus(status, sandboxv1alpha1.BatchSandboxConditionResumeFailed, sandboxv1alpha1.ConditionTrue, summary.primaryReason, summary.message(true)) | ||
| setConditionInStatus(status, sandboxv1alpha1.BatchSandboxConditionPodFailed, sandboxv1alpha1.ConditionTrue, summary.primaryReason, summary.message(false)) | ||
| status.FailedPodUIDs = nil | ||
| status.Phase = sandboxv1alpha1.BatchSandboxPhaseFailed | ||
| return | ||
| } | ||
| if status.Ready > 0 { | ||
| status.Phase = sandboxv1alpha1.BatchSandboxPhaseSucceed | ||
| status.FailedPodUIDs = nil | ||
| setConditionInStatus(status, sandboxv1alpha1.BatchSandboxConditionPodFailed, sandboxv1alpha1.ConditionFalse, "", "") | ||
| } | ||
| } | ||
|
|
||
| func failedPodsRecovered(failedPodUIDs []types.UID, pods []*corev1.Pod) bool { | ||
| // Without provenance, recovery is unsafe because a replacement Pod may reuse the same name. | ||
| if len(failedPodUIDs) == 0 { | ||
| return false | ||
| } | ||
|
|
||
| recoveredUIDs := make(map[types.UID]struct{}, len(failedPodUIDs)) | ||
| for _, pod := range pods { | ||
| if pod.DeletionTimestamp != nil || !utils.IsPodReady(pod) || len(pod.Spec.Containers) == 0 { | ||
| continue | ||
| } | ||
|
|
||
| // The first container is the sandbox's main runtime container by convention. | ||
| mainContainerName := pod.Spec.Containers[0].Name | ||
| for _, containerStatus := range pod.Status.ContainerStatuses { | ||
| if containerStatus.Name == mainContainerName && containerStatus.State.Running != nil { | ||
| recoveredUIDs[pod.UID] = struct{}{} | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for _, uid := range failedPodUIDs { | ||
| if _, recovered := recoveredUIDs[uid]; !recovered { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| func applySteadyRuntimePhase(batchSbx *sandboxv1alpha1.BatchSandbox, status *sandboxv1alpha1.BatchSandboxStatus, pods []*corev1.Pod) { | ||
| if summary, hasFailures := summarizePodFailures(pods); hasFailures { | ||
| if batchSbx.Status.Phase != sandboxv1alpha1.BatchSandboxPhaseFailed { | ||
| setConditionInStatus(status, sandboxv1alpha1.BatchSandboxConditionPodFailed, sandboxv1alpha1.ConditionTrue, summary.primaryReason, summary.message(false)) | ||
| status.FailedPodUIDs = append([]types.UID(nil), summary.podUIDs...) | ||
| status.Phase = sandboxv1alpha1.BatchSandboxPhaseFailed | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if status.Phase == sandboxv1alpha1.BatchSandboxPhaseFailed { | ||
| return | ||
| if !failedPodsRecovered(status.FailedPodUIDs, pods) { | ||
| return | ||
| } | ||
| } | ||
|
|
||
| status.FailedPodUIDs = nil | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a failed pod recovers, setting AGENTS.md reference: kubernetes/AGENTS.md:L166-L166 Useful? React with 👍 / 👎. |
||
| setConditionInStatus(status, sandboxv1alpha1.BatchSandboxConditionPodFailed, sandboxv1alpha1.ConditionFalse, "", "") | ||
| if status.Ready > 0 { | ||
| status.Phase = sandboxv1alpha1.BatchSandboxPhaseSucceed | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This status field is added to the Go type and the Kustomize CRD, but the bundled Helm CRD copy at
kubernetes/charts/opensandbox-controller/templates/crds/batchsandboxes.yamlstill lacksstatus.failedPodUIDs. In Helm-installed clusters, that unknown status field is pruned by the CRD schema, so the controller cannot persist pod UID provenance and the transient Pod recovery path remains ineffective for Helm users.AGENTS.md reference: kubernetes/AGENTS.md:L164-L164
Useful? React with 👍 / 👎.