How do I properly format and send an application/json-patch+json

5/13/2019

I am trying to use Godaddy's K8s client to patch a CRD.

Following their example, I have tried:

Translating their example directly:

                const patchService = await client.apis["serving.knative.dev"].v1alpha1.namespaces('default').services.patch({body: {
                  "spec": {
                    "template": {
                      "spec": {
                        "containers": [
                          {
                            "image": "docker.io/quantomworks/mcsh-site:" + build.metadata.annotations['hook-to-k8s']
                          }
                        ]
                      }
                    }
                  }
                }})

                console.log("Patch result:")
                console.log(patchService)

Creating a service patch object manually, following this example.

const servicePatchObject = [{ op: "replace", path: "/spec/runLatest/configuration/revisionTemplate/spec/container/image", value: "docker.io/quantomworks/mcsh-site:" + build.metadata.annotations['hook-to-k8s']}]

const patchService = await client.apis["serving.knative.dev"].v1alpha1.namespaces('default').services('mcserverhosting-net-site').patch({body: servicePatchObject})

Taking a yaml file that I can run with contents that match a successful kubectl patch services.serving.knative.dev mcserverhosting-net-site --type json -p

- op: replace
  path: /spec/runLatest/configuration/revisionTemplate/spec/container/image
  value: docker.io/quantomworks/mcsh-site:latest

And then posting it directly

const patchService = await client.apis["serving.knative.dev"].v1alpha1.namespaces('default').services('mcserverhosting-net-site').patch(servicePatch)

All result in the following error:

node-webhook┊ (node:17) UnhandledPromiseRejectionWarning: Error: the body of the request was in an unknown format - accepted media types include: application/json-patch+json, application/merge-patch+json
node-webhook┊     at /usr/src/app/node_modules/kubernetes-client/lib/backends/request.js:189:25
node-webhook┊     at Request._callback (/usr/src/app/node_modules/kubernetes-client/lib/backends/request.js:148:14)
node-webhook┊     at Request.self.callback (/usr/src/app/node_modules/request/request.js:185:22)
node-webhook┊     at Request.emit (events.js:196:13)
node-webhook┊     at Request.<anonymous> (/usr/src/app/node_modules/request/request.js:1161:10)
node-webhook┊     at Request.emit (events.js:196:13)
node-webhook┊     at IncomingMessage.<anonymous> (/usr/src/app/node_modules/request/request.js:1083:12)
node-webhook┊     at Object.onceWrapper (events.js:284:20)
node-webhook┊     at IncomingMessage.emit (events.js:201:15)
node-webhook┊     at endReadableNT (_stream_readable.js:1130:12)
node-webhook┊ (node:17) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with
.catch(). (rejection id: 2)

Full source code can be found on GitHub.

-- quantomworks
javascript
kubernetes
node.js
webhooks

1 Answer

5/17/2019

It's not a complete answer, but an alternative. Switched put instead of patch. kubectl apply equivalent. Also aware that I didn't moc the spec version proplery for API type services.service.knative.dev, but this was alreay done, undone, and then re-corrected for sanity in the code. Still didn't accept anything I gave it. A cleaner solution would be nice. Currently having to increment [ResourceVersion][1] this way, which isn't bad.

-- quantomworks
Source: StackOverflow