Skip to content

Commit f2da2e8

Browse files
committed
Fix issue with authentication on watch due to async not being awaited
1 parent 632b3f9 commit f2da2e8

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

examples/typescript/watch/watch-example.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const kc = new k8s.KubeConfig();
44
kc.loadFromDefault();
55

66
const watch = new k8s.Watch(kc);
7-
const req = watch.watch('/api/v1/namespaces',
7+
watch.watch('/api/v1/namespaces',
88
// optional query parameters can go here.
99
{},
1010
// callback is called for each received object.
@@ -29,7 +29,8 @@ const req = watch.watch('/api/v1/namespaces',
2929
(err) => {
3030
// tslint:disable-next-line:no-console
3131
console.log(err);
32-
});
33-
34-
// watch returns a request object which you can use to abort the watch.
35-
setTimeout(() => { req.abort(); }, 10 * 1000);
32+
})
33+
.then((req) => {
34+
// watch returns a request object which you can use to abort the watch.
35+
setTimeout(() => { req.abort(); }, 10 * 1000);
36+
});

src/cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
7373
const list = result.body;
7474
deleteItems(this.objects, list.items, this.callbackCache[DELETE].slice());
7575
this.addOrUpdateItems(list.items);
76-
this.watch.watch(
76+
await this.watch.watch(
7777
this.path,
7878
{ resourceVersion: list.metadata!.resourceVersion },
7979
this.watchHandler.bind(this),

src/watch.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ export class Watch {
3131
}
3232
}
3333

34-
public watch(
34+
public async watch(
3535
path: string,
3636
queryParams: any,
3737
callback: (phase: string, obj: any) => void,
3838
done: (err: any) => void,
39-
): any {
39+
): Promise<any> {
4040
const cluster = this.config.getCurrentCluster();
4141
if (!cluster) {
4242
throw new Error('No currently active cluster');
@@ -54,7 +54,7 @@ export class Watch {
5454
useQuerystring: true,
5555
json: true,
5656
};
57-
this.config.applyToRequest(requestOptions);
57+
await this.config.applyToRequest(requestOptions);
5858

5959
const stream = byline.createStream();
6060
stream.on('data', (line) => {
@@ -69,6 +69,7 @@ export class Watch {
6969
done(err);
7070
});
7171
stream.on('close', () => done(null));
72+
7273
const req = this.requestImpl.webRequest(requestOptions, (error, response, body) => {
7374
if (error) {
7475
done(error);

src/watch_test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('Watch', () => {
3939
const watch = new Watch(kc);
4040
});
4141

42-
it('should watch correctly', () => {
42+
it('should watch correctly', async () => {
4343
const kc = new KubeConfig();
4444
Object.assign(kc, fakeConfig);
4545
const fakeRequestor = mock(DefaultRequest);
@@ -75,7 +75,7 @@ describe('Watch', () => {
7575
let doneCalled = false;
7676
let doneErr: any;
7777

78-
watch.watch(
78+
await watch.watch(
7979
path,
8080
{},
8181
(phase: string, obj: string) => {
@@ -112,7 +112,7 @@ describe('Watch', () => {
112112
expect(doneErr).to.deep.equal(errIn);
113113
});
114114

115-
it('should handle errors correctly', () => {
115+
it('should handle errors correctly', async () => {
116116
const kc = new KubeConfig();
117117
Object.assign(kc, fakeConfig);
118118
const fakeRequestor = mock(DefaultRequest);
@@ -142,7 +142,7 @@ describe('Watch', () => {
142142
let doneCalled = false;
143143
let doneErr: any;
144144

145-
watch.watch(
145+
await watch.watch(
146146
path,
147147
{},
148148
(phase: string, obj: string) => {
@@ -171,7 +171,7 @@ describe('Watch', () => {
171171
expect(doneErr).to.deep.equal(errIn);
172172
});
173173

174-
it('should handle server side close correctly', () => {
174+
it('should handle server side close correctly', async () => {
175175
const kc = new KubeConfig();
176176
Object.assign(kc, fakeConfig);
177177
const fakeRequestor = mock(DefaultRequest);
@@ -200,7 +200,7 @@ describe('Watch', () => {
200200
let doneCalled = false;
201201
let doneErr: any;
202202

203-
watch.watch(
203+
await watch.watch(
204204
path,
205205
{},
206206
(phase: string, obj: string) => {
@@ -229,7 +229,7 @@ describe('Watch', () => {
229229
expect(doneErr).to.be.null;
230230
});
231231

232-
it('should ignore JSON parse errors', () => {
232+
it('should ignore JSON parse errors', async () => {
233233
const kc = new KubeConfig();
234234
Object.assign(kc, fakeConfig);
235235
const fakeRequestor = mock(DefaultRequest);
@@ -256,7 +256,7 @@ describe('Watch', () => {
256256
const receivedTypes: string[] = [];
257257
const receivedObjects: string[] = [];
258258

259-
watch.watch(
259+
await watch.watch(
260260
path,
261261
{},
262262
(recievedType: string, recievedObject: string) => {

0 commit comments

Comments
 (0)