Skip to content

feat(kubevirt): add run policy support to VM lifecycle management#1012

Open
awels wants to merge 1 commit intocontainers:mainfrom
awels:update_start_vm_different_policy
Open

feat(kubevirt): add run policy support to VM lifecycle management#1012
awels wants to merge 1 commit intocontainers:mainfrom
awels:update_start_vm_different_policy

Conversation

@awels
Copy link
Copy Markdown

@awels awels commented Apr 3, 2026

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:

  • 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
  • Added 19 unit tests covering all run policy combinations
  • Added 3 integration tests for vm_lifecycle tool
  • Updated tool schema with enum values and documentation
  • Auto-generated README.md updates

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:
- 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
- Added 19 unit tests covering all run policy combinations
- Added 3 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>
@awels
Copy link
Copy Markdown
Author

awels commented Apr 3, 2026

/cc @lyarwood

@ksimon1
Copy link
Copy Markdown
Contributor

ksimon1 commented Apr 10, 2026

/lgtm

@Cali0707
Copy link
Copy Markdown
Collaborator

/run-mcpchecker kubevirt

Comment thread pkg/kubevirt/vm.go
case RunPolicyOnce:
return RunStrategyOnce
}
return RunStrategyAlways
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.

This silent default to Always has a broader impact: RestartVM (line 200) hardcodes RunStrategyAlways when restarting. After this PR, if a user starts a VM with run_policy=Once then restarts it, the strategy silently reverts to Always. Should RestartVM accept and forward the run_policy parameter, or at minimum preserve the VM's current runStrategy?


// Parse optional run_policy parameter (defaults to HighAvailability)
runPolicyStr := api.OptionalString(params, "run_policy", string(kubevirt.RunPolicyHighAvailability))
runPolicy := kubevirt.RunPolicy(runPolicyStr)
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.

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))
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.

nit: run_policy is parsed for all actions but only used by start. Consider moving this inside the ActionStart case to avoid confusion.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants