-
Notifications
You must be signed in to change notification settings - Fork 551
Add Health class to call health check endpoints on current context #2035
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
k8s-ci-robot
merged 7 commits into
kubernetes-client:release-1.x
from
feloy:feat/health-1.X
Nov 24, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f790c7c
feat: health check
feloy eb5c536
fix: review
feloy b6b89e0
fix: do not import URL
feloy 7804b22
reject when fetch fails
feloy 7a9c338
export Health
feloy 15d6e52
fix: get signal from options
feloy c234aa5
pass AbortError to caller
feloy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import fetch, { AbortError } from 'node-fetch'; | ||
import { KubeConfig } from './config'; | ||
import { RequestOptions } from 'node:https'; | ||
|
||
export class Health { | ||
public config: KubeConfig; | ||
|
||
public constructor(config: KubeConfig) { | ||
this.config = config; | ||
} | ||
|
||
public async readyz(opts: RequestOptions): Promise<boolean> { | ||
return this.check('/readyz', opts); | ||
} | ||
|
||
public async livez(opts: RequestOptions): Promise<boolean> { | ||
return this.check('/livez', opts); | ||
} | ||
|
||
private async healthz(opts: RequestOptions): Promise<boolean> { | ||
return this.check('/healthz', opts); | ||
} | ||
|
||
private async check(path: string, opts: RequestOptions): Promise<boolean> { | ||
const cluster = this.config.getCurrentCluster(); | ||
if (!cluster) { | ||
throw new Error('No currently active cluster'); | ||
} | ||
|
||
const requestURL = new URL(cluster.server + path); | ||
const requestInit = await this.config.applyToFetchOptions(opts); | ||
if (opts.signal) { | ||
requestInit.signal = opts.signal; | ||
} | ||
requestInit.method = 'GET'; | ||
|
||
try { | ||
const response = await fetch(requestURL.toString(), requestInit); | ||
const status = response.status; | ||
if (status === 200) { | ||
return true; | ||
} | ||
if (status === 404) { | ||
if (path === '/healthz') { | ||
// /livez/readyz return 404 and healthz also returns 404, let's consider it is live | ||
return true; | ||
} | ||
return this.healthz(opts); | ||
} | ||
return false; | ||
} catch (err: unknown) { | ||
if (err instanceof Error && err.name === 'AbortError') { | ||
throw err; | ||
} | ||
throw new Error('Error occurred in health request'); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { expect } from 'chai'; | ||
import nock from 'nock'; | ||
|
||
import { KubeConfig } from './config'; | ||
import { Health } from './health'; | ||
import { Cluster, User } from './config_types'; | ||
|
||
describe('Health', () => { | ||
describe('livez', () => { | ||
it('should throw an error if no current active cluster', async () => { | ||
const kc = new KubeConfig(); | ||
const health = new Health(kc); | ||
await expect(health.livez({})).to.be.rejectedWith('No currently active cluster'); | ||
}); | ||
|
||
it('should return true if /livez returns with status 200', async () => { | ||
const kc = new KubeConfig(); | ||
const cluster = { | ||
name: 'foo', | ||
server: 'https://server.com', | ||
} as Cluster; | ||
|
||
const user = { | ||
name: 'my-user', | ||
password: 'some-password', | ||
} as User; | ||
kc.loadFromClusterAndUser(cluster, user); | ||
|
||
const scope = nock('https://server.com').get('/livez').reply(200); | ||
const health = new Health(kc); | ||
|
||
const r = await health.livez({}); | ||
expect(r).to.be.true; | ||
scope.done(); | ||
}); | ||
|
||
it('should return false if /livez returns with status 500', async () => { | ||
const kc = new KubeConfig(); | ||
const cluster = { | ||
name: 'foo', | ||
server: 'https://server.com', | ||
} as Cluster; | ||
|
||
const user = { | ||
name: 'my-user', | ||
password: 'some-password', | ||
} as User; | ||
kc.loadFromClusterAndUser(cluster, user); | ||
|
||
const scope = nock('https://server.com').get('/livez').reply(500); | ||
const health = new Health(kc); | ||
|
||
const r = await health.livez({}); | ||
expect(r).to.be.false; | ||
scope.done(); | ||
}); | ||
|
||
it('should return true if /livez returns status 404 and /healthz returns status 200', async () => { | ||
const kc = new KubeConfig(); | ||
const cluster = { | ||
name: 'foo', | ||
server: 'https://server.com', | ||
} as Cluster; | ||
|
||
const user = { | ||
name: 'my-user', | ||
password: 'some-password', | ||
} as User; | ||
kc.loadFromClusterAndUser(cluster, user); | ||
|
||
const scope = nock('https://server.com'); | ||
scope.get('/livez').reply(404); | ||
scope.get('/healthz').reply(200); | ||
const health = new Health(kc); | ||
|
||
const r = await health.livez({}); | ||
expect(r).to.be.true; | ||
scope.done(); | ||
}); | ||
|
||
it('should return false if /livez returns status 404 and /healthz returns status 500', async () => { | ||
const kc = new KubeConfig(); | ||
const cluster = { | ||
name: 'foo', | ||
server: 'https://server.com', | ||
} as Cluster; | ||
|
||
const user = { | ||
name: 'my-user', | ||
password: 'some-password', | ||
} as User; | ||
kc.loadFromClusterAndUser(cluster, user); | ||
|
||
const scope = nock('https://server.com'); | ||
scope.get('/livez').reply(404); | ||
scope.get('/healthz').reply(500); | ||
const health = new Health(kc); | ||
|
||
const r = await health.livez({}); | ||
expect(r).to.be.false; | ||
scope.done(); | ||
}); | ||
|
||
it('should return true if both /livez and /healthz return status 404', async () => { | ||
const kc = new KubeConfig(); | ||
const cluster = { | ||
name: 'foo', | ||
server: 'https://server.com', | ||
} as Cluster; | ||
|
||
const user = { | ||
name: 'my-user', | ||
password: 'some-password', | ||
} as User; | ||
kc.loadFromClusterAndUser(cluster, user); | ||
|
||
const scope = nock('https://server.com'); | ||
scope.get('/livez').reply(404); | ||
scope.get('/healthz').reply(200); | ||
const health = new Health(kc); | ||
|
||
const r = await health.livez({}); | ||
expect(r).to.be.true; | ||
scope.done(); | ||
}); | ||
|
||
it('should throw an error when fetch throws an error', async () => { | ||
const kc = new KubeConfig(); | ||
const cluster = { | ||
name: 'foo', | ||
server: 'https://server.com', | ||
} as Cluster; | ||
|
||
const user = { | ||
name: 'my-user', | ||
password: 'some-password', | ||
} as User; | ||
kc.loadFromClusterAndUser(cluster, user); | ||
|
||
const scope = nock('https://server.com'); | ||
scope.get('/livez').replyWithError(new Error('an error')); | ||
const health = new Health(kc); | ||
|
||
await expect(health.livez({})).to.be.rejectedWith('Error occurred in health request'); | ||
scope.done(); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.