项目作者: godaddy

项目描述 :
Simplified Kubernetes API client for Node.js.
高级语言: JavaScript
项目地址: git://github.com/godaddy/kubernetes-client.git
创建时间: 2016-07-31T02:18:15Z
项目社区:https://github.com/godaddy/kubernetes-client

开源协议:MIT License

下载


kubernetes-client

Build Status Greenkeeper badge

Simplified Kubernetes API client for Node.js.

Installation

Install via npm:

  1. 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:

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

provide your own path to a file:

  1. const { KubeConfig } = require('kubernetes-client')
  2. const kubeconfig = new KubeConfig()
  3. kubeconfig.loadFromFile('~/some/path')
  4. const Request = require('kubernetes-client/backends/request')
  5. const backend = new Request({ kubeconfig })
  6. const client = new Client({ backend, version: '1.13' })

provide a configuration object from memory:

  1. // Should match the kubeconfig file format exactly
  2. const config = {
  3. apiVersion: 'v1',
  4. clusters: [],
  5. contexts: [],
  6. 'current-context': '',
  7. kind: 'Config',
  8. users: []
  9. }
  10. const { KubeConfig } = require('kubernetes-client')
  11. const kubeconfig = new KubeConfig()
  12. kubeconfig.loadFromString(JSON.stringify(config))
  13. const Request = require('kubernetes-client/backends/request')
  14. const backend = new Request({ kubeconfig })
  15. const client = new Client({ backend, version: '1.13' })

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

  1. kubeconfig.setCurrentContext('dev')

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

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

or load a specification dynamically from the kube-apiserver:

  1. const client = new Client()
  2. 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:

  1. 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:

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

and then fetch your newly created Deployment:

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

and finally, remove the Deployment:

  1. 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:

  1. import * as ApiClient from 'kubernetes-client';
  2. const Client = ApiClient.Client1_13;
  3. 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:

  1. npm test

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

  1. npm run test-integration

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

  1. KUBERNETES_CLIENT_BACKEND=client-node npm run test-integration

References

License

MIT