Skip to content

Commit 2d4e19a

Browse files
authored
Merge pull request #1 from kuberhealthy/codex/add-github-actions-for-build-and-publish
Add Kuberhealthy Java client and build workflows
2 parents 9ee4096 + cd5bebb commit 2d4e19a

File tree

8 files changed

+171
-73
lines changed

8 files changed

+171
-73
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Build container
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Build example container
14+
run: docker build .

.github/workflows/publish.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Publish client
2+
3+
on:
4+
release:
5+
types: [created]
6+
7+
jobs:
8+
publish:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: write
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-java@v4
15+
with:
16+
distribution: temurin
17+
java-version: '17'
18+
- name: Build jar
19+
run: mvn -q package
20+
- name: Upload release asset
21+
uses: softprops/action-gh-release@v1
22+
with:
23+
files: target/kuberhealthy-client-*.jar

Dockerfile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
FROM openjdk:17-jdk-slim AS build
1+
FROM maven:3.9.5-eclipse-temurin-17 AS build
22
WORKDIR /app
3-
COPY ExampleCheck.java .
4-
RUN javac ExampleCheck.java
3+
COPY pom.xml .
4+
COPY src ./src
5+
RUN mvn -q package
56

6-
FROM openjdk:17-jre-slim
7+
FROM eclipse-temurin:17-jre
78
WORKDIR /app
8-
COPY --from=build /app/ExampleCheck.class .
9-
ENTRYPOINT ["java", "ExampleCheck"]
9+
COPY --from=build /app/target/kuberhealthy-client-*.jar app.jar
10+
ENTRYPOINT ["java", "-cp", "app.jar", "io.kuberhealthy.example.ExampleCheck"]

ExampleCheck.java

Lines changed: 0 additions & 57 deletions
This file was deleted.

README.md

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
1-
# Java Client Example
1+
# Java Client and Example
22

