Skip to content

Port of #1934 and #1936 #1937

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

private watchHandler(phase: string, obj: T, watchObj?: any): void {
switch (phase) {
case 'ERROR':
if ((obj as { code?: number }).code === 410) {
this.resourceVersion = '';
}
break;
case 'ADDED':
case 'MODIFIED':
addOrUpdateObject(
Expand Down
69 changes: 69 additions & 0 deletions src/cache_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,75 @@ describe('ListWatchCache', () => {
expect(listCalls).to.be.equal(2);
});

it('should list if the watch cannot be restarted from the latest resourceVersion with an ERROR event', 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<V1NamespaceList> {
return new Promise<V1NamespaceList>((resolve) => {
listCalls++;
resolve(listObj);
});
};
let promise = new Promise((resolve) => {
mock.when(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
).thenCall(() => {
resolve(new AbortController());
});
});

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',
} as V1ObjectMeta,
} as V1Namespace,
{ 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 AbortController());
});
});

informer.start();
await promise;

const [, , watchHandler2, doneHandler] = mock.capture(fakeWatch.watch).last();
watchHandler2('ERROR', {
code: 410,
});
doneHandler(undefined);
mock.verify(
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
).twice();
expect(errorEmitted).to.equal(false);
expect(listCalls).to.be.equal(2);
});

it('should send label selector', async () => {
const APP_LABEL_SELECTOR = 'app=foo';

Expand Down
Loading