9.0.0 • Published 5 years ago

kubernetes-client v9.0.0

Weekly downloads
65,611
License
MIT
Repository
github
Last release
5 years ago

kubernetes-client

Join Slack Build Status Greenkeeper badge

Simplified Kubernetes API client for Node.js.

Installation

Install via npm:

npm i kubernetes-client --save

Initializing

kubernetes-client generates a Kubernetes API client at runtime based on a Swagger / OpenAPI specification. You can generate a client using the cluster's kubeconfig file and that cluster's API specification.

To create the config required to make a client, you can either:

let kubernetes-client configure automatically by trying the KUBECONFIG environment variable first, then ~/.kube/config, then an in-cluster service account, and lastly settling on a default proxy configuration:

const client = new Client({ version: '1.13' })

provide your own path to a file:

const { KubeConfig } = require('kubernetes-client')
const kubeconfig = new KubeConfig()
kubeconfig.loadFromFile('~/some/path')
const Request = require('kubernetes-client/backends/request')

const backend = new Request({ kubeconfig })
const client = new Client({ backend, version: '1.13' })

provide a configuration object from memory:

// Should match the kubeconfig file format exactly
const config = {
  apiVersion: 'v1',
  clusters: [],
  contexts: [],
  'current-context': '',
  kind: 'Config',
  users: []
}
const { KubeConfig } = require('kubernetes-client')
const kubeconfig = new KubeConfig()
kubeconfig.loadFromString(JSON.stringify(config))

const Request = require('kubernetes-client/backends/request')
const backend = new Request({ kubeconfig })
const client = new Client({ backend, version: '1.13' })

and you can also specify the context by setting it in the kubeconfig object:

kubeconfig.setCurrentContext('dev')

You can also elide the .version and pass an OpenAPI specification:

const spec = require('./swagger.json')
const client = new Client({ spec })

or load a specification dynamically from the kube-apiserver:

const client = new Client()
await client.loadSpec()

See Examples for more configuration examples.

Basic usage

kubernetes-client translates Path Item Objects [1] (e.g., /api/v1/namespaces) to object chains ending in HTTP methods (e.g., api.v1.namespaces.get).

So, to fetch all Namespaces:

const namespaces = await client.api.v1.namespaces.get()

kubernetes-client translates Path Templating [2] (e.g., /apis/apps/v1/namespaces/{namespace}/deployments) to function calls (e.g., apis.apps.v1.namespaces('default').deployments).

So, to create a new Deployment in the default Namespace:

const deploymentManifest = require('./nginx-deployment.json')
const create = await client.apis.apps.v1.namespaces('default').deployments.post({ body: deploymentManifest })

and then fetch your newly created Deployment:

const deployment = await client.apis.apps.v1.namespaces('default').deployments(deploymentManifest.metadata.name).get()

and finally, remove the Deployment:

await client.apis.apps.v1.namespaces('default').deployments(deploymentManifest.metadata.name).delete()

kubernetes-client supports .delete, .get, .patch, .post, and .put.

Documentation

kubernetes-client generates documentation for the included specifications:

TypeScript

kubernetes-client includes a typings declartion file for Kubernetes API 1.13 and a complimentry Client1_13 class:

import * as ApiClient from 'kubernetes-client';

const Client = ApiClient.Client1_13;
const client = new Client({ version: '1.13' });

When using TypeScript, kubernetes-client does not support dynamically generating a client via .loadSpec().

Examples

examples/ has snippets for using kubernetes-client:

Contributing

See the kubernetes-client Issues if you're interested in helping out; and look over the CONTRIBUTING.md before submitting new Issues and Pull Requests.

Testing

Run the unit tests:

npm test

The integration tests use the current-context in your kubeconfig file. Run the integration tests:

npm run test-integration

Run integration tests with the @kubernetes/client-node backend:

KUBERNETES_CLIENT_BACKEND=client-node npm run test-integration

References

License

MIT

