You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the output, you can see that it is not possible to set `type` as `Recreate` when a value is defined for `spec.strategy.rollingUpdate`:
321
+
322
+
```shell
323
+
The Deployment "retainkeys-demo" is invalid: spec.strategy.rollingUpdate: Forbidden: may not be specified when strategy `type` is 'Recreate'
324
+
```
325
+
326
+
The way to remove the value for `spec.strategy.rollingUpdate` when updating the value for `type` is to use the `retainKeys` strategy for the strategic merge.
327
+
328
+
Create another file named `patch-file-retainkeys.yaml` that has this content:
329
+
330
+
```yaml
331
+
spec:
332
+
strategy:
333
+
$retainKeys:
334
+
- type
335
+
type: Recreate
336
+
```
337
+
338
+
With this patch, we indicate that we want to retain only the `type` key of the `strategy` object. Thus, the `rollingUpdate` will be removed during the patch operation.
kubectl get deployment retainkeys-demo --output yaml
355
+
```
356
+
357
+
The output shows that the strategy object in the Deployment does not contain the `rollingUpdate` key anymore:
358
+
359
+
```shell
360
+
spec:
361
+
strategy:
362
+
type: Recreate
363
+
template:
364
+
```
365
+
366
+
### Notes on the strategic merge patch using the retainKeys strategy
367
+
368
+
The patch you did in the preceding exercise is called a *strategic merge patch with retainKeys strategy*. This method introduces a new directive `$retainKeys` that has the following strategies:
369
+
370
+
- It contains a list of strings.
371
+
- All fields needing to be preserved must be present in the `$retainKeys` list.
372
+
- The fields that are present will be merged with live object.
373
+
- All of the missing fields will be cleared when patching.
374
+
- All fields in the `$retainKeys` list must be a superset or the same as the fields present in the patch.
375
+
376
+
The `retainKeys` strategy does not work for all objects. It only works when the value of the `patchStrategy` key in a field tag in the Kubernetes source code contains `retainKeys`. For example, the `Strategy` field of the `DeploymentSpec` struct has a `patchStrategy` of `retainKeys`:
You can also see the `retainKeys` strategy in the [OpenApi spec](https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json):
0 commit comments