Skip to content

Commit 447750e

Browse files
authored
Merge pull request #212 from sheldonkwok/feature/prettier
Feature/prettier
2 parents fb6c06c + 43466f1 commit 447750e

24 files changed

+314
-242
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/api.ts

README.md

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
[![Client Capabilities](https://img.shields.io/badge/Kubernetes%20client-Silver-blue.svg?style=flat&colorB=C0C0C0&colorA=306CE8)](http://bit.ly/kubernetes-client-capabilities-badge)
55
[![Client Support Level](https://img.shields.io/badge/kubernetes%20client-beta-green.svg?style=flat&colorA=306CE8)](http://bit.ly/kubernetes-client-support-badge)
66

7-
87
The Javascript clients for Kubernetes is implemented in
98
[typescript](https://typescriptlang.org), but can be called from either
109
Javascript or Typescript.
@@ -16,29 +15,30 @@ There are future plans to also build a jQuery compatible library but
1615
for now, all of the examples and instructions assume the node client.
1716

1817
# Installation
18+
1919
```console
2020
npm install @kubernetes/client-node
2121
```
2222

2323
# Example code
2424

2525
## List all pods
26+
2627
```javascript
2728
const k8s = require('@kubernetes/client-node');
2829

29-
3030
const kc = new k8s.KubeConfig();
3131
kc.loadFromDefault();
3232

3333
const k8sApi = kc.makeApiClient(k8s.Core_v1Api);
3434

35-
k8sApi.listNamespacedPod('default')
36-
.then((res) => {
37-
console.log(res.body);
38-
});
35+
k8sApi.listNamespacedPod('default').then((res) => {
36+
console.log(res.body);
37+
});
3938
```
4039

4140
## Create a new namespace
41+
4242
```javascript
4343
const k8s = require('@kubernetes/client-node');
4444

@@ -48,29 +48,28 @@ kc.loadFromDefault();
4848
const k8sApi = kc.makeApiClient(k8s.Core_v1Api);
4949

5050
var namespace = {
51-
metadata: {
52-
name: 'test'
53-
}
51+
metadata: {
52+
name: 'test',
53+
},
5454
};
5555

5656
k8sApi.createNamespace(namespace).then(
57-
(response) => {
58-
console.log('Created namespace');
59-
console.log(response);
60-
k8sApi.readNamespace(namespace.metadata.name).then(
61-
(response) => {
57+
(response) => {
58+
console.log('Created namespace');
6259
console.log(response);
63-
k8sApi.deleteNamespace(
64-
namespace.metadata.name, {} /* delete options */);
65-
});
66-
},
67-
(err) => {
68-
console.log('Error!: ' + err);
69-
}
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+
},
7068
);
7169
```
7270

7371
# Additional Examples
72+
7473
There are several more examples in the [examples](https://github.com/kubernetes-client/javascript/tree/master/examples) directory.
7574

7675
# Development
@@ -92,6 +91,14 @@ cd javascript
9291
../gen/openapi/typescript.sh src settings
9392
```
9493

94+
## Formatting
95+
96+
Run `npm run format` or install an editor plugin like https://github.com/prettier/prettier-vscode.
97+
98+
## Linting
99+
100+
Run `npm run lint` or install an editor plugin like https://github.com/Microsoft/vscode-typescript-tslint-plugin
101+
95102
# Testing
96103

97104
Tests are written using the [Chai](http://chaijs.com/) library. See

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"main": "dist/index.js",
1515
"types": "dist/index.d.ts",
1616
"scripts": {
17-
"lint": "tslint --project \".\"",
17+
"format": "prettier --loglevel error --write './src/**/*.ts'",
18+
"lint": "tslint --project \".\" && prettier --check './src/**/*.ts'",
1819
"lint-examples": "tslint --project \"./examples/typescript\"",
1920
"clean": "rm -Rf node_modules/ dist/",
2021
"build": "tsc",
@@ -71,6 +72,7 @@
7172
"mocha": "^5.2.0",
7273
"mock-fs": "^4.7.0",
7374
"nyc": "^13.1.0",
75+
"prettier": "~1.16.4",
7476
"source-map-support": "^0.5.9",
7577
"stream-buffers": "^3.0.2",
7678
"ts-mockito": "^2.3.1",
@@ -85,5 +87,12 @@
8587
"keywords": [
8688
"kubernetes",
8789
"client"
88-
]
90+
],
91+
"prettier": {
92+
"tabWidth": 4,
93+
"printWidth": 110,
94+
"trailingComma": "all",
95+
"singleQuote": true,
96+
"arrowParens": "always"
97+
}
8998
}

src/attach.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ export class Attach {
1616
}
1717
}
1818

19-
public async attach(namespace: string, podName: string, containerName: string,
20-
stdout: stream.Writable | any, stderr: stream.Writable | any, stdin: stream.Readable | any,
21-
tty: boolean): Promise<WebSocket> {
19+
public async attach(
20+
namespace: string,
21+
podName: string,
22+
containerName: string,
23+
stdout: stream.Writable | any,
24+
stderr: stream.Writable | any,
25+
stdin: stream.Readable | any,
26+
tty: boolean,
27+
): Promise<WebSocket> {
2228
const query = {
2329
container: containerName,
2430
stderr: stderr != null,
@@ -28,10 +34,14 @@ export class Attach {
2834
};
2935
const queryStr = querystring.stringify(query);
3036
const path = `/api/v1/namespaces/${namespace}/pods/${podName}/attach?${queryStr}`;
31-
const conn = await this.handler.connect(path, null, (streamNum: number, buff: Buffer): boolean => {
32-
WebSocketHandler.handleStandardStreams(streamNum, buff, stdout, stderr);
33-
return true;
34-
});
37+
const conn = await this.handler.connect(
38+
path,
39+
null,
40+
(streamNum: number, buff: Buffer): boolean => {
41+
WebSocketHandler.handleStandardStreams(streamNum, buff, stdout, stderr);
42+
return true;
43+
},
44+
);
3545
if (stdin != null) {
3646
WebSocketHandler.handleStandardInput(conn, stdin);
3747
}

src/attach_test.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,25 @@ describe('Attach', () => {
2121
const pod = 'somepod';
2222
const container = 'somecontainer';
2323

24-
await attach.attach(
25-
namespace, pod, container, osStream, errStream, isStream, false);
24+
await attach.attach(namespace, pod, container, osStream, errStream, isStream, false);
2625

2726
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/attach`;
2827
let args = `container=${container}&stderr=true&stdin=true&stdout=true&tty=false`;
2928
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
3029

31-
await attach.attach(
32-
namespace, pod, container, null, null, null, false);
30+
await attach.attach(namespace, pod, container, null, null, null, false);
3331
args = `container=${container}&stderr=false&stdin=false&stdout=false&tty=false`;
3432
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
3533

36-
await attach.attach(
37-
namespace, pod, container, osStream, null, null, false);
34+
await attach.attach(namespace, pod, container, osStream, null, null, false);
3835
args = `container=${container}&stderr=false&stdin=false&stdout=true&tty=false`;
3936
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
4037

41-
await attach.attach(
42-
namespace, pod, container, osStream, errStream, null, false);
38+
await attach.attach(namespace, pod, container, osStream, errStream, null, false);
4339
args = `container=${container}&stderr=true&stdin=false&stdout=true&tty=false`;
4440
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
4541

46-
await attach.attach(
47-
namespace, pod, container, osStream, errStream, null, true);
42+
await attach.attach(namespace, pod, container, osStream, errStream, null, true);
4843
args = `container=${container}&stderr=true&stdin=false&stdout=true&tty=true`;
4944
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();
5045
});
@@ -67,8 +62,7 @@ describe('Attach', () => {
6762
const fakeConn: WebSocket = mock(WebSocket);
6863
when(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).thenResolve(fakeConn);
6964

70-
await attach.attach(
71-
namespace, pod, container, osStream, errStream, isStream, false);
65+
await attach.attach(namespace, pod, container, osStream, errStream, isStream, false);
7266
const [, , outputFn] = capture(fakeWebSocket.connect).last();
7367

7468
/* tslint:disable:no-unused-expression */

src/cache.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T> {
1212
private objects: T[] = [];
1313
private readonly indexCache: { [key: string]: T[] } = {};
1414

15-
public constructor(private readonly path: string,
16-
private readonly watch: Watch,
17-
private readonly listFn: (callback: ListCallback<T>) => void) {
15+
public constructor(
16+
private readonly path: string,
17+
private readonly watch: Watch,
18+
private readonly listFn: (callback: ListCallback<T>) => void,
19+
) {
1820
this.watch = watch;
1921
this.listFn = listFn;
2022
this.doneHandler(null);
2123
}
2224

2325
public get(name: string, namespace?: string): T | undefined {
24-
return this.objects.find((obj: T): boolean => {
25-
return (obj.metadata.name === name &&
26-
(!namespace || obj.metadata.namespace === namespace));
27-
});
26+
return this.objects.find(
27+
(obj: T): boolean => {
28+
return obj.metadata.name === name && (!namespace || obj.metadata.namespace === namespace);
29+
},
30+
);
2831
}
2932

3033
public list(namespace?: string | undefined): ReadonlyArray<T> {
@@ -86,8 +89,7 @@ export function addOrUpdateObject<T extends KubernetesObject>(objects: T[], obj:
8689
}
8790

8891
function isSameObject<T extends KubernetesObject>(o1: T, o2: T): boolean {
89-
return o1.metadata.name === o2.metadata.name &&
90-
o1.metadata.namespace === o2.metadata.namespace;
92+
return o1.metadata.name === o2.metadata.name && o1.metadata.namespace === o2.metadata.namespace;
9193
}
9294

9395
function findKubernetesObject<T extends KubernetesObject>(objects: T[], obj: T): number {

src/cache_test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('ListWatchCache', () => {
3131
expect(cache.get('name1')).to.equal(list[0]);
3232
expect(cache.get('name2')).to.equal(list[1]);
3333

34-
watchHandler('ADDED', {
34+
watchHandler('ADDED', {
3535
metadata: {
3636
name: 'name3',
3737
} as V1ObjectMeta,
@@ -40,7 +40,7 @@ describe('ListWatchCache', () => {
4040
expect(cache.list().length).to.equal(3);
4141
expect(cache.get('name3')).to.not.equal(null);
4242

43-
watchHandler('MODIFIED', {
43+
watchHandler('MODIFIED', {
4444
metadata: {
4545
name: 'name3',
4646
resourceVersion: 'baz',
@@ -96,7 +96,7 @@ describe('ListWatchCache', () => {
9696
expect(cache.list('ns2').length).to.equal(1);
9797
expect(cache.list('ns2')[0].metadata.name).to.equal('name2');
9898

99-
watchHandler('ADDED', {
99+
watchHandler('ADDED', {
100100
metadata: {
101101
name: 'name3',
102102
namespace: 'ns3',
@@ -106,7 +106,7 @@ describe('ListWatchCache', () => {
106106
expect(cache.list().length).to.equal(3);
107107
expect(cache.get('name3', 'ns3')).to.not.equal(null);
108108

109-
watchHandler('MODIFIED', {
109+
watchHandler('MODIFIED', {
110110
metadata: {
111111
name: 'name3',
112112
namespace: 'ns3',

src/cloud_auth.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ interface Config {
1919
}
2020
export class CloudAuth implements Authenticator {
2121
public isAuthProvider(user: User): boolean {
22-
return (
23-
user.authProvider.name === 'azure' ||
24-
user.authProvider.name === 'gcp'
25-
);
22+
return user.authProvider.name === 'azure' || user.authProvider.name === 'gcp';
2623
}
2724

2825
public getToken(user: User): string | null {

0 commit comments

Comments
 (0)