robust-express-spraynodepolis-aascodiac-clikubedefkooberlogdna-workers@spacemakerai/serverless-ingress@dimensionfourcloud/commonyarik-testracy-deploy@everything-registry/sub-chunk-2033teraslicevs-tool@doing-things-with-node-red/node-red-contrib-openshift-client@docamole/kubernetes-pilot@codefresh-io/kube-integration@codefresh-io/cf-variable-resolver@coweb/infrastructurek8s-migratek8s-opskit-deployerkwatchsp-channel-managertiendvd-logagent@atomist/k8-automation@atomist/k8-sdm@atomist/sdm-pack-k8@adpt/testutils@balena/katapult@behygge/cli@brickblock/bbk-devtools@brickblock/ci-healthcheck@brickblock/kube-watch@certd/plugin-common@certd/plugin-util@certd/plugins@codiac.io/codiac-cli@datapio/sdk-k8s-operator@dustlight/serverless-fun@dtk/comm-interface@ibmgaragecloud/cloud-native-toolkit-web-cli@hkube/hkube@hkube/kubernetes-client@garage-catalyst/ibm-garage-cloud-cli@nestcfork/common@nestcloud/common@nestcloud2/common@nicodoggie/devctl-plugin-kind@eservices/servicebot@mojoio/kubernetes@jchurque/k8s-loadconfig@jchurque/k8s-secret@sakuraapp/service-client@promptly/k8s@sematext/logagentmiracle.core.driversmiracle.dashboardopenshift-clientopenshift-rest-clientolympusserverless-fnserverless-kubeless-jinserverless-kubelessseno-ldinode-red-contrib-openshift-clientnode-kubernode-red-contrib-ocquick-netrealm-object-serversoajs.infrasoajs.installersoajs.controllersoajs.core.driverssoajs.dashboardgrafana-gsuite-syncibmapm-restclienthanoman-ldikubechainkubemotekuberkubernetes-mongodb-labelerknopkouneli@useful/serverless-kubeless@zalastax/nolb-kubalfred-kubernetesadira-sdk-nodecomm-interfacecodefreshdenis-codefresh-kube-integrationbackend-commons-libbrimleycaf_deploybr2k-clibr2k-truffledomesdayeasy-k8segg-kubernetesendb-k8sfruster-tools
9.0.0

5 years ago

8.3.7

5 years ago

8.3.6

6 years ago

8.3.5

6 years ago

8.3.4

6 years ago

8.3.3

6 years ago

8.3.2

6 years ago

8.3.1

6 years ago

8.3.0

6 years ago

8.2.1

6 years ago

8.2.0

6 years ago

8.1.3

6 years ago

8.1.2

6 years ago

8.1.1

6 years ago

8.1.0

6 years ago

8.0.0

6 years ago

7.0.1

6 years ago

7.0.0

6 years ago

6.12.1

6 years ago

6.12.0

6 years ago

6.11.1

6 years ago

6.11.0

6 years ago

6.10.0

6 years ago

6.9.0

6 years ago

6.8.3

6 years ago

6.8.2

6 years ago

6.8.1

6 years ago

6.8.0

6 years ago

6.7.1

6 years ago

6.7.0

7 years ago

6.6.2

7 years ago

6.6.1

7 years ago

6.6.0

7 years ago

6.5.1

7 years ago

6.5.0

7 years ago

6.4.1

7 years ago

6.4.0

7 years ago

6.3.2

7 years ago

6.3.1

7 years ago

6.3.0

7 years ago

6.2.0

7 years ago

6.1.0

7 years ago

6.0.1

7 years ago

6.0.0

7 years ago

5.4.0

7 years ago

5.3.1

7 years ago

5.3.0

7 years ago

5.2.0

7 years ago

5.1.0

7 years ago

5.0.1

7 years ago

5.0.0

7 years ago

4.0.1

7 years ago

4.0.0

7 years ago

3.18.1

7 years ago

3.18.0

7 years ago

3.17.2

7 years ago

3.17.1

8 years ago

3.17.0

8 years ago

3.16.1

8 years ago

3.16.0

8 years ago

3.15.0

8 years ago

3.14.0

8 years ago

3.13.0

8 years ago

3.12.0

8 years ago

3.11.0

8 years ago

3.10.1

8 years ago

3.10.0

8 years ago

3.9.0

8 years ago

3.8.0

8 years ago

3.7.0

8 years ago

3.6.0

8 years ago

3.5.1

8 years ago

3.5.0

8 years ago

3.4.0

8 years ago

3.3.0

8 years ago

3.2.0

8 years ago

3.1.0

9 years ago

3.0.0

9 years ago

2.2.3

9 years ago

2.2.2

9 years ago

2.2.1

9 years ago

2.2.0

9 years ago

2.1.1

9 years ago

2.1.0

9 years ago

2.0.0

9 years ago

1.1.4

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.0.1

9 years ago