Keep pace with Kubernetes (NodeJS client)

Open source Kubernetes client library with automatic API discovery

Ronny Roeller
NEXT Engineering

--

We love Kubernetes for all the amazing things it gives us today. And we love Kubernetes even more for the pace with which the guys are adding new wizfeatures and APIs.

Too fast?

Yet, the pace of change introduced also an issue: Whenever Kubernetes launched a new API, all Kubernetes NodeJS client libraries (that we are aware of) require a patch to support them. This slows things down unnecessarily.

Luckily, Kubernetes offers a Discover API. Based on it, we developed a NodeJS client library that doesn’t require any static definition of available Kubernetes APIs but detects those dynamically.

So how do you use the client library?

First, add the client library to your project:

$ npm install --save auto-kubernetes-client

Now, we can create our NodeJS application that connects to Kubernetes. For that, import the client library and create a configuration object:

const fs = require('fs');
const path = require('path');
const K8sClient = require('auto-kubernetes-client');
const config = {
url: 'https://k8s.example.com',
ca: 'PEM encoded CA certificate',
cert: 'PEM encoded client certificate',
key: 'PEM encoded client key'
}

Finally, you can add the code to connect to Kubernetes and do funky stuff like listing all pods:

K8sClient(config)
.then(client => client.ns('default').pods.list())
.then(podList => podList.items
.forEach(pod => console.log(`Pod: ${pod.metadata.namespace}/${pod.metadata.name}`)))
.catch(err => console.error(`Error: ${err.message}`));

You can find further examples here.

Check out the client library on Github and let us know what you think!

Happy coding!

--

--

Ronny Roeller
NEXT Engineering

CTO at nextapp.co # Product discovery platform for high performing teams that bring their customers into every decision