
# PATCH请求方法操作示例
本章节主要介绍PATCH请求方法中，"Merge Patch"与"Strategic Merge Patch"类型的操作示例。文中仅为示例，具体以实际为准。
#### 操作示例
以创建Deployment资源对象为例。
**请求示例**
```
{
  "apiVersion": "cci/v2",
  "kind": "Deployment",
  "metadata": {
    "annotations": {
      "description": ""
    },
    "labels": {},
    "name": "nginx"
  },
  "spec": {
    "replicas": 2,
    "selector": {
      "matchLabels": {
        "app": "nginx"
      }
    },
    "template": {
      "metadata": {
        "annotations": {
          "vm.cci.io/pod-size-specs": "2.00_4.0",
          "resource.cci.io/pod-size-specs": "2.00_4.0",
        },
        "labels": {
          "app": "nginx"
        }
      },
      "spec": {
        "containers": [
          {
            "image": "library/nginx:stable-alpine-perl",
            "name": "container-0",
            "resources": {
              "limits": {
                "cpu": "2000m",
                "memory": "4096Mi"
              },
              "requests": {
                "cpu": "2000m",
                "memory": "4096Mi"
              }
            },
            "command": [],
            "lifecycle": {}
          }
        ],
        "dnsPolicy": "",
        "imagePullSecrets": [
          {
            "name": "imagepull-secret"
          }
        ],
        "dnsConfig": {}
      }
    },
    "minReadySeconds": 0,
    "strategy": {
      "type": "RollingUpdate",
      "rollingUpdate": {
        "maxSurge": 0,
        "maxUnavailable": 1
      }
    }
  }
}
```
**响应示例**
```
{
  "kind": "Deployment",
  "apiVersion": "cci/v2",
  "metadata": {
    "name": "nginx",
    "namespace": "zsqwkload2",
    "uid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "resourceVersion": "1080724",
    "generation": 1,
    "creationTimestamp": "2024-04-30T08:48:10Z",
    "managedFields": []
  },
  "spec": {
    "replicas": 2,
    "selector": {
      "matchLabels": {
        "app": "nginx"
      }
    },
    "template": {
      "metadata": {
        "creationTimestamp": null,
        "labels": {
          "app": "nginx"
        },
        "annotations": {
          "deployment.cci.io/pod-template-uid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
          "resource.cci.io/pod-size-specs": "2.00_4.0",
          "vm.cci.io/pod-size-specs": "2.00_4.0"
        }
      },
      "spec": {
        "containers": [
          {
            "name": "container-0",
            "image": "library/nginx:stable-alpine-perl",
            "resources": {
              "limits": {
                "cpu": "2",
                "memory": "4Gi"
              },
              "requests": {
                "cpu": "2",
                "memory": "4Gi"
              }
            },
            "lifecycle": {},
            "terminationMessagePath": "/dev/termination-log",
            "terminationMessagePolicy": "File"
          }
        ],
        "restartPolicy": "Always",
        "terminationGracePeriodSeconds": 30,
        "dnsPolicy": "Default",
        "securityContext": {},
        "imagePullSecrets": [
          {
            "name": "imagepull-secret"
          }
        ],
        "schedulerName": "default-scheduler",
        "dnsConfig": {}
      }
    },
    "strategy": {
      "type": "RollingUpdate",
      "rollingUpdate": {
        "maxUnavailable": 1,
        "maxSurge": 0
      }
    },
    "progressDeadlineSeconds": 600
  },
  "status": {}
}
```
- 如果使用"Merge Patch"类型操作，添加一个容器到指定Deployment的"template"参数中，则其中的整个容器列表将被新添加的容器所替换。 **Merge Patch请求**
  ```
  {
      "spec": {
          "template": {
              "spec": {
                  "containers": [
                      {
                          "name": "hello-world",
                          "image": "busybox:latest"
                      }
                  ]
              }
          }
      }
  }
  ```
  **Merge Patch响应**
  ```
  {
    "kind": "Deployment",
    "apiVersion": "cci/v2",
    "metadata": {
      "name": "nginx",
      "namespace": "zsqwkload2",
      "uid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
      "resourceVersion": "1080882",
      "generation": 2,
      "creationTimestamp": "2024-04-30T08:51:37Z",
      "annotations": {
        "deployment.kubernetes.io/revision": "1",
        "description": "",
        "tenant.cci.io/tenant-id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
      },
      "managedFields": []
    },
    "spec": {
      "replicas": 2,
      "selector": {
        "matchLabels": {
          "app": "nginx"
        }
      },
      "template": {
        "metadata": {
          "creationTimestamp": null,
          "labels": {
            "app": "nginx"
          },
          "annotations": {
            "deployment.cci.io/pod-template-uid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            "resource.cci.io/pod-size-specs": "2.00_4.0",
            "vm.cci.io/pod-size-specs": "2.00_4.0"
          }
        },
        "spec": {
          "containers": [
            {
              "name": "hello-world",
              "image": "busybox:latest",
              "resources": {},
              "terminationMessagePath": "/dev/termination-log",
              "terminationMessagePolicy": "File"
            }
          ],
          "restartPolicy": "Always",
          "terminationGracePeriodSeconds": 30,
          "dnsPolicy": "Default",
          "securityContext": {},
          "imagePullSecrets": [
            {
              "name": "imagepull-secret"
            }
          ],
          "schedulerName": "default-scheduler",
          "dnsConfig": {}
        }
      },
      "strategy": {
        "type": "RollingUpdate",
        "rollingUpdate": {
          "maxUnavailable": 1,
          "maxSurge": 0
        }
      },
      "progressDeadlineSeconds": 600
    },
    "status": {
      "observedGeneration": 1,
      "replicas": 2,
      "updatedReplicas": 2,
      "unavailableReplicas": 2,
      "conditions": [
        {
          "type": "Available",
          "status": "False",
          "lastUpdateTime": "2024-04-30T08:51:37Z",
          "lastTransitionTime": "2024-04-30T08:51:37Z",
          "reason": "MinimumReplicasUnavailable",
          "message": "Deployment does not have minimum availability."
        },
        {
          "type": "Progressing",
          "status": "True",
          "lastUpdateTime": "2024-04-30T08:51:37Z",
          "lastTransitionTime": "2024-04-30T08:51:37Z",
          "reason": "ReplicaSetUpdated",
          "message": "ReplicaSet \"nginx-78f5ff5dff\" is progressing."
        }
      ]
    }
  }
  ```
  其中"containers"参数列表被新添加的内容所替换。
  

