Skip to content

Commit a34755a

Browse files
k8s-ci-robotfeloy
authored andcommitted
Merge pull request #1934 from feloy/fix-1933/reset-resourceVersion
Reset resource version
2 parents 62caf10 + c1e00b8 commit a34755a

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

src/cache.ts

+5
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
186186

187187
private watchHandler(phase: string, obj: T, watchObj?: any): void {
188188
switch (phase) {
189+
case 'ERROR':
190+
if ((obj as { code?: number }).code === 410) {
191+
this.resourceVersion = '';
192+
}
193+
break;
189194
case 'ADDED':
190195
case 'MODIFIED':
191196
addOrUpdateObject(

src/cache_test.ts

+69
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,75 @@ describe('ListWatchCache', () => {
11001100
expect(listCalls).to.be.equal(2);
11011101
});
11021102

1103+
it('should list if the watch cannot be restarted from the latest resourceVersion with an ERROR event', async () => {
1104+
const fakeWatch = mock.mock(Watch);
1105+
const list: V1Pod[] = [];
1106+
const listObj = {
1107+
metadata: {
1108+
resourceVersion: '12345',
1109+
} as V1ListMeta,
1110+
items: list,
1111+
} as V1NamespaceList;
1112+
1113+
let listCalls = 0;
1114+
const listFn: ListPromise<V1Namespace> = function (): Promise<V1NamespaceList> {
1115+
return new Promise<V1NamespaceList>((resolve) => {
1116+
listCalls++;
1117+
resolve(listObj);
1118+
});
1119+
};
1120+
let promise = new Promise((resolve) => {
1121+
mock.when(
1122+
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
1123+
).thenCall(() => {
1124+
resolve(new AbortController());
1125+
});
1126+
});
1127+
1128+
const informer = new ListWatch('/some/path', mock.instance(fakeWatch), listFn, false);
1129+
1130+
informer.start();
1131+
await promise;
1132+
1133+
const [, , watchHandler] = mock.capture(fakeWatch.watch).last();
1134+
watchHandler(
1135+
'ADDED',
1136+
{
1137+
metadata: {
1138+
name: 'name3',
1139+
} as V1ObjectMeta,
1140+
} as V1Namespace,
1141+
{ metadata: { resourceVersion: '23456' } },
1142+
);
1143+
1144+
await informer.stop();
1145+
1146+
let errorEmitted = false;
1147+
informer.on('error', () => (errorEmitted = true));
1148+
1149+
promise = new Promise((resolve) => {
1150+
mock.when(
1151+
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
1152+
).thenCall(() => {
1153+
resolve(new AbortController());
1154+
});
1155+
});
1156+
1157+
informer.start();
1158+
await promise;
1159+
1160+
const [, , watchHandler2, doneHandler] = mock.capture(fakeWatch.watch).last();
1161+
watchHandler2('ERROR', {
1162+
code: 410,
1163+
});
1164+
doneHandler(undefined);
1165+
mock.verify(
1166+
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
1167+
).twice();
1168+
expect(errorEmitted).to.equal(false);
1169+
expect(listCalls).to.be.equal(2);
1170+
});
1171+
11031172
it('should send label selector', async () => {
11041173
const APP_LABEL_SELECTOR = 'app=foo';
11051174

0 commit comments

Comments
 (0)