Kubernetes client for Node.JS
Contents
- Setup - General usage - Rate-limiting API access - Watching resources - Accessing CRDsInstallation
You can install this package via NPM:$ npm install @mittwald/kubernetes
Usage
Setup
External configuration using akubeconfig
file:import {
FileBasedConfig,
KubernetesRESTClient,
KubernetesAPI,
} from "@mittwald/kubernetes";
const config = new FileBasedConfig("/home/mhelmich/.kube/config");
const client = new KubernetesRESTClient(config);
const api = new KubernetesAPI(client);
Internal configuration (when the client is running within a Kubernetes cluster):
import {
InClusterConfig,
KubernetesRESTClient,
KubernetesAPI,
} from "@mittwald/kubernetes";
const config = new InClusterConfig();
const client = new KubernetesRESTClient(config);
const api = new KubernetesAPI(client);
General usage
api
.core()
.v1()
.pods.namespace("default")
.list()
.then((pods) => {
console.log("Found " + pods.length + " Pods:");
pods.forEach((pod) => {
console.log(pod.metadata.name);
});
});
Rate-limiting API access
import {
InClusterConfig,
KubernetesRESTClient,
RatelimitingKubernetesRESTClient,
KubernetesAPI,
} from "@mittwald/kubernetes";
const config = new InClusterConfig();
const client = new KubernetesRESTClient(config);
const limitedClient = new RatelimitingKubernetesRESTClient(client);
const api = new KubernetesAPI(limitedClient);
Watching resources
api
.core()
.v1()
.pods.namespace("default")
.watch({ "some-label": "foo" }, (ev) => {
console.log(`Pod: ${ev.type}: ${ev.object}`);
});
Accessing CRDs
If you have a package that offers a client for Custom Resource Definitions (take a look at the @mittwald/kubernetes-rook package as an example), you can use theextend
method to add the respective
API client:import { RookCustomResourceAPI } from "@mittwald/kubernetes-rook";
// ...
let extendedAPI = api.extend("rook", new RookCustomResourceAPI(client));
Supported resources
This library supports a reasonable subset of Kubernetes resources (these were implemented on an as-needed basis). Feel free to open a new issue or pull request to add support for additional API objects.- core/v1
- apps/v1
- apps/v1beta1
- batch/v1
- batch/v1beta1
- extensions/v1beta1
- rbac/v1
- autoscaling/v1
- apiextensions.k8s.io/v1beta1
- admissionRegistration.k8s.io/v1
References
- https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.22/
- https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md