Skip to content

Commit

Permalink
restore rest client ability to handle "application/apply-patch+yaml" …
Browse files Browse the repository at this point in the history
…content-type. (#317)
  • Loading branch information
Meallia authored Jun 27, 2024
1 parent fa20ec4 commit 4a0331f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
5 changes: 4 additions & 1 deletion kubernetes_asyncio/client/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ async def request(self, method, url, query_params=None, headers=None,

# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']:
if re.search('json', headers['Content-Type'], re.IGNORECASE):
if (
re.search('json', headers['Content-Type'], re.IGNORECASE)
or headers['Content-Type'] in ["application/apply-patch+yaml"]
):
if body is not None:
body = json.dumps(body)
args["data"] = body
Expand Down
58 changes: 58 additions & 0 deletions kubernetes_asyncio/e2e_test/test_apply_patch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import uuid
from unittest import IsolatedAsyncioTestCase

from kubernetes_asyncio.client import api_client
from kubernetes_asyncio.client.api import core_v1_api
from kubernetes_asyncio.e2e_test import base


class TestApplyPatch(IsolatedAsyncioTestCase):

@classmethod
def setUpClass(cls):
cls.config = base.get_e2e_configuration()

async def test_apply_patch(self):
client = api_client.ApiClient(configuration=self.config)
api = core_v1_api.CoreV1Api(client)

name = "cm-test" + str(uuid.uuid4())
manifest = dict(
apiVersion="v1",
kind="ConfigMap",
metadata=dict(
namespace="default",
name=name,
),
data={"hello": "world!"},
)

resp = await api.patch_namespaced_config_map(
_content_type="application/apply-patch+yaml",
field_manager="test",
body=manifest,
name=name,
namespace="default",
)
self.assertEqual(name, resp.metadata.name)

resp = await api.read_namespaced_config_map(name=name, namespace="default")
self.assertEqual(name, resp.metadata.name)

resp = await api.delete_namespaced_config_map(
name=name, body={}, namespace="default"
)
15 changes: 15 additions & 0 deletions scripts/rest_client_apply_patch_patch.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
diff --git a/kubernetes_asyncio/client/rest.py b/kubernetes_asyncio/client/rest.py
--- a/kubernetes_asyncio/client/rest.py
+++ b/kubernetes_asyncio/client/rest.py
@@ -142,7 +142,10 @@

# For `POST`, `PUT`, `PATCH`, `OPTIONS`, `DELETE`
if method in ['POST', 'PUT', 'PATCH', 'OPTIONS', 'DELETE']:
- if re.search('json', headers['Content-Type'], re.IGNORECASE):
+ if (
+ re.search('json', headers['Content-Type'], re.IGNORECASE)
+ or headers['Content-Type'] in ["application/apply-patch+yaml"]
+ ):
if body is not None:
body = json.dumps(body)
args["data"] = body
2 changes: 2 additions & 0 deletions scripts/update-client.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ sed -i'' "s,^DEVELOPMENT_STATUS = .*,DEVELOPMENT_STATUS = \\\"${DEVELOPMENT_STAT

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

0 comments on commit 4a0331f

Please sign in to comment.