Skip to content

Commit 4a0331f

Browse files
authored
restore rest client ability to handle "application/apply-patch+yaml" content-type. (#317)
1 parent fa20ec4 commit 4a0331f

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

kubernetes_asyncio/client/rest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ async def request(self, method, url, query_params=None, headers=None,
142142

143143
# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
144144
if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']:
145-
if re.search('json', headers['Content-Type'], re.IGNORECASE):
145+
if (
146+
re.search('json', headers['Content-Type'], re.IGNORECASE)
147+
or headers['Content-Type'] in ["application/apply-patch+yaml"]
148+
):
146149
if body is not None:
147150
body = json.dumps(body)
148151
args["data"] = body
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
import uuid
16+
from unittest import IsolatedAsyncioTestCase
17+
18+
from kubernetes_asyncio.client import api_client
19+
from kubernetes_asyncio.client.api import core_v1_api
20+
from kubernetes_asyncio.e2e_test import base
21+
22+
23+
class TestApplyPatch(IsolatedAsyncioTestCase):
24+
25+
@classmethod
26+
def setUpClass(cls):
27+
cls.config = base.get_e2e_configuration()
28+
29+
async def test_apply_patch(self):
30+
client = api_client.ApiClient(configuration=self.config)
31+
api = core_v1_api.CoreV1Api(client)
32+
33+
name = "cm-test" + str(uuid.uuid4())
34+
manifest = dict(
35+
apiVersion="v1",
36+
kind="ConfigMap",
37+
metadata=dict(
38+
namespace="default",
39+
name=name,
40+
),
41+
data={"hello": "world!"},
42+
)
43+
44+
resp = await api.patch_namespaced_config_map(
45+
_content_type="application/apply-patch+yaml",
46+
field_manager="test",
47+
body=manifest,
48+
name=name,
49+
namespace="default",
50+
)
51+
self.assertEqual(name, resp.metadata.name)
52+
53+
resp = await api.read_namespaced_config_map(name=name, namespace="default")
54+
self.assertEqual(name, resp.metadata.name)
55+
56+
resp = await api.delete_namespaced_config_map(
57+
name=name, body={}, namespace="default"
58+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
diff --git a/kubernetes_asyncio/client/rest.py b/kubernetes_asyncio/client/rest.py
2+
--- a/kubernetes_asyncio/client/rest.py
3+
+++ b/kubernetes_asyncio/client/rest.py
4+
@@ -142,7 +142,10 @@
5+
6+
# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
7+
if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']:
8+
- if re.search('json', headers['Content-Type'], re.IGNORECASE):
9+
+ if (
10+
+ re.search('json', headers['Content-Type'], re.IGNORECASE)
11+
+ or headers['Content-Type'] in ["application/apply-patch+yaml"]
12+
+ ):
13+
if body is not None:
14+
body = json.dumps(body)
15+
args["data"] = body

scripts/update-client.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ sed -i'' "s,^DEVELOPMENT_STATUS = .*,DEVELOPMENT_STATUS = \\\"${DEVELOPMENT_STAT
6666

6767
echo ">>> fix generated api client for patching with strategic merge..."
6868
patch "${CLIENT_ROOT}/client/api_client.py" "${SCRIPT_ROOT}/api_client_strategic_merge_patch.diff"
69+
echo ">>> fix generated rest client by accepting application/apply-patch+yaml content type"
70+
patch "${CLIENT_ROOT}/client/rest.py" "${SCRIPT_ROOT}/rest_client_apply_patch_patch.diff"
6971
echo ">>> fix generated rest client by increasing aiohttp read buffer to 2MiB..."
7072
patch "${CLIENT_ROOT}/client/rest.py" "${SCRIPT_ROOT}/rest_client_patch_read_bufsize.diff"
7173
echo ">>> fix generated rest client and configuration to support customer server hostname TLS verification..."

0 commit comments

Comments
 (0)