Skip to content

Commit c49525a

Browse files
committed
feat: added labelSelector to cache and informer
1 parent 37144cf commit c49525a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/cache.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { ObjectSerializer } from './api';
12
import { ADD, CHANGE, DELETE, ERROR, Informer, ListPromise, ObjectCallback, UPDATE } from './informer';
23
import { KubernetesObject } from './types';
34
import { RequestResult, Watch } from './watch';
45

56
export interface ObjectCache<T> {
67
get(name: string, namespace?: string): T | undefined;
8+
79
list(namespace?: string): ReadonlyArray<T>;
810
}
911

@@ -20,6 +22,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
2022
private readonly watch: Watch,
2123
private readonly listFn: ListPromise<T>,
2224
autoStart: boolean = true,
25+
private readonly labelSelector?: string,
2326
) {
2427
this.callbackCache[ADD] = [];
2528
this.callbackCache[UPDATE] = [];
@@ -125,9 +128,18 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
125128
}
126129
});
127130
this.addOrUpdateItems(list.items);
131+
const queryParams = {
132+
resourceVersion: list.metadata!.resourceVersion,
133+
} as {
134+
resourceVersion: string | undefined,
135+
labelSelector: string | undefined,
136+
};
137+
if (this.labelSelector !== undefined) {
138+
queryParams.labelSelector = ObjectSerializer.serialize(this.labelSelector, 'string');
139+
}
128140
this.request = await this.watch.watch(
129141
this.path,
130-
{ resourceVersion: list.metadata!.resourceVersion },
142+
queryParams,
131143
this.watchHandler.bind(this),
132144
this.doneHandler.bind(this),
133145
);

src/informer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export function makeInformer<T>(
2929
kubeconfig: KubeConfig,
3030
path: string,
3131
listPromiseFn: ListPromise<T>,
32+
labelSelector?: string,
3233
): Informer<T> {
3334
const watch = new Watch(kubeconfig);
34-
return new ListWatch<T>(path, watch, listPromiseFn, false);
35+
return new ListWatch<T>(path, watch, listPromiseFn, false, labelSelector);
3536
}

0 commit comments

Comments
 (0)