策略合并 patch

对于策略性合并 patch,列表可以根据其 patch 策略进行替换或合并。 patch 策略由 Kubernetes 源代码中字段标记中的 patchStrategy 键的值指定。 例如,PodSpec 结构体的 Containers 字段的 patchStrategymerge

  1. type PodSpec struct {
  2. ...
  3. Containers []Container `json:"containers" patchStrategy:"merge" patchMergeKey:"name" ...`

策略合并 patch 操作使用默认的 patch 策略,也就是 replace

  1. type PodSpec struct {
  2. ...
  3. Tolerations []Toleration `json:"tolerations,omitempty" protobuf:"bytes,22,opt,name=tolerations"`

JSON 合并 patch

https://erosb.github.io/post/json-patch-vs-merge-patch/

As a quick example (taken from the spec) if we have the following document:

  1. {
  2. "a": "b",
  3. "c": {
  4. "d": "e",
  5. "f": "g"
  6. }
  7. }

Then we can run the following patch on it:

  1. {
  2. "a":"z",
  3. "c": {
  4. "f": null
  5. }
  6. }

which will change the value of "a" to "z" and will delete the "f" key.

JSON patch

As a short example lets consider the following JSON document:

  1. {
  2. "users" : [
  3. { "name" : "Alice" , "email" : "alice@example.org" },
  4. { "name" : "Bob" , "email" : "bob@example.org" }
  5. ]
  6. }

We can run the following patch on it, which changes Alice’s email address then adds a new element to the array:

  1. [
  2. {
  3. "op" : "replace" ,
  4. "path" : "/users/0/email" ,
  5. "value" : "alice@wonderland.org"
  6. },
  7. {
  8. "op" : "add" ,
  9. "path" : "/users/-" ,
  10. "value" : {
  11. "name" : "Christine",
  12. "email" : "christine@example.org"
  13. }
  14. }
  15. ]

The result will be:

  1. {
  2. "users" : [
  3. { "name" : "Alice" , "email" : "alice@wonderland.org" },
  4. { "name" : "Bob" , "email" : "bob@example.org" },
  5. { "name" : "Christine" , "email" : "christine@example.org" }
  6. ]
  7. }