Skip to content

Commit 174d4f8

Browse files
committed
initial import
0 parents  commit 174d4f8

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM node:20-alpine
2+
WORKDIR /app
3+
COPY package.json ./
4+
RUN npm install --production
5+
COPY check.js ./
6+
CMD ["node", "check.js"]

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
IMAGE ?= kuberhealthy-example-js:latest
2+
3+
build:
4+
docker build -t $(IMAGE) .
5+
6+
push:
7+
docker push $(IMAGE)

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# JavaScript Kuberhealthy Client
2+
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.
4+
5+
## Usage
6+
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.
8+
2. **Build the image**: run `make build IMAGE=my-registry/my-check:tag` to build a container image containing your check.
9+
3. **Push the image**: `make push IMAGE=my-registry/my-check:tag`.
10+
4. **Create a KuberhealthyCheck**: write a khcheck resource that references your image and apply it to clusters where Kuberhealthy runs.
11+
12+
The check relies on two environment variables set automatically by Kuberhealthy:
13+
14+
- `KH_REPORTING_URL` – the endpoint where status reports are posted.
15+
- `KH_RUN_UUID` – the UUID for this check run. It must be sent back in the `kh-run-uuid` header.
16+
17+
## Example khcheck
18+
19+
```yaml
20+
apiVersion: kuberhealthy.github.io/v2
21+
kind: KuberhealthyCheck
22+
metadata:
23+
name: example-js-check
24+
namespace: kuberhealthy
25+
spec:
26+
runInterval: 1m
27+
podSpec:
28+
containers:
29+
- name: check
30+
image: my-registry/my-check:tag
31+
```
32+
33+
Apply the file with `kubectl apply -f <file>`.

check.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const reportingURL = process.env.KH_REPORTING_URL;
2+
const runUUID = process.env.KH_RUN_UUID;
3+
4+
if (!reportingURL || !runUUID) {
5+
console.error('KH_REPORTING_URL and KH_RUN_UUID must be set');
6+
process.exit(1);
7+
}
8+
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+
}
27+
28+
async function main() {
29+
try {
30+
// Add your check logic here.
31+
await report(true, []);
32+
console.log('Reported success to Kuberhealthy');
33+
} catch (err) {
34+
console.error('Check logic failed:', err);
35+
try {
36+
await report(false, [err.message]);
37+
} catch (e) {
38+
console.error('Failed to report failure:', e);
39+
}
40+
process.exit(1);
41+
}
42+
}
43+
44+
main();
45+
46+
module.exports = { report };

package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "kuberhealthy-javascript-client",
3+
"version": "0.1.0",
4+
"description": "Example Kuberhealthy external check written in JavaScript",
5+
"main": "check.js",
6+
"scripts": {
7+
"start": "node check.js"
8+
},
9+
"dependencies": {}
10+
}

0 commit comments

Comments
 (0)