@dkx/k8s-client
TypeScript icon, indicating that this package has built-in type declarations

1.2.5 • Public • Published

K8S Client

Kubernetes client in typescript/javascript.

Installation

$ npm install --save @dkx/k8s-client

Supported version

  • 1.14
  • 1.15
  • 1.16
  • 1.17

Generate client

Because each kubernetes cluster can be different, you'll have to first generate the client. This makes it possible to use custom resources (read more here.

There are two ways how to generate a client. The preferred way is to generate it from the specification from your server. Simpler option is to generate it from specification on github (swagger.json).

Generate from own specification:

  1. Download open-api specification from your cluster:

    Replace <TOKEN> with your cluster token and <HOST> with your cluster host name.

    $ curl --cacert ca.pem -H "Authorization: Bearer <TOKEN>" https://<HOST>/openapi/v2 -o spec.json
  2. Generate client

    $ npx k8s-client gen --file spec.json

You can commit downloaded specification with your project. This will ensure that all your colleagues will end up with exactly the same client.

Generate from version:

  1. Generate client

    $ npx k8s-client gen --ver 1.17

Custom output directory:

$ npx k8s-client gen --ver 1.17 --out ./k8s-client

The --out option can be used to generate own npm package for your organization and storing it in eg. Verdaccio or any other private npm registry.

Add script to package.json:

{
    "scripts": {
        "k8s-client:gen": "k8s-client gen --file spec.json"
    }
}

and run it with:

$ npm run k8s-client:gen

Create client

import {Fetch, Client} from '@dkx/k8s-client';

const fetch = new Fetch('https://xxx.xxx.xxx.xxx', {
    token: getMyToken(),
    ca: getMyCertificate(),
});

const client = new Client(fetch);

Create deployment

import {Deployment} from '@dkx/k8s-client/resource/api/apps/v1/Deployment';

const deployment = new Deployment({
    metadata: {
        namespace: 'default',
        name: 'my-app',
    },
    spec: {
        selector: {
            matchLabels: {/* ... */},
        },
        template: {/* ... */},
    },
});

Use API

await client.apis.apps.v1.namespaces.byNamespace(deployment.metadata.namespace).deployments.post(deployment);

Shortcuts

exists:

await client.apis.apps.v1.namespaces.byNamespace(deployment.metadata.namespace).deployments.byName(deployment.metadata.name).exists();

patchResource:

await client.apis.apps.v1.namespaces.byNamespace(deployment.metadata.namespace).deployments.patchResource(deployment);

postOrPut:

await client.apis.apps.v1.namespaces.byNamespace(deployment.metadata.namespace).deployments.postOrPut(deployment);

postOrPatch:

await client.apis.apps.v1.namespaces.byNamespace(deployment.metadata.namespace).deployments.postOrPatch(deployment);

Logging

Predefined loggers:

  • StdOutLogger: Simple logs with console.log (default)
  • NullLogger: Logs nowhere

Using StdOutLogger:

import {Fetch, Client, StdOutLogger} from '@dkx/k8s-client';

const fetch = new Fetch('https://xxx.xxx.xxx.xxx');
const logger = new StdOutLogger();
const client = new Client(fetch, logger);

Custom logger:

Check out the StdOutLogger and Logger interface source codes.

Readme

Keywords

none

Package Sidebar

Install

npm i @dkx/k8s-client

Weekly Downloads

7

Version

1.2.5

License

MIT

Unpacked Size

281 kB

Total Files

147

Last publish

Collaborators

  • davidkcz