File tree Expand file tree Collapse file tree 2 files changed +37
-16
lines changed Expand file tree Collapse file tree 2 files changed +37
-16
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import fetch from 'node-fetch';
2
2
import { AbortSignal } from 'node-fetch/externals' ;
3
3
import { URL } from 'url' ;
4
4
import { KubeConfig } from './config' ;
5
- import { RequestOptions } from 'https' ;
5
+ import { RequestOptions } from 'node: https' ;
6
6
7
7
export class Health {
8
8
public config : KubeConfig ;
@@ -35,23 +35,22 @@ export class Health {
35
35
requestInit . signal = controller . signal as AbortSignal ;
36
36
requestInit . method = 'GET' ;
37
37
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
42
46
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 ;
51
47
}
52
- } )
53
- . catch ( ( err ) => {
48
+ return this . healthz ( opts ) ;
49
+ } else {
54
50
return false ;
55
- } ) ;
51
+ }
52
+ } catch {
53
+ return false ;
54
+ }
56
55
}
57
56
}
Original file line number Diff line number Diff line change @@ -123,5 +123,27 @@ describe('Health', () => {
123
123
expect ( r ) . to . be . true ;
124
124
scope . done ( ) ;
125
125
} ) ;
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
+ } ) ;
126
148
} ) ;
127
149
} ) ;
You can’t perform that action at this time.
0 commit comments