shivam
shivam

Reputation: 249

Unable to get all the jobs inside a cluster using @kubernetes/client-node

I am using @kubernetes/client-node to access Kubernetes server API. I can get all the Pods from default using:

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

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

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

k8sApi.listNamespace().then((res) => {  // or using listAllNamespacedPods
    console.log(res.body);
});

and the body of the response from the above code looks like this:

Response From loadFromDefault

but when I am using kc.loadFromFile('pathToKubeConfigFile'), it is unable to read it (the config.yaml which is saved inside .kube folder). I have checked all the paths to certificates and keys files inside this file and they are correct.

import { KubeConfig, CoreV1Api } from '@kubernetes/client-node';

const kc = new KubeConfig();
kc.loadFromFile('./config/k8sConfig.yaml');

const k8sApi = kc.makeApiClient(CoreV1Api);

k8sApi.listPodForAllNamespaces().then((res) => {
    console.log(res.body);
});

and I need to return all the active Kubernetes Jobs (or the pods for that). Can anyone please suggest me how to achieve it?

Upvotes: 1

Views: 1608

Answers (2)

matt_j
matt_j

Reputation: 4614

As the problem has already been resolved in the comments section, I decided to provide a Community Wiki answer just for better visibility to other community members. I would also like to describe how to return all active Kubernetes Jobs using the Javascript Kubernetes Client

Using the loadFromFile() method.

When using the loadFromFile() method, it's important to make sure that the kubeconfig file is correct. In case the kubeconfig file is invalid, we may get various error messages such as:

Error: ENOENT: no such file or directory, open '.kube/confi'

or

Error: unable to verify the first certificate

The exact error message depends on what is incorrect in the kubeconfig file.

List all/active Kubernetes Jobs.

To list all Kubernetes Jobs, we can use the listJobForAllNamespaces() method.

I've created the listAllJobs.js script to demonstrate how it works:

$ cat listAllJobs.js
const k8s = require('@kubernetes/client-node')
const kc = new k8s.KubeConfig()
kc.loadFromFile('.kube/config')

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

k8sApi.listJobForAllNamespaces().then((res) => {
    res.body.items.forEach(job => console.log(job.metadata.name));
});


$ kubectl get jobs
NAME    COMPLETIONS   DURATION   AGE
job-1   0/1           3s         3s
job-2   1/1           10s        48m
job-3   1/1           10s        48m 

$ node listAllJobs.js
job-1
job-2
job-3

To list only active Jobs, we need to slightly modify the res.body.items.forEach(job => console.log(job.metadata.name)); line to check if the Job is active:

$ cat listActiveJobs.js
const k8s = require('@kubernetes/client-node')
const kc = new k8s.KubeConfig()
kc.loadFromFile('.kube/config')

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

k8sApi.listJobForAllNamespaces().then((res) => {
    res.body.items.forEach(job => job.status.active >= 1 && console.log(job.metadata.name));
});


$ kubectl get jobs
NAME    COMPLETIONS   
job-1   0/1           
job-2   1/1           
job-3   1/1          

$ node listActiveJobs.js 
job-1

Upvotes: 1

Harsh Manvar
Harsh Manvar

Reputation: 30110

You have to pass the Kube config file location instead of any YAML file

const { KubeConfig } = require('kubernetes-client')
const kubeconfig = new KubeConfig()
kubeconfig.loadFromFile('~/some/path')
const Request = require('kubernetes-client/backends/request')

const backend = new Request({ kubeconfig })
const client = new Client({ backend, version: '1.13' })

https://github.com/godaddy/kubernetes-client#initializing

However if you are planning to run the POD or container on K8s cluster you can also use : kc.loadFromCluster();

https://github.com/kubernetes-client/javascript/blob/master/examples/in-cluster.js

If you want to pass any YAML and apply those changes to cluster you can use the

https://github.com/kubernetes-client/javascript/blob/master/examples/yaml-example.js

Upvotes: 0

Related Questions