Skip to content

Commit ced85fc

Browse files
committed
Add error handling to informer
1 parent 632b3f9 commit ced85fc

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

examples/typescript/informer/informer.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,12 @@ const informer = k8s.makeInformer(kc, '/api/v1/namespaces/default/pods', listFn)
1313
informer.on('add', (obj: k8s.V1Pod) => { console.log(`Added: ${obj.metadata!.name}`); });
1414
informer.on('update', (obj: k8s.V1Pod) => { console.log(`Updated: ${obj.metadata!.name}`); });
1515
informer.on('delete', (obj: k8s.V1Pod) => { console.log(`Deleted: ${obj.metadata!.name}`); });
16+
informer.on('error', (err: k8s.V1Pod) => {
17+
console.error(err);
18+
// Restart informer after 5sec
19+
setTimeout(() => {
20+
informer.start();
21+
}, 5000);
22+
});
1623

1724
informer.start();

src/cache.ts

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

@@ -23,6 +23,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
2323
this.callbackCache[ADD] = [];
2424
this.callbackCache[UPDATE] = [];
2525
this.callbackCache[DELETE] = [];
26+
this.callbackCache[ERROR] = [];
2627
if (autoStart) {
2728
this.doneHandler(null);
2829
}
@@ -68,6 +69,10 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
6869
}
6970

7071
private async doneHandler(err: any) {
72+
if (err) {
73+
this.callbackCache[ERROR].forEach((elt: ObjectCallback<T>) => elt(err));
74+
return;
75+
}
7176
const promise = this.listFn();
7277
const result = await promise;
7378
const list = result.body;

src/informer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type ListPromise<T extends KubernetesObject> = () => Promise<{
1515
export const ADD: string = 'add';
1616
export const UPDATE: string = 'update';
1717
export const DELETE: string = 'delete';
18+
export const ERROR: string = 'error';
1819

1920
export interface Informer<T> {
2021
on(verb: string, fn: ObjectCallback<T>);

0 commit comments

Comments
 (0)