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

Implement StatusWriter so you can patch just the status field in an API object #1442

Closed
dsyer opened this issue Dec 15, 2020 · 4 comments · Fixed by #1470
Closed

Implement StatusWriter so you can patch just the status field in an API object #1442

dsyer opened this issue Dec 15, 2020 · 4 comments · Fixed by #1470

Comments

@dsyer
Copy link
Contributor

dsyer commented Dec 15, 2020

In the Java client is there an equivalent of StatusWriter in Go (where you can update just the status of an API object)? I don't see it anywhere so I guess this is a feature request.

@dsyer
Copy link
Contributor Author

dsyer commented Dec 15, 2020

I managed to patch a status using code a bit like this. I hope I did it wrong and there's a better way (object is a KubernetesObject):

CustomObjectsApi api = new CustomObjectsApi(apiClient);
GroupVersion gv = GroupVersion.parse(object);
api.patchNamespacedCustomObjectStatus(gv.getGroup(), gv.getVersion(),
		object.getMetadata().getNamespace(), this.pluralName, object.getMetadata().getName(),
		Arrays.asList(new ObjectPatch(extractStatus(object))), null, null, null);

where ParentPatch is:

public static class ObjectPatch {
private String op = "replace";
private String path = "/status";
private Object value;
// ... getters and setters
}

If that really is the best way to do it then StatusWriter would be very helpful.

@brendandburns
Copy link
Contributor

I think that this is the best way to do it. There are methods for updating status for concrete objects (e.g. patchNamespacedPodStatus) but it's not any easier to use.

Probably worth adding the the existing patch utilities, rather than adding a new 'StatusWriter' but I think adding utility methods is a good idea.

@dsyer
Copy link
Contributor Author

dsyer commented Dec 15, 2020

OK, so that's useful to know. Can you explain why there is this logic in ApiClient with no obvious way to change it?

  public String selectHeaderContentType(String[] contentTypes) {
    if (contentTypes.length == 0 || contentTypes[0].equals("*/*")) {
      return "application/json";
    }
    for (String contentType : contentTypes) {
      if (isJsonMime(contentType)) {
        return contentType;
      }
    }
    return contentTypes[0];
  }

where contentTypes is a hard-coded array (here from patchNamespacedCustomObjectStatusCall()):

    final String[] localVarContentTypes = {
      "application/json-patch+json", "application/merge-patch+json", "application/apply-patch+yaml"
    };

so it always responds with the same value (the first one).

It's actually easier to create the patch using application/merge-patch+json, but I don't see a way to make the ApiClient send that header.

@brendandburns
Copy link
Contributor

Yeah, this code is generated, and it is not awesome™

We have hacked around it in the PatchUtils class:
https://github.com/kubernetes-client/java/blob/master/util/src/main/java/io/kubernetes/client/util/PatchUtils.java#L34

Which uses a custom RequestBody:
https://github.com/kubernetes-client/java/blob/master/util/src/main/java/io/kubernetes/client/util/ProxyContentTypeRequestBody.java

To override this code.

You'll want to write your code in terms of that patch method in PatchUtils

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants