[英]Patch K8s Custom Resource with @kubernetes/client-node
我正在构建基于 Istio/K8s 的平台,用于使用 NodeJS 控制流量路由。 我需要能够以编程方式修改自定义资源,并且我想为此使用@kubernetes/node-client 。 我无法找到正确的 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
我的代码:
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)
虚拟服务.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
在该方法中,我对body
object 使用了错误的类型。 我按照这个例子让它工作。
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)});
您可以使用 customObjectsApi 的patchClusterCustomObject
或patchNamespacedCustomObject
方法,具体取决于给定的customObjectsApi
是否已命名空间。
在使用@kubernetes/node-client 解决了无休止的正文解析错误之后,我选择在我的 NodeJS worker 上安装kubectl并使用shell.js来运行它。
我的结论是, @kubernetes/node-client在与 Istio 自定义资源一起使用时有问题,但不想花时间调试到底出了什么问题。 我将很快在他们的仓库中发布关于它的 Github 问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.