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.
This commit is contained in:
Mitsuru Kariya 2022-06-24 11:02:00 +09:00 committed by Mitsuru Kariya
parent 2677e9c810
commit 5529dedcb3
No known key found for this signature in database
GPG Key ID: D04A2FC72FE3C396
3 changed files with 6 additions and 5 deletions

View File

@ -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 @@ class TestDynamicClient(unittest.TestCase):
'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)

View File

@ -157,7 +157,8 @@ class RESTClientObject(object):
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'] = \

View File

@ -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'] = \