feat(kubevirt): add run policy support to VM lifecycle management#1012
feat(kubevirt): add run policy support to VM lifecycle management#1012awels wants to merge 1 commit into
Conversation
|
/cc @lyarwood |
|
/lgtm |
|
/run-mcpchecker kubevirt |
|
|
||
| // Parse optional run_policy parameter (defaults to HighAvailability) | ||
| runPolicyStr := api.OptionalString(params, "run_policy", string(kubevirt.RunPolicyHighAvailability)) | ||
| runPolicy := kubevirt.RunPolicy(runPolicyStr) |
There was a problem hiding this comment.
The run_policy value is cast directly to RunPolicy without server-side validation. getRunStrategyFromRunPolicy silently defaults invalid values to Always, which masks errors. Consider adding explicit validation:
if !kubevirt.IsValidRunPolicy(runPolicy) {
return api.NewToolCallResult("", fmt.Errorf("invalid run_policy %q: must be one of HighAvailability, RestartOnFailure, Once", runPolicyStr)), nil
}| } | ||
|
|
||
| // Parse optional run_policy parameter (defaults to HighAvailability) | ||
| runPolicyStr := api.OptionalString(params, "run_policy", string(kubevirt.RunPolicyHighAvailability)) |
There was a problem hiding this comment.
nit: run_policy is parsed for all actions but only used by start. Consider moving this inside the ActionStart case to avoid confusion.
c55d4c1 to
640436a
Compare
|
/run-mcpchecker kubevirt |
|
@awels can you fix the merge conflict on the readme? |
|
Sure give me a bit to rebase. |
Adds a new run_policy parameter to the vm_lifecycle tool that allows users to control the VM's runStrategy when starting/restarting a virtual machine. The parameter supports three policies: - HighAvailability: VM runs continuously (sets runStrategy to Always) - RestartOnFailure: VM restarts on failure (sets runStrategy to RerunOnFailure) - Once: VM runs once and stops after completion (sets runStrategy to Once) The run_policy parameter is optional and defaults to HighAvailability to maintain backward compatibility with existing usage. Changes include: - Updated StartVM function to accept RunPolicy parameter - Updated RestartVM function to accept RunPolicy parameter - StopVM ignores run_policy parameter - Added unit tests covering all run policy combinations - Added integration tests for vm_lifecycle tool - Updated tool schema with enum values and documentation - Auto-generated README.md updates Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> Signed-off-by: Alexander Wels <awels@redhat.com>
640436a to
323bb8e
Compare
|
@Cali0707 I rebased if you could run the tests? |
manusa
left a comment
There was a problem hiding this comment.
Overview
Independent review of the diff (not the PR description). The feature is well-scoped and the public-API change is backward-compatible for run_policy, but there are several issues worth addressing before merge — most notably a latent footgun in getRunStrategyFromRunPolicy, a stale README.md that does not match the new action description, and broken markdown rendering for the run_policy description.
Strengths
- Backward compatible:
run_policyis optional and defaults toHighAvailability, which preserves the priorAlwaysbehavior forstart/restart. - Schema-side
enumplus runtime validation gives the LLM an authoritative list. - Good test coverage of policy/strategy combinations (
pkg/kubevirt/vm_test.go,pkg/mcp/kubevirt_test.go). pkg/mcp/testdata/toolsets-kubevirt-tools.jsonis updated to match the schema.- Migration to
api.WrapParamsinpkg/toolsets/kubevirt/vm/lifecycle/tool.go:77-84matches the newer pattern used elsewhere in the codebase.
Issues
Critical (Must Fix)
getRunStrategyFromRunPolicysilently falls back toRunStrategyAlwaysfor unknown policies (pkg/kubevirt/vm.go:116-126). Today, all production callers gate onIsValidRunPolicyfirst, so the fallback is unreachable — but if any future caller forgets, an unknown policy boots the VM withAlwaysinstead of failing loudly.TestGetRunStrategyFromRunPolicyeven encodes this surprise as expected behavior (pkg/kubevirt/vm_test.go:469-473, "Invalid policy defaults to Always"), making accidental regressions silent. Either drop the fallback and panic / return a secondbool, error, or remove the dead branch entirely now that validation is mandatory.
Important (Should Fix)
-
README.mdactiondescription is out of sync.pkg/toolsets/kubevirt/vm/lifecycle/tool.go:44was shortened to"The lifecycle action to perform: 'start', 'stop', or 'restart'"(andpkg/mcp/testdata/toolsets-kubevirt-tools.json:168was regenerated), butREADME.md:535still shows the old'start' (changes runStrategy to Always), 'stop' (changes runStrategy to Halted), or 'restart' (stops then starts the VM). Runningmake update-readme-toolsproduces a non-empty diff, so the auto-generated section was not regenerated. -
run_policydescription renders as broken markdown inREADME.md. The description inpkg/toolsets/kubevirt/vm/lifecycle/tool.go:53-57embeds\n - 'HighAvailability': …etc. The README generator (internal/tools/update-readme/main.go:90-97) emits each property at 2-space indent, so the embedded-bullets land at the same indent level as therun_policyline itself — they render as siblings ofrun_policy, not as nested explanations.README.md:539-541show three "Once / HighAvailability / RestartOnFailure" bullets that look like additional top-level parameters, andREADME.md:542(Defaults to 'HighAvailability' …) drops out of the list entirely because it has no indent. Move the option list into the parentaction/run_policydescription as a single line, or render the options outside the schema description, or deeper-indent the bullets (e.g.\n - …). -
Misleading "started successfully" message when only the strategy changes (
pkg/toolsets/kubevirt/vm/lifecycle/tool.go:104-108,pkg/kubevirt/vm.go:73-74).StartVMnow returnswasStarted == truewhenevercurrentStrategy != desiredStrategy, which includes transitions where the VM was already running (e.g.Always→Once). The tool then says# VirtualMachine started successfully with run policy 'Once', even though the VM never stopped. Either differentiate the path inStartVM(e.g. return a tri-statestarted | strategyChanged | noChange) or branch the message on whethercurrentStrategy == RunStrategyHalted/ empty. The doc comment onStartVMoverloads "started" with "strategy changed" in the same way and should be updated alongside. -
TestGetRunStrategyFromRunPolicyviolates the project's black-box testing rule (pkg/kubevirt/vm_test.go:448-484).CLAUDE.mdis explicit: tests should be black-box and not access unexported functions;getRunStrategyFromRunPolicyis lowercase. The mapping is already fully covered byTestStartVM(viawantRunStrategy) andTestRestartVMWithDifferentRunPolicies. Delete this test — keeping it also locks the buggyAlwaysfallback in as expected behavior (see Critical above). -
run_policyis validated twice.pkg/toolsets/kubevirt/vm/lifecycle/tool.go:97-99and:125-127validate, thenpkg/kubevirt/vm.go:77-79and:196-198validate again — same check, four hard-coded copies of the error string. Pick one layer (the library is the public API, so prefer keeping the check there) and drop the other. As-is, adding a fourth policy means touching four strings.
Minor (Nice to Have)
-
Unused exported constants (
pkg/kubevirt/vm.go:19,22).RunStrategyWaitAsReceiverhas zero references anywhere;RunStrategyManualis referenced only from tests in the same package. Both inflate the exported surface for downstream consumers without a current need. Either remove them or introduce them when a production caller actually needs them.RunStrategyRerunOnFailure/RunStrategyOnceare legitimately used. -
Type declaration style (
pkg/kubevirt/vm.go:13-14). Two stackedtype X stringlines are valid Go but a groupedtype ( … )block reads better when introducing a sibling type. Optional polish. -
Stale comment (
pkg/kubevirt/vm.go:92,// Check if already running). With the new semantics this branch checks "already at the desired strategy", not "already running". Update the comment so it doesn't describe the old behavior. -
Action description loses useful detail. Trimming
actionto just the three verbs (pkg/toolsets/kubevirt/vm/lifecycle/tool.go:44) removes therunStrategymapping that an LLM previously relied on to predict behavior. If the goal was to move that mapping into therun_policydescription, consider keeping a short "(seerun_policyfor details)" hint onactionso the link is discoverable.
Assessment: Ready to merge? No — with fixes
The README desync and the broken-markdown rendering should be fixed before merge (auto-generated docs are easy to drift again later), and the silent Always fallback in getRunStrategyFromRunPolicy is worth tightening up while the file is open. The remaining items are quality-of-life.
Adds a new run_policy parameter to the vm_lifecycle tool that allows users to control the VM's runStrategy when starting a virtual machine.
The parameter supports three policies:
The run_policy parameter is optional and defaults to HighAvailability to maintain backward compatibility with existing usage.
Changes include: