Skip to content

Commit ecacd42

Browse files
committed
Add in the typescript client.
1 parent 3dac8b2 commit ecacd42

20 files changed

+65937
-7
lines changed

README.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
# Javascript Kubernetes Client information
22

3-
The Javascript clients for Kubernetes simply uses the
4-
[typescript client for kubernetes](https://github.com/kubernetes-client/typescript) from
5-
vanilla Javascript.
3+
The Javascript clients for Kubernetes is implemented in
4+
[typescript](https://typescriptlang.org), but can be called from either
5+
Javascript or Typescript.
6+
7+
For now, the client is implemented for server-side use with node
8+
using the `request` library.
9+
10+
There are future plans to also build a jQuery compatible library but
11+
for now, all of the examples and instructions assume the node client.
612

713
# Installation
814
```sh
9-
# Don't worry, you can call Typescript code from Javascript too...
10-
$ npm install @kubernetes/typescript-node
15+
$ npm install @kubernetes/client-node
1116
```
1217

1318
# Example code
@@ -52,3 +57,24 @@ k8sApi.createNamespace(namespace).then(
5257
);
5358
```
5459

60+
# Development
61+
62+
All dependencies of this project are expressed in its
63+
[`package.json` file](./package.json). Before you start developing, ensure
64+
that you have [NPM](https://www.npmjs.com/) installed, then run:
65+
66+
```console
67+
npm install
68+
```
69+
70+
# Testing
71+
72+
Tests are written using the [Chai](http://chaijs.com/) library. See
73+
[`config_test.ts`](./config_test.ts) for an example.
74+
75+
To run tests, execute the following:
76+
77+
```console
78+
npm test
79+
```
80+

examples/example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const k8s = require('@kubernetes/typescript-node');
1+
const k8s = require('@kubernetes/client-node');
22

33
let k8sApi = k8s.Config.defaultClient();
44
k8sApi.listNamespacedPod('default')

examples/namespace.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const k8s = require('@kubernetes/typescript-node');
1+
const k8s = require('@kubernetes/client-node');
22

33
var k8sApi = k8s.Config.defaultClient();
44

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import k8s = require('@kubernetes/client-node');
2+
3+
let kc = new k8s.KubeConfig();
4+
kc.loadFromFile(process.env['HOME'] + '/.kube/config');
5+
6+
let attach = new k8s.Attach(kc);
7+
attach.attach('default', 'nginx-4217019353-9gl4s', 'nginx', process.stdout, process.stderr, null /* stdin */, false /* tty */);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import k8s = require('@kubernetes/client-node');
2+
3+
let command = process.argv[2];
4+
5+
let kc = new k8s.KubeConfig();
6+
kc.loadFromFile(process.env['HOME'] + '/.kube/config');
7+
8+
let exec = new k8s.Exec(kc);
9+
exec.exec('default', 'nginx-4217019353-9gl4s', 'nginx', command, process.stdout, process.stderr, process.stdin, true /* tty */);

examples/typescript/simple/example.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import k8s = require('@kubernetes/client-node');
2+
3+
let k8sApi = k8s.Config.defaultClient();
4+
5+
k8sApi.listNamespacedPod('default')
6+
.then((res) => {
7+
console.log(res.body);
8+
});
9+
10+
// Example of instantiating a Pod object.
11+
let pod = {
12+
} as k8s.V1Pod;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import k8s = require('@kubernetes/client-node');
2+
3+
let kc = new k8s.KubeConfig();
4+
kc.loadFromFile(process.env['HOME'] + '/.kube/config');
5+
6+
let watch = new k8s.Watch(kc);
7+
let req = watch.watch('/api/v1/namespaces',
8+
// optional query parameters can go here.
9+
{},
10+
// callback is called for each received object.
11+
(type, obj) => {
12+
if (type == 'ADDED') {
13+
console.log('new object:');
14+
} else if (type == 'MODIFIED') {
15+
console.log('changed object:')
16+
} else if (type == 'DELETED') {
17+
console.log('deleted object:');
18+
} else {
19+
console.log('unknown type: ' + type);
20+
}
21+
console.log(obj);
22+
},
23+
// done callback is called if the watch terminates normally
24+
(err) => {
25+
if (err) {
26+
console.log(err);
27+
}
28+
});
29+
30+
// watch returns a request object which you can use to abort the watch.
31+
setTimeout(() => { req.abort(); }, 10 * 1000);

0 commit comments

Comments
 (0)