Skip to content

Commit 060b30b

Browse files
author
ymqytw
committed
address comments
1 parent b34d96c commit 060b30b

File tree

1 file changed

+52
-33
lines changed

1 file changed

+52
-33
lines changed

contributors/design-proposals/preserve-order-in-strategic-merge-patch.md

+52-33
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,35 @@ issue [40373](https://github.com/kubernetes/kubernetes/issues/40373).
1717

1818
## Proposed Change
1919

20+
We will use the following notions through the doc.
21+
Notion:
22+
list to be merged: same as live list, which is the list current in the server.
23+
parallel list: the list with `$setElementOrder` directive in the patch.
24+
patch list: the list in the patch that contains the value changes.
25+
2026
Changes are all in strategic merge patch package.
2127
The proposed solution is similar to the solution used for deleting elements from lists of primitives.
2228

2329
Add to the current patch, a directive ($setElementOrder) containing a list of element keys -
2430
either the patch merge key, or for primitives the value. When applying the patch,
2531
the server ensures that the relative ordering of elements matches the directive.
2632

27-
Here is an simple example:
33+
The server will reject the patch if it doesn't satisfy the following 2 requirement.
34+
- the relative order of any two items in the `$setElementOrder` list
35+
matches that in the patch list if they present.
36+
- the items in the patch list must be a subset or the same as the `$setElementOrder` list if the directive presents.
37+
38+
The relative order of two items are determined by the following order:
39+
40+
1. relative order in the $setElementOrder if both items are present
41+
2. else relative order in the patch if both items are present
42+
3. else relative order in the server-side list if both items are present
43+
4. else append to the end
44+
45+
If the relative order of the live config in the server is different from the order of the parallel list,
46+
the user's patch will always override the order in the server.
47+
48+
Here is an simple example of the patch format:
2849

2950
Suppose we have a type called list. The patch will look like below.
3051
The order from the parallel list ($setElementOrder/list) will be respected.
@@ -33,40 +54,32 @@ The order from the parallel list ($setElementOrder/list) will be respected.
3354
$setElementOrder/list:
3455
- A
3556
- B
57+
- C
3658
list:
37-
- B
3859
- A
60+
- C
3961
```
4062
41-
All the items in the server's live list but not in the parallel list will be append to the end of the parallel list.
63+
All the items in the server's live list but not in the parallel list will be come before the parallel list.
4264
The relative order between these appended items are kept.
43-
If the relative order of the live config in the server is different from the order of the parallel list,
44-
the user's patch will always override the order in the server.
4565
4666
The patched list will look like:
4767
4868
```
4969
mergingList:
70+
- serverOnlyItem1 \
71+
... |===> items in the server's list but not in the parallel list
72+
- serverOnlyItemM /
5073
- parallelListItem1 \
5174
... |===> items from the parallel list
5275
- parallelListItemN /
53-
- otherItem1 \
54-
... |===> items in the server's list but not in the parallel list
55-
- otherItemN /
5676
```
5777
5878
### When $setElementOrder is not present and patching a list
5979
6080
The new directive $setElementOrder is optional.
61-
The new server applying a patch request without the directive will be
62-
a best-effort operation to keep the order of elements that are already in the list:
63-
64-
For changes and deletions, the server will just merge the change without sorting them.
65-
So the items in the list will retain the order.
66-
67-
For additions, the items will be appended to the end of the list.
68-
69-
So it changes a little from previous releases, but is still fully backward compatible with old clients.
81+
When the $setElementOrder is missing,
82+
relative order in the patch list will be respected.
7083
7184
Examples where A and C have been changed, B has been deleted and D has been added.
7285
@@ -75,26 +88,26 @@ Patch:
7588
```yaml
7689
list:
7790
- A'
78-
- $patch: delete
79-
B
91+
- B'
8092
- D
8193
```
8294
8395
Live:
8496
8597
```yaml
8698
list:
87-
- C
8899
- B
100+
- C
89101
- A
90102
```
91103
92104
Result:
93105
94106
```yaml
95107
list:
96-
- C
97-
- A
108+
- C # server-only item comes first
109+
- A'
110+
- B'
98111
- D
99112
```
100113
@@ -132,10 +145,12 @@ list:
132145
### When the list to be merged contains elements not found in `$setElementOrder`
133146

134147
If the list to be merged contains elements not found in $setElementOrder,
135-
they will come after all elements defined in $setElementOrder, but keep their relative ordering.
148+
they will come before all elements defined in $setElementOrder, but keep their relative ordering.
136149

137150
Example where A & B have been changed:
138151

152+
Patch:
153+
139154
```yaml
140155
$setElementOrder/list:
141156
- A
@@ -145,6 +160,8 @@ list:
145160
- B
146161
```
147162

163+
Live:
164+
148165
```yaml
149166
list:
150167
- C
@@ -158,11 +175,11 @@ Result:
158175

159176
```yaml
160177
list:
161-
- A
162-
- B
163178
- C
164179
- D
165180
- E
181+
- A
182+
- B
166183
```
167184

168185
### When `$setElementOrder` contains elements not found in the list to be merged
@@ -207,7 +224,9 @@ Patch requests without the directive will change a little,
207224
but still be fully backward compatible.
208225

209226
### kubectl
210-
If an old kubectl sends a old patch to a new server, the server will not preserve the order which behave as an old server.
227+
If an old kubectl sends a old patch to a new server,
228+
the server will honor the order in the list as mentioned above.
229+
The behavior is a little different from before but is not a breaking change.
211230

212231
If a new kubectl sends a new patch to an old server, the server doesn't recognise the parallel list and will drop it.
213232
So it will behave the same as before.
@@ -284,20 +303,20 @@ env:
284303
value: new-env
285304
```
286305

287-
After server applying the patch:
306+
After server applying the new patch:
288307

289308
```yaml
290309
env:
310+
- name: ENV5
311+
value: server-added-2
312+
- name: ENV4
313+
value: server-added-1
291314
- name: ENV1
292315
value: foo
293316
- name: ENV2
294317
value: bar
295318
- name: ENV6
296319
value: new-env
297-
- name: ENV5
298-
value: server-added-2
299-
- name: ENV4
300-
value: server-added-1
301320
```
302321

303322
### List of Primitives
@@ -361,11 +380,11 @@ After server applying the patch:
361380

362381
```yaml
363382
finalizers:
383+
- e
384+
- d
364385
- a
365386
- b
366387
- f
367-
- e
368-
- d
369388
```
370389

371390

0 commit comments

Comments
 (0)