Skip to content

Commit

Permalink
Add error handling to informer
Browse files Browse the repository at this point in the history
  • Loading branch information
kellycampbell committed Jan 15, 2020
1 parent 632b3f9 commit ced85fc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
7 changes: 7 additions & 0 deletions examples/typescript/informer/informer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ const informer = k8s.makeInformer(kc, '/api/v1/namespaces/default/pods', listFn)
informer.on('add', (obj: k8s.V1Pod) => { console.log(`Added: ${obj.metadata!.name}`); });
informer.on('update', (obj: k8s.V1Pod) => { console.log(`Updated: ${obj.metadata!.name}`); });
informer.on('delete', (obj: k8s.V1Pod) => { console.log(`Deleted: ${obj.metadata!.name}`); });
informer.on('error', (err: k8s.V1Pod) => {
console.error(err);
// Restart informer after 5sec
setTimeout(() => {
informer.start();
}, 5000);
});

informer.start();
7 changes: 6 additions & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ADD, DELETE, Informer, ListPromise, ObjectCallback, UPDATE } from './informer';
import { ADD, DELETE, ERROR, Informer, ListPromise, ObjectCallback, UPDATE } from './informer';
import { KubernetesObject } from './types';
import { Watch } from './watch';

Expand All @@ -23,6 +23,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
this.callbackCache[ADD] = [];
this.callbackCache[UPDATE] = [];
this.callbackCache[DELETE] = [];
this.callbackCache[ERROR] = [];
if (autoStart) {
this.doneHandler(null);
}
Expand Down Expand Up @@ -68,6 +69,10 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
}

private async doneHandler(err: any) {
if (err) {
this.callbackCache[ERROR].forEach((elt: ObjectCallback<T>) => elt(err));
return;
}
const promise = this.listFn();
const result = await promise;
const list = result.body;
Expand Down
1 change: 1 addition & 0 deletions src/informer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type ListPromise<T extends KubernetesObject> = () => Promise<{
export const ADD: string = 'add';
export const UPDATE: string = 'update';
export const DELETE: string = 'delete';
export const ERROR: string = 'error';

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

0 comments on commit ced85fc

Please sign in to comment.