From 5529dedcb35fd076301b8c6c894647aca593916c Mon Sep 17 00:00:00 2001 From: Mitsuru Kariya Date: Fri, 24 Jun 2022 11:02:00 +0900 Subject: [PATCH] Fix DynamicClient.server_side_apply DynamicClient.server_side_apply is designed to accept a dict or a ResourceInstance as body. However, if a dict or a ResourceInstance is passed actually, an error occurs because RESTClientObject.rest cannot interpret the Content-Type application/apply-patch+yaml. So, modify RESTClientObject.rest to treat application/apply-patch+yaml as other json-based Content-Types. --- kubernetes/base/dynamic/test_client.py | 4 +--- kubernetes/client/rest.py | 3 ++- scripts/rest_client_patch.diff | 4 +++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/kubernetes/base/dynamic/test_client.py b/kubernetes/base/dynamic/test_client.py index f0ca26b549..1c9fa6d94c 100644 --- a/kubernetes/base/dynamic/test_client.py +++ b/kubernetes/base/dynamic/test_client.py @@ -15,7 +15,6 @@ import time import unittest import uuid -import json from kubernetes.e2e_test import base from kubernetes.client import api_client @@ -527,9 +526,8 @@ def test_server_side_apply_api(self): 'ports': [{'containerPort': 80, 'protocol': 'TCP'}]}]}} - body = json.dumps(pod_manifest).encode() resp = api.server_side_apply( - name=name, namespace='default', body=body, + namespace='default', body=pod_manifest, field_manager='kubernetes-unittests', dry_run="All") self.assertEqual('kubernetes-unittests', resp.metadata.managedFields[0].manager) diff --git a/kubernetes/client/rest.py b/kubernetes/client/rest.py index f6e476c271..3fc0eb92a3 100644 --- a/kubernetes/client/rest.py +++ b/kubernetes/client/rest.py @@ -157,7 +157,8 @@ def request(self, method, url, query_params=None, headers=None, if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']: if query_params: url += '?' + urlencode(query_params) - if re.search('json', headers['Content-Type'], re.IGNORECASE): + if (re.search('json', headers['Content-Type'], re.IGNORECASE) or + headers['Content-Type'] == 'application/apply-patch+yaml'): if headers['Content-Type'] == 'application/json-patch+json': if not isinstance(body, list): headers['Content-Type'] = \ diff --git a/scripts/rest_client_patch.diff b/scripts/rest_client_patch.diff index b47e6e0a78..2a6c0bca93 100644 --- a/scripts/rest_client_patch.diff +++ b/scripts/rest_client_patch.diff @@ -5,7 +5,9 @@ index 65fbe95..e174317 100644 @@ -152,6 +152,10 @@ class RESTClientObject(object): if query_params: url += '?' + urlencode(query_params) - if re.search('json', headers['Content-Type'], re.IGNORECASE): +- if re.search('json', headers['Content-Type'], re.IGNORECASE): ++ if (re.search('json', headers['Content-Type'], re.IGNORECASE) or ++ headers['Content-Type'] == 'application/apply-patch+yaml'): + if headers['Content-Type'] == 'application/json-patch+json': + if not isinstance(body, list): + headers['Content-Type'] = \