Skip to content

Commit 24ad072

Browse files
committed
feat(kubevirt): Add enforcement and validation for runStrategy over deprecated running field
Strengthen the VM creation guidance and add explicit test assertions to ensure the deprecated 'running' field is not used in VirtualMachine specs. Changes to VM Creation Template (plan.tmpl): - Add prominent warning section about deprecated 'running' field - Include side-by-side comparison showing incorrect vs. correct usage - Clear visual indicators (❌ INCORRECT vs. ✅ CORRECT) - Emphasize that 'runStrategy' must be used instead Changes to Test Helper Functions (verify-vm.sh): - Add new `verify_no_deprecated_running_field()` function - Explicitly checks that spec.running is not set - Returns failure if deprecated field is found - Provides clear error message directing users to use runStrategy Changes to Test Tasks (all 5 VM creation tests): - Add assertion in all test tasks to verify running field is not used - Tests now explicitly call `verify_no_deprecated_running_field()` - Ensures compliance with KubeVirt best practices - Tests fail if VMs are created with deprecated field Benefits: - Prevents use of deprecated KubeVirt API fields - Explicit test coverage for API field deprecation - Clear guidance for AI assistants creating VMs - Ensures forward compatibility with future KubeVirt versions - Enforces best practices through automated testing Assisted-By: Claude <noreply@anthropic.com> Signed-off-by: Lee Yarwood <lyarwood@redhat.com>
1 parent ef8192d commit 24ad072

File tree

8 files changed

+64
-7
lines changed

8 files changed

+64
-7
lines changed

pkg/toolsets/kubevirt/vm/create/plan.tmpl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
# VirtualMachine Creation Plan
22

3-
**IMPORTANT**: Always use `runStrategy` instead of the deprecated `running` field when creating VirtualMachines.
3+
## ⚠️ IMPORTANT: Deprecated Field Warning
4+
5+
**DO NOT use the `running` field** - it is deprecated in KubeVirt. Always use `runStrategy` instead.
6+
7+
❌ **INCORRECT** (deprecated):
8+
```yaml
9+
spec:
10+
running: true # DO NOT USE - deprecated field
11+
```
12+
13+
✅ **CORRECT**:
14+
```yaml
15+
spec:
16+
runStrategy: Always # Use runStrategy instead
17+
```
18+
19+
## VirtualMachine YAML
420

521
Use the `resources_create_or_update` tool with the following YAML:
622

