Skip to content

Commit ec2a471

Browse files
committed
refactor: isolate client library
1 parent 174d4f8 commit ec2a471

File tree

8 files changed

+118
-29
lines changed

8 files changed

+118
-29
lines changed

.github/workflows/build.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Build example container
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- name: Build Docker image
13+
run: docker build -t kuberhealthy-js-check .

.github/workflows/publish.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Publish package
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
publish:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: read
12+
packages: write
13+
steps:
14+
- uses: actions/checkout@v3
15+
- uses: actions/setup-node@v3
16+
with:
17+
node-version: 18
18+
registry-url: https://npm.pkg.github.com
19+
- run: npm publish
20+
working-directory: client
21+
env:
22+
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FROM node:20-alpine
22
WORKDIR /app
33
COPY package.json ./
4+
COPY client ./client
45
RUN npm install --production
56
COPY check.js ./
67
CMD ["node", "check.js"]

README.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
# JavaScript Kuberhealthy Client
22

3-
This directory contains an example external check for [Kuberhealthy](https://github.com/kuberhealthy/kuberhealthy) written in JavaScript. The script demonstrates how to report a successful run or a failure back to Kuberhealthy using environment variables provided to every checker pod.
3+
This repository contains a small client library and example external check for [Kuberhealthy](https://github.com/kuberhealthy/kuberhealthy).
44

5-
## Usage
5+
The reusable client lives in the `client/` directory and is published as `@kuberhealthy/client`. It can be imported into your own Node.js applications to report check results back to Kuberhealthy. An example checker using the client is provided in `check.js`.
66

7-
1. **Add your logic**: edit `check.js` and replace the placeholder section in `main` with your own check logic. Call `report(true, [])` when the check succeeds or `report(false, ["message"])` on failure.
7+
## Client Library
8+
9+
The package is published to the GitHub Packages npm registry under `@kuberhealthy/client`.
10+
11+
Install it by configuring npm to use the GitHub Packages registry for the `@kuberhealthy` scope:
12+
13+
```bash
14+
echo "@kuberhealthy:registry=https://npm.pkg.github.com" >> ~/.npmrc
15+
npm install @kuberhealthy/client
16+
```
17+
18+
Use the client in your code:
19+
20+
```javascript
21+
const { KuberhealthyClient } = require('@kuberhealthy/client');
22+
23+
const client = new KuberhealthyClient(process.env.KH_REPORTING_URL, process.env.KH_RUN_UUID);
24+
await client.report(true, []);
25+
```
26+
27+
Node.js 18 or newer is required for the built in `fetch` API used by the client.
28+
29+
## Example Checker
30+
31+
1. **Add your logic**: edit `check.js` and replace the placeholder section in `main` with your own check logic. Call `client.report(true, [])` when the check succeeds or `client.report(false, ["message"])` on failure.
832
2. **Build the image**: run `make build IMAGE=my-registry/my-check:tag` to build a container image containing your check.
933
3. **Push the image**: `make push IMAGE=my-registry/my-check:tag`.
1034
4. **Create a KuberhealthyCheck**: write a khcheck resource that references your image and apply it to clusters where Kuberhealthy runs.
@@ -14,7 +38,7 @@ The check relies on two environment variables set automatically by Kuberhealthy:
1438
- `KH_REPORTING_URL` – the endpoint where status reports are posted.
1539
- `KH_RUN_UUID` – the UUID for this check run. It must be sent back in the `kh-run-uuid` header.
1640

17-
## Example khcheck
41+
### Example khcheck
1842

1943
```yaml
2044
apiVersion: kuberhealthy.github.io/v2

check.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { KuberhealthyClient } = require('@kuberhealthy/client');
2+
13
const reportingURL = process.env.KH_REPORTING_URL;
24
const runUUID = process.env.KH_RUN_UUID;
35

@@ -6,34 +8,17 @@ if (!reportingURL || !runUUID) {
68
process.exit(1);
79
}
810

9-
// report sends a result to Kuberhealthy
10-
async function report(ok, errors) {
11-
const res = await fetch(reportingURL, {
12-
method: 'POST',
13-
headers: {
14-
'Content-Type': 'application/json',
15-
'kh-run-uuid': runUUID,
16-
},
17-
body: JSON.stringify({ ok, errors }),
18-
});
19-
20-
if (res.ok) {
21-
return;
22-
}
23-
24-
const text = await res.text();
25-
throw new Error(`Kuberhealthy responded with ${res.status}: ${text}`);
26-
}
11+
const client = new KuberhealthyClient(reportingURL, runUUID);
2712

2813
async function main() {
2914
try {
3015
// Add your check logic here.
31-
await report(true, []);
16+
await client.report(true, []);
3217
console.log('Reported success to Kuberhealthy');
3318
} catch (err) {
3419
console.error('Check logic failed:', err);
3520
try {
36-
await report(false, [err.message]);
21+
await client.report(false, [err.message]);
3722
} catch (e) {
3823
console.error('Failed to report failure:', e);
3924
}
@@ -42,5 +27,3 @@ async function main() {
4227
}
4328

4429
main();
45-
46-
module.exports = { report };

client/index.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class KuberhealthyClient {
2+
constructor(reportingURL, runUUID) {
3+
if (!reportingURL || !runUUID) {
4+
throw new Error('reportingURL and runUUID are required');
5+
}
6+
this.reportingURL = reportingURL;
7+
this.runUUID = runUUID;
8+
}
9+
10+
async report(ok, errors) {
11+
const res = await fetch(this.reportingURL, {
12+
method: 'POST',
13+
headers: {
14+
'Content-Type': 'application/json',
15+
'kh-run-uuid': this.runUUID,
16+
},
17+
body: JSON.stringify({ ok, errors }),
18+
});
19+
20+
if (!res.ok) {
21+
const text = await res.text();
22+
throw new Error(`Kuberhealthy responded with ${res.status}: ${text}`);
23+
}
24+
}
25+
}
26+
27+
module.exports = { KuberhealthyClient };

client/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@kuberhealthy/client",
3+
"version": "0.1.0",
4+
"description": "Node.js client for Kuberhealthy",
5+
"main": "index.js",
6+
"files": ["index.js"],
7+
"publishConfig": {
8+
"registry": "https://npm.pkg.github.com"
9+
},
10+
"engines": {
11+
"node": ">=18"
12+
}
13+
}

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
{
2-
"name": "kuberhealthy-javascript-client",
2+
"name": "kuberhealthy-check-example",
33
"version": "0.1.0",
4-
"description": "Example Kuberhealthy external check written in JavaScript",
4+
"private": true,
5+
"description": "Example external check for Kuberhealthy using the JavaScript client",
56
"main": "check.js",
67
"scripts": {
78
"start": "node check.js"
89
},
9-
"dependencies": {}
10+
"engines": {
11+
"node": ">=18"
12+
},
13+
"dependencies": {
14+
"@kuberhealthy/client": "file:./client"
15+
}
1016
}

0 commit comments

Comments
 (0)