Skip to content

Commit 52cf1cd

Browse files
committed
initial
0 parents  commit 52cf1cd

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM python:3.11-alpine
2+
3+
WORKDIR /app
4+
COPY client.py .
5+
6+
CMD ["python3", "/app/client.py"]

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
IMG ?= example-python-check:latest
2+
3+
.PHONY: build push
4+
5+
build:
6+
docker build -t $(IMG) .
7+
8+
push: build
9+
docker push $(IMG)

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Python Kuberhealthy Client
2+
3+
This directory contains a minimal Python application that demonstrates how to
4+
report status back to [Kuberhealthy](https://github.com/kuberhealthy/kuberhealthy).
5+
The example loads the `KH_REPORTING_URL` and `KH_RUN_UUID` environment variables
6+
provided to checker pods and includes commented calls to `report_ok` and
7+
`report_error`.
8+
9+
## Running the example
10+
11+
Set the `KH_REPORTING_URL` and `KH_RUN_UUID` environment variables, add your
12+
check logic to `client.py`, and then run:
13+
14+
```bash
15+
python3 client.py
16+
```
17+
18+
Within the `main` function, uncomment either `report_ok()` or
19+
`report_error("message")` after your logic depending on the result.
20+
21+
## Building and pushing the check
22+
23+
Use the provided `Makefile` and `Dockerfile` to build and publish the check
24+
image.
25+
26+
```bash
27+
make build IMG=myrepo/example-check:latest
28+
make push IMG=myrepo/example-check:latest
29+
```
30+
31+
## Using in your own checks
32+
33+
1. Add your check logic to `client.py` by replacing the placeholder in `main`.
34+
Call `report_ok()` when the check succeeds or `report_error("message")`
35+
when it fails.
36+
2. Build and push your image as shown above.
37+
3. Create a `KuberhealthyCheck` resource pointing at your image and apply it to any
38+
cluster where Kuberhealthy runs:
39+
40+
```yaml
41+
apiVersion: kuberhealthy.github.io/v2
42+
kind: KuberhealthyCheck
43+
metadata:
44+
name: example-python-check
45+
spec:
46+
image: myrepo/example-check:latest
47+
runInterval: 1m
48+
```

client.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
"""Example Kuberhealthy client in Python."""
3+
4+
import json
5+
import os
6+
import urllib.request
7+
8+
9+
KH_REPORTING_URL = "KH_REPORTING_URL"
10+
KH_RUN_UUID = "KH_RUN_UUID"
11+
12+
13+
def _get_env(name: str) -> str:
14+
"""Return the value of the environment variable *name* or raise an error."""
15+
value = os.getenv(name)
16+
if not value:
17+
raise EnvironmentError(f"{name} must be set")
18+
return value
19+
20+
21+
def _post_status(payload: dict) -> None:
22+
"""Send *payload* to the Kuberhealthy reporting URL."""
23+
url = _get_env(KH_REPORTING_URL)
24+
run_uuid = _get_env(KH_RUN_UUID)
25+
data = json.dumps(payload).encode("utf-8")
26+
request = urllib.request.Request(
27+
url,
28+
data=data,
29+
headers={"content-type": "application/json", "kh-run-uuid": run_uuid},
30+
)
31+
with urllib.request.urlopen(request, timeout=10) as response: # nosec B310
32+
response.read()
33+
34+
35+
def report_ok() -> None:
36+
"""Report a successful check to Kuberhealthy."""
37+
_post_status({"OK": True, "Errors": []})
38+
39+
40+
def report_error(message: str) -> None:
41+
"""Report a failure to Kuberhealthy with *message* as the error."""
42+
_post_status({"OK": False, "Errors": [message]})
43+
44+
45+
def main() -> None:
46+
"""Run the example client."""
47+
# INSERT YOUR CHECK LOGIC HERE
48+
# report_ok()
49+
# report_error("something went wrong")
50+
pass
51+
52+
53+
if __name__ == "__main__":
54+
main()

0 commit comments

Comments
 (0)