如何正确格式化和发送application / json-patch + json

时间:2019-05-13 08:04:36

标签: javascript node.js kubernetes webhooks

我正在尝试使用Godaddy的K8s客户端修补CRD。

their example之后,我尝试过:

直接翻译其示例:

                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)

手动创建服务补丁程序对象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})

获取一个Yaml文件,我可以使用与成功的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

然后然后直接发布

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

全部导致以下错误:

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)

可以找到完整的源代码on GitHub

1 个答案:

答案 0 :(得分:0)

这不是一个完整的答案,而是另一种选择。切换了put而不是patch。相当于kubectl apply。另外请注意,我并未移动API类型services.service.knative.dev的规范版本属性,但这已经做完了,然后撤消,然后为了代码的合理性而进行了更正。仍然不接受我给的任何东西。一个更清洁的解决方案将是不错的。当前必须以这种方式递增[ResourceVersion][1],这还不错。