Skip to content

Commit b57fd8c

Browse files
committed
Fallback to merge patch
Add fallback to merge patch if strategic merge patch is unavailable, as is the case for the custom objects API.
1 parent 84e4e28 commit b57fd8c

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

examples/patch.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async def main():
9999
SERVICE_NAME,
100100
SERVICE_NS,
101101
patch,
102-
_content_type="application/merge-patch+json", # required to force merge patch
102+
_content_type="application/merge-patch+json", # required to force merge patch when strategic merge patch would otherwise be used
103103
)
104104

105105

kubernetes_asyncio/client/api_client.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -536,12 +536,16 @@ def select_header_content_type(self, content_types, method=None, body=None):
536536
content_types = [x.lower() for x in content_types]
537537

538538
if method == 'PATCH':
539-
if ('application/json-patch+json' in content_types and
540-
isinstance(body, list)):
539+
if 'application/json-patch+json' in content_types and
540+
isinstance(body, list):
541541
return 'application/json-patch+json'
542-
if ('application/strategic-merge-patch+json' in content_types and
543-
(isinstance(body, dict) or hasattr(body, "to_dict"))):
544-
return 'application/strategic-merge-patch+json'
542+
if isinstance(body, dict) or hasattr(body, "to_dict"):
543+
if 'application/strategic-merge-patch+json' in content_types:
544+
return 'application/strategic-merge-patch+json'
545+
elif 'application/merge-patch+json' in content_types:
546+
# Intended for cases where strategic merge patch is not
547+
# supported, like when patching custom objects.
548+
return 'application/merge-patch+json'
545549

546550
if 'application/json' in content_types or '*/*' in content_types:
547551
return 'application/json'
+15-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
--- /tmp/api_client.py 2024-02-25 20:40:28.143350042 +0100
2-
+++ kubernetes_asyncio/client/api_client.py 2024-02-25 20:40:32.954201652 +0100
3-
@@ -535,10 +535,13 @@
4-
1+
--- /tmp/api_client.py 2024-12-18 03:36:59.552742383 +0000
2+
+++ kubernetes_asyncio/client/api_client.py 2024-12-18 03:36:11.062928089 +0000
3+
@@ -535,10 +535,17 @@
4+
55
content_types = [x.lower() for x in content_types]
6-
6+
77
- if (method == 'PATCH' and
88
- 'application/json-patch+json' in content_types and
99
- isinstance(body, list)):
1010
- return 'application/json-patch+json'
1111
+ if method == 'PATCH':
12-
+ if ('application/json-patch+json' in content_types and
13-
+ isinstance(body, list)):
12+
+ if 'application/json-patch+json' in content_types and
13+
+ isinstance(body, list):
1414
+ return 'application/json-patch+json'
15-
+ if ('application/strategic-merge-patch+json' in content_types and
16-
+ (isinstance(body, dict) or hasattr(body, "to_dict"))):
17-
+ return 'application/strategic-merge-patch+json'
18-
15+
+ if isinstance(body, dict) or hasattr(body, "to_dict"):
16+
+ if 'application/strategic-merge-patch+json' in content_types:
17+
+ return 'application/strategic-merge-patch+json'
18+
+ elif 'application/merge-patch+json' in content_types:
19+
+ # Intended for cases where strategic merge patch is not
20+
+ # supported, like when patching custom objects.
21+
+ return 'application/merge-patch+json'
22+
1923
if 'application/json' in content_types or '*/*' in content_types:
2024
return 'application/json'

scripts/update-client.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pushd "${CLIENT_ROOT}" > /dev/null
3535
CLIENT_ROOT=`pwd`
3636
popd > /dev/null
3737

38-
TEMP_FOLDER=$(mktemp -d)
38+
TEMP_FOLDER=$(mktemp -d)
3939
trap "rm -rf ${TEMP_FOLDER}" EXIT SIGINT
4040

4141
SETTING_FILE="${TEMP_FOLDER}/settings"

0 commit comments

Comments
 (0)