3-
This directory shows how to write a minimal Java check for [Kuberhealthy](https://github.com/kuberhealthy/kuberhealthy). The `ExampleCheck` program reads the `KH_REPORTING_URL` and `KH_RUN_UUID` environment variables that Kuberhealthy injects into checker pods and posts a JSON status report.
3+
This repository provides a minimal Java client for reporting check results to [Kuberhealthy](https://github.com/kuberhealthy/kuberhealthy) and an example check that uses it. The `ExampleCheck` program reads the `KH_REPORTING_URL` and `KH_RUN_UUID` environment variables that Kuberhealthy injects into checker pods and posts a JSON status report.
44

5-
## Extending the check
5+
## Using the client library
66

7-
Add your own logic inside `ExampleCheck.java` before the call to `report`. The file defaults to reporting success and includes commented-out lines that show how to report a failure:
7+
Applications can depend on the client published to GitHub Packages:
8+
9+
```xml
10+
<dependency>
11+
<groupId>io.kuberhealthy</groupId>
12+
<artifactId>kuberhealthy-client</artifactId>
13+
<version>0.1.0-SNAPSHOT</version>
14+
</dependency>
15+
```
16+
17+
Add `https://maven.pkg.github.com/OWNER/REPO` as a Maven repository and authenticate with a GitHub token that has permission to read packages.
18+
19+
Within your code, create a `KuberhealthyClient` and call `report`:
20+
21+
```java
22+
import io.kuberhealthy.client.KuberhealthyClient;
23+
24+
KuberhealthyClient client = new KuberhealthyClient(url, uuid);
25+
client.report(true, "");
26+
```
27+
28+
## Extending the example check
29+
30+
Add your own logic inside `ExampleCheck.java` before reporting the result. The file defaults to reporting success and includes commented-out lines that show how to report a failure:
831

932
```java
1033
// ok = false;
1134
// error = "something went wrong";
1235
```
1336

14-
Call `report` with `ok=true` when the check succeeds or `ok=false` with an error message when it fails.
15-
16-
`report` expects an HTTP 200 response from Kuberhealthy; any other status
17-
code causes the program to exit with an error so rejected payloads surface
18-
immediately.
37+
Call `report` with `ok=true` when the check succeeds or `ok=false` with an error message when it fails. Non-200 responses from Kuberhealthy cause the program to exit with an error so rejected payloads surface immediately.
1938

20-
## Building and pushing
39+
## Building and pushing the container
2140

2241
Build and push the container image:
2342

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>io.kuberhealthy</groupId>
6+
<artifactId>kuberhealthy-client</artifactId>
7+
<version>0.1.0-SNAPSHOT</version>
8+
<name>Kuberhealthy Java Client</name>
9+
<description>Client library and example check for Kuberhealthy</description>
10+
<properties>
11+
<maven.compiler.source>17</maven.compiler.source>
12+
<maven.compiler.target>17</maven.compiler.target>
13+
</properties>
14+
</project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.kuberhealthy.client;
2+
3+
import java.io.IOException;
4+
import java.io.OutputStream;
5+
import java.net.HttpURLConnection;
6+
import java.net.URL;
7+
import java.nio.charset.StandardCharsets;
8+
9+
/**
10+
* Minimal client for reporting check results to Kuberhealthy.
11+
*/
12+
public class KuberhealthyClient {
13+
private final String url;
14+
private final String uuid;
15+
16+
public KuberhealthyClient(String url, String uuid) {
17+
this.url = url;
18+
this.uuid = uuid;
19+
}
20+
21+
/**
22+
* Reports the result of a check to Kuberhealthy.
23+
*
24+
* @param ok true if the check succeeded
25+
* @param errorMessage error description when ok is false
26+
* @throws IOException when the report cannot be delivered
27+
*/
28+
public void report(boolean ok, String errorMessage) throws IOException {
29+
String payload = "{\"ok\":true,\"errors\":[]}";
30+
if (!ok) {
31+
payload = "{\"ok\":false,\"errors\":[\"" + escape(errorMessage) + "\"]}";
32+
}
33+
34+
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
35+
conn.setRequestMethod("POST");
36+
conn.setRequestProperty("Content-Type", "application/json");
37+
conn.setRequestProperty("kh-run-uuid", uuid);
38+
conn.setDoOutput(true);
39+
40+
try (OutputStream os = conn.getOutputStream()) {
41+
os.write(payload.getBytes(StandardCharsets.UTF_8));
42+
}
43+
44+
int code = conn.getResponseCode();
45+
if (code != 200) {
46+
throw new IOException("unexpected response code: " + code);
47+
}
48+
}
49+
50+
private static String escape(String s) {
51+
return s.replace("\\", "\\\\").replace("\"", "\\\"");
52+
}
53+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.kuberhealthy.example;
2+
3+
import io.kuberhealthy.client.KuberhealthyClient;
4+
5+
/**
6+
* Example check that always reports success to Kuberhealthy.
7+
*/
8+
public class ExampleCheck {
9+
public static void main(String[] args) throws Exception {
10+
String url = System.getenv("KH_REPORTING_URL");
11+
String uuid = System.getenv("KH_RUN_UUID");
12+
13+
if (url == null || url.isEmpty() || uuid == null || uuid.isEmpty()) {
14+
System.err.println("KH_REPORTING_URL and KH_RUN_UUID must be set");
15+
System.exit(1);
16+
}
17+
18+
// Replace this with your own check logic. If the check passes, leave
19+
// ok as true. To report a failure, set ok to false and provide an
20+
// error message.
21+
boolean ok = true;
22+
String error = "";
23+
24+
// Example: report a failure with an error message
25+
// ok = false;
26+
// error = "something went wrong";
27+
28+
KuberhealthyClient client = new KuberhealthyClient(url, uuid);
29+
client.report(ok, error);
30+
}
31+
}

0 commit comments

Comments
 (0)