Skip to content

Commit 4766413

Browse files
author
ymqytw
committed
support multi-fields merge key
1 parent ea894bc commit 4766413

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Multi-fields Merge Key in Strategic Merge Patch
2+
3+
## Abstract
4+
5+
Support multi-fields merge key in Strategic Merge Patch.
6+
7+
## Background
8+
9+
Strategic Merge Patch is covered in this [doc](https://github.com/kubernetes/community/blob/master/contributors/devel/strategic-merge-patch.md).
10+
In Strategic Merge Patch, we use Merge Key to identify the entries in the list of non-primitive types.
11+
It must always be present and unique to perform the merge on the list of non-primitive types,
12+
and will be preserved.
13+
14+
The merge key exists in the struct tag (e.g. in [types.go](https://github.com/kubernetes/kubernetes/blob/5a9759b0b41d5e9bbd90d5a8f3a4e0a6c0b23b47/pkg/api/v1/types.go#L2831))
15+
and the [OpenAPI spec](https://github.com/kubernetes/kubernetes/blob/master/api/openapi-spec/swagger.json).
16+
17+
## Motivation
18+
19+
The current implementation only support a single field as merge key.
20+
For some element Kinds, the identity is actually defined using multiple fields.
21+
[Service port](https://github.com/kubernetes/kubernetes/issues/39188) is an evidence indicating that
22+
we need to support multi-fields Merge Key.
23+
24+
## Scope
25+
26+
This proposal only covers how we introduce ability to support multi-fields merge key for strategic merge patch.
27+
It will cover how we support new APIs with multi-fields merge key.
28+
29+
This proposal does NOT cover how we change the merge keys from one single field to multi-fields
30+
for existing APIs without breaking backward compatibility,
31+
e.g. we are not addressing the service port issue mentioned above.
32+
That part will be addressed by [#476](https://github.com/kubernetes/community/pull/476).
33+
34+
## Proposed Change
35+
36+
### API Change
37+
38+
If a merge key has multiple fields, it will be a string of merge key fields seperated by ",", i.e. `patchMergeKey:"<key1>,<key2>,<key3>"`.
39+
40+
If a merge key only has one field, it will be the same as before, i.e. `patchMergeKey:"<key1>"`.
41+
42+
There are no patch format changes.
43+
Additional fields are just extra fields in the patch.
44+
45+
E.g.
46+
foo and bar are the merge keys.
47+
48+
Live list:
49+
```yaml
50+
list:
51+
- foo: a
52+
bar: x
53+
other: 1
54+
- foo: a
55+
bar: y
56+
other: 2
57+
- foo: b
58+
bar: x
59+
other: 3
60+
```
61+
62+
Patch 1:
63+
```yaml
64+
list:
65+
- foo: a # field 1 of merge key
66+
bar: x # field 2 of merge key
67+
other: 4
68+
another: val
69+
```
70+
71+
Result after merging patch 1:
72+
```yaml
73+
list:
74+
- foo: a
75+
bar: x
76+
other: 4
77+
another: val
78+
- foo: a
79+
bar: y
80+
other: 2
81+
- foo: b
82+
bar: x
83+
other: 3
84+
```
85+
86+
Patch 2:
87+
```yaml
88+
list:
89+
- $patch: delete
90+
foo: a # field 1 of merge key
91+
bar: x # field 2 of merge key
92+
```
93+
94+
Result after merging patch 2:
95+
```yaml
96+
list:
97+
- foo: a
98+
bar: y
99+
other: 2
100+
- foo: b
101+
bar: x
102+
other: 3
103+
```
104+
105+
### Strategic Merge Patch pkg
106+
107+
We will add logic to support
108+
- returning a list of fields instead of one single field when looking up merge key.
109+
- merging list respecting a list of fields as merge key.
110+
111+
### Open API
112+
113+
Open API will not be affected,
114+
since multi-fields merge key is still in one single string as an extension in Open API spec.
115+
116+
### Docs
117+
118+
Document that the developer should make sure the merge key can uniquely identify an entry in all cases.
119+
120+
## Version Skew and Backward Compatibility
121+
122+
It is fully backward compatibility,
123+
because there are no patch format changes and no changes to the existing APIs.

0 commit comments

Comments
 (0)