Skip to content

Commit 4bf47d1

Browse files
author
Phillip Wittrock
authored
Merge pull request #610 from mengqiy/multi_fields_merge_keys
Proposal: support multi-fields merge key
2 parents 44467c5 + 84c680b commit 4bf47d1

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
Patches for fields that have multiple fields in the merge key must include all of the fields of the merge key in the patch.
44+
45+
If a new API uses multi-fields merge key, all the fields of the merge key are required to present.
46+
Otherwise, the server will reject the patch.
47+
48+
E.g.
49+
foo and bar are the merge keys.
50+
51+
Live list:
52+
```yaml
53+
list:
54+
- foo: a
55+
bar: x
56+
other: 1
57+
- foo: a
58+
bar: y
59+
other: 2
60+
- foo: b
61+
bar: x
62+
other: 3
63+
```
64+
65+
Patch 1:
66+
```yaml
67+
list:
68+
- foo: a # field 1 of merge key
69+
bar: x # field 2 of merge key
70+
other: 4
71+
another: val
72+
```
73+
74+
Result after merging patch 1:
75+
```yaml
76+
list:
77+
- foo: a
78+
bar: x
79+
other: 4
80+
another: val
81+
- foo: a
82+
bar: y
83+
other: 2
84+
- foo: b
85+
bar: x
86+
other: 3
87+
```
88+
89+
Patch 2:
90+
```yaml
91+
list:
92+
- $patch: delete
93+
foo: a # field 1 of merge key
94+
bar: x # field 2 of merge key
95+
```
96+
97+
Result after merging patch 2:
98+
```yaml
99+
list:
100+
- foo: a
101+
bar: y
102+
other: 2
103+
- foo: b
104+
bar: x
105+
other: 3
106+
```
107+
108+
### Strategic Merge Patch pkg
109+
110+
We will add logic to support
111+
- returning a list of fields instead of one single field when looking up merge key.
112+
- merging list respecting a list of fields as merge key.
113+
114+
### Open API
115+
116+
Open API will not be affected,
117+
since multi-fields merge key is still in one single string as an extension in Open API spec.
118+
119+
### Docs
120+
121+
Document that the developer should make sure the merge key can uniquely identify an entry in all cases.
122+
123+
## Version Skew and Backward Compatibility
124+
125+
It is fully backward compatibility,
126+
because there are no patch format changes and no changes to the existing APIs.

0 commit comments

Comments
 (0)