Help Center/ Cloud Container Instance/ API Reference/ Appendix/ Patch Request Method Operation Examples
Updated on 2023-12-21 GMT+08:00

Patch Request Method Operation Examples

This section provides examples of Merge Patch and Strategic Merge Patch method operations.

Operation Examples

The following is an example request for creating a deployment.

Example request

{
    "apiVersion": "v1",
    "kind": "Deployment",
    "metadata": {
        "name": "nginx"
    },
    "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",                
                "dnsPolicy": "ClusterFirst",
                "securityContext": {}
            }
        }
    },
    "status": {
        "replicas": 0
    }
}
  • If you use Merge Patch to add a container to the template field of a specified deployment, the list of existing containers is 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",                                
                    "dnsPolicy": "ClusterFirst",
                    "securityContext": {}
                }
            }
        },
        "status": {
            "replicas": 2,
            "fullyLabeledReplicas": 2,
            "observedGeneration": 1
        }
    }

    The containers list is replaced by the new container.

  • If you use Strategic Merge Patch to add metadata to a resource object, the new metadata then determines which list should be merged and which 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",                                
                    "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.