I am working on a NextJS project to create a Kubernetes Job. To create a Job on minikube, I am using config.yaml that is generally generated whenever we start the minikube. So I am using the same file to make connection with Kubernetes API. Now to make connection with kubernetes(minikube), I am using following code:
import k8s from "@kubernetes/client-node";
let kubeAPI = null;
const createKubeAPI = async () => {
const kc = new k8s.KubeConfig();
const path = "../config/k8sConfig.yaml";
console.log(path);
kc.loadFromFile(path);
const k8sApi = kc.makeApiClient(k8s.BatchV1Api);
return k8sApi;
};
export const get = () => kubeAPI;
export const create = async () => {
kubeAPI = await createKubeAPI();
};
And in another .js file, by using the function created in the above code creating a new job or displaying the running the jobs.
But when running above code, it is showing the error. Complete error looks like:
event - compiled client and server successfully in 7.4s (224 modules)
null
error - utils\kube-utils\kubeClient.js (6:17) @ createKubeAPI
TypeError: Cannot read properties of undefined (reading 'KubeConfig')
4 |
5 | const createKubeAPI = async () => {
> 6 | const kc = new k8s.KubeConfig();
| ^
7 | // kc.loadFromDefault();
8 | const path = "../config/k8sConfig.yaml";
9 | console.log(path);
Can someone help me or guide me, what am I doing wrong here?
It seems there is no default export, so when using ES6 module imports, you need to either use named imports:
import { KubeConfig, CoreV1Api } from '@kubernetes/client-node';
or import everything:
import * as k8s from '@kubernetes/client-node';
new k8s.KubeConfig();