Skip to content

fix(kubernetes): fail sandbox after main container restart - #1482

Open
ruirui6946 wants to merge 10 commits into
opensandbox-group:mainfrom
ruirui6946:fix/batchsandbox-restart-status-1450
Open

fix(kubernetes): fail sandbox after main container restart#1482
ruirui6946 wants to merge 10 commits into
opensandbox-group:mainfrom
ruirui6946:fix/batchsandbox-restart-status-1450

Conversation

@ruirui6946

@ruirui6946 ruirui6946 commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Summary

  • mark a succeeded BatchSandbox as failed when its main sandbox container terminates or restarts after the sandbox became ready
  • preserve the prior Ready baseline per existing Pod during scaling by checking previous endpoint membership and Pod creation time
  • ignore pre-allocation/pre-ready restart history, newly added or replacement Pods, and supporting sidecar restarts

Testing

  • go test -v ./internal/controller -run '^(TestBuildRuntimeView|TestGetPodFailureReasonAndMessage)' -count=1
  • go test ./internal/controller -run '^Test[^C]' -count=1
  • go test ./apis/... ./cmd/... ./examples/... ./internal/... ./pkg/... -skip TestControllers -count=1
  • go vet ./...
  • make manifests generate fmt (no generated output changes)

The Docker-backed E2E packages cannot start in this WSL environment because Docker Desktop integration is not enabled; their setup failed before any E2E spec ran. The envtest-backed TestControllers suite also remains unavailable because the official Kubernetes envtest release asset download timed out. GitHub Actions jobs on this external-fork PR require maintainer approval.

Fixes #1450

Copilot AI lite review requested due to automatic review settings August 12, 2026 09:40
@github-actions github-actions Bot added component/k8s For kubernetes runtime size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Aug 12, 2026
@ruirui6946
ruirui6946 force-pushed the fix/batchsandbox-restart-status-1450 branch from c592784 to eaa4329 Compare August 12, 2026 09:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the Kubernetes BatchSandbox status controller so a sandbox that previously reached Succeed is transitioned to Failed if the main (first) container restarts after the sandbox became ready, preventing “Succeeded” from masking a reset runtime (e.g., OOM + restart) as described in #1450.

Changes:

  • Extend pod failure detection to treat a post-ready main-container restart as a failure signal (while ignoring sidecar restarts).
  • Introduce a restart-detection baseline keyed off the existing Ready transition time plus stable generation/replica count and stable endpoint membership.
  • Add focused unit tests covering post-ready restarts, pre-ready restart history, sidecar restarts, and baseline resets when pod membership changes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
