npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dkx/k8s-client

v1.2.5

Published

Typescript client for kubernetes

Downloads

17

Readme

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.