Patch Request Examples
This section illustrates how to perform a merge patch and a strategic merge patch.
Operation Examples
The following is an example request for creating a ReplicationController.
Example request
{
"apiVersion": "v1",
"kind": "ReplicationController",
"metadata": {
"name": "frontend-controller"
},
"spec": {
"replicas": 2,
"selector": {
"app": "nginx"
},
"template": {
"metadata": {
"labels": {
"app": "nginx"
}
},
"spec": {
"containers": [
{
"name": "redis",
"image": "redis:latest",
"ports": [
{
"containerPort": 80
}
]
}
]
}
}
}
}
Example response
{
"kind": "ReplicationController",
"apiVersion": "v1",
"metadata": {
"name": "frontend-controller",
"namespace": "default",
"selfLink": "/api/v1/namespaces/default/replicationcontrollers/nginx-controller",
"uid": "549b2234-5d46-11e6-aeb9-286ed488fafe",
"resourceVersion": "4110",
"generation": 1,
"creationTimestamp": "2016-08-08T08:58:52Z",
"labels": {
"app": "nginx"
}
},
"spec": {
"replicas": 2,
"selector": {
"app": "nginx"
},
"template": {
"metadata": {
"creationTimestamp": null,
"labels": {
"app": "nginx"
}
},
"spec": {
"containers": [
{
"name": "redis",
"image": "redis:latest",
"ports": [
{
"containerPort": 80,
"protocol": "TCP"
}
],
"resources": {},
"terminationMessagePath": "/dev/termination-log",
"imagePullPolicy": "Always"
}
],
"restartPolicy": "Always",
"terminationGracePeriodSeconds": 30,
"dnsPolicy": "ClusterFirst",
"securityContext": {}
}
}
},
"status": {
"replicas": 0
}
}
- If you use a merge patch to add a container to the template field of a specified ReplicationController, all the existing containers are then replaced by the new container.
Merge patch request
{ "spec": { "template": { "spec": { "containers": [ { "name": "hello-world", "image": "busybox:latest" } ] } } } }Merge patch response
{ "kind": "ReplicationController", "apiVersion": "v1", "metadata": { "name": "frontend-controller", "namespace": "default", "selfLink": "/api/v1/namespaces/default/replicationcontrollers/nginx-controller", "uid": "549b2234-5d46-11e6-aeb9-286ed488fafe", "resourceVersion": "4159", "generation": 2, "creationTimestamp": "2016-08-08T08:58:52Z", "labels": { "app": "nginx" } }, "spec": { "replicas": 2, "selector": { "app": "nginx" }, "template": { "metadata": { "creationTimestamp": null, "labels": { "app": "nginx" } }, "spec": { "containers": [ { "name": "hello-world", "image": "busybox:latest", "resources": {}, "terminationMessagePath": "/dev/termination-log", "imagePullPolicy": "Always" } ], "restartPolicy": "Always", "terminationGracePeriodSeconds": 30, "dnsPolicy": "ClusterFirst", "securityContext": {} } } }, "status": { "replicas": 2, "fullyLabeledReplicas": 2, "observedGeneration": 1 } }The containers list is replaced by the new container values.
- If you use a strategic merge patch, metadata will be added to a resource object and the new metadata then determines which lists should be merged and which ones should not.
Strategic merge patch request
{ "spec": { "template": { "spec": { "containers": [ { "name": "hello-world", "image": "busybox:latest" } ] } } } }'Strategic merge patch response
{ "kind": "ReplicationController", "apiVersion": "v1", "metadata": { "name": "frontend-controller", "namespace": "default", "selfLink": "/api/v1/namespaces/default/replicationcontrollers/nginx-controller", "uid": "f2e048bb-5d46-11e6-aeb9-286ed488fafe", "resourceVersion": "4250", "generation": 2, "creationTimestamp": "2016-08-08T09:03:18Z", "labels": { "app": "nginx" } }, "spec": { "replicas": 2, "selector": { "app": "nginx" }, "template": { "metadata": { "creationTimestamp": null, "labels": { "app": "nginx" } }, "spec": { "containers": [ { "name": "redis", "image": "redis:latest", "ports": [ { "containerPort": 80, "protocol": "TCP" } ], "resources": {}, "terminationMessagePath": "/dev/termination-log", "imagePullPolicy": "Always" }, { "name": "hello-world", "image": "busybox:latest", "resources": {}, "terminationMessagePath": "/dev/termination-log", "imagePullPolicy": "Always" } ], "restartPolicy": "Always", "terminationGracePeriodSeconds": 30, "dnsPolicy": "ClusterFirst", "securityContext": {} } } }, "status": { "replicas": 2, "fullyLabeledReplicas": 2, "observedGeneration": 1 } }The containers list merges with the new content according to the value of the name field.
Last Article: Patch Request Method Operation Description
Next Article: Status Code
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.