【问题标题】:Patch K8s Custom Resource with @kubernetes/client-node使用 @kubernetes/client-node 修补 K8s 自定义资源
【发布时间】:2021-05-25 22:36:41
【问题描述】:

我正在构建基于 Istio/K8s 的平台,用于使用 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)

虚拟服务.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

【问题讨论】:

    标签: node.js kubernetes istio


    【解决方案1】:

    我在该方法中为 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)});
    

    【讨论】:

      【解决方案2】:

      您可以使用customObjectsApipatchClusterCustomObjectpatchNamespacedCustomObject 方法,具体取决于给定对象是否已命名空间。

      【讨论】:

      • 使用 CustomObjectApi 时,我从 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
      • @AleksanderNowak 你能用replaceNamespacedCustomObject 作为替代品吗?只是看看它是否有效。
      • 当然。它给出了另一个错误:virtualservices.networking.istio.io "message-service" is invalid: metadata.resourceVersion: Invalid value: 0x0: must be specified for an update。附上我上面的 VirtualService 规范。
      【解决方案3】:

      在使用 @kubernetes/node-client 解决了无穷无尽的正文解析错误之后,我选择在我的 NodeJS 工作人员上安装 kubectl 并使用 shell.js 来运行它。

      我的结论是,@kubernetes/node-client 在与 Istio 自定义资源一起使用时会出现问题,但不想花时间调试到底出了什么问题。我很快就会在他们的仓库中发布关于它的 Github 问题。

      【讨论】:

        猜你喜欢
        • 2020-07-15
        • 2021-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-07
        • 2019-12-28
        • 2019-05-22
        • 2021-11-06
        相关资源
        最近更新 更多