Skip to content

feat: implement replica_rightsizing, rightsize_pvc, volume_delete agent_task remediations - #479

Merged
mayankpande88 merged 6 commits into
mainfrom
feat/agent-task-remediations
Jun 16, 2026
Merged

feat: implement replica_rightsizing, rightsize_pvc, volume_delete agent_task remediations#479
mayankpande88 merged 6 commits into
mainfrom
feat/agent-task-remediations

Conversation

@mayankpande88

Copy link
Copy Markdown
Contributor

What & why

The api-server enqueues several remediation/apply actions onto the agent_task queue that the Go agent's poller dispatches by action_name. Three were unimplemented — the poller replied "action not registered", so applying these recommendations failed:

  • replica_rightsizing — scale-to-zero of abandoned workloads + event-resolution increase_replicas
  • rightsize_pvc — PVC resize recommendations (expand and downsize)
  • volume_delete — delete unused PersistentVolumes

This PR implements all three (ports the legacy robusta behavior), plus the execution-model changes the long-running downsize migration needs.

Changes

Handlers (pkg/mutate)

  • replica_rightsizing: scale Deployment/StatefulSet/Rollout via the dynamic client; replica_count coerced from JSON number or numeric string (both wire forms).
  • rightsize_pvc: routes by provisioned capacity. Expansion patches the storage request after checking allowVolumeExpansion. Downsize runs a copy-migration — Deployment (new PVC → copy → repoint → delete original) and StatefulSet (name-preserving double copy with PV reclaim→Retain before the destructive delete). Data is moved by a transient ubuntu mover pod (cp -a) exec'd over SPDY, with config-parity verification and stage-aware rollback that keeps two data copies past the point of no return.
  • volume_delete: deletes the PV (payload name is the cluster-scoped PV) and its bound PVC via claimRef; 404-tolerant.
  • All three are delivered via the trusted agent_task poller and stay out of the light-action allowlist (same posture as rightsizing_resource).

Execution model (pkg/dispatch, pkg/tasks)

  • Per-action LongTaskTimeout so the migration isn't killed by the 180s dispatch cap. Default 50m (env LONG_TASK_TIMEOUT_SECONDS), kept under the server's 60m PROCESSING→TIMEOUT reap.
  • Poller runs long actions on a detached goroutine so a migration doesn't stall the queue drain or get cancelled by the next poll tick.

Tooling / tests

  • pkg/mutate/live_test.go (//go:build live): asserted end-to-end tests against a real cluster — expand, replica scale, downsize-with-data-survival, volume_delete. Excluded from normal builds; sizes/SC env-tunable.
  • cmd/actionctl: small dev CLI to invoke a single handler against the cluster without the relay/backend.

Testing

  • Unit tests (fake client/dynamic/exec) for every handler, rollback, parity, coercion; plus dispatch long-timeout and poller async-dispatch tests.
  • Live-verified on GKE dev — all four TestLive* pass, including data preserved across the full downsize migration (mover pod copy → repoint → original deleted → token read back from the new PVC).

Notes

  • RBAC: the runner ServiceAccount already grants everything the migration needs (PV patch/delete, PVC create/delete, pods + pods/exec, deployments/statefulsets update, storageclasses get) — no chart change.
  • Per-copy is bounded by a shell timeout 300; integrity check is cp -a exit + COPY_SUCCESS marker (no checksum) — both inherited from the legacy action.
  • An agent restart mid-migration orphans the task (reaps to TIMEOUT, recoverable by re-applying).

…k dispatch

The agent_task poller dispatches through HandleTrusted, which wraps every
handler in the 180s TaskTimeout. The upcoming rightsize_pvc downsize migration
copies volume data via a mover pod and routinely exceeds that, so:

- dispatch.Config gains LongTaskTimeout + LongActions; HandleTrusted applies the
  long ceiling only to those actions. The WS Handle path is untouched (no long
  action reaches it). 0 disables the override.
- The poller runs LongActions on a detached goroutine (context.WithoutCancel)
  so a multi-minute task neither stalls the queue drain nor gets cancelled by
  the next poll tick. The collector already flips the row to PROCESSING on GET,
  so it is not re-handed while running.
…iations

Adds the three agent_task remediation actions the api-server enqueues but the Go
agent did not yet implement (the poller would reply "action not registered"):

- replica_rightsizing: scale Deployment/StatefulSet/Rollout via the dynamic
  client; replica_count coerced from JSON number or numeric string (both wire
  forms the server sends).
- rightsize_pvc: RightsizePVC routes by provisioned capacity. Expansion patches
  spec.resources.requests.storage after checking allowVolumeExpansion. Downsize
  can't shrink a PV in place, so it runs a copy-migration (faithful port of the
  legacy robusta path): Deployment = new PVC -> copy -> repoint -> delete
  original (point of no return = repoint); StatefulSet = name-preserving double
  copy with PV reclaim flipped to Retain before the original PVC is deleted.
  Data is moved by a transient ubuntu mover pod (cp -a) exec'd over SPDY, with
  config-parity verification and stage-aware rollback that leaves data on two
  copies after the point of no return.
