Skip to content

Commit 2a961a0

Browse files
committed
Updates/fixes for watch
1 parent e0d3695 commit 2a961a0

File tree

2 files changed

+38
-47
lines changed

2 files changed

+38
-47
lines changed

src/cache.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
168168
}
169169
this.objects = deleteItems(this.objects, list.items, this.callbackCache[DELETE].slice());
170170
this.addOrUpdateItems(list.items);
171-
this.resourceVersion = list.metadata!.resourceVersion || '';
171+
this.resourceVersion = list.metadata ? list.metadata!.resourceVersion || '' : '';
172172
}
173173
const queryParams = {
174174
resourceVersion: this.resourceVersion,
@@ -192,6 +192,9 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
192192
}
193193

194194
private addOrUpdateItems(items: T[]): void {
195+
if (items === undefined || items === null) {
196+
return;
197+
}
195198
items.forEach((obj: T) => {
196199
addOrUpdateObject(
197200
this.objects,
@@ -212,7 +215,8 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
212215
if ((obj as { code?: number }).code === 410) {
213216
this.resourceVersion = '';
214217
}
215-
break;
218+
// We don't restart here, because it should be handled by the watch exiting if necessary
219+
return;
216220
case 'ADDED':
217221
case 'MODIFIED':
218222
addOrUpdateObject(
@@ -228,17 +232,17 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
228232
case 'BOOKMARK':
229233
// nothing to do, here for documentation, mostly.
230234
break;
231-
case 'ERROR':
232-
await this.doneHandler(obj);
233-
return;
234235
}
235-
this.resourceVersion = obj.metadata!.resourceVersion || '';
236+
this.resourceVersion = obj.metadata ? obj.metadata!.resourceVersion || '' : '';
236237
}
237238
}
238239

239240
// exported for testing
240241
export function cacheMapFromList<T extends KubernetesObject>(newObjects: T[]): CacheMap<T> {
241242
const objects: CacheMap<T> = new Map();
243+
if (newObjects === undefined || newObjects === null) {
244+
return objects;
245+
}
242246
// build up the new list
243247
for (const obj of newObjects) {
244248
let namespaceObjects = objects.get(obj.metadata!.namespace || '');

src/cache_test.ts

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ describe('ListWatchCache', () => {
235235
watchHandler('ADDED', {
236236
metadata: {
237237
name: 'name3',
238+
resourceVersion: 'blah',
238239
} as V1ObjectMeta,
239240
} as V1Namespace);
240241

@@ -245,40 +246,28 @@ describe('ListWatchCache', () => {
245246
} as V1ObjectMeta,
246247
} as V1Namespace);
247248

248-
watchHandler(
249-
'DELETED',
250-
{
251-
metadata: {
252-
name: 'name2',
253-
resourceVersion: 'blah',
254-
} as V1ObjectMeta,
255-
} as V1Namespace,
256-
{
257-
metadata: {
258-
resourceVersion: '54321',
259-
},
260-
},
261-
);
249+
watchHandler('DELETED', {
250+
metadata: {
251+
name: 'name2',
252+
resourceVersion: '54321',
253+
} as V1ObjectMeta,
254+
} as V1Namespace);
262255

263256
const [addResult, updateResult, deleteResult] = await Promise.all([
264257
addPromise,
265258
updatePromise,
266259
deletePromise,
267260
]);
268-
deepStrictEqual(addResult.metadata, { name: 'name3' });
261+
deepStrictEqual(addResult.metadata, { name: 'name3', resourceVersion: 'blah' });
269262
deepStrictEqual(updateResult.metadata, { name: 'name3', resourceVersion: 'baz' });
270-
deepStrictEqual(deleteResult.metadata, { name: 'name2', resourceVersion: 'blah' });
263+
deepStrictEqual(deleteResult.metadata, { name: 'name2', resourceVersion: '54321' });
271264
strictEqual(informer.latestResourceVersion(), '54321');
272265

273-
watchHandler(
274-
'BOOKMARK',
275-
{},
276-
{
277-
metadata: {
278-
resourceVersion: '5454',
279-
},
266+
watchHandler('BOOKMARK', {
267+
metadata: {
268+
resourceVersion: '5454',
280269
},
281-
);
270+
});
282271
strictEqual(informer.latestResourceVersion(), '5454');
283272
});
284273

@@ -1275,7 +1264,7 @@ describe('ListWatchCache', () => {
12751264
mock.when(
12761265
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
12771266
).thenCall(() => {
1278-
resolve(new FakeRequest());
1267+
resolve({});
12791268
});
12801269
});
12811270

@@ -1294,39 +1283,35 @@ describe('ListWatchCache', () => {
12941283
code: 410,
12951284
};
12961285
await watchHandler('ERROR', object, { type: 'ERROR', object });
1286+
await doneHandler(null);
12971287

12981288
mock.verify(
12991289
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
13001290
).thrice();
1301-
expect(errorEmitted).to.equal(false);
1302-
expect(listCalls).to.be.equal(2);
1291+
strictEqual(errorEmitted, false);
1292+
strictEqual(listCalls, 2);
13031293
});
13041294

13051295
it('should list if the watch errors from the last version', async () => {
13061296
const fakeWatch = mock.mock(Watch);
1307-
const list: V1Pod[] = [];
1308-
const listObj = {
1309-
metadata: {
1310-
resourceVersion: '12345',
1311-
} as V1ListMeta,
1312-
items: list,
1313-
} as V1NamespaceList;
13141297

13151298
let listCalls = 0;
1316-
const listFn: ListPromise<V1Namespace> = function(): Promise<{
1317-
response: http.IncomingMessage;
1318-
body: V1NamespaceList;
1319-
}> {
1320-
return new Promise<{ response: http.IncomingMessage; body: V1NamespaceList }>((resolve) => {
1299+
const listFn: ListPromise<V1Namespace> = function (): Promise<V1NamespaceList> {
1300+
return new Promise<V1NamespaceList>((resolve, reject) => {
13211301
listCalls++;
1322-
resolve({ response: {} as http.IncomingMessage, body: listObj });
1302+
resolve({
1303+
metadata: {
1304+
resourceVersion: '12345',
1305+
} as V1ListMeta,
1306+
items: [],
1307+
} as V1NamespaceList);
13231308
});
13241309
};
13251310
let promise = new Promise((resolve) => {
13261311
mock.when(
13271312
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
13281313
).thenCall(() => {
1329-
resolve(new FakeRequest());
1314+
resolve({});
13301315
});
13311316
});
13321317

@@ -1418,6 +1403,8 @@ describe('ListWatchCache', () => {
14181403
);
14191404

14201405
await informer.stop();
1406+
strictEqual(listCalls, 1);
1407+
listCalls = 0;
14211408

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

0 commit comments

Comments
 (0)