UNPKG

3.62 kBMarkdownView Raw
1# Javascript Kubernetes Client information
2
3[![Build Status](https://travis-ci.org/kubernetes-client/javascript.svg?branch=master)](https://travis-ci.org/kubernetes-client/javascript)
4[![Client Capabilities](https://img.shields.io/badge/Kubernetes%20client-Gold-blue.svg?style=flat&colorB=FFD700&colorA=306CE8)](http://bit.ly/kubernetes-client-capabilities-badge)
5[![Client Support Level](https://img.shields.io/badge/kubernetes%20client-beta-green.svg?style=flat&colorA=306CE8)](http://bit.ly/kubernetes-client-support-badge)
6
7The Javascript clients for Kubernetes is implemented in
8[typescript](https://typescriptlang.org), but can be called from either
9Javascript or Typescript.
10
11For now, the client is implemented for server-side use with node
12using the `request` library.
13
14There are future plans to also build a jQuery compatible library but
15for now, all of the examples and instructions assume the node client.
16
17# Installation
18
19```console
20npm install @kubernetes/client-node
21```
22
23# Example code
24
25## List all pods
26
27```javascript
28const k8s = require('@kubernetes/client-node');
29
30const kc = new k8s.KubeConfig();
31kc.loadFromDefault();
32
33const k8sApi = kc.makeApiClient(k8s.Core_v1Api);
34
35k8sApi.listNamespacedPod('default').then((res) => {
36 console.log(res.body);
37});
38```
39
40## Create a new namespace
41
42```javascript
43const k8s = require('@kubernetes/client-node');
44
45const kc = new k8s.KubeConfig();
46kc.loadFromDefault();
47
48const k8sApi = kc.makeApiClient(k8s.Core_v1Api);
49
50var namespace = {
51 metadata: {
52 name: 'test',
53 },
54};
55
56k8sApi.createNamespace(namespace).then(
57 (response) => {
58 console.log('Created namespace');
59 console.log(response);
60 k8sApi.readNamespace(namespace.metadata.name).then((response) => {
61 console.log(response);
62 k8sApi.deleteNamespace(namespace.metadata.name, {} /* delete options */);
63 });
64 },
65 (err) => {
66 console.log('Error!: ' + err);
67 },
68);
69```
70
71## Create a cluster configuration programatically
72```javascript
73const k8s = require('@kubernetes/client-node');
74
75const cluster = {
76 name: 'my-server',
77 server: 'http://server.com',
78};
79
80const user = {
81 name: 'my-user',
82 password: 'some-password',
83};
84
85const context = {
86 name: 'my-context',
87 user: user.name,
88 cluster: cluster.name,
89};
90
91const kc = new k8s.KubeConfig();
92kc.loadFromOptions({
93 clusters: [cluster],
94 users: [user],
95 contexts: [context],
96 currentContext: context.name,
97});
98const k8sApi = kc.makeApiClient(k8s.Core_v1Api);
99...
100```
101
102# Additional Examples
103
104There are several more examples in the [examples](https://github.com/kubernetes-client/javascript/tree/master/examples) directory.
105
106# Development
107
108All dependencies of this project are expressed in its
109[`package.json` file](package.json). Before you start developing, ensure
110that you have [NPM](https://www.npmjs.com/) installed, then run:
111
112```console
113npm install
114```
115
116## (re) Generating code
117
118```console
119cd ../
120git clone https://github.com/kubernetes-client/gen
121cd javascript
122../gen/openapi/typescript.sh src settings
123```
124
125## Formatting
126
127Run `npm run format` or install an editor plugin like https://github.com/prettier/prettier-vscode.
128
129## Linting
130
131Run `npm run lint` or install an editor plugin like https://github.com/Microsoft/vscode-typescript-tslint-plugin
132
133# Testing
134
135Tests are written using the [Chai](http://chaijs.com/) library. See
136[`config_test.ts`](./src/config_test.ts) for an example.
137
138To run tests, execute the following:
139
140```console
141npm test
142```
143
144## Contributing
145
146Please see [CONTRIBUTING.md](CONTRIBUTING.md) for instructions on how to contribute.