Skip to content

Commit eb5c536

Browse files
committed
fix: review
1 parent f790c7c commit eb5c536

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

src/health.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fetch from 'node-fetch';
22
import { AbortSignal } from 'node-fetch/externals';
33
import { URL } from 'url';
44
import { KubeConfig } from './config';
5-
import { RequestOptions } from 'https';
5+
import { RequestOptions } from 'node:https';
66

77
export class Health {
88
public config: KubeConfig;
@@ -35,23 +35,22 @@ export class Health {
3535
requestInit.signal = controller.signal as AbortSignal;
3636
requestInit.method = 'GET';
3737

38-
return await fetch(requestURL.toString(), requestInit)
39-
.then((response) => {
40-
const status = response.status;
41-
if (status === 200) {
38+
try {
39+
const response = await fetch(requestURL.toString(), requestInit);
40+
const status = response.status;
41+
if (status === 200) {
42+
return true;
43+
} else if (status === 404) {
44+
if (path === '/healthz') {
45+
// /livez/readyz return 404 and healthz also returns 404, let's consider it is live
4246
return true;
43-
} else if (status === 404) {
44-
if (path === '/healthz') {
45-
// /livez/readyz return 404 and healthz also returns 404, let's consider it is live
46-
return true;
47-
}
48-
return this.healthz(opts);
49-
} else {
50-
return false;
5147
}
52-
})
53-
.catch((err) => {
48+
return this.healthz(opts);
49+
} else {
5450
return false;
55-
});
51+
}
52+
} catch {
53+
return false;
54+
}
5655
}
5756
}

src/health_test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,27 @@ describe('Health', () => {
123123
expect(r).to.be.true;
124124
scope.done();
125125
});
126+
127+
it('should return false when fetch throws an error', async () => {
128+
const kc = new KubeConfig();
129+
const cluster = {
130+
name: 'foo',
131+
server: 'https://server.com',
132+
} as Cluster;
133+
134+
const user = {
135+
name: 'my-user',
136+
password: 'some-password',
137+
} as User;
138+
kc.loadFromClusterAndUser(cluster, user);
139+
140+
const scope = nock('https://server.com');
141+
scope.get('/livez').replyWithError(new Error('an error'));
142+
const health = new Health(kc);
143+
144+
const r = await health.livez({});
145+
expect(r).to.be.false;
146+
scope.done();
147+
});
126148
});
127149
});

0 commit comments

Comments
 (0)