Skip to content

Commit

Permalink
Fallback to merge patch
Browse files Browse the repository at this point in the history
Add fallback to merge patch if strategic merge patch is unavailable, as
is the case for the custom objects API.
  • Loading branch information
JacobHenner committed Dec 18, 2024
1 parent 84e4e28 commit 043429a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
3 changes: 2 additions & 1 deletion examples/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ async def main():
SERVICE_NAME,
SERVICE_NS,
patch,
_content_type="application/merge-patch+json", # required to force merge patch
# required to force merge patch when strategic merge patch would otherwise be used
_content_type="application/merge-patch+json",
)


Expand Down
10 changes: 7 additions & 3 deletions kubernetes_asyncio/client/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,13 @@ def select_header_content_type(self, content_types, method=None, body=None):
if ('application/json-patch+json' in content_types and
isinstance(body, list)):
return 'application/json-patch+json'
if ('application/strategic-merge-patch+json' in content_types and
(isinstance(body, dict) or hasattr(body, "to_dict"))):
return 'application/strategic-merge-patch+json'
if isinstance(body, dict) or hasattr(body, "to_dict"):
if 'application/strategic-merge-patch+json' in content_types:
return 'application/strategic-merge-patch+json'
elif 'application/merge-patch+json' in content_types:
# Intended for cases where strategic merge patch is not
# supported, like when patching custom objects.
return 'application/merge-patch+json'

if 'application/json' in content_types or '*/*' in content_types:
return 'application/json'
Expand Down
22 changes: 13 additions & 9 deletions scripts/api_client_strategic_merge_patch.diff
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
--- /tmp/api_client.py 2024-02-25 20:40:28.143350042 +0100
+++ kubernetes_asyncio/client/api_client.py 2024-02-25 20:40:32.954201652 +0100
@@ -535,10 +535,13 @@
--- /tmp/api_client.py 2024-12-18 03:36:59.552742383 +0000
+++ kubernetes_asyncio/client/api_client.py 2024-12-18 03:36:11.062928089 +0000
@@ -535,10 +535,17 @@

content_types = [x.lower() for x in content_types]

- if (method == 'PATCH' and
- 'application/json-patch+json' in content_types and
- isinstance(body, list)):
Expand All @@ -12,9 +12,13 @@
+ if ('application/json-patch+json' in content_types and
+ isinstance(body, list)):
+ return 'application/json-patch+json'
+ if ('application/strategic-merge-patch+json' in content_types and
+ (isinstance(body, dict) or hasattr(body, "to_dict"))):
+ return 'application/strategic-merge-patch+json'

+ if isinstance(body, dict) or hasattr(body, "to_dict"):
+ if 'application/strategic-merge-patch+json' in content_types:
+ return 'application/strategic-merge-patch+json'
+ elif 'application/merge-patch+json' in content_types:
+ # Intended for cases where strategic merge patch is not
+ # supported, like when patching custom objects.
+ return 'application/merge-patch+json'

if 'application/json' in content_types or '*/*' in content_types:
return 'application/json'
2 changes: 1 addition & 1 deletion scripts/update-client.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pushd "${CLIENT_ROOT}" > /dev/null
CLIENT_ROOT=`pwd`
popd > /dev/null

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

SETTING_FILE="${TEMP_FOLDER}/settings"
Expand Down

0 comments on commit 043429a

Please sign in to comment.