Skip to content

support fieldSelector for Informer #965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
private readonly listFn: ListPromise<T>,
autoStart: boolean = true,
private readonly labelSelector?: string,
private readonly fieldSelector?: string,
) {
this.callbackCache[ADD] = [];
this.callbackCache[UPDATE] = [];
Expand Down Expand Up @@ -169,10 +170,14 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
} as {
resourceVersion: string | undefined;
labelSelector: string | undefined;
fieldSelector: string | undefined;
};
if (this.labelSelector !== undefined) {
queryParams.labelSelector = ObjectSerializer.serialize(this.labelSelector, 'string');
}
if (this.fieldSelector !== undefined) {
queryParams.fieldSelector = ObjectSerializer.serialize(this.fieldSelector, 'string');
}
this.request = await this.watch.watch(
this.path,
queryParams,
Expand Down
51 changes: 51 additions & 0 deletions src/cache_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,57 @@ describe('ListWatchCache', () => {
expect(reqOpts.qs.labelSelector).to.equal(APP_LABEL_SELECTOR);
});

it('should send field selector', async () => {
const APP_FIELD_SELECTOR = 'metadata.name=name1';

const list: V1Namespace[] = [
{
metadata: {
name: 'name1',
} as V1ObjectMeta,
} as V1Namespace,
{
metadata: {
name: 'name2',
} as V1ObjectMeta,
} as V1Namespace,
];
const listObj = {
metadata: {
resourceVersion: '12345',
} as V1ListMeta,
items: list,
} as V1NamespaceList;

const listFn: ListPromise<V1Namespace> = function (): Promise<{
response: http.IncomingMessage;
body: V1NamespaceList;
}> {
return new Promise<{ response: http.IncomingMessage; body: V1NamespaceList }>(
(resolve, reject) => {
resolve({ response: {} as http.IncomingMessage, body: listObj });
},
);
};

const kc = new KubeConfig();
Object.assign(kc, fakeConfig);
const fakeRequestor = mock.mock(DefaultRequest);
const watch = new Watch(kc, mock.instance(fakeRequestor));

const fakeRequest = new FakeRequest();
mock.when(fakeRequestor.webRequest(mock.anything())).thenReturn(fakeRequest);

const informer = new ListWatch('/some/path', watch, listFn, false, undefined, APP_FIELD_SELECTOR);

await informer.start();

mock.verify(fakeRequestor.webRequest(mock.anything()));
const [opts] = mock.capture(fakeRequestor.webRequest).last();
const reqOpts: request.OptionsWithUri = opts as request.OptionsWithUri;
expect(reqOpts.qs.fieldSelector).to.equal(APP_FIELD_SELECTOR);
});

it('should ignore request errors after it is aborted', async () => {
const fakeWatch = mock.mock(Watch);
const list: V1Pod[] = [];
Expand Down
3 changes: 2 additions & 1 deletion src/informer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export function makeInformer<T extends KubernetesObject>(
path: string,
listPromiseFn: ListPromise<T>,
labelSelector?: string,
fieldSelector?: string,
): Informer<T> & ObjectCache<T> {
const watch = new Watch(kubeconfig);
return new ListWatch<T>(path, watch, listPromiseFn, false, labelSelector);
return new ListWatch<T>(path, watch, listPromiseFn, false, labelSelector, fieldSelector);
}