- 而使用"Strategic Merge Patch"类型操作，是添加元数据到资源对象中，并通过这些新元数据来决定各个列表是否需要被合并。 **Strategic Merge Patch请求**
  ```
  {
      "spec": {
          "template": {
              "spec": {
                  "containers": [
                      {
                          "name": "hello-world22",
                          "image": "busybox:latest"
                      }
                  ]
              }
          }
      }
  }
  ```
  **Strategic Merge Patch响应**
  ```
  {
    "kind": "Deployment",
    "apiVersion": "cci/v2",
    "metadata": {
      "name": "nginx",
      "namespace": "zsqwkload2",
      "uid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
      "resourceVersion": "1080952",
      "generation": 3,
      "creationTimestamp": "2024-04-30T08:51:37Z",
      "annotations": {
        "deployment.kubernetes.io/revision": "2",
        "description": "",
        "tenant.cci.io/tenant-id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
      },
      "managedFields": []
    },
    "spec": {
      "replicas": 2,
      "selector": {
        "matchLabels": {
          "app": "nginx"
        }
      },
      "template": {
        "metadata": {
          "creationTimestamp": null,
          "labels": {
            "app": "nginx"
          },
          "annotations": {
            "deployment.cci.io/pod-template-uid": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            "resource.cci.io/pod-size-specs": "2.00_4.0",
            "vm.cci.io/pod-size-specs": "2.00_4.0"
          }
        },
        "spec": {
          "containers": [
            {
              "name": "hello-world22",
              "image": "busybox:latest",
              "resources": {},
              "terminationMessagePath": "/dev/termination-log",
              "terminationMessagePolicy": "File"
            },
            {
              "name": "hello-world",
              "image": "busybox:latest",
              "resources": {},
              "terminationMessagePath": "/dev/termination-log",
              "terminationMessagePolicy": "File"
            }
          ],
          "restartPolicy": "Always",
          "terminationGracePeriodSeconds": 30,
          "dnsPolicy": "Default",
          "securityContext": {},
          "imagePullSecrets": [
            {
              "name": "imagepull-secret"
            }
          ],
          "schedulerName": "default-scheduler",
          "dnsConfig": {}
        }
      },
      "strategy": {
        "type": "RollingUpdate",
        "rollingUpdate": {
          "maxUnavailable": 1,
          "maxSurge": 0
        }
      },
      "progressDeadlineSeconds": 600
    },
    "status": {
      "observedGeneration": 2,
      "replicas": 2,
      "updatedReplicas": 1,
      "readyReplicas": 1,
      "availableReplicas": 1,
      "unavailableReplicas": 1,
      "conditions": [
        {
          "type": "Available",
          "status": "True",
          "lastUpdateTime": "2024-04-30T08:52:57Z",
          "lastTransitionTime": "2024-04-30T08:52:57Z",
          "reason": "MinimumReplicasAvailable",
          "message": "Deployment has minimum availability."
        },
        {
          "type": "Progressing",
          "status": "True",
          "lastUpdateTime": "2024-04-30T08:52:57Z",
          "lastTransitionTime": "2024-04-30T08:51:37Z",
          "reason": "ReplicaSetUpdated",
          "message": "ReplicaSet \"nginx-5dc68ffd6d\" is progressing."
        }
      ]
    }
  }
  ```
  其中"containers"参数列表与新添加的内容合并，而不是替换，合并的依据为"name"域的值。
  
 
