[英]Patch K8s Custom Resource with @kubernetes/client-node
I'm building Istio/K8s-based platform for controlling traffic routing with NodeJS.我正在构建基于 Istio/K8s 的平台,用于使用 NodeJS 控制流量路由。 I need to be able to programmatically modify Custom Resources and I'd like to use the @kubernetes/node-client for that.
我需要能够以编程方式修改自定义资源,并且我想为此使用@kubernetes/node-client 。 I wasn't able to find the right API for accessing Custome Resources in docs and the repo.
我无法找到正确的 API 来访问文档和存储库中的客户资源。 Am I missing something?
我错过了什么吗? Thx in adv.
谢谢。
EDIT: When using CustomObjectApi.patchNamespacedCustomObject function, I'm getting the following error back from K8s API:编辑:使用 CustomObjectApi.patchNamespacedCustomObject function 时,我从 K8s API 收到以下错误:
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
My Code:我的代码:
const k8sYamls = `${path.resolve(path.dirname(__filename), '..')}/k8sYamls`
const vServiceSpec = read(`${k8sYamls}/${service}/virtual-service.yaml`)
const kc = new k8s.KubeConfig()
kc.loadFromDefault()
const client = kc.makeApiClient(k8s.CustomObjectsApi)
const result = await client.patchNamespacedCustomObject(vServiceSpec.apiVersion.split('/')[0], vServiceSpec.apiVersion.split('/')[1], namespace, 'virtualservices', vServiceSpec.metadata.name, vServiceSpec)
virtual-service.yaml:虚拟服务.yaml:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: message-service
spec:
hosts:
- message-service
http:
- name: 'production'
route:
- destination:
host: message-service
weight: 100
retries:
attempts: 3
perTryTimeout: 2s
retryOn: 5xx
I was using the wrong type for the body
object in that method.在该方法中,我对
body
object 使用了错误的类型。 I got it to work following this example .我按照这个例子让它工作。
const patch = [
{
"op": "replace",
"path":"/metadata/labels",
"value": {
"foo": "bar"
}
}
];
const options = { "headers": { "Content-type": k8s.PatchUtils.PATCH_FORMAT_JSON_PATCH}};
k8sApi.patchNamespacedPod(res.body.items[0].metadata.name, 'default', patch, undefined, undefined, undefined, undefined, options)
.then(() => { console.log("Patched.")})
.catch((err) => { console.log("Error: "); console.log(err)});
You can use the patchClusterCustomObject
or patchNamespacedCustomObject
methods, depending on whether the given object is namespaced or not, of the customObjectsApi
.您可以使用 customObjectsApi 的
patchClusterCustomObject
或patchNamespacedCustomObject
方法,具体取决于给定的customObjectsApi
是否已命名空间。
After fighting through endless body-parsing errors using @kubernetes/node-client , I opted to install kubectl on my NodeJS workers and use shell.js to run it with.在使用@kubernetes/node-client 解决了无休止的正文解析错误之后,我选择在我的 NodeJS worker 上安装kubectl并使用shell.js来运行它。
My conclusion is that the @kubernetes/node-client is buggy when used with Istio Custom Resources, but didn't want to spend time debugging exactly what's wrong.我的结论是, @kubernetes/node-client在与 Istio 自定义资源一起使用时有问题,但不想花时间调试到底出了什么问题。 I will post the Github Issue about it in their repo soon.
我将很快在他们的仓库中发布关于它的 Github 问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.