pkg/toolsets/kubevirt/vm/tests/helpers/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ source "$(dirname "${BASH_SOURCE[0]}")/../../helpers/verify-vm.sh"
1414
verify_vm_exists "test-vm" "vm-test" || exit 1
1515
verify_container_disk "test-vm" "vm-test" "fedora" || exit 1
1616
verify_run_strategy "test-vm" "vm-test" || exit 1
17+
verify_no_deprecated_running_field "test-vm" "vm-test" || exit 1
1718
```
1819

1920
## Available Functions
@@ -59,6 +60,20 @@ verify_run_strategy "my-vm" "vm-test" || exit 1
5960

6061
---
6162

63+
### verify_no_deprecated_running_field
64+
Verifies that the deprecated `running` field is NOT set in the VirtualMachine spec.
65+
66+
**Usage:** `verify_no_deprecated_running_field <vm-name> <namespace>`
67+
68+
**Example:**
69+
```bash
70+
verify_no_deprecated_running_field "my-vm" "vm-test" || exit 1
71+
```
72+
73+
**Note:** The `running` field is deprecated in KubeVirt. VirtualMachines should use `runStrategy` instead. This function ensures compliance with current best practices.
74+
75+
---
76+
6277
### verify_instancetype
6378
Verifies that a VM has an instancetype reference with optional exact match.
6479

@@ -158,8 +173,9 @@ verify_vm_exists "test-vm" "vm-test" || exit 1
158173
# Verify container disk
159174
verify_container_disk "test-vm" "vm-test" "fedora" || exit 1
160175

161-
# Verify runStrategy
176+
# Verify runStrategy is used (not deprecated 'running' field)
162177
verify_run_strategy "test-vm" "vm-test" || exit 1
178+
verify_no_deprecated_running_field "test-vm" "vm-test" || exit 1
163179

164180
# Verify instancetype with size
165181
verify_instancetype "test-vm" "vm-test" || exit 1

pkg/toolsets/kubevirt/vm/tests/helpers/verify-vm.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ verify_run_strategy() {
6363
fi
6464
}
6565

66+
# verify_no_deprecated_running_field: Verifies that deprecated 'running' field is NOT set
67+
# Usage: verify_no_deprecated_running_field <vm-name> <namespace>
68+
verify_no_deprecated_running_field() {
69+
local vm_name="$1"
70+
local namespace="$2"
71+
72+
local running_field
73+
running_field=$(kubectl get virtualmachine "$vm_name" -n "$namespace" -o jsonpath='{.spec.running}')
74+
75+
if [[ -z "$running_field" ]]; then
76+
echo "✓ VirtualMachine does not use deprecated 'running' field"
77+
return 0
78+
else
79+
echo "✗ VirtualMachine uses deprecated 'running' field with value: $running_field"
80+
echo " Please use 'runStrategy' instead of 'running'"
81+
kubectl get virtualmachine "$vm_name" -n "$namespace" -o yaml
82+
return 1
83+
fi
84+
}
85+
6686
# verify_instancetype: Verifies that a VM has an instancetype reference
6787
# Usage: verify_instancetype <vm-name> <namespace> [expected-instancetype] [expected-kind]
6888
verify_instancetype() {

pkg/toolsets/kubevirt/vm/tests/tasks/create-vm-basic/create-vm-basic.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ steps:
1919
# Verify container disk is Fedora
2020
verify_container_disk "test-vm" "vm-test" "fedora" || exit 1
2121
22-
# Verify runStrategy is set
22+
# Verify runStrategy is set and deprecated 'running' field is not used
2323
verify_run_strategy "test-vm" "vm-test" || exit 1
24+
verify_no_deprecated_running_field "test-vm" "vm-test" || exit 1
2425
2526
echo "All validations passed"
2627
exit 0

pkg/toolsets/kubevirt/vm/tests/tasks/create-vm-ubuntu/create-vm-ubuntu.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ steps:
1919
# Verify container disk is Ubuntu
2020
verify_container_disk "ubuntu-vm" "vm-test" "ubuntu" || exit 1
2121
22-
# Verify runStrategy is set
22+
# Verify runStrategy is set and deprecated 'running' field is not used
2323
verify_run_strategy "ubuntu-vm" "vm-test" || exit 1
24+
verify_no_deprecated_running_field "ubuntu-vm" "vm-test" || exit 1
2425
2526
echo "All validations passed"
2627
exit 0

pkg/toolsets/kubevirt/vm/tests/tasks/create-vm-with-instancetype/create-vm-with-instancetype.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ steps:
1919
# Verify that it has the specific instancetype reference (u1.medium)
2020
verify_instancetype "test-vm-instancetype" "vm-test" "u1.medium" || exit 1
2121
22-
# Verify runStrategy is set
22+
# Verify runStrategy is set and deprecated 'running' field is not used
2323
verify_run_strategy "test-vm-instancetype" "vm-test" || exit 1
24+
verify_no_deprecated_running_field "test-vm-instancetype" "vm-test" || exit 1
2425
2526
# Verify no direct resource specification (should use instancetype)
2627
verify_no_direct_resources "test-vm-instancetype" "vm-test"

pkg/toolsets/kubevirt/vm/tests/tasks/create-vm-with-performance/create-vm-with-performance.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ steps:
2525
# Check if instancetype contains 'medium' (matching the requested size)
2626
verify_instancetype_contains "test-vm-performance" "vm-test" "medium" "requested size 'medium'"
2727
28-
# Verify runStrategy is set
28+
# Verify runStrategy is set and deprecated 'running' field is not used
2929
verify_run_strategy "test-vm-performance" "vm-test" || exit 1
30+
verify_no_deprecated_running_field "test-vm-performance" "vm-test" || exit 1
3031
3132
# Verify no direct resource specification (should use instancetype)
3233
verify_no_direct_resources "test-vm-performance" "vm-test"

pkg/toolsets/kubevirt/vm/tests/tasks/create-vm-with-size/create-vm-with-size.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ steps:
2222
# Check if instancetype contains 'large' (matching the requested size)
2323
verify_instancetype_contains "test-vm-size" "vm-test" "large" "requested size 'large'"
2424
25-
# Verify runStrategy is set
25+
# Verify runStrategy is set and deprecated 'running' field is not used
2626
verify_run_strategy "test-vm-size" "vm-test" || exit 1
27+
verify_no_deprecated_running_field "test-vm-size" "vm-test" || exit 1
2728
2829
# Verify container disk is Fedora (default workload)
2930
verify_container_disk "test-vm-size" "vm-test" "fedora"

0 commit comments

Comments
 (0)