Skip to content

Commit 26833a6

Browse files
authored
Merge pull request #615 from brendandburns/change
Add a 'change' event to the informer.
2 parents 117362d + 7050056 commit 26833a6

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

src/cache.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ADD, DELETE, ERROR, Informer, ListPromise, ObjectCallback, UPDATE } from './informer';
1+
import { ADD, CHANGE, DELETE, ERROR, Informer, ListPromise, ObjectCallback, UPDATE } from './informer';
22
import { KubernetesObject } from './types';
33
import { RequestResult, Watch } from './watch';
44

@@ -42,13 +42,25 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
4242
}
4343

4444
public on(verb: string, cb: ObjectCallback<T>): void {
45+
if (verb === CHANGE) {
46+
this.on(ADD, cb);
47+
this.on(UPDATE, cb);
48+
this.on(DELETE, cb);
49+
return;
50+
}
4551
if (this.callbackCache[verb] === undefined) {
4652
throw new Error(`Unknown verb: ${verb}`);
4753
}
4854
this.callbackCache[verb].push(cb);
4955
}
5056

5157
public off(verb: string, cb: ObjectCallback<T>): void {
58+
if (verb === CHANGE) {
59+
this.off(ADD, cb);
60+
this.off(UPDATE, cb);
61+
this.off(DELETE, cb);
62+
return;
63+
}
5264
if (this.callbackCache[verb] === undefined) {
5365
throw new Error(`Unknown verb: ${verb}`);
5466
}

src/cache_test.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { EventEmitter } from 'ws';
99

1010
import { V1Namespace, V1NamespaceList, V1ObjectMeta, V1Pod, V1ListMeta } from './api';
1111
import { deleteObject, ListWatch, deleteItems } from './cache';
12-
import { ADD, UPDATE, DELETE, ERROR, ListPromise } from './informer';
12+
import { ADD, UPDATE, DELETE, ERROR, ListPromise, CHANGE } from './informer';
1313

1414
use(chaiAsPromised);
1515

@@ -265,6 +265,81 @@ describe('ListWatchCache', () => {
265265
]);
266266
});
267267

268+
it('should handle change events correctly', async () => {
269+
const fakeWatch = mock.mock(Watch);
270+
const list: V1Namespace[] = [
271+
{
272+
metadata: {
273+
name: 'name1',
274+
} as V1ObjectMeta,
275+
} as V1Namespace,
276+
{
277+
metadata: {
278+
name: 'name2',
279+
} as V1ObjectMeta,
280+
} as V1Namespace,
281+
];
282+
const listObj = {
283+
metadata: {
284+
resourceVersion: '12345',
285+
} as V1ListMeta,
286+
items: list,
287+
} as V1NamespaceList;
288+
289+
const listFn: ListPromise<V1Namespace> = function(): Promise<{
290+
response: http.IncomingMessage;
291+
body: V1NamespaceList;
292+
}> {
293+
return new Promise<{ response: http.IncomingMessage; body: V1NamespaceList }>(
294+
(resolve, reject) => {
295+
resolve({ response: {} as http.IncomingMessage, body: listObj });
296+
},
297+
);
298+
};
299+
const promise = new Promise((resolve) => {
300+
mock.when(
301+
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
302+
).thenCall(() => {
303+
resolve(new FakeRequest());
304+
});
305+
});
306+
const informer = new ListWatch('/some/path', mock.instance(fakeWatch), listFn);
307+
await promise;
308+
const [pathOut, , watchHandler] = mock.capture(fakeWatch.watch).last();
309+
expect(pathOut).to.equal('/some/path');
310+
311+
let count = 0;
312+
const changePromise = new Promise<boolean>((resolve: (V1Namespace) => void) => {
313+
informer.on(CHANGE, (obj: V1Namespace) => {
314+
count++;
315+
if (count == 3) {
316+
resolve(true);
317+
}
318+
});
319+
});
320+
321+
watchHandler('ADDED', {
322+
metadata: {
323+
name: 'name3',
324+
} as V1ObjectMeta,
325+
} as V1Namespace);
326+
327+
watchHandler('MODIFIED', {
328+
metadata: {
329+
name: 'name3',
330+
resourceVersion: 'baz',
331+
} as V1ObjectMeta,
332+
} as V1Namespace);
333+
334+
watchHandler('DELETED', {
335+
metadata: {
336+
name: 'name2',
337+
} as V1ObjectMeta,
338+
} as V1Namespace);
339+
340+
expect(changePromise).to.eventually.be.true;
341+
});
342+
268343
it('should perform work as an informer with multiple handlers', async () => {
269344
const fakeWatch = mock.mock(Watch);
270345
const listObj = {

src/informer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type ListPromise<T extends KubernetesObject> = () => Promise<{
1414

1515
export const ADD: string = 'add';
1616
export const UPDATE: string = 'update';
17+
export const CHANGE: string = 'change';
1718
export const DELETE: string = 'delete';
1819
export const ERROR: string = 'error';
1920

0 commit comments

Comments
 (0)