Skip to content

How to patch a status of a custom resource? #2434

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

Open
tom-tuvis opened this issue May 14, 2025 · 1 comment
Open

How to patch a status of a custom resource? #2434

tom-tuvis opened this issue May 14, 2025 · 1 comment

Comments

@tom-tuvis
Copy link

I've been trying to send a patch request to a custom object using the javascript client and it seems that if the status doesn't exist I'm getting rejected. That's the code I'm using:

const update = { state: "synced" }

const body = Object.entries(update).map(([key, val]) => {
      return {
        "op": "replace",
        "path": `/status/${key}`,
        "value": val
      }
    })

  return await k8sApi.patchNamespacedCustomObjectStatus({
    group: GROUP,
    version: VERSION,
    namespace,
    plural: crd,
    name,
    body,
    fieldValidation: "Strict"
  })

What I do to get around it is to make another call before sending the patch to check if the resource has a status subresource, and if it doesn't, I add an add operation at the start. But then I end up with 2 calls where I could have done it with one.

I've been trying to find a way to send it a merge patch, and to my understanding I need to change the Content-Type to application/merge-patch+json - but I couldn't find where to do it. Looking at the [code][1], I see that there's some internal logic to determine the Content-Type header, rather than taking it from the any argument in the call.

Is this a bug or am I doing something wrong? How can I patch the status of a resource that might not have a status yet (and do it with a single call)?

@brendandburns
Copy link
Contributor

Have a look at the example here:

https://github.com/kubernetes-client/javascript/blob/main/examples/patch-example.js#L23

To see how to set the content type.

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

No branches or pull requests

2 participants