kubernetes/internal/controller/batchsandbox_status.go Adds restart detection logic and a baseline mechanism, and wires it into steady-state runtime phase evaluation.
kubernetes/internal/controller/batchsandbox_pause_resume_test.go Adds new test cases validating the new restart-based failure behavior and baseline reset behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c592784c5d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread kubernetes/internal/controller/batchsandbox_status.go Outdated
@ruirui6946
ruirui6946 force-pushed the fix/batchsandbox-restart-status-1450 branch 2 times, most recently from 10a2c94 to 02f162f Compare August 12, 2026 09:48
@ruirui6946

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@ruirui6946
ruirui6946 force-pushed the fix/batchsandbox-restart-status-1450 branch from 02f162f to 4141987 Compare August 12, 2026 09:51

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 41419870f7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread kubernetes/internal/controller/batchsandbox_status.go Outdated
@github-actions github-actions Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Aug 12, 2026
Comment thread kubernetes/internal/controller/batchsandbox_status.go Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6d3aa4ab7a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread kubernetes/internal/controller/batchsandbox_status.go
Comment thread kubernetes/internal/controller/batchsandbox_status.go Outdated
@Pangjiping Pangjiping self-assigned this Aug 13, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dcc16e9b6b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread kubernetes/internal/controller/batchsandbox_status.go Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b44c4452c9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread kubernetes/internal/controller/batchsandbox_status.go Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6a087245bd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +278 to +279
if _, published := baseline.previousEndpointIPs[pod.Status.PodIP]; published {
baseline.desiredPerPod[key] = now

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve endpoint publication time when activating baselines

If patchBatchSandboxEndpoints succeeds but the controller crashes or the subsequent Pod baseline patch fails, the new endpoint is already visible through sandbox.opensandbox.io/endpoints while the Pod still has startedAt:0. On the next reconcile this branch sees the endpoint as published and advances the baseline to the current time, so any main-container restart that happened while the endpoint was already exposed is treated as pre-baseline and is permanently missed. Persist the original publication time or fail closed until activation succeeds.

AGENTS.md reference: kubernetes/AGENTS.md:L47-L51

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ace0ef4de3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

status := batchSbx.Status
baseline := restartDetectionBaseline{
batchSandboxUID: batchSbx.UID,
detectRestarts: status.Phase == sandboxv1alpha1.BatchSandboxPhaseSucceed,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Retain restart detection while the sandbox is pending

When a previously ready single-replica sandbox temporarily becomes unready without one of the recognized waiting reasons, the first reconcile changes its phase to Pending and removes the Ready condition; if the main container then restarts, or its termination update arrives afterward, this phase check disables the persisted restart baseline. Recovery creates a fresh Ready transition, permanently hiding that post-ready restart. Preserve the last successful baseline through transient Pending states and cover this sequence with a focused regression test.

AGENTS.md reference: kubernetes/AGENTS.md:L165-L165

Useful? React with 👍 / 👎.

Comment on lines +479 to +480
publishedAt := time.Now().UnixNano()
patched, err := r.activatePublishedPodRestartBaselines(ctx, batchSbx.UID, publishedAt, view)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use a common precision for publication baselines

When a newly published endpoint's main container terminates before the next whole-second boundary, this baseline retains controller-clock nanoseconds, whereas metav1.Time termination timestamps round-trip through the Kubernetes API at RFC3339 second precision. The decoded FinishedAt is therefore earlier than publishedAt, so the strict After comparison ignores a restart that actually occurred after publication and loses it permanently. Use restart-count state or timestamps normalized to a common precision, with a regression test for the same-second case.

AGENTS.md reference: kubernetes/AGENTS.md:L165-L165

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 49da87fa7b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

status := batchSbx.Status
baseline := restartDetectionBaseline{
batchSandboxUID: batchSbx.UID,
detectRestarts: status.Phase == sandboxv1alpha1.BatchSandboxPhaseSucceed,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Watch allocated pooled Pods for status changes

When the sandbox uses spec.poolRef, a post-ready restart does not itself cause this detection to run: BatchSandboxReconciler.SetupWithManager only uses Owns(&corev1.Pod{}), while pooled Pods are owned by the Pool and therefore enqueue only PoolReconciler; that reconciler does not update the BatchSandbox allocation annotation for a status-only Pod change. Consequently the sandbox can remain Succeed indefinitely after its allocated main container restarts, until some unrelated BatchSandbox event occurs. Add a Pod watch that maps allocated pooled Pods back to their BatchSandbox.

AGENTS.md reference: kubernetes/AGENTS.md:L150-L154

Useful? React with 👍 / 👎.

Comment on lines +196 to +200
if _, existedWhenReady := b.previousEndpointIPs[pod.Status.PodIP]; !existedWhenReady {
return nil
}
if !pod.CreationTimestamp.IsZero() && pod.CreationTimestamp.After(b.readySince.Time) {
return nil

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Identify previous endpoint members by Pod UID

When a replacement is a prewarmed pooled Pod whose IP was reused from the removed member, this fallback treats it as the old Pod because its IP is in previousEndpointIPs and its creation predates the Ready transition. Any restart it experienced while idle after that transition is then reported as a post-ready sandbox failure before its endpoint was allocated or published. Fresh evidence in the current code is that an unannotated Pod falls back solely to endpoint IP and creation time despite the per-Pod baseline being described as UID-based; persist the prior member UID or initialize a pending baseline for every newly allocated UID.

AGENTS.md reference: kubernetes/AGENTS.md:L150-L154

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component/k8s For kubernetes runtime size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest Features: 优化BatchSandbox状态维护机制

5 participants