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