|
| 1 | +# vSphere Policies |
| 2 | + |
| 3 | +vSphere Policies provide a declarative way to manage compute policies, tags, and operational constraints for virtual machines running in a vSphere environment. These policies are part of the `vsphere.policy.vmware.com` API group and integrate with VM Operator to enable policy-driven VM management. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The vSphere Policy APIs introduce three main resource types: |
| 8 | + |
| 9 | +- **ComputePolicy**: Defines compute-related policies for workloads |
| 10 | +- **TagPolicy**: Defines vSphere tags to be applied to workloads |
| 11 | +- **PolicyEvaluation**: Evaluates which policies should apply to a workload |
| 12 | + |
| 13 | +These resources work together to provide flexible, declarative policy management that can be applied either explicitly or automatically based on matching criteria. |
| 14 | + |
| 15 | +## ComputePolicy |
| 16 | + |
| 17 | +A `ComputePolicy` resource defines compute-related policies that control various aspects of VM behavior, placement, and resource allocation in vSphere. |
| 18 | + |
| 19 | +### Specification |
| 20 | + |
| 21 | +```yaml |
| 22 | +apiVersion: vsphere.policy.vmware.com/v1alpha1 |
| 23 | +kind: ComputePolicy |
| 24 | +metadata: |
| 25 | + name: production-policy |
| 26 | + namespace: my-namespace |
| 27 | +spec: |
| 28 | + description: "Production workload policy" |
| 29 | + policyID: "vsphere-policy-123" # Optional vSphere policy ID |
| 30 | + enforcementMode: Mandatory # Or Optional |
| 31 | + match: |
| 32 | + workload: |
| 33 | + labels: |
| 34 | + - key: tier |
| 35 | + operator: In |
| 36 | + values: |
| 37 | + - production |
| 38 | + kind: VirtualMachine |
| 39 | + tags: |
| 40 | + - production-tag-policy |
| 41 | +``` |
| 42 | +
|
| 43 | +### Enforcement Modes |
| 44 | +
|
| 45 | +ComputePolicy resources support two enforcement modes: |
| 46 | +
|
| 47 | +#### Mandatory Policies |
| 48 | +
|
| 49 | +- Automatically applied to all matching workloads in the namespace |
| 50 | +- Cannot be overridden or removed by users |
| 51 | +- Applied based on the `match` criteria |
| 52 | +- If no `match` is specified, applies to all workloads in the namespace |
| 53 | + |
| 54 | +#### Optional Policies |
| 55 | + |
| 56 | +- Must be explicitly referenced in a VM's `spec.policies` field |
| 57 | +- Users can choose whether to apply these policies |
| 58 | +- Provide flexibility for workload-specific requirements |
| 59 | + |
| 60 | +### Match Specifications |
| 61 | + |
| 62 | +The `match` field allows sophisticated matching based on: |
| 63 | + |
| 64 | +#### Workload Properties |
| 65 | +- **Labels**: Match based on workload labels using label selector requirements |
| 66 | +- **Kind**: Filter by workload type (`Pod` or `VirtualMachine`) |
| 67 | +- **Guest OS**: Match based on guest ID or guest family |
| 68 | + |
| 69 | +#### Image Properties |
| 70 | +- **Name**: Match based on image name patterns |
| 71 | +- **Labels**: Match based on image labels |
| 72 | + |
| 73 | +#### Boolean Operations |
| 74 | +- **And**: All conditions must match (default) |
| 75 | +- **Or**: At least one condition must match |
| 76 | + |
| 77 | +Example of complex matching: |
| 78 | + |
| 79 | +```yaml |
| 80 | +spec: |
| 81 | + match: |
| 82 | + op: Or |
| 83 | + match: |
| 84 | + - workload: |
| 85 | + labels: |
| 86 | + - key: tier |
| 87 | + operator: In |
| 88 | + values: [production] |
| 89 | + - image: |
| 90 | + name: |
| 91 | + op: HasPrefix |
| 92 | + value: "production-" |
| 93 | +``` |
| 94 | + |
| 95 | +## TagPolicy |
| 96 | + |
| 97 | +A `TagPolicy` resource defines vSphere tags that should be applied to workloads. These are typically referenced by ComputePolicy objects. |
| 98 | + |
| 99 | +### Specification |
| 100 | + |
| 101 | +```yaml |
| 102 | +apiVersion: vsphere.policy.vmware.com/v1alpha1 |
| 103 | +kind: TagPolicy |
| 104 | +metadata: |
| 105 | + name: production-tag-policy |
| 106 | + namespace: my-namespace |
| 107 | +spec: |
| 108 | + tags: |
| 109 | + - "urn:vmomi:InventoryServiceTag:12345678-1234-1234-1234-123456789abc:GLOBAL" |
| 110 | + - "urn:vmomi:InventoryServiceTag:87654321-4321-4321-4321-cba987654321:GLOBAL" |
| 111 | +``` |
| 112 | + |
| 113 | +Tags are specified as UUIDs that correspond to tags configured in vSphere. When a ComputePolicy references a TagPolicy, the tags are applied to activate the policy on the workload. |
| 114 | + |
| 115 | +## PolicyEvaluation |
| 116 | + |
| 117 | +A `PolicyEvaluation` resource is used to evaluate which policies should apply to a theoretical workload configuration. This is useful for testing and validation before actually creating VMs. |
| 118 | + |
| 119 | +### Specification |
| 120 | + |
| 121 | +```yaml |
| 122 | +apiVersion: vsphere.policy.vmware.com/v1alpha1 |
| 123 | +kind: PolicyEvaluation |
| 124 | +metadata: |
| 125 | + name: test-evaluation |
| 126 | + namespace: my-namespace |
| 127 | +spec: |
| 128 | + workload: |
| 129 | + labels: |
| 130 | + tier: production |
| 131 | + guest: |
| 132 | + guestID: ubuntu64Guest |
| 133 | + guestFamily: Linux |
| 134 | + image: |
| 135 | + name: ubuntu-22.04 |
| 136 | + labels: |
| 137 | + os: ubuntu |
| 138 | + policies: |
| 139 | + - apiVersion: vsphere.policy.vmware.com/v1alpha1 |
| 140 | + kind: ComputePolicy |
| 141 | + name: specific-policy |
| 142 | +``` |
| 143 | + |
| 144 | +### Status |
| 145 | + |
| 146 | +After evaluation, the status will contain the list of policies that would apply: |
| 147 | + |
| 148 | +```yaml |
| 149 | +status: |
| 150 | + policies: |
| 151 | + - apiVersion: vsphere.policy.vmware.com/v1alpha1 |
| 152 | + kind: ComputePolicy |
| 153 | + name: production-policy |
| 154 | + observedGeneration: 2 |
| 155 | + tags: |
| 156 | + - "urn:vmomi:InventoryServiceTag:12345678-1234-1234-1234-123456789abc:GLOBAL" |
| 157 | + - apiVersion: vsphere.policy.vmware.com/v1alpha1 |
| 158 | + kind: ComputePolicy |
| 159 | + name: namespace-default |
| 160 | + observedGeneration: 1 |
| 161 | + conditions: |
| 162 | + - type: Ready |
| 163 | + status: "True" |
| 164 | +``` |
| 165 | + |
| 166 | +## Applying Policies to VirtualMachines |
| 167 | + |
| 168 | +### Explicit Application |
| 169 | + |
| 170 | +Optional policies can be explicitly applied to a VM using the `spec.policies` field: |
| 171 | + |
| 172 | +```yaml |
| 173 | +apiVersion: vmoperator.vmware.com/v1alpha5 |
| 174 | +kind: VirtualMachine |
| 175 | +metadata: |
| 176 | + name: my-vm |
| 177 | + namespace: my-namespace |
| 178 | +spec: |
| 179 | + className: my-vm-class |
| 180 | + imageName: ubuntu-22.04 |
| 181 | + storageClass: my-storage-class |
| 182 | + policies: |
| 183 | + - apiVersion: vsphere.policy.vmware.com/v1alpha1 |
| 184 | + kind: ComputePolicy |
| 185 | + name: performance-policy |
| 186 | + - apiVersion: vsphere.policy.vmware.com/v1alpha1 |
| 187 | + kind: ComputePolicy |
| 188 | + name: compliance-policy |
| 189 | +``` |
| 190 | + |
| 191 | +### Automatic Application |
| 192 | + |
| 193 | +Mandatory policies are automatically applied based on their match criteria. The VM's status will reflect all applied policies: |
| 194 | + |
| 195 | +```yaml |
| 196 | +status: |
| 197 | + policies: |
| 198 | + - apiVersion: vsphere.policy.vmware.com/v1alpha1 |
| 199 | + kind: ComputePolicy |
| 200 | + name: performance-policy |
| 201 | + generation: 3 |
| 202 | + - apiVersion: vsphere.policy.vmware.com/v1alpha1 |
| 203 | + kind: ComputePolicy |
| 204 | + name: compliance-policy |
| 205 | + generation: 1 |
| 206 | + - apiVersion: vsphere.policy.vmware.com/v1alpha1 |
| 207 | + kind: ComputePolicy |
| 208 | + name: namespace-mandatory-policy |
| 209 | + generation: 2 |
| 210 | +``` |
| 211 | + |
| 212 | +The `generation` field indicates which version of the policy was applied, helping track when policies need to be re-evaluated. |
| 213 | + |
| 214 | +## Match Operations |
| 215 | + |
| 216 | +The policy matching system supports various string and pattern matching operations: |
| 217 | + |
| 218 | +| Operation | Description | Example | |
| 219 | +|-----------|-------------|---------| |
| 220 | +| `Equal` | Exact match | `value: "production"` | |
| 221 | +| `NotEqual` | Does not match | `value: "development"` | |
| 222 | +| `HasPrefix` | Starts with | `value: "prod-"` | |
| 223 | +| `NotHasPrefix` | Does not start with | `value: "test-"` | |
| 224 | +| `HasSuffix` | Ends with | `value: "-prod"` | |
| 225 | +| `NotHasSuffix` | Does not end with | `value: "-test"` | |
| 226 | +| `Contains` | Contains substring | `value: "production"` | |
| 227 | +| `NotContains` | Does not contain | `value: "test"` | |
| 228 | +| `Match` | Regex match | `value: "^prod-.*-v[0-9]+$"` | |
| 229 | +| `NotMatch` | Does not match regex | `value: "^test-.*"` | |
| 230 | + |
| 231 | +## Guest Family Types |
| 232 | + |
| 233 | +When matching based on guest operating system family, the following values are supported: |
| 234 | + |
| 235 | +- `Darwin`: macOS guests |
| 236 | +- `Linux`: Linux-based guests |
| 237 | +- `Netware`: NetWare guests |
| 238 | +- `Other`: Other guest types |
| 239 | +- `Solaris`: Solaris guests |
| 240 | +- `Windows`: Windows guests |
| 241 | + |
| 242 | +## Best Practices |
| 243 | + |
| 244 | +### Policy Naming |
| 245 | +- Use descriptive names that indicate the policy's purpose |
| 246 | +- Include environment or tier indicators (e.g., `production-compute-policy`) |
| 247 | +- Be consistent with naming conventions across namespaces |
| 248 | + |
| 249 | +### Match Criteria |
| 250 | +- Keep match criteria as specific as needed but not overly complex |
| 251 | +- Use labels for flexible, user-controllable matching |
| 252 | +- Use image and guest properties for system-enforced matching |
| 253 | + |
| 254 | +### Enforcement Modes |
| 255 | +- Use mandatory policies for namespace-wide requirements |
| 256 | +- Use optional policies for workload-specific optimizations |
| 257 | +- Document which policies are available and their purposes |
| 258 | + |
| 259 | +### Policy Updates |
| 260 | +- Monitor the `generation` field in VM status to track policy versions |
| 261 | +- Test policy changes with PolicyEvaluation before applying |
| 262 | +- Consider the impact on existing VMs when updating policies |
| 263 | + |
| 264 | +## Troubleshooting |
| 265 | + |
| 266 | +### Policies Not Applied |
| 267 | +- Check the policy's `enforcementMode` - optional policies need explicit reference |
| 268 | +- Verify match criteria using a PolicyEvaluation resource |
| 269 | +- Ensure the policy exists in the same namespace as the VM |
| 270 | + |
| 271 | +### Policy Conflicts |
| 272 | +- Review all mandatory policies in the namespace |
| 273 | +- Check for overlapping match criteria |
| 274 | +- Use PolicyEvaluation to test combinations |
| 275 | + |
| 276 | +### Tag Activation |
| 277 | +- Verify TagPolicy resources contain valid vSphere tag UUIDs |
| 278 | +- Ensure referenced TagPolicy resources exist |
| 279 | +- Check vSphere permissions for tag application |
0 commit comments