1

I have https://containers.cloud.ibm.com/global/swagger-global-api - Kubernetes API swagger to drill upto each clusters details till namespaces.Using IAM token to access these API’s. Further, i want to get all the pods of that specific namespace and in turn image version of each pod.

I have tried using https://github.com/kubernetes-client/javascript client libraries to fetch all pods,But how will i get the pods of namespaces from my cluster of my account programatically?

This i want to achieve either by using REST API's or some client libraries.How to use these libraries?

var k8s = require('@kubernetes/client-node');
const kc = new k8s.KubeConfig();
    kc.loadFromDefault();
 const k8sApi = kc.makeApiClient(k8s.CoreV1Api);

    const listFn = () => k8sApi.listPodForAllNamespaces()
    console.log(listFn);

I get nothing from the above code.

New123
  • 219
  • 1
  • 4
  • 13

1 Answers1

1

The code that you have is not capturing errors. Hence you are not seeing any error even if it's not working.

First setup kubeconfig following this guide and use below code to get all pods of a specific namespace for example default

k8sApi.listNamespacedPod('default')
    .then((res) => {
    console.log(res.body);
    })
    .catch((err) => {
        console.log(err);
    });
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107
  • Thanks! But it throws error Error: connect ECONNREFUSED 127.0.0.1:8080 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) { errno: 'ECONNREFUSED', code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 8080 } – New123 Jun 13 '20 at 04:56
  • this app is running as pod inside the cluster or its running somewhere outside the cluster? – Arghya Sadhu Jun 13 '20 at 04:57
  • it's outside the cluster. – New123 Jun 13 '20 at 04:58
  • You have a kubeconfig file in .kube/config and you are able to use that kubeconfig with kubectl to run command kubectl get pods -n default? – Arghya Sadhu Jun 13 '20 at 05:00
  • This is an external node app trying to get cluster details of ibm cloud account and in turn namespaces,pods and image version of each pod. – New123 Jun 13 '20 at 05:02
  • Yes but its need a kubeconfig file to connect to the kubernetes cluster just like kubectl does. kc.loadFromDefault method will load Kubeconfig file from default location – Arghya Sadhu Jun 13 '20 at 05:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215858/discussion-between-new123-and-arghya-sadhu). – New123 Jun 13 '20 at 05:04