Skip to content

Commit 1ff5d5c

Browse files
author
Jan Kryl
committed
cache: skip callback upon restart if object did not change
Currently MOD callbacks are called for all objects in the cache when the watcher is restarted. This commit makes the algorithm smarter and call MOD callback only the object has really changed. The decision is based on "resourceVersion" metadata field in the object.
1 parent 5988253 commit 1ff5d5c

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/cache.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,11 @@ export function addOrUpdateObject<T extends KubernetesObject>(
197197
addCallback.forEach((elt: ObjectCallback<T>) => elt(obj));
198198
}
199199
} else {
200-
objects[ix] = obj;
201-
if (updateCallback) {
202-
updateCallback.forEach((elt: ObjectCallback<T>) => elt(obj));
200+
if (!isSameVersion(objects[ix], obj)) {
201+
objects[ix] = obj;
202+
if (updateCallback) {
203+
updateCallback.forEach((elt: ObjectCallback<T>) => elt(obj));
204+
}
203205
}
204206
}
205207
}
@@ -208,6 +210,14 @@ function isSameObject<T extends KubernetesObject>(o1: T, o2: T): boolean {
208210
return o1.metadata!.name === o2.metadata!.name && o1.metadata!.namespace === o2.metadata!.namespace;
209211
}
210212

213+
function isSameVersion<T extends KubernetesObject>(o1: T, o2: T): boolean {
214+
return (
215+
o1.metadata!.resourceVersion !== undefined &&
216+
o1.metadata!.resourceVersion !== null &&
217+
o1.metadata!.resourceVersion === o2.metadata!.resourceVersion
218+
);
219+
}
220+
211221
function findKubernetesObject<T extends KubernetesObject>(objects: T[], obj: T): number {
212222
return objects.findIndex((elt: T) => {
213223
return isSameObject(elt, obj);

src/cache_test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,13 @@ describe('ListWatchCache', () => {
330330
{
331331
metadata: {
332332
name: 'name1',
333+
resourceVersion: '9876',
333334
} as V1ObjectMeta,
334335
} as V1Namespace,
335336
{
336337
metadata: {
337338
name: 'name2',
339+
resourceVersion: '8765',
338340
} as V1ObjectMeta,
339341
} as V1Namespace,
340342
];
@@ -387,7 +389,7 @@ describe('ListWatchCache', () => {
387389
doneHandler(null);
388390
await promise;
389391
expect(addObjects).to.deep.equal(list);
390-
expect(updateObjects).to.deep.equal(list);
392+
expect(updateObjects).to.deep.equal([]);
391393
});
392394

393395
it('should perform work as an informer with initial list and delete after', async () => {
@@ -396,18 +398,21 @@ describe('ListWatchCache', () => {
396398
{
397399
metadata: {
398400
name: 'name1',
401+
resourceVersion: '9876',
399402
} as V1ObjectMeta,
400403
} as V1Namespace,
401404
{
402405
metadata: {
403406
name: 'name2',
407+
resourceVersion: '8765',
404408
} as V1ObjectMeta,
405409
} as V1Namespace,
406410
];
407411
const list2: V1Namespace[] = [
408412
{
409413
metadata: {
410414
name: 'name1',
415+
resourceVersion: '9999',
411416
} as V1ObjectMeta,
412417
} as V1Namespace,
413418
];
@@ -467,6 +472,7 @@ describe('ListWatchCache', () => {
467472
{
468473
metadata: {
469474
name: 'name2',
475+
resourceVersion: '8765',
470476
} as V1ObjectMeta,
471477
} as V1Namespace,
472478
]);

0 commit comments

Comments
 (0)