Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions examples/typescript/watch/watch-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const kc = new k8s.KubeConfig();
kc.loadFromDefault();

const watch = new k8s.Watch(kc);
const req = watch.watch('/api/v1/namespaces',
watch.watch('/api/v1/namespaces',
// optional query parameters can go here.
{},
// callback is called for each received object.
Expand All @@ -29,7 +29,8 @@ const req = watch.watch('/api/v1/namespaces',
(err) => {
// tslint:disable-next-line:no-console
console.log(err);
});

// watch returns a request object which you can use to abort the watch.
setTimeout(() => { req.abort(); }, 10 * 1000);
})
.then((req) => {
// watch returns a request object which you can use to abort the watch.
setTimeout(() => { req.abort(); }, 10 * 1000);
});
2 changes: 1 addition & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
const list = result.body;
deleteItems(this.objects, list.items, this.callbackCache[DELETE].slice());
this.addOrUpdateItems(list.items);
this.watch.watch(
await this.watch.watch(
this.path,
{ resourceVersion: list.metadata!.resourceVersion },
this.watchHandler.bind(this),
Expand Down
7 changes: 4 additions & 3 deletions src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ export class Watch {
}
}

public watch(
public async watch(
path: string,
queryParams: any,
callback: (phase: string, obj: any) => void,
done: (err: any) => void,
): any {
): Promise<any> {
const cluster = this.config.getCurrentCluster();
if (!cluster) {
throw new Error('No currently active cluster');
Expand All @@ -54,7 +54,7 @@ export class Watch {
useQuerystring: true,
json: true,
};
this.config.applyToRequest(requestOptions);
await this.config.applyToRequest(requestOptions);

const stream = byline.createStream();
stream.on('data', (line) => {
Expand All @@ -69,6 +69,7 @@ export class Watch {
done(err);
});
stream.on('close', () => done(null));

const req = this.requestImpl.webRequest(requestOptions, (error, response, body) => {
if (error) {
done(error);
Expand Down
16 changes: 8 additions & 8 deletions src/watch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('Watch', () => {
const watch = new Watch(kc);
});

it('should watch correctly', () => {
it('should watch correctly', async () => {
const kc = new KubeConfig();
Object.assign(kc, fakeConfig);
const fakeRequestor = mock(DefaultRequest);
Expand Down Expand Up @@ -75,7 +75,7 @@ describe('Watch', () => {
let doneCalled = false;
let doneErr: any;

watch.watch(
await watch.watch(
path,
{},
(phase: string, obj: string) => {
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('Watch', () => {
expect(doneErr).to.deep.equal(errIn);
});

it('should handle errors correctly', () => {
it('should handle errors correctly', async () => {
const kc = new KubeConfig();
Object.assign(kc, fakeConfig);
const fakeRequestor = mock(DefaultRequest);
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('Watch', () => {
let doneCalled = false;
let doneErr: any;

watch.watch(
await watch.watch(
path,
{},
(phase: string, obj: string) => {
Expand Down Expand Up @@ -171,7 +171,7 @@ describe('Watch', () => {
expect(doneErr).to.deep.equal(errIn);
});

it('should handle server side close correctly', () => {
it('should handle server side close correctly', async () => {
const kc = new KubeConfig();
Object.assign(kc, fakeConfig);
const fakeRequestor = mock(DefaultRequest);
Expand Down Expand Up @@ -200,7 +200,7 @@ describe('Watch', () => {
let doneCalled = false;
let doneErr: any;

watch.watch(
await watch.watch(
path,
{},
(phase: string, obj: string) => {
Expand Down Expand Up @@ -229,7 +229,7 @@ describe('Watch', () => {
expect(doneErr).to.be.null;
});

it('should ignore JSON parse errors', () => {
it('should ignore JSON parse errors', async () => {
const kc = new KubeConfig();
Object.assign(kc, fakeConfig);
const fakeRequestor = mock(DefaultRequest);
Expand All @@ -256,7 +256,7 @@ describe('Watch', () => {
const receivedTypes: string[] = [];
const receivedObjects: string[] = [];

watch.watch(
await watch.watch(
path,
{},
(recievedType: string, recievedObject: string) => {
Expand Down