- volume_delete: delete an unused PV (payload name is the cluster-scoped PV) and
  its bound PVC via claimRef; both deletes are 404-tolerant.

All three are delivered via the trusted agent_task poller and stay OUT of the
light-action allowlist (same posture as rightsizing_resource).
…v CLI

- main.go: wire mut.SetExec (SPDY mover-pod exec for downsize), register
  rightsize_pvc in dispatch+poller LongActions, and set LongTaskTimeout
  (50m default, env LONG_TASK_TIMEOUT_SECONDS) under the server's 60m
  PROCESSING reap window.
- pkg/mutate/live_test.go (build tag `live`): asserted end-to-end tests against
  a real cluster — expand, replica scale, downsize-with-data-survival, and
  volume_delete. Excluded from normal builds; sizes/SC env-tunable. Verified
  green on GKE (incl. data preserved across the downsize migration).
- cmd/actionctl: small dev CLI to invoke a single remediation handler against
  the cluster without the relay/backend, for ad-hoc testing.
@mayankpande88
mayankpande88 requested a review from a team as a code owner June 16, 2026 06:09

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces local development tools and implements agent remediation handlers for scaling workloads (replica_rightsizing), resizing PVCs (rightsize_pvc), and deleting volumes (volume_delete). It also updates the task poller to run long-running tasks asynchronously. The feedback highlights critical reliability issues: retrying workload scaling can overwrite the original replica count; retrying deployment repointing can fail if already updated; detaching the context prevents graceful cleanup on agent shutdown; and hardcoded copy timeouts and mover images limit scalability and enterprise compatibility.

Comment thread runner/pkg/mutate/pvcdownsize_helpers.go
Comment thread runner/pkg/mutate/pvcdownsize_helpers.go
Comment thread runner/pkg/tasks/poller.go
Comment thread runner/pkg/mutate/pvcdownsize_helpers.go Outdated
Comment thread runner/pkg/mutate/pvcdownsize.go Outdated
From PR #479 review:

- scaleWorkloadTyped: capture the original replica count only on the first
  read, so a conflict-retry that re-Gets an already-scaled object can't clobber
  `old` with 0 (which would leave the workload restored to 0).
- repointDeploymentPVC: treat "volume already points to newPVC" as success —
  a prior Update may apply server-side before the client sees a conflict, and
  the retry must not hard-fail the migration.
- copyData: bound the copy by the remaining LongTaskTimeout budget (copyTimeout)
  instead of a fixed `timeout 300`, so large volumes aren't cut off at 5m.
- mover image: configurable via MOVER_IMAGE (fallback "ubuntu") for clusters
  that can't pull from Docker Hub.
- poller: keep the agent context for long actions instead of detaching with
  WithoutCancel, so a graceful shutdown propagates cancellation and the
  migration's rollback defers run within the termination grace.
@github-actions

Copy link
Copy Markdown
Contributor

📦 Image Tags Updated

I've automatically updated the image tags in `charts/nudgebee-agent/values.yaml` to the latest versions from GHCR for the `main` branch.

The image tags are now synchronized with the latest builds and ready for release.

RamanKharchee
RamanKharchee previously approved these changes Jun 16, 2026
- actionctl: call cancel() explicitly before os.Exit (gocritic exitAfterDefer);
  declare params with var to drop the unused initializer (staticcheck SA4006).
- poller_test: tagged switch on r.Method (staticcheck QF1002).
@github-actions

Copy link
Copy Markdown
Contributor

📦 Image Tags Updated

I've automatically updated the image tags in `charts/nudgebee-agent/values.yaml` to the latest versions from GHCR for the `main` branch.

The image tags are now synchronized with the latest builds and ready for release.

@mayankpande88
mayankpande88 merged commit 9f3d1d6 into main Jun 16, 2026
8 checks passed
@mayankpande88
mayankpande88 deleted the feat/agent-task-remediations branch June 16, 2026 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants