使用@kubernetes/client-node 修补 K8s 自定义资源
Patch K8s Custom Resource with @kubernetes/client-node
我正在构建 Istio/K8s-based 平台以使用 NodeJS 控制流量路由。我需要能够以编程方式修改自定义资源,我想为此使用 @kubernetes/node-client。我无法找到正确的 API 来访问文档和存储库中的客户资源。我错过了什么吗?感谢广告。
编辑:使用 CustomObjectApi.patchNamespacedCustomObject 函数时,我从 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)
虚拟-service.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
您可以使用 patchClusterCustomObject
或 patchNamespacedCustomObject
方法,具体取决于给定对象是否已命名空间,customObjectsApi
。
在使用 @kubernetes/node-client, I opted to install kubectl on my NodeJS workers and use shell.js 到 运行 解决无休止的正文解析错误之后。
我的结论是 @kubernetes/node-client 在与 Istio 自定义资源一起使用时存在错误,但我不想花时间调试到底出了什么问题。我很快就会 post Github 在他们的回购中发布关于它的问题。
我在该方法中为 body
对象使用了错误的类型。我在 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)});
我正在构建 Istio/K8s-based 平台以使用 NodeJS 控制流量路由。我需要能够以编程方式修改自定义资源,我想为此使用 @kubernetes/node-client。我无法找到正确的 API 来访问文档和存储库中的客户资源。我错过了什么吗?感谢广告。
编辑:使用 CustomObjectApi.patchNamespacedCustomObject 函数时,我从 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)
虚拟-service.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
您可以使用 patchClusterCustomObject
或 patchNamespacedCustomObject
方法,具体取决于给定对象是否已命名空间,customObjectsApi
。
在使用 @kubernetes/node-client, I opted to install kubectl on my NodeJS workers and use shell.js 到 运行 解决无休止的正文解析错误之后。
我的结论是 @kubernetes/node-client 在与 Istio 自定义资源一起使用时存在错误,但我不想花时间调试到底出了什么问题。我很快就会 post Github 在他们的回购中发布关于它的问题。
我在该方法中为 body
对象使用了错误的类型。我在 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)});