Running NodeJs kubernetes client example error

1/8/2020

I am following this link: https://github.com/kubernetes-client/javascript

and trying to list pods in the given namespace, here the code that I am referring:

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);
});

I have successfully logged in to Kubernetes console, In the console, I am able to do the normal operation, but when I am running this code I am getting below error:

(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.

Just wanted to mention that RBAC is enabled on k8s cluster

My Config file looks like this:

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

Please let me know what I am missing

-- focode
azure
kubernetes
node.js

3 Answers

1/9/2020

It seems your token is empty which is the reason that throws the error. You can see the code here:

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

And on my side, there is a token in the config file for the user in the users:

enter image description here

Maybe you can try to add the token in your config and try it again.

-- Charles Xu
Source: StackOverflow

1/10/2020

Sorry for the late reply, I had posted the same question on GitHub https://github.com/kubernetes-client/javascript/issues/389 and they suggested to run kubectl version command, and after running this command I was able to see access token field with value in config file, and I was successfully able to execute the program and it gave me correct output.

-- focode
Source: StackOverflow

1/30/2020

So, it appeared that as of now (Jan-2020) Javascript client library doesn't support automatic rewriting of the config when it refreshes a token.

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

The workaround is to validate it manually by runing kubectl version which will refresh the token in your kubeconfig file.

If that works, the option is to add the following to the kubeconfig:

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

Developers will definitely be ading better support for refreshing tokens to the client SDK.

Because there is already an utility that calls exportConfig() to write the config into a string (which could then be written into a file), so it's possible to fix the behavior.

To do it right a large-ish refactor of the authenticator interface is required. That will be done under this github issue track.

-- Nick
Source: StackOverflow