feat: handle operatorAction commands (annotate + dry run) #383
Conversation
…+ dry-run) Signed-off-by: yugal07 <yashsadhwani544@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughDefines remediator contracts, adds an AnnotateRemediator (plan/apply/revert with server-side dry-run), integrates operator-action routing, enforces Phase‑1 safety checks (explicit target, no selectors, excluded namespaces), records results and emits audit events, and adds tests plus a dependency bump. ChangesOperator Action Remediation Implementation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Signed-off-by: yugal07 <yashsadhwani544@gmail.com>
operator: handle
operatorActioncommands — Phase 1 remediation (annotate + dry-run)Related issue: kubescape/kubescape#1770 — Kubescape CLI control over cluster operations
Design: kubescape/designs-and-proposals#5 — Kubescape CLI Control over Cluster Operations (merged)
Contract: armosec/armoapi-go#655 —
operatorActioncommand contract (merged, shipped inv0.0.720)Pairs with: kubescape/helm-charts#855
What this PR does
This is Phase 1 of the merged design: it teaches the in-cluster operator to
execute a
TypeOperatorActioncommand end to end, with the lowest-blast-radiusaction (
annotate) and dry-run as the default. It proves theCLI → operator →
OperatorCommandstatus pipeline with essentially zero risk,and lays the extensible
Remediatorframework thatquarantine/cordonplug into in later phases.
No new transport, no new endpoint: remediation is a new verb on the existing
apis.Commandpipeline that already powerskubescape operator scan.go.mod/go.sum— bumparmosec/armoapi-gov0.0.673→v0.0.720topick up the merged
operatorActioncontract (TypeOperatorAction,OperatorActionArgs,IsDryRun,ToArgs/OperatorActionArgsFromMap).mainhandler/handlerequests.go— wire dispatch in two places:case apis.TypeOperatorActioninrunCommand(→handleOperatorAction), andin the itemize switch so an action is handled as a single request (it carries
its own target) rather than fanned out per pod.
mainhandler/actionhandler.go(new) —handleOperatorAction: parses thetyped args, enforces the Phase-1 safety rails, dispatches to the matching
Remediator, writes the result to theOperatorCommandstatus payload, andemits a best-effort Kubernetes Event for audit.
mainhandler/remediators/remediator.go(new) — theRemediatorinterface(
Plan/Apply/Revert), theTarget/Request/Plan/Resulttypes, andNewRegistry(annotate only for now; new actions are added by extending the map,not the pipeline).
mainhandler/remediators/annotate.go(new) —AnnotateRemediatoroverDeployment/StatefulSet/DaemonSet/Pod: a JSON merge patch that sets the
kubescape.io/remediated,…/remediation-reason,…/remediation-finding-refannotations;
Revertdeletes them (null-merge); server-side dry-run support.mainhandler/remediators/annotate_test.go,mainhandler/actionhandler_test.go(new) — 16 unit tests.
Safe-by-default behaviour
OperatorActionArgs.IsDryRun()returnstrueunless the caller set
dryRun=falseexplicitly (the CLI's--confirm), so amissing flag can never silently perform a real write.
with server-side
metav1.DryRunAllso the change is validated againstadmission controllers, never persisted.
(
config.SkipNamespace) are rejected before any client call.default install does not grant (see the paired helm-charts PR). Without it,
annotatereturns a cleanForbiddenthat is recorded on the command status —nothing is mutated.
quarantine/cordonreturn a clear"not implemented yet (later phase)" error; selector-driven (findings) targeting
returns a clear "phase 2" error; unknown actions error explicitly. Phase 1 acts
only on an explicit
target.Audit trail
Every action records its
Plan/Result(action, target, dryRun, applied) on theOperatorCommandstatuspayload(the GitOps- and Headlamp-friendly channel) andemits a
KubescapeRemediationKubernetes Event on the target's namespace.Testing
go build ./...andgo vet ./...— clean.mainhandler,mainhandler/remediators); dry-run assertions usea pass-through reactor to verify the
PatchOptions.DryRunactually sent —necessary because the fake clientset's tracker ignores server-side dry-run and
always persists, so "did not persist" cannot be asserted directly.
capabilities.json(now carrying the new
remediationkey) parses cleanly through the operator'sreal
LoadCapabilitiesConfig; unknown keys are tolerated (viper default), exactlyas the existing
riskAcceptancekey already is.Phasing
This is Phase 1 of the merged design. Next: the
kubescapeCLIoperator remediate annotatesubcommand (completes the user-facing loop), thenPhase 2
quarantine+ findings-driven targeting, then Phase 3cordon+operator-native auto-remediation. The
Remediatorregistry is designed so eachadds an implementation without touching the command pipeline.
Summary by CodeRabbit
New Features
Tests
Chores