|
| 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