Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to patch custom object with CustomObjectsApi.patchNamespacedCustomObject #3106

Open
garribas-atlassian opened this issue Feb 22, 2024 · 3 comments
Labels
kind/bug Categorizes issue or PR as related to a bug.

Comments

@garribas-atlassian
Copy link

Describe the bug
Hello,

I'm trying to patch a custom resource following this official example:
https://github.com/kubernetes-client/java/blob/master/kubernetes/docs/CustomObjectsApi.md#patchNamespacedCustomObject

Object result = apiInstance.patchNamespacedCustomObject(group, version, namespace, plural, name, body).execute();

No matter what I specify in the body param (json patch string, yaml patch, a Map with CRD attributes), the result is always an HTTP 415 error response from the server:

{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"the body of the request was in an unknown format - accepted media types include: application/json-patch+json, application/merge-patch+json, application/apply-patch+yaml","reason":"UnsupportedMediaType","code":415}

Java client Version
20.0.0

Kubernetes Version
1.28

Java Version
Corretto-21.0.2

To Reproduce
Steps to reproduce the behavior:

CustomObjectsApi apiInstance = new CustomObjectsApi(defaultClient);
...
String body = "[\n" +
      "  {\n" +
      "    \"op\": \"add\",\n" +
      "    \"path\": \"/metadata/annotations/new-annotation\",\n" +
      "    \"value\": \"value of the new annotation\"\n" +
      "  }\n" +
      "]";
Object result = apiInstance.patchNamespacedCustomObject(group, version, namespace, plural, name, body).execute();

Results in:

2024-02-22T14:58:56.876+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : Content-Length: 215
2024-02-22T14:58:56.876+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : Accept: application/json
2024-02-22T14:58:56.876+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : Content-Type: application/json
2024-02-22T14:58:56.876+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : User-Agent: Kubernetes Java Client/20.0.0-SNAPSHOT
2024-02-22T14:58:56.876+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : 
2024-02-22T14:58:56.876+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : "[\n              {\n                \"op\": \"add\",\n                \"path\": \"/metadata/annotations/new-annotation\",\n                \"value\": \"value of the new annotation\"\n              }\n            ]"
2024-02-22T14:58:56.876+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : --> END PATCH (215-byte body)
2024-02-22T14:58:56.881+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : audit-id: 9c309609-0e88-4ee1-9399-c438249fe8d2
2024-02-22T14:58:56.881+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : cache-control: no-cache, private
2024-02-22T14:58:56.881+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : content-type: application/json
2024-02-22T14:58:56.881+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : x-kubernetes-pf-flowschema-uid: 891cc6ff-2e12-450f-b9c8-94f26b2adda8
2024-02-22T14:58:56.881+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : x-kubernetes-pf-prioritylevel-uid: dbc7143a-c8fc-4f36-a0b9-451153992497
2024-02-22T14:58:56.881+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : content-length: 293
2024-02-22T14:58:56.881+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : date: Thu, 22 Feb 2024 03:58:56 GMT
2024-02-22T14:58:56.881+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : 
2024-02-22T14:58:56.882+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"the body of the request was in an unknown format - accepted media types include: application/json-patch+json, application/merge-patch+json, application/apply-patch+yaml","reason":"UnsupportedMediaType","code":415}

2024-02-22T14:58:56.882+11:00  INFO 81582 --- [nio-8080-exec-1] okhttp3.OkHttpClient                     : <-- END HTTP (293-byte body)
2024-02-22T14:58:56.884+11:00 ERROR 81582 --- [nio-8080-exec-1] c.a.a.homa.k8s.api.http.ExceptionHelper  : ApiException: Message: 
HTTP response code: 415
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"the body of the request was in an unknown format - accepted media types include: application/json-patch+json, application/merge-patch+json, application/apply-patch+yaml","reason":"UnsupportedMediaType","code":415}

Expected behavior
The expectation is that the above code would modify the CRD.

Using PatchUtils doesn't seem an alternative for custom resources since the class to be patched (which would be Object for a CRD) must be supplied:

public static <ApiType> ApiType patch(Class<ApiType> apiTypeClass, PatchCallFunc callFunc, String patchFormat, ApiClient apiClient) throws ApiException {

https://github.com/kubernetes-client/java/blob/master/examples/examples-release-18/src/main/java/io/kubernetes/client/examples/PatchExample.java

`

@garribas-atlassian
Copy link
Author

I've tried as well to adapt the examples for PatchUtils, but CustomObjectsApi.patchNamespacedCustomObjectCall is private?

Object patch =
  PatchUtils.patch(
      Object.class,
      () ->
	  customObjectsApi.patchNamespacedCustomObjectCall(
	      group, version, namespace, plural, name,
	      new V1Patch(applyYamlStr),
	      null,
	      null,
	      null
	      null,
	      true,
	      null),
      V1Patch.PATCH_FORMAT_APPLY_YAML,
      customObjectsApi.getApiClient());

@yue9944882 yue9944882 added the kind/bug Categorizes issue or PR as related to a bug. label Feb 23, 2024
@yue9944882
Copy link
Member

thanks for reporting this, before we release a fix, please use either 19.x or 20.0.0-legacy to workaround the issue

mnlipp added a commit to mnlipp/VM-Operator that referenced this issue Mar 7, 2024
@mnlipp
Copy link

mnlipp commented Mar 7, 2024

This bug is also in v20-legacy. Using v19 helps, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug.
Projects
None yet
Development

No branches or pull requests

3 participants