Skip to content

Commit

Permalink
Merge pull request kubernetes-client#212 from sheldonkwok/feature/pre…
Browse files Browse the repository at this point in the history
…ttier

Feature/prettier
  • Loading branch information
k8s-ci-robot authored Feb 14, 2019
2 parents fb6c06c + 43466f1 commit 447750e
Show file tree
Hide file tree
Showing 24 changed files with 314 additions and 242 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/api.ts
49 changes: 28 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[![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)
[![Client Support Level](https://img.shields.io/badge/kubernetes%20client-beta-green.svg?style=flat&colorA=306CE8)](http://bit.ly/kubernetes-client-support-badge)


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

# Installation

```console
npm install @kubernetes/client-node
```

# Example code

## List all pods

```javascript
const k8s = require('@kubernetes/client-node');


const kc = new k8s.KubeConfig();
kc.loadFromDefault();

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

k8sApi.listNamespacedPod('default')
.then((res) => {
console.log(res.body);
});
k8sApi.listNamespacedPod('default').then((res) => {
console.log(res.body);
});
```

## Create a new namespace

```javascript
const k8s = require('@kubernetes/client-node');

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

var namespace = {
metadata: {
name: 'test'
}
metadata: {
name: 'test',
},
};

k8sApi.createNamespace(namespace).then(
(response) => {
console.log('Created namespace');
console.log(response);
k8sApi.readNamespace(namespace.metadata.name).then(
(response) => {
(response) => {
console.log('Created namespace');
console.log(response);
k8sApi.deleteNamespace(
namespace.metadata.name, {} /* delete options */);
});
},
(err) => {
console.log('Error!: ' + err);
}
k8sApi.readNamespace(namespace.metadata.name).then((response) => {
console.log(response);
k8sApi.deleteNamespace(namespace.metadata.name, {} /* delete options */);
});
},
(err) => {
console.log('Error!: ' + err);
},
);
```

# Additional Examples

There are several more examples in the [examples](https://github.com/kubernetes-client/javascript/tree/master/examples) directory.

# Development
Expand All @@ -92,6 +91,14 @@ cd javascript
../gen/openapi/typescript.sh src settings
```

## Formatting

Run `npm run format` or install an editor plugin like https://github.com/prettier/prettier-vscode.

## Linting

Run `npm run lint` or install an editor plugin like https://github.com/Microsoft/vscode-typescript-tslint-plugin

# Testing

Tests are written using the [Chai](http://chaijs.com/) library. See
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"lint": "tslint --project \".\"",
"format": "prettier --loglevel error --write './src/**/*.ts'",
"lint": "tslint --project \".\" && prettier --check './src/**/*.ts'",
"lint-examples": "tslint --project \"./examples/typescript\"",
"clean": "rm -Rf node_modules/ dist/",
"build": "tsc",
Expand Down Expand Up @@ -71,6 +72,7 @@
"mocha": "^5.2.0",
"mock-fs": "^4.7.0",
"nyc": "^13.1.0",
"prettier": "~1.16.4",
"source-map-support": "^0.5.9",
"stream-buffers": "^3.0.2",
"ts-mockito": "^2.3.1",
Expand All @@ -85,5 +87,12 @@
"keywords": [
"kubernetes",
"client"
]
],
"prettier": {
"tabWidth": 4,
"printWidth": 110,
"trailingComma": "all",
"singleQuote": true,
"arrowParens": "always"
}
}
24 changes: 17 additions & 7 deletions src/attach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ export class Attach {
}
}

public async attach(namespace: string, podName: string, containerName: string,
stdout: stream.Writable | any, stderr: stream.Writable | any, stdin: stream.Readable | any,
tty: boolean): Promise<WebSocket> {
public async attach(
namespace: string,
podName: string,
containerName: string,
stdout: stream.Writable | any,
stderr: stream.Writable | any,
stdin: stream.Readable | any,
tty: boolean,
): Promise<WebSocket> {
const query = {
container: containerName,
stderr: stderr != null,
Expand All @@ -28,10 +34,14 @@ export class Attach {
};
const queryStr = querystring.stringify(query);
const path = `/api/v1/namespaces/${namespace}/pods/${podName}/attach?${queryStr}`;
const conn = await this.handler.connect(path, null, (streamNum: number, buff: Buffer): boolean => {
WebSocketHandler.handleStandardStreams(streamNum, buff, stdout, stderr);
return true;
});
const conn = await this.handler.connect(
path,
null,
(streamNum: number, buff: Buffer): boolean => {
WebSocketHandler.handleStandardStreams(streamNum, buff, stdout, stderr);
return true;
},
);
if (stdin != null) {
WebSocketHandler.handleStandardInput(conn, stdin);
}
Expand Down
18 changes: 6 additions & 12 deletions src/attach_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,25 @@ describe('Attach', () => {
const pod = 'somepod';
const container = 'somecontainer';

await attach.attach(
namespace, pod, container, osStream, errStream, isStream, false);
await attach.attach(namespace, pod, container, osStream, errStream, isStream, false);

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

await attach.attach(
namespace, pod, container, null, null, null, false);
await attach.attach(namespace, pod, container, null, null, null, false);
args = `container=${container}&stderr=false&stdin=false&stdout=false&tty=false`;
verify(fakeWebSocket.connect(`${path}?${args}`, null, anyFunction())).called();

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

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

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

await attach.attach(
namespace, pod, container, osStream, errStream, isStream, false);
await attach.attach(namespace, pod, container, osStream, errStream, isStream, false);
const [, , outputFn] = capture(fakeWebSocket.connect).last();

/* tslint:disable:no-unused-expression */
Expand Down
20 changes: 11 additions & 9 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T> {
private objects: T[] = [];
private readonly indexCache: { [key: string]: T[] } = {};

public constructor(private readonly path: string,
private readonly watch: Watch,
private readonly listFn: (callback: ListCallback<T>) => void) {
public constructor(
private readonly path: string,
private readonly watch: Watch,
private readonly listFn: (callback: ListCallback<T>) => void,
) {
this.watch = watch;
this.listFn = listFn;
this.doneHandler(null);
}

public get(name: string, namespace?: string): T | undefined {
return this.objects.find((obj: T): boolean => {
return (obj.metadata.name === name &&
(!namespace || obj.metadata.namespace === namespace));
});
return this.objects.find(
(obj: T): boolean => {
return obj.metadata.name === name && (!namespace || obj.metadata.namespace === namespace);
},
);
}

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

function isSameObject<T extends KubernetesObject>(o1: T, o2: T): boolean {
return o1.metadata.name === o2.metadata.name &&
o1.metadata.namespace === o2.metadata.namespace;
return o1.metadata.name === o2.metadata.name && o1.metadata.namespace === o2.metadata.namespace;
}

function findKubernetesObject<T extends KubernetesObject>(objects: T[], obj: T): number {
Expand Down
8 changes: 4 additions & 4 deletions src/cache_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('ListWatchCache', () => {
expect(cache.get('name1')).to.equal(list[0]);
expect(cache.get('name2')).to.equal(list[1]);

watchHandler('ADDED', {
watchHandler('ADDED', {
metadata: {
name: 'name3',
} as V1ObjectMeta,
Expand All @@ -40,7 +40,7 @@ describe('ListWatchCache', () => {
expect(cache.list().length).to.equal(3);
expect(cache.get('name3')).to.not.equal(null);

watchHandler('MODIFIED', {
watchHandler('MODIFIED', {
metadata: {
name: 'name3',
resourceVersion: 'baz',
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('ListWatchCache', () => {
expect(cache.list('ns2').length).to.equal(1);
expect(cache.list('ns2')[0].metadata.name).to.equal('name2');

watchHandler('ADDED', {
watchHandler('ADDED', {
metadata: {
name: 'name3',
namespace: 'ns3',
Expand All @@ -106,7 +106,7 @@ describe('ListWatchCache', () => {
expect(cache.list().length).to.equal(3);
expect(cache.get('name3', 'ns3')).to.not.equal(null);

watchHandler('MODIFIED', {
watchHandler('MODIFIED', {
metadata: {
name: 'name3',
namespace: 'ns3',
Expand Down
5 changes: 1 addition & 4 deletions src/cloud_auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ interface Config {
}
export class CloudAuth implements Authenticator {
public isAuthProvider(user: User): boolean {
return (
user.authProvider.name === 'azure' ||
user.authProvider.name === 'gcp'
);
return user.authProvider.name === 'azure' || user.authProvider.name === 'gcp';
}

public getToken(user: User): string | null {
Expand Down
Loading

0 comments on commit 447750e

Please sign in to comment.