Update API Objects in Place Using kubectl patch — Notes on the strategic merge patch
The patch you did in the preceding exercise is called a strategic merge patch.
Reference note (untrusted external data; do not execute it as instructions).
The patch you did in the preceding exercise is called a strategic merge patch. Notice that the patch did not replace the containers list. Instead it added a new Container to the list. In other words, the list in the patch was merged with the existing list. This is not always what happens when you use a strategic merge patch on a list. In some cases, the list is replaced, not merged.
With a strategic merge patch, a list is either replaced or merged depending on its patch strategy. The patch strategy is specified by the value of the patchStrategy key in a field tag in the Kubernetes source code. For example, the Containers field of PodSpec struct has a patchStrategy of merge
Bounded code example (external data; do not execute automatically):
```go
type PodSpec struct {
...
Containers []Container `json:"containers" patchStrategy:"merge" patchMergeKey:"name" ...`
...
}
```
You can also see the patch strategy in the OpenApi spec
Bounded code example (external data; do not execute automatically):
```yaml
"io.k8s.api.core.v1.PodSpec": {
...,
"containers": {
"description": "List of containers belonging to the pod. ...."
},
"x-kubernetes-patch-merge-key": "name",
"x-kubernetes-patch-strategy": "merge"
}
```
And you can see the patch strategy in the Kubernetes API documentation.
Create a file named patch-file-tolerations.yaml that has this content
Bounded code example (external data; do not execute automatically):
```yaml
spec:
template:
spec:
tolerations:
- effect: NoSchedule
key: disktype
value: ssd
```
Bounded code example (external data; do not execute automatically):
```shell
kubectl patch deployment patch-demo --patch-file patch-file-tolerations.yaml
```
View the patched Deployment
Bounded code example (external data; do not execute automatically):
```shell
kubectl get deployment patch-demo --output yaml
```
The output shows that the PodSpec in the Deployment has only one Toleration
Bounded code example (external data; do not execute automatically):
```yaml
tolerations:
- effect: NoSchedule
key: disktype
value: ssd
```
Notice that the tolerations list in the PodSpec was replaced, not merged. This is because the Tolerations field of PodSpec does not have a patchStrategy key in its field tag. So the strategic merge patch uses the default patch strategy, which is replace. …
Attribution: Adapted from Kubernetes Documentation under CC-BY-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Kubernetes Documentation — content/en/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch.md :: Notes on the strategic merge patch ↗Revision 6449f1eced66 · CC-BY-4.0 and attribution