|
94 | 94 | - [Common Patterns Reference Enabled By Existing + New Tags: Non-Lists/Maps](#common-patterns-reference-enabled-by-existing--new-tags-non-listsmaps) |
95 | 95 | - [Common Patterns Reference Enabled By Existing + New Tags: Lists/Maps](#common-patterns-reference-enabled-by-existing--new-tags-listsmaps) |
96 | 96 | - [Common Patterns Reference Enabled By Existing + New Tags: List Items](#common-patterns-reference-enabled-by-existing--new-tags-list-items) |
| 97 | + - [Immutability Patterns and Tags Demonstrated](#immutability-patterns-and-tags-demonstrated) |
97 | 98 | - [Cross-Field Validation](#cross-field-validation) |
98 | 99 | - [Handling Ratcheting In Cross-Field Validation Tags](#handling-ratcheting-in-cross-field-validation-tags) |
99 | 100 | - [Referencing Fields in Validation-Gen For Cross-Field Validation Rules](#referencing-fields-in-validation-gen-for-cross-field-validation-rules) |
@@ -1241,6 +1242,73 @@ csr.Conditions = [{Type: "InProgress", Status: "True"}] |
1241 | 1242 |
|
1242 | 1243 | ² = Must remain at zero value (`+k8s:forbidden` validation logic) |
1243 | 1244 |
|
| 1245 | +##### Immutability Patterns and Tags Demonstrated |
| 1246 | + |
| 1247 | +```go |
| 1248 | +type DeploymentSpec struct { |
| 1249 | + // Pattern: Required |
| 1250 | + // +k8s:required |
| 1251 | + // Operation: |
| 1252 | + Replicas *int32 |
| 1253 | +
|
| 1254 | + // ✅ CREATE: replicas = 3 |
| 1255 | + // ✅ UPDATE(modify): replicas = 5 |
| 1256 | + // ❌ UPDATE(clear): replicas = nil |
| 1257 | +} |
| 1258 | +
|
| 1259 | +type PersistentVolumeStatus struct { |
| 1260 | + // Pattern: RequiredOnceSet (Optional, but cannot be cleared once set) |
| 1261 | + // +k8s:optional |
| 1262 | + // +k8s:update=NoClear |
| 1263 | + Phase *PersistentVolumePhase |
| 1264 | +
|
| 1265 | + // Operation: |
| 1266 | + // ✅ CREATE: phase = nil (volume created) |
| 1267 | + // ✅ UPDATE(set): phase = "Available" (set once) |
| 1268 | + // ✅ UPDATE(modify): phase = "Bound" (modify allowed) |
| 1269 | + // ❌ UPDATE(clear): phase = nil (cannot clear due to NoClear) |
| 1270 | +} |
| 1271 | +
|
| 1272 | +type PersistentVolumeClaimSpec struct { |
| 1273 | + // Pattern: Set Once (Optional, can be set at create or update, then immutable) |
| 1274 | + // +k8s:optional |
| 1275 | + // +k8s:update=NoModify |
| 1276 | + // +k8s:update=NoClear |
| 1277 | + VolumeName string |
| 1278 | +
|
| 1279 | + // Operation: |
| 1280 | + // ✅ CREATE: volumeName = "" |
| 1281 | + // ✅ UPDATE(set): volumeName = "pv-123" (set once by PV controller) |
| 1282 | + // ❌ UPDATE(modify): volumeName = "pv-456" (cannot change binding) |
| 1283 | + // ❌ UPDATE(clear): volumeName = "" (cannot unbind) |
| 1284 | +} |
| 1285 | +
|
| 1286 | +type PersistentVolumeSpec struct { |
| 1287 | + // Pattern: Required at creation, then immutable |
| 1288 | + // +k8s:required |
| 1289 | + // +k8s:immutable |
| 1290 | + Capacity ResourceList |
| 1291 | +
|
| 1292 | + // Operation: |
| 1293 | + // ✅ CREATE: capacity = {"storage": "10Gi"} |
| 1294 | + // ❌ UPDATE(modify): capacity = {"storage": "20Gi"} |
| 1295 | +} |
| 1296 | +
|
| 1297 | +type PodSpec struct { |
| 1298 | + // Pattern: Immutable (State decided at creation, cannot transition set/unset or modify) |
| 1299 | + // +k8s:optional |
| 1300 | + // +k8s:immutable // functionally equivalent to NoSet,NoModify,NoClear |
| 1301 | + HostNetwork bool |
| 1302 | + |
| 1303 | + // Operation scenario 1: |
| 1304 | + // ✅ CREATE: hostNetwork = true |
| 1305 | + // ❌ UPDATE(modify): hostNetwork = false |
| 1306 | + // Operation scenario 2: |
| 1307 | + // ✅ CREATE: hostNetwork = false (or unset) |
| 1308 | + // ❌ UPDATE(set/modify): hostNetwork = true |
| 1309 | +} |
| 1310 | +``` |
| 1311 | + |
1244 | 1312 |
|
1245 | 1313 | ### Cross-Field Validation |
1246 | 1314 |
|
|
0 commit comments