| 1 | # Merge (2-way) |
| 2 | |
| 3 | 2-way merges fields from a source to a destination, overriding the destination fields |
| 4 | where they differ. |
| 5 | |
| 6 | ### Merge Rules |
| 7 | |
| 8 | Fields are recursively merged using the following rules: |
| 9 | |
| 10 | - scalars |
| 11 | - if present only in the dest, it keeps its value |
| 12 | - if present in the src and is non-null, take the src value -- if `null`, clear it |
| 13 | - example src: `5`, dest: `3` => result: `5` |
| 14 | |
| 15 | - non-associative lists -- lists without a merge key |
| 16 | - if present only in the dest, it keeps its value |
| 17 | - if present in the src and is non-null, take the src value -- if `null`, clear it |
| 18 | - example src: `[1, 2, 3]`, dest: `[a, b, c]` => result: `[1, 2, 3]` |
| 19 | |
| 20 | - map keys and fields -- paired by the map-key / field-name |
| 21 | - if present only in the dest, it keeps its value |
| 22 | - if present only in the src, it is added to the dest |
| 23 | - if the field is present in both the src and dest, and the src value is |
| 24 | `null`, the field is removed from the dest |
| 25 | - if the field is present in both the src and dest, the value is recursively merged |
| 26 | - example src: `{'key1': 'value1', 'key2': 'value2'}`, |
| 27 | dest: `{'key2': 'value0', 'key3': 'value3'}` |
| 28 | => result: `{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}` |
| 29 | |
| 30 | - associative list elements -- paired by the associative key |
| 31 | - if present only in the dest, it keeps its value in the list |
| 32 | - if present only in the src, it is added to the dest list |
| 33 | - if the field is present in both the src and dest, the value is recursively merged |
| 34 | |
| 35 | ### Associative Keys |
| 36 | |
| 37 | Associative keys are used to identify "same" elements within 2 different lists, and merge them. |
| 38 | The following fields are recognized as associative keys: |
| 39 | |
| 40 | [`mountPath`, `devicePath`, `ip`, `type`, `topologyKey`, `name`, `containerPort`] |
| 41 | |
| 42 | Any lists where all of the elements contain associative keys will be merged as associative lists. |
| 43 | |
| 44 | ### Example |
| 45 | |
| 46 | > Source |
| 47 | |
| 48 | apiVersion: apps/v1 |
| 49 | kind: Deployment |
| 50 | spec: |
| 51 | replicas: 3 # scalar |
| 52 | template: |
| 53 | spec: |
| 54 | containers: # associative list -- (name) |
| 55 | - name: nginx |
| 56 | image: nginx:1.7 |
| 57 | command: ['new_run.sh', 'arg1'] # non-associative list |
| 58 | - name: sidecar2 |
| 59 | image: sidecar2:v1 |
| 60 | |
| 61 | > Destination |
| 62 | |
| 63 | apiVersion: apps/v1 |
| 64 | kind: Deployment |
| 65 | spec: |
| 66 | replicas: 1 |
| 67 | template: |
| 68 | spec: |
| 69 | containers: |
| 70 | - name: nginx |
| 71 | image: nginx:1.6 |
| 72 | command: ['old_run.sh', 'arg0'] |
| 73 | - name: sidecar1 |
| 74 | image: sidecar1:v1 |
| 75 | |
| 76 | > Result |
| 77 | |
| 78 | apiVersion: apps/v1 |
| 79 | kind: Deployment |
| 80 | spec: |
| 81 | replicas: 3 # scalar |
| 82 | template: |
| 83 | spec: |
| 84 | containers: # associative list -- (name) |
| 85 | - name: nginx |
| 86 | image: nginx:1.7 |
| 87 | command: ['new_run.sh', 'arg1'] # non-associative list |
| 88 | - name: sidecar1 |
| 89 | image: sidecar1:v1 |
| 90 | - name: sidecar2 |
| 91 | image: sidecar2:v1 |