# node-kubernetes-client

> Kubernetes client of Node.js

Latest version **0.3.2** (published 2017-10-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install node-kubernetes-client
pnpm add node-kubernetes-client
yarn add node-kubernetes-client
bun add node-kubernetes-client
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.2 |
| Published | 2017-10-05 |
| First published | 2015-03-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 88 |
| Author | huangqg |
| Maintainers | emaildanwilson, grdryn, huangqg, tenxcloud, wanglei |
| Keywords | kubernetes, k8s, docker |

## Links

- npm: https://www.npmjs.com/package/node-kubernetes-client
- Repository: https://github.com/tenxcloud/node-kubernetes-client
- Issues: https://github.com/tenxcloud/node-kubernetes-client/issues
- npm.io page: https://npm.io/package/node-kubernetes-client

## Dependencies (6)

- [glob](https://npm.io/package/glob.md) *
- [async](https://npm.io/package/async.md) ^2.5.0
- [sugar](https://npm.io/package/sugar.md) ^1.5.0
- [request](https://npm.io/package/request.md) ^2.81.0
- [form-data](https://npm.io/package/form-data.md) ^2.2.0
- [json-gate](https://npm.io/package/json-gate.md) ^0.8.21

## Recent versions

- 0.3.2 (latest) — 2017-10-05
- 0.3.1 — 2017-08-15
- 0.3.0 — 2017-08-02
- 0.2.3 — 2015-09-06
- 0.2.1 — 2015-06-22
- 0.2.0 — 2015-06-11
- 0.1.9 — 2015-04-01
- 0.1.8 — 2015-03-31
- 0.1.7 — 2015-03-20
- 0.1.6 — 2015-03-19
- 0.1.5 — 2015-03-19
- 0.1.4 — 2015-03-19
- 0.1.3 — 2015-03-19
- 0.1.2 — 2015-03-17
- 0.1.1 — 2015-03-17
- … 1 more at https://npm.io/package/node-kubernetes-client/versions

## README

# Google Kubernetes client

Node.js client library for Google's Kubernetes API (https://github.com/GoogleCloudPlatform/kubernetes)
You can use this library to call a Kubernetes API hosted in a Kubernetes master node using Node.js.

Install:

    npm install node-kubernetes-client

Current endpoint support includes:

* events
* endpoints
* namespaces
* pods
* minions
* services
* replicationControllers
* nodes
* proxyMinions
* proxyNodes
* proxyPods
* proxyServices
* watchPods

Interaction is accomplished via `client.<endpoint>.<method>`. (see examples below)

# Usage

## Create client

Authentication can be done via either token or login. If, however, the token
expires, the login info will be used to acquire a new token.

```js
var Client = require('node-kubernetes-client');

var client = new Client({
    host:  'xx.xx.xx.xx',
    protocol: 'https',
    version: 'v1beta2',
    token: 'XYZ'
});
```

### Optional params:
Some optional params also exist on initialising the client. 
```js
{
    namespace:  'someNamespace', // filter all client requests by a namespace - default is no namespace
    timeout: 20000 // A timeout (in ms) for requests to k8 apis
    reqOptions: {} // array of options used to override the npm request module for this client proxy, auth, etc.
}
```

## Getting from pods

Paging is accomplished automatically. For example, a request for `pods` will
return all pods, not just those returned on the first page.

For example, to get all pods:

```js
client.pods.get(function (err, pods) {
    console.log('pods:', pods);
});
```
## Retrieving from Custom Collections

Retrieving from custom k8 collections is enabled by using the `createCollection` functionality. 

For example, to create a custom collection called "routes":
```js
var schema = null, //optional param
innerCollections = null, // optional param
options = { apiPrefix : 'api2' }; // optionally set a per-collection api prefix
client.routes = client.createCollection('routes', schema, innerCollections, options);
// then use the routes collection like any other
```

## createCollection options

```
apiPrefix: "apis" // Sets the prefix to the api path for the new collection.
namespaced: true // Controls if paths include "/namespaces/${namespace}".
```

## Custom Collection for k8s deployments

```js
client = new Client({
  host:  'xx.xx.xx.xx',
  protocol: 'https',
  version: 'extensions/v1beta1',
  token: 'XYZ',
  namespace:  'mynamespace',
  reqOptions: {proxy: configLocation.proxy || null},
  timeout: 20000 
});
// add deployments to the api
client.deployments = client.createCollection('deployments', null, null, { apiPrefix : 'apis' });

var deploymentJSON = {
          "apiVersion": "extensions/v1beta1",
          "kind": "Deployment",
          ...
          }

// then use the deployments collection like any other
client.deployments.get(deploymentJSON.metadata.name, function (err, data) {
  if (err && err.statusCode != 404) {
    //something is wrong, bail
    console.log("error checking for deployment:", err);
    return;
  } else if (err && err.statusCode == 404) {
    //create if not found
    client.deployments.create(deploymentJSON, function (err, data) {
      if (err) {
        console.log("error updating deployment:", err);
        return;
      } else {
        console.log("deployment created:", deploymentJSON.metadata.name);
        return;
      }
    });
  } else {
    //update since it did not exist
    client.deployments.update(deploymentJSON.metadata.name, deploymentJSON, function (err, data) {
      if (err) {
        console.log("error updating deployment:", err);
        return;
      }
      console.log("deployment updated:", deploymentJSON.metadata.name);
      return;
    }); 
  }
});
```

# How to run the test cases
## install mocha
```js
  npm install mocha
```
## run testcase
```bash
  minikube start
  kubectl proxy
  mocha test/test-*
```
The results will be output to test/results/ directory with formatted JSON.

# Roadmap

See issues.

# Others

You may interested in kubernetes client library using other programming languanges, please check the below link
(https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/client-libraries.md)

---
_Source: https://npm.io/package/node-kubernetes-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
