|
2 | 2 |
|
3 | 3 | The `VirtualMachine` controller is responsible for reconciling `VirtualMachine` objects. |
4 | 4 |
|
5 | | -## Reconcile |
| 5 | +## Priority |
| 6 | + |
| 7 | +The VirtualMachine controller leverages controller-runtime's [priority queue feature](https://github.com/kubernetes-sigs/controller-runtime/issues/2374) to intelligently order reconciliation requests based on the operational state of each VM. Priority queues allow the controller to process more urgent operations (like VM creation or deletion) ahead of routine maintenance operations (like resync events), improving system responsiveness and resource utilization. |
| 8 | + |
| 9 | +### Priority queues |
| 10 | + |
| 11 | +Controller-runtime's priority queue implementation uses a min-heap data structure where items with **higher numerical values have higher priority** and are dequeued first. When a Kubernetes event (create, update, delete, or generic) triggers a reconciliation, the controller can assign a specific priority value to that request. This ensures that critical operations don't get stuck behind less urgent work, especially during periods of high activity like controller restarts when many resync events are generated simultaneously. |
| 12 | + |
| 13 | +The priority queue feature addresses the common problem of event storms during resyncs, where hundreds or thousands of objects might be queued at once. By assigning lower priorities to resync events and higher priorities to user-initiated changes, the system remains responsive to important operations. |
| 14 | + |
| 15 | +### Priority levels |
| 16 | + |
| 17 | +The VirtualMachine controller implements five priority levels, evaluated in the following order: |
| 18 | + |
| 19 | +| Priority Level | Value | VM State | Description | |
| 20 | +|----------------|-------|----------|-------------| |
| 21 | +| **Creating** | 100 | `VirtualMachineConditionCreated` is not `True` | Highest priority. Assigned to VMs that are being created for the first time. This ensures new VM requests are processed quickly. | |
| 22 | +| **PowerStateChange** | 99 | `spec.powerState` ≠ `status.powerState` | Second highest priority. Assigned when a VM needs a power state transition (power on, power off, or suspend). Power operations are time-sensitive and user-visible. | |
| 23 | +| **Deleting** | 98 | `deletionTimestamp` is set | Assigned to VMs being deleted. Cleanup operations have high priority to free resources promptly. | |
| 24 | +| **WaitingForIP** | 97 | Powered on, networking enabled, but no IP address assigned | Assigned when a VM is powered on with networking enabled but hasn't yet received an IP address (neither IPv4 nor IPv6). This ensures the controller actively monitors for network readiness. | |
| 25 | +| **WaitingForDiskPromo** | 96 | `spec.promoteDisksMode` ≠ `Disabled` and `VirtualMachineDiskPromotionSynced` is not `True` | Assigned when disk promotion is configured but not yet completed. Ensures timely processing of disk promotion operations. | |
| 26 | +| **Default** | -1 to -4 | All other cases | Used for routine reconciliations of stable VMs. The default value varies by event type (see below), with all defaults being negative numbers to indicate lower priority than the specific VM state priorities. | |
| 27 | + |
| 28 | +The **Default** priority level varies based on the Kubernetes event type that triggered the reconciliation: |
| 29 | + |
| 30 | +| Event Type | Default Priority | When Applied | |
| 31 | +|------------|------------------|--------------| |
| 32 | +| Create | -1 | Events from initial controller cache sync | |
| 33 | +| Update | -2 | Events caused by an update to the object's desired state | |
| 34 | +| Delete | -3 | Delete events (though typically overridden by state-based priorities) | |
| 35 | +| Generic | -4 | Generic events like periodic resyncs | |
| 36 | + |
| 37 | +This tiered default system ensures that even among routine operations, there's an inherent ordering: create events from cache sync are processed before unchanged updates, which are processed before generic resyncs. |
| 38 | + |
| 39 | +### Priority evaluation |
| 40 | + |
| 41 | +The controller evaluates priorities using a cascade of conditions, with each check returning immediately if matched: |
| 42 | + |
| 43 | +1. **Annotation Override**: If the VM has the `vmoperator.vmware.com.protected/reconcile-priority` annotation set to a valid integer, that value is used directly, bypassing all other checks. |
| 44 | + |
| 45 | +!!! note "Privileged users only" |
| 46 | + |
| 47 | + Only privileged users, such as the VM Operator service account, may override the reconcile priority via annotation. |
| 48 | + |
| 49 | +2. **Non-VM Objects**: If the object is not a `VirtualMachine`, the default priority is returned. |
| 50 | + |
| 51 | +3. **Deletion**: If the VM has a `deletionTimestamp`, return `PriorityVirtualMachineDeleting`. |
| 52 | + |
| 53 | +4. **Creation**: If the `VirtualMachineConditionCreated` condition is not `True`, return `PriorityVirtualMachineCreating`. |
| 54 | + |
| 55 | +5. **Power State Change**: If `spec.powerState` doesn't match `status.powerState`, return `PriorityVirtualMachinePowerStateChange`. |
| 56 | + |
| 57 | +6. **Waiting for IP**: If the VM is powered on (`status.powerState == PoweredOn`) and networking is enabled (`spec.network.disabled != true`), but no IP address is assigned (`status.network.primaryIP4` and `status.network.primaryIP6` are both empty), return `PriorityVirtualMachineWaitingForIP`. |
| 58 | + |
| 59 | +7. **Disk Promotion**: If `spec.promoteDisksMode` is not `Disabled` and the `VirtualMachineDiskPromotionSynced` condition is not `True`, return `PriorityVirtualMachineWaitingForDiskPromo`. |
| 60 | + |
| 61 | +8. **Default**: If none of the above conditions match, return the default priority. |
| 62 | + |
| 63 | +### Priority assignment |
| 64 | + |
| 65 | +Different Kubernetes event types receive different priority handling: |
| 66 | + |
| 67 | +- **Create Events**: During initial controller startup (when processing the initial list of objects from the API server), create events are assigned the default priority. For new objects created after startup, the priority function is evaluated. |
| 68 | + |
| 69 | +- **Update Events**: If the resource version hasn't changed between old and new objects, the default priority is used. Otherwise, the priority function evaluates the VM's current state. |
| 70 | + |
| 71 | +- **Delete Events**: Always evaluated through the priority function, typically resulting in `PriorityVirtualMachineDeleting`. |
| 72 | + |
| 73 | +- **Generic Events**: Always evaluated through the priority function based on the VM's current state. |
| 74 | + |
| 75 | +### Priority examples |
| 76 | + |
| 77 | +**Scenario 1: New VM Creation** |
| 78 | +When a user creates a new VirtualMachine resource, the reconcile request receives `PriorityVirtualMachineCreating (100)`, ensuring it's processed ahead of routine updates to existing VMs. |
| 79 | + |
| 80 | +**Scenario 2: Power State Change on Existing VM** |
| 81 | +When a user changes `spec.powerState` from `PoweredOff` to `PoweredOn`, the reconcile request receives `PriorityVirtualMachinePowerStateChange (99)`, placing it near the front of the queue. |
| 82 | + |
| 83 | +**Scenario 3: Waiting for IP Address** |
| 84 | +After a VM is powered on and networking is configured, but the IP hasn't been assigned yet, subsequent reconciliations receive `PriorityVirtualMachineWaitingForIP (97)`, ensuring the controller actively monitors for IP assignment completion. |
| 85 | + |
| 86 | +**Scenario 4: Routine Reconciliation** |
| 87 | +For a stable, running VM with no pending changes, reconcile requests (like periodic resyncs) receive the default priority, allowing more urgent operations to be processed first. |
| 88 | + |
| 89 | +**Scenario 5: Controller Restart with Many VMs** |
| 90 | +When the controller starts and processes its initial cache sync with 1,000 VMs, all initial reconciliations receive the default (lower) priority. If during this time a user creates a new VM or requests a power state change, those operations receive higher priority values and are processed first, maintaining system responsiveness. |
| 91 | + |
| 92 | +## Workflows |
| 93 | + |
| 94 | +### Overview |
6 | 95 |
|
7 | 96 | The main `Reconcile` function orchestrates the entire VirtualMachine reconciliation process, handling both creation/updates and deletions: |
8 | 97 |
|
@@ -36,7 +125,7 @@ flowchart TD |
36 | 125 | ReturnResult --> End |
37 | 126 | ``` |
38 | 127 |
|
39 | | -## ReconcileDelete |
| 128 | +### Delete |
40 | 129 |
|
41 | 130 | The `ReconcileDelete` method handles the deletion of VirtualMachine resources, ensuring proper cleanup of the underlying vSphere VM and associated resources: |
42 | 131 |
|
@@ -72,12 +161,10 @@ flowchart TD |
72 | 161 | Success --> End |
73 | 162 | ``` |
74 | 163 |
|
75 | | -### ReconcileNormal |
| 164 | +### Normal |
76 | 165 |
|
77 | 166 | The `ReconcileNormal` method handles the creation and updating of VirtualMachine resources. It manages finalizers, checks for paused annotations, and orchestrates the main reconcile logic including fast deploy support and VMI cache readiness. |
78 | 167 |
|
79 | | -#### High-Level Reconcile Normal Flow |
80 | | - |
81 | 168 | ```mermaid |
82 | 169 | flowchart TD |
83 | 170 | Start([Start ReconcileNormal]) --> CheckPause{Has pause<br />annotation?} |
@@ -108,7 +195,9 @@ flowchart TD |
108 | 195 | SkipReconcile --> Return |
109 | 196 | ``` |
110 | 197 |
|
111 | | -#### VM Provider CreateOrUpdate Decision Logic |
| 198 | +## vSphere provider |
| 199 | + |
| 200 | +### CreateOrUpdate |
112 | 201 |
|
113 | 202 | The VM provider determines whether to create or update a VM based on whether it exists in vSphere: |
114 | 203 |
|
@@ -143,7 +232,7 @@ flowchart TD |
143 | 232 | ReturnErr --> End |
144 | 233 | ``` |
145 | 234 |
|
146 | | -#### VM Creation Workflow with Fast Deploy |
| 235 | +### Fast deploy |
147 | 236 |
|
148 | 237 | When creating a VM, the system supports both traditional content library deployment and fast deploy optimization: |
149 | 238 |
|
@@ -180,7 +269,41 @@ flowchart TD |
180 | 269 | Success --> End |
181 | 270 | ``` |
182 | 271 |
|
183 | | -#### VM Update Workflow |
| 272 | +#### VMI Cache Integration (Fast Deploy) |
| 273 | + |
| 274 | +When Fast Deploy is enabled, the controller integrates with VirtualMachineImageCache resources to optimize VM creation: |
| 275 | + |
| 276 | +```mermaid |
| 277 | +flowchart TD |
| 278 | + Start([Check VMI Cache Ready]) --> HasLabel{VM has VMI<br />cache label?} |
| 279 | + HasLabel -->|No| Ready[Return ready - no cache needed] |
| 280 | + HasLabel -->|Yes| GetCache[Get VMI cache object] |
| 281 | +
|
| 282 | + GetCache --> CacheExists{Cache<br />exists?} |
| 283 | + CacheExists -->|No| NotReady[Return not ready] |
| 284 | + CacheExists -->|Yes| CheckOVF{OVF condition<br />true?} |
| 285 | +
|
| 286 | + CheckOVF -->|No| NotReady |
| 287 | + CheckOVF -->|Yes| HasLocation{VM has location<br />annotation?} |
| 288 | +
|
| 289 | + HasLocation -->|No| RemoveLabel[Remove VMI cache label] |
| 290 | + HasLocation -->|Yes| FindLocation[Find matching location status] |
| 291 | +
|
| 292 | + RemoveLabel --> Ready |
| 293 | + FindLocation --> LocationReady{Location files<br />ready?} |
| 294 | +
|
| 295 | + LocationReady -->|No| NotReady |
| 296 | + LocationReady -->|Yes| RemoveAnnotations[Remove cache label & annotation] |
| 297 | + RemoveAnnotations --> Ready |
| 298 | +
|
| 299 | + Ready --> End([End - Ready]) |
| 300 | + NotReady --> End2([End - Not Ready]) |
| 301 | +``` |
| 302 | + |
| 303 | +This comprehensive workflow documentation shows how the VirtualMachine controller orchestrates VM lifecycle management, including the sophisticated fast deploy optimization that uses cached VM images for faster provisioning. |
| 304 | + |
| 305 | + |
| 306 | +### Update |
184 | 307 |
|
185 | 308 | When updating an existing VM, the system reconciles various aspects in a specific order: |
186 | 309 |
|
@@ -230,7 +353,7 @@ flowchart TD |
230 | 353 | Success --> End |
231 | 354 | ``` |
232 | 355 |
|
233 | | -#### Status Reconciliation Details |
| 356 | +### Status |
234 | 357 |
|
235 | 358 | The status reconciliation updates the VM's observed state from vSphere: |
236 | 359 |
|
@@ -274,7 +397,7 @@ flowchart TD |
274 | 397 | Success --> End([End]) |
275 | 398 | ``` |
276 | 399 |
|
277 | | -#### Config Reconciliation Process |
| 400 | +### Config |
278 | 401 |
|
279 | 402 | Config reconciliation ensures the VM configuration matches the desired spec: |
280 | 403 |
|
@@ -302,7 +425,7 @@ flowchart TD |
302 | 425 | Success --> End([End]) |
303 | 426 | ``` |
304 | 427 |
|
305 | | -#### Power State Reconciliation |
| 428 | +### Power state |
306 | 429 |
|
307 | 430 | Power state reconciliation manages VM power operations: |
308 | 431 |
|
@@ -349,37 +472,3 @@ flowchart TD |
349 | 472 | Success --> End |
350 | 473 | ``` |
351 | 474 |
|
352 | | -### VMI Cache Integration (Fast Deploy) |
353 | | - |
354 | | -When Fast Deploy is enabled, the controller integrates with VirtualMachineImageCache resources to optimize VM creation: |
355 | | - |
356 | | -#### VMI Cache Readiness Check |
357 | | - |
358 | | -```mermaid |
359 | | -flowchart TD |
360 | | - Start([Check VMI Cache Ready]) --> HasLabel{VM has VMI<br />cache label?} |
361 | | - HasLabel -->|No| Ready[Return ready - no cache needed] |
362 | | - HasLabel -->|Yes| GetCache[Get VMI cache object] |
363 | | -
|
364 | | - GetCache --> CacheExists{Cache<br />exists?} |
365 | | - CacheExists -->|No| NotReady[Return not ready] |
366 | | - CacheExists -->|Yes| CheckOVF{OVF condition<br />true?} |
367 | | -
|
368 | | - CheckOVF -->|No| NotReady |
369 | | - CheckOVF -->|Yes| HasLocation{VM has location<br />annotation?} |
370 | | -
|
371 | | - HasLocation -->|No| RemoveLabel[Remove VMI cache label] |
372 | | - HasLocation -->|Yes| FindLocation[Find matching location status] |
373 | | -
|
374 | | - RemoveLabel --> Ready |
375 | | - FindLocation --> LocationReady{Location files<br />ready?} |
376 | | -
|
377 | | - LocationReady -->|No| NotReady |
378 | | - LocationReady -->|Yes| RemoveAnnotations[Remove cache label & annotation] |
379 | | - RemoveAnnotations --> Ready |
380 | | -
|
381 | | - Ready --> End([End - Ready]) |
382 | | - NotReady --> End2([End - Not Ready]) |
383 | | -``` |
384 | | - |
385 | | -This comprehensive workflow documentation shows how the VirtualMachine controller orchestrates VM lifecycle management, including the sophisticated fast deploy optimization that uses cached VM images for faster provisioning. |
|
0 commit comments