繁体   English   中英

运行 NodeJs kubernetes 客户端示例错误

[英]Running NodeJs kubernetes client example error

我正在关注此链接: https : //github.com/kubernetes-client/javascript

并尝试在给定的命名空间中列出 pod,这里是我指的代码:

const k8s = require('@kubernetes/client-node');

const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

k8sApi.listNamespacedPod('abc').then((res) => {
    console.log(res.body);
});

我已成功登录到 Kubernetes 控制台,在控制台中,我能够进行正常操作,但是当我运行此代码时,出现以下错误:

(node:30816) UnhandledPromiseRejectionWarning: Error: Token is expired!
    at CloudAuth.updateAccessToken (D:\project\IOT-KT\dl\node_modules\@kubernetes\client-node\dist\cloud_auth.js:46:19)
    at CloudAuth.getToken (D:\project\IOT-KT\dl\node_modules\@kubernetes\client-node\dist\cloud_auth.js:24:18)
    at CloudAuth.<anonymous> (D:\project\IOT-KT\dl\node_modules\@kubernetes\client-node\dist\cloud_auth.js:15:32)
    at Generator.next (<anonymous>)
    at D:\project\IOT-KT\dl\node_modules\tslib\tslib.js:110:75
    at new Promise (<anonymous>)
    at Object.__awaiter (D:\project\IOT-KT\dl\node_modules\tslib\tslib.js:106:16)
    at CloudAuth.applyAuthentication (D:\project\IOT-KT\dl\node_modules\@kubernetes\client-node\dist\cloud_auth.js:14:24)
    at KubeConfig.<anonymous> (D:\project\IOT-KT\dl\node_modules\@kubernetes\client-node\dist\config.js:299:37)
    at Generator.next (<anonymous>)
(node:30816) 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)
(node:30816) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

只想提一下k8s集群上启用了RBAC

我的配置文件如下所示:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1C..................Cg==
    server: https://<removed>.azmk8s.io:443
  name: removed>
contexts:
- context:
    cluster: removed>
    user: removed>
  name: removed>
current-context: removed>
kind: Config
preferences: {}
users:
- name: removed>
  user:
    auth-provider:
      config:
        apiserver-id: removed>
        client-id: removed>
        environment: removed>
        tenant-id: removed>
      name: azure

请让我知道我缺少什么

因此,截至目前(2020 年 1 月),Javascript 客户端库似乎不支持在刷新令牌时自动重写配置。

private updateAccessToken(config: Config) {
    let cmd = config['cmd-path'];
    if (!cmd) {
        throw new Error('Token is expired!');

解决方法是通过运行kubectl version手动验证它,这将刷新kubeconfig文件中的令牌。

如果kubeconfig ,可以选择将以下内容添加到kubeconfig

user:
   ...
   cmd-path: kubectl
   cmd-args: version

开发人员肯定会为客户端 SDK 的刷新令牌提供更好的支持。

因为已经有一个实用程序调用exportConfig()将配置写入字符串(然后可以写入文件),因此可以修复该行为。

要做到这一点,需要对身份验证器接口进行大量重构。 这将在这个github issue track下完成。

您的令牌似乎是空的,这就是引发错误的原因。 你可以在这里看到代码:

let cmd = config['cmd-path'];
        if (!cmd) {
            throw new Error('Token is expired!');
        }

在我这边,用户的配置文件中有一个token

在此处输入图片说明

也许您可以尝试在配置中添加令牌并重试。

抱歉回复晚了,我在 GitHub https://github.com/kubernetes-client/javascript/issues/389上发布了同样的问题,他们建议运行 kubectl version 命令,运行此命令后我能够看到访问令牌字段在配置文件中具有值,并且我能够成功执行该程序并且它给了我正确的输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM