tomplus / kubernetes_asyncio

Python asynchronous client library for Kubernetes http://kubernetes.io/
Apache License 2.0
336 stars 69 forks source link

`application/apply-patch+yaml` content type is no longer supported since release 30.1.0 #316

Closed Meallia closed 2 days ago

Meallia commented 2 days ago

Hi

The following code:

import asyncio
from pprint import pprint

from kubernetes_asyncio.client import ApiClient, CoreV1Api
from kubernetes_asyncio.config import load_kube_config

async def create_update_cm():
    await load_kube_config()
    async with ApiClient() as client:
        corev1 = CoreV1Api(client)
        try:
            await corev1.patch_namespaced_config_map(
                _content_type="application/apply-patch+yaml",
                field_manager="test",
                name="test",
                namespace="default",
                body=dict(
                    apiVersion="v1",
                    kind="ConfigMap",
                    metadata=dict(
                        namespace="default",
                        name="test",
                    ),
                    data={"hello": "world!"},
                ),
            )
            pprint(
                await corev1.read_namespaced_config_map(
                    name="test",
                    namespace="default",
                )
            )
        finally:
            await corev1.delete_namespaced_config_map(name="test", namespace="default")

if __name__ == "__main__":
    asyncio.run(create_update_cm())

Now fails with

Traceback (most recent call last):
  File "site-packages/kubernetes_asyncio/client/api_client.py", line 185, in __call_api
    response_data = await self.request(
                    ^^^^^^^^^^^^^^^^^^^
  File "site-packages/kubernetes_asyncio/client/rest.py", line 253, in PATCH
    return (await self.request("PATCH", url,
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "site-packages/kubernetes_asyncio/client/rest.py", line 177, in request
    raise ApiException(status=0, reason=msg)
kubernetes_asyncio.client.exceptions.ApiException: (0)
Reason: Cannot prepare a request message for provided
                         arguments. Please check that your arguments match
                         declared content type.

The issue seems related to changes in #303, especially https://github.com/tomplus/kubernetes_asyncio/pull/303/files#diff-481a59905e58931309ea5b06ec23b1a38bf6618415a0378a03585a407f7b16f1L145

tomplus commented 2 days ago

Thanks for reporting this issue.

You are right, it's caused by #303. I'll fix it soon.

As a work-around you can remove this line: _content_type="application/apply-patch+yaml", new logic should use the strategic-merge patch in this case and it should work properly too.

Meallia commented 2 days ago

The workaround I'm using is to convert the dict to a json byte string and going directly in the bytes case that does not enforce any content type checking.

the strategic merge and apply patches don't exactly the same way, in particular, apply patches allow to only use the PATCH method even if the object is not present on the cluster.

I added a MR with a test and a quick fix for that (basically reverting part of the changes from #303 )

tomplus commented 2 days ago

Thanks for your contribution!

Meallia commented 2 days ago

You're welcome. Thanks for maintaining this repo.

On Thu, Jun 27, 2024, 9:17 AM Tomasz Prus @.***> wrote:

Thanks for your contribution!

— Reply to this email directly, view it on GitHub https://github.com/tomplus/kubernetes_asyncio/issues/316#issuecomment-2193969152, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABYOKRG2MYMNNBR4MS7VTHTZJO4CDAVCNFSM6AAAAABJ5Z4L6WVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOJTHE3DSMJVGI . You are receiving this because you authored the thread.Message ID: @.***>