Skip to content

Improve closing behavior for cache/informer. #505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 6 additions & 1 deletion examples/typescript/watch/watch-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as k8s from '@kubernetes/client-node';
const kc = new k8s.KubeConfig();
kc.loadFromDefault();

let request: k8s.RequestResult = null;
const watch = new k8s.Watch(kc);
watch.watch('/api/v1/namespaces',
// optional query parameters can go here.
Expand Down Expand Up @@ -34,8 +35,12 @@ watch.watch('/api/v1/namespaces',
(err) => {
// tslint:disable-next-line:no-console
console.log(err);
if (request != null) {
request!.destroy();
}
})
.then((req) => {
request = req;
// watch returns a request object which you can use to abort the watch.
setTimeout(() => { req.abort(); }, 10 * 1000);
setTimeout(() => { req.abort(); req.destroy(); }, 10 * 1000);
});
19 changes: 16 additions & 3 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ADD, DELETE, ERROR, Informer, ListPromise, ObjectCallback, UPDATE } from './informer';
import { KubernetesObject } from './types';
import { Watch } from './watch';
import { RequestResult, Watch } from './watch';

export interface ObjectCache<T> {
get(name: string, namespace?: string): T | undefined;
Expand All @@ -12,6 +12,8 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
private resourceVersion: string;
private readonly indexCache: { [key: string]: T[] } = {};
private readonly callbackCache: { [key: string]: Array<ObjectCallback<T>> } = {};
private request: RequestResult | null;
private stopped: boolean;

public constructor(
private readonly path: string,
Expand All @@ -23,7 +25,9 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
this.callbackCache[UPDATE] = [];
this.callbackCache[DELETE] = [];
this.callbackCache[ERROR] = [];
this.request = null;
this.resourceVersion = '';
this.stopped = false;
if (autoStart) {
this.doneHandler(null);
}
Expand Down Expand Up @@ -73,8 +77,17 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
}

private async doneHandler(err: any) {
if (this.request != null) {
this.request.destroy();
this.request = null;
}
if (err) {
this.callbackCache[ERROR].forEach((elt: ObjectCallback<T>) => elt(err));
// On an error, the done handler is called twice with the same error, see details in
// watch.ts
if (!this.stopped) {
this.stopped = true;
this.callbackCache[ERROR].forEach((elt: ObjectCallback<T>) => elt(err));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see that we would set this.stopped to false again in the start() method. If I understand correctly it prevents the cache from being reused. And if we set stopped to false in start() method then it would create a race condition when doneHandler is called twice and stopped is set to false between those two calls. To me, that does not look like a good way to go. Fixing the watcher to call doneCallback just once sounds like a better option.

}
return;
}
// TODO: Don't always list here for efficiency
Expand All @@ -85,7 +98,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);
await this.watch.watch(
this.request = await this.watch.watch(
this.path,
{ resourceVersion: list.metadata!.resourceVersion },
this.watchHandler.bind(this),
Expand Down
15 changes: 13 additions & 2 deletions src/watch.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import byline = require('byline');
import request = require('request');
import { Duplex } from 'stream';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to move stream-buffers from devDependencies to dependencies in package.json for this to work.

import { KubeConfig } from './config';

export interface WatchUpdate {
type: string;
object: object;
}

export interface RequestResult {
pipe(stream: Duplex);
destroy();
}

export interface RequestInterface {
webRequest(opts: request.Options, callback: (err, response, body) => void): any;
webRequest(opts: request.Options, callback: (err, response, body) => void): RequestResult;
}

export class DefaultRequest implements RequestInterface {
public webRequest(opts: request.Options, callback: (err, response, body) => void): any {
public webRequest(opts: request.Options, callback: (err, response, body) => void): RequestResult {
return request(opts, callback);
}
}
Expand Down Expand Up @@ -53,6 +59,7 @@ export class Watch {
uri: url,
useQuerystring: true,
json: true,
pool: false,
};
await this.config.applyToRequest(requestOptions);

Expand All @@ -70,6 +77,10 @@ export class Watch {
errOut = err;
done(err);
});
// TODO: I don't love this, because both 'error' and 'close' call the done handler with the same error
// We should probably only do one or the other, but there's challenges because of async delivery and it's
// important to know if the close event is occurring because of an error. So for now, this needs to be
// handled in the client.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of leaving the code in watcher as it is and having workarounds for all potential users of the watcher that are IMHO unreliable anyway, I think it would have been better to fix it here. Would a boolean variable indicating that the done callback was called be sufficient? That way we could guarantee that the done callback is called at most once, that is exactly what users of the watcher expect.

stream.on('close', () => done(errOut));

const req = this.requestImpl.webRequest(requestOptions, (error, response, body) => {
Expand Down
5 changes: 5 additions & 0 deletions src/watch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('Watch', () => {

const fakeRequest = {
pipe: (stream) => {},
destroy: () => {},
};

when(fakeRequestor.webRequest(anything(), anyFunction())).thenReturn(fakeRequest);
Expand Down Expand Up @@ -112,6 +113,7 @@ describe('Watch', () => {
stream.write(JSON.stringify(obj1) + '\n');
stream.write(JSON.stringify(obj2) + '\n');
},
destroy: () => {},
};

when(fakeRequestor.webRequest(anything(), anyFunction())).thenReturn(fakeRequest);
Expand Down Expand Up @@ -180,6 +182,7 @@ describe('Watch', () => {
stream.emit('error', errIn);
stream.emit('close');
},
destroy: () => {},
};

when(fakeRequestor.webRequest(anything(), anyFunction())).thenReturn(fakeRequest);
Expand Down Expand Up @@ -240,6 +243,7 @@ describe('Watch', () => {
stream.write(JSON.stringify(obj1) + '\n');
stream.emit('close');
},
destroy: () => {},
};

when(fakeRequestor.webRequest(anything(), anyFunction())).thenReturn(fakeRequest);
Expand Down Expand Up @@ -298,6 +302,7 @@ describe('Watch', () => {
stream.write(JSON.stringify(obj) + '\n');
stream.write('{"truncated json\n');
},
destroy: () => {},
};

when(fakeRequestor.webRequest(anything(), anyFunction())).thenReturn(fakeRequest);
Expand Down