Skip to content

fix(cache): watch errors must call done handler #781

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

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
18 changes: 13 additions & 5 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In

private async doneHandler(err: any): Promise<any> {
this._stop();
if (err && err.statusCode === 410) {
if (
err &&
((err as { statusCode?: number }).statusCode === 410 || (err as { code?: number }).code === 410)
) {
this.resourceVersion = '';
} else if (err) {
this.callbackCache[ERROR].forEach((elt: ErrorCallback) => elt(err));
Expand Down Expand Up @@ -187,7 +190,11 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
addOrUpdateObject(namespaceList, obj);
}

private watchHandler(phase: string, obj: T, watchObj?: any): void {
private async watchHandler(
phase: string,
obj: T,
watchObj?: { type: string; object: KubernetesObject },
): Promise<void> {
switch (phase) {
case 'ADDED':
case 'MODIFIED':
Expand All @@ -213,10 +220,11 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
case 'BOOKMARK':
// nothing to do, here for documentation, mostly.
break;
case 'ERROR':
await this.doneHandler(obj);
return;
}
if (watchObj && watchObj.metadata) {
this.resourceVersion = watchObj.metadata.resourceVersion;
}
this.resourceVersion = obj.metadata!.resourceVersion || '';
}
}

Expand Down
87 changes: 85 additions & 2 deletions src/cache_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1096,9 +1096,10 @@ describe('ListWatchCache', () => {
{
metadata: {
name: 'name3',
resourceVersion: '23456',
} as V1ObjectMeta,
} as V1Namespace,
{ metadata: { resourceVersion: '23456' } },
{ type: 'ADDED', metadata: { resourceVersion: '23456' } },
);

await informer.stop();
Expand Down Expand Up @@ -1153,9 +1154,91 @@ describe('ListWatchCache', () => {
{
metadata: {
name: 'name3',
resourceVersion: '23456',
} as V1ObjectMeta,
} as V1Namespace,
{ metadata: { resourceVersion: '23456' } },
{ type: 'ADDED', metadata: { resourceVersion: '23456' } },
);

await informer.stop();

let errorEmitted = false;
informer.on('error', () => (errorEmitted = true));

promise = new Promise((resolve) => {
mock.when(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
).thenCall(() => {
resolve(new FakeRequest());
});
});

informer.start();
await promise;

const [, , , doneHandler] = mock.capture(fakeWatch.watch).last();

const object = {
kind: 'Status',
apiVersion: 'v1',
metadata: {},
status: 'Failure',
message: 'too old resource version: 12345 (1234)',
reason: 'Expired',
code: 410,
};
await watchHandler('ERROR', object, { type: 'ERROR', object });

mock.verify(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
).thrice();
expect(errorEmitted).to.equal(false);
expect(listCalls).to.be.equal(2);
});

it('should list if the watch errors from the last version', async () => {
const fakeWatch = mock.mock(Watch);
const list: V1Pod[] = [];
const listObj = {
metadata: {
resourceVersion: '12345',
} as V1ListMeta,
items: list,
} as V1NamespaceList;

let listCalls = 0;
const listFn: ListPromise<V1Namespace> = function(): Promise<{
response: http.IncomingMessage;
body: V1NamespaceList;
}> {
return new Promise<{ response: http.IncomingMessage; body: V1NamespaceList }>((resolve) => {
listCalls++;
resolve({ response: {} as http.IncomingMessage, body: listObj });
});
};
let promise = new Promise((resolve) => {
mock.when(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
).thenCall(() => {
resolve(new FakeRequest());
});
});

const informer = new ListWatch('/some/path', mock.instance(fakeWatch), listFn, false);

informer.start();
await promise;

const [, , watchHandler] = mock.capture(fakeWatch.watch).last();
watchHandler(
'ADDED',
{
metadata: {
name: 'name3',
resourceVersion: '23456',
} as V1ObjectMeta,
} as V1Namespace,
{ type: 'ADDED', metadata: { resourceVersion: '23456' } },
);

await informer.stop();
Expand Down