Skip to content

Commit 4b1b60b

Browse files
committed
add java build with dockerfile sample
Signed-off-by: wanjunlei <wanjunlei@kubesphere.io>
1 parent df33cf7 commit 4b1b60b

13 files changed

Lines changed: 380 additions & 0 deletions

File tree

apps/buildah/java/.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Dockerfile
2+
target
3+
bin
4+
.project
5+
.classpath

apps/buildah/java/Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### builder stage, used to compile the application
2+
FROM openfunction/maven:3-jdk-8-openj9 AS builder
3+
4+
# copy the sources into the builder stage
5+
COPY . /app
6+
WORKDIR /app
7+
8+
# build the application
9+
RUN mvn package
10+
11+
### final stage, used to setup the open liberty runtime
12+
FROM openfunction/open-liberty:kernel-java8-openj9-ubi
13+
14+
# copy liberty config
15+
COPY --chown=1001:0 src/main/liberty/config /config/
16+
17+
# copy compiled war file from the builder stage
18+
COPY --chown=1001:0 --from=builder /app/target/*.war /config/apps/
19+
20+
# configure liberty based on the server.xml
21+
RUN configure.sh

apps/buildah/java/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Sample Function Java
2+
3+
## Prerequisites
4+
5+
### OpenFunction
6+
7+
You can refer to the [Installation Guide](https://openfunction.dev/docs/getting-started/installation/) to setup OpenFunction.
8+
9+
## Deployment
10+
11+
1. Create secret
12+
13+
Follow [this guide](https://openfunction.dev/docs/getting-started/quickstarts/prerequisites/#registry-credential) to create a registry credential.
14+
15+
2. Create function
16+
17+
For sample function below, modify the ``spec.image`` field in ``function-buildah-java.yaml`` to your own container registry address:
18+
19+
```yaml
20+
apiVersion: core.openfunction.io/v1beta1
21+
kind: Function
22+
metadata:
23+
name: function-buildah-java
24+
spec:
25+
image: "<your registry name>/sample-buildah-java:latest"
26+
```
27+
28+
Use the following command to create this Function:
29+
30+
```shell
31+
kubectl apply -f function-buildah-java.yaml
32+
```
33+
34+
3. Access function
35+
36+
You can observe the process of a function with the following command:
37+
38+
```shell
39+
kubectl get functions.core.openfunction.io
40+
41+
NAME BUILDSTATE SERVINGSTATE BUILDER SERVING ADDRESS AGE
42+
function-buildah-java Succeeded Running builder-jgnzp serving-q6wdp http://function-buildah-java.default.svc.cluster.local/ 22m
43+
```
44+
45+
The `Function.status.addresses` field provides various methods for accessing functions.
46+
Get `Function` addresses by running following command:
47+
48+
```shell
49+
kubectl get function function-buildah-java -o=jsonpath='{.status.addresses}'
50+
```
51+
52+
You will get the following address:
53+
54+
```json
55+
[{"type":"External","value":"http://function-buildah-java.default.ofn.io/"},
56+
{"type":"Internal","value":"http://function-buildah-java.default.svc.cluster.local/"}]
57+
```
58+
59+
> You can use the following command to create a pod in the cluster and access the function from the pod:
60+
>
61+
> ```shell
62+
> kubectl run curl --image=radial/busyboxplus:curl -i --tty
63+
> ```
64+
65+
Access functions by the internal address:
66+
67+
```shell
68+
curl http://function-buildah-java.default.svc.cluster.local
69+
```
70+
71+
Access functions by the external address:
72+
> To access the function via the Address of type `External` in `Funtion.status`, you should configure local domain first, see [Configure Local Domain](https://openfunction.dev/docs/operations/networking/local-domain/).
73+
74+
```shell
75+
curl http://function-buildah-java.default.ofn.io
76+
```
77+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: core.openfunction.io/v1beta1
2+
kind: Function
3+
metadata:
4+
name: function-buildah-java
5+
spec:
6+
version: "v1.0.0"
7+
image: "openfunctiondev/sample-buildah-java:latest"
8+
imageCredentials:
9+
name: push-secret
10+
#port: 8080 # default to 8080
11+
build:
12+
builder: openfunction/buildah:v1.23.1
13+
srcRepo:
14+
url: "https://github.com/OpenFunction/samples.git"
15+
sourceSubPath: "apps/buildah/java"
16+
revision: "main"
17+
shipwright:
18+
strategy:
19+
name: buildah
20+
kind: ClusterBuildStrategy
21+
serving:
22+
runtime: knative
23+
template:
24+
containers:
25+
- name: function
26+
imagePullPolicy: Always
27+

apps/buildah/java/pom.xml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>dev.openfunction.samples</groupId>
6+
<artifactId>java-open-liberty</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>war</packaging>
9+
10+
<properties>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
15+
<failOnMissingWebXml>false</failOnMissingWebXml>
16+
17+
<version.liberty-maven-plugin>3.3.4</version.liberty-maven-plugin>
18+
<version.maven-war-plugin>3.3.1</version.maven-war-plugin>
19+
<version.maven-surefire-plugin>2.22.2</version.maven-surefire-plugin>
20+
<version.maven-failsafe-plugin>2.22.2</version.maven-failsafe-plugin>
21+
22+
<liberty.var.default.http.port>8080</liberty.var.default.http.port>
23+
<liberty.var.default.https.port>9443</liberty.var.default.https.port>
24+
</properties>
25+
26+
<dependencies>
27+
<!-- Provided dependencies -->
28+
<dependency>
29+
<groupId>jakarta.platform</groupId>
30+
<artifactId>jakarta.jakartaee-web-api</artifactId>
31+
<version>8.0.0</version>
32+
<scope>provided</scope>
33+
</dependency>
34+
<!-- For tests -->
35+
<dependency>
36+
<groupId>org.junit.jupiter</groupId>
37+
<artifactId>junit-jupiter</artifactId>
38+
<version>5.7.1</version>
39+
<scope>test</scope>
40+
</dependency>
41+
</dependencies>
42+
43+
<build>
44+
<finalName>${project.artifactId}</finalName>
45+
<plugins>
46+
<plugin>
47+
<groupId>io.openliberty.tools</groupId>
48+
<artifactId>liberty-maven-plugin</artifactId>
49+
<version>${version.liberty-maven-plugin}</version>
50+
</plugin>
51+
<plugin>
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-war-plugin</artifactId>
54+
<version>${version.maven-war-plugin}</version>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-surefire-plugin</artifactId>
59+
<version>${version.maven-surefire-plugin}</version>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-failsafe-plugin</artifactId>
64+
<version>${version.maven-failsafe-plugin}</version>
65+
<configuration>
66+
<systemPropertyVariables>
67+
<http.port>${liberty.var.default.http.port}</http.port>
68+
</systemPropertyVariables>
69+
</configuration>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package dev.openfunction.samples.liberty;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import javax.ws.rs.ApplicationPath;
7+
import javax.ws.rs.core.Application;
8+
9+
import dev.openfunction.samples.liberty.health.HealthCheckResource;
10+
import dev.openfunction.samples.liberty.health.HealthCheckServiceImpl;
11+
12+
@ApplicationPath("/api")
13+
public class LibertyApplication extends Application {
14+
15+
@Override
16+
public Set<Object> getSingletons() {
17+
final Set<Object> set = new HashSet<>();
18+
set.add(new HealthCheckResource(new HealthCheckServiceImpl()));
19+
return set;
20+
}
21+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package dev.openfunction.samples.liberty.health;
2+
3+
import java.util.logging.Level;
4+
import java.util.logging.Logger;
5+
6+
import javax.ws.rs.GET;
7+
import javax.ws.rs.Path;
8+
import javax.ws.rs.core.Response;
9+
10+
@Path("/health")
11+
public class HealthCheckResource {
12+
13+
private static final String CLASS_NAME = HealthCheckResource.class.getName();
14+
private static final Logger LOGGER = Logger.getLogger(CLASS_NAME);
15+
16+
private final HealthCheckService healthCheckService;
17+
18+
public HealthCheckResource(HealthCheckService healthCheckService) {
19+
this.healthCheckService = healthCheckService;
20+
}
21+
22+
@GET
23+
public Response check() {
24+
final String METHOD_NAME = "check";
25+
if (LOGGER.isLoggable(Level.FINER)) {
26+
LOGGER.entering(CLASS_NAME, METHOD_NAME);
27+
}
28+
29+
final Response response = this.healthCheckService.isJvmHealthy() ? Response.ok().build()
30+
: Response.serverError().build();
31+
32+
if (LOGGER.isLoggable(Level.FINER)) {
33+
LOGGER.exiting(CLASS_NAME, METHOD_NAME, response);
34+
}
35+
return response;
36+
}
37+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dev.openfunction.samples.liberty.health;
2+
3+
public interface HealthCheckService {
4+
5+
/**
6+
* Checks if the JVM is healthy
7+
*
8+
* @return true if it is healthy, otherwise false
9+
*/
10+
public boolean isJvmHealthy();
11+
12+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dev.openfunction.samples.liberty.health;
2+
3+
import java.lang.management.ManagementFactory;
4+
import java.lang.management.MemoryMXBean;
5+
import java.lang.management.MemoryUsage;
6+
import java.util.logging.Level;
7+
import java.util.logging.Logger;
8+
9+
public class HealthCheckServiceImpl implements HealthCheckService {
10+
11+
private static final String CLASS_NAME = HealthCheckServiceImpl.class.getName();
12+
private static final Logger LOGGER = Logger.getLogger(CLASS_NAME);
13+
private static final MemoryMXBean MEMORY_BEAN = ManagementFactory.getMemoryMXBean();
14+
15+
@Override
16+
public boolean isJvmHealthy() {
17+
final String METHOD_NAME = "isJvmHealthy";
18+
if (LOGGER.isLoggable(Level.FINER)) {
19+
LOGGER.entering(CLASS_NAME, METHOD_NAME);
20+
}
21+
22+
// retrieve the heap memory usage
23+
final MemoryUsage memoryUsage = MEMORY_BEAN.getHeapMemoryUsage();
24+
final long memUsed = memoryUsage.getUsed();
25+
final long memMax = memoryUsage.getMax();
26+
27+
// assume it is healthy if at most 90 % of the heap are used
28+
final boolean healthy = memUsed < memMax * 0.9;
29+
30+
if (LOGGER.isLoggable(Level.FINER)) {
31+
LOGGER.exiting(CLASS_NAME, METHOD_NAME, healthy);
32+
}
33+
return healthy;
34+
}
35+
}

0 commit comments

Comments
 (0)