Skip to content

Commit 3b8601b

Browse files
committed
K8s deployment :wip
1 parent 5670d13 commit 3b8601b

File tree

15 files changed

+67
-49
lines changed

15 files changed

+67
-49
lines changed

.dockerignore

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11

22
*
3-
!/build/distributions/*
4-
!/build/native/nativeCompile/*
53
!/build/todo_backend/

.github/workflows/push.yml

+16-6
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@ jobs:
66
runs-on: ubuntu-latest
77
steps:
88
- uses: actions/checkout@v4
9-
- uses: graalvm/setup-graalvm@v1
9+
- uses: actions/cache@v4
1010
with:
11-
version: latest
12-
distribution: graalvm
13-
java-version: 21
14-
cache: gradle
15-
- run: ./gradlew nativeTest
11+
path: ~/.sdkman
12+
key: "${{ runner.os }}-sdkman-${{ hashFiles('.sdkmanrc') }}"
13+
restore-keys: "${{ runner.os }}-sdkman-"
14+
- uses: actions/cache@v4
15+
with:
16+
path: |
17+
~/.gradle/caches
18+
~/.gradle/wrapper
19+
key: "${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/*.properties') }}"
20+
restore-keys: "${{ runner.os }}-gradle-"
21+
- run: curl -s "https://get.sdkman.io?rcupdate=false" | bash
22+
- run: |
23+
source "$HOME/.sdkman/bin/sdkman-init.sh"
24+
sdk env install
25+
./gradlew jpackage
File renamed without changes.

README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11

22
# Todo
3-
Todo backend example for [Todo Backend](http://www.todobackend.com/index.html) using Hexagon. Built
4-
with [Travis CI](https://travis-ci.org) and hosted at Heroku here:
5-
[https://todo-backend-hexagonkt.herokuapp.com/](https://todo-backend-hexagonkt.herokuapp.com/)
3+
Todo backend example for [Todo Backend](http://www.todobackend.com/index.html) using Hexagon.
64

75
The spec is:
86

@@ -54,6 +52,7 @@ Response:
5452
```
5553

5654
PATCH `/tasks/{id}`
55+
5756
Request:
5857
```
5958
{
@@ -73,16 +72,21 @@ Response:
7372
}
7473
```
7574
DELETE `/tasks/{id}`
75+
7676
Response: 200 OK
7777

7878
DELETE `/tasks`
79+
7980
Response: 200 OK
8081

8182
### Build
82-
Import the gradle project to build the code.
83+
```bash
84+
./gradlew build jpackage
85+
docker compose --profile local build
86+
```
8387

8488
### Deploy
85-
Set the environment variable `SERVICE_serviceURL` to whatever your host is
89+
Set the environment variable `SERVICE_URL` to whatever your host is
8690
to generate the correct url for tasks.
8791

88-
Set `SERVICE_mongoDbUrl` to the url for your MongoDb database.
92+
Set `MONGODB_URL` to the url for your MongoDb database.

build.gradle.kts

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ val logbackVersion = "1.5.6"
1111
val gradleScripts = "https://raw.githubusercontent.com/hexagontk/hexagon/$hexagonVersion/gradle"
1212

1313
ext.set("modules", "java.xml,java.naming")
14-
ext.set("options", "-Xms64M -Xmx1G -XX:+UseNUMA")
14+
ext.set("options", "-Xms32M -Xmx128m -XX:+UseNUMA")
1515
ext.set("applicationClass", "com.hexagontk.todo.backend.ApplicationKt")
1616

1717
apply(from = "$gradleScripts/kotlin.gradle")
1818
apply(from = "$gradleScripts/application.gradle")
19-
apply(from = "$gradleScripts/native.gradle")
2019

2120
dependencies {
2221
implementation("com.hexagonkt:http_server_jetty:$hexagonVersion")

docker-compose.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ services:
1717
depends_on: [ mongodb ]
1818
image: ${REGISTRY:-}todo
1919
build:
20-
dockerfile: jpackage.dockerfile
20+
dockerfile: Dockerfile
2121
environment:
2222
GLOBAL_LOG_LEVEL: warn
2323
APPLICATION_LOG_LEVEL: info

k8s/config.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ metadata:
88

99
data:
1010
bindAddress: "0.0.0.0"
11-
mongodbUrl: "mongodb://mongodb"
11+
mongodbUrl: "mongodb://root:password@mongodb/todo?authSource=admin"
12+
mongodbRootUsername: "root"
13+
mongodbRootPassword: "password"
14+
mongodbDatabase: "todo"

k8s/mongodb.yaml

+17-1
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,31 @@ spec:
2424
annotations:
2525
environment: local
2626
spec:
27+
restartPolicy: Always
2728
containers:
2829
- image: docker.io/mongo:7-jammy
2930
name: mongodb
31+
env:
32+
- name: MONGO_INITDB_ROOT_USERNAME
33+
valueFrom:
34+
configMapKeyRef:
35+
name: todo-configmap
36+
key: mongodbRootUsername
37+
- name: MONGO_INITDB_ROOT_PASSWORD
38+
valueFrom:
39+
configMapKeyRef:
40+
name: todo-configmap
41+
key: mongodbRootPassword
42+
- name: MONGO_INITDB_DATABASE
43+
valueFrom:
44+
configMapKeyRef:
45+
name: todo-configmap
46+
key: mongodbDatabase
3047
ports:
3148
- containerPort: 27017
3249
volumeMounts:
3350
- mountPath: /data/db
3451
name: mongodb
35-
restartPolicy: Always
3652
volumes:
3753
- name: mongodb
3854
persistentVolumeClaim:

k8s/todo.yaml

+9-7
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@ spec:
2424
app: todo
2525
type: backend
2626
spec:
27+
restartPolicy: Always
2728
containers:
28-
- env:
29-
- name: bindAddress
29+
- image: k3d.localhost:5000/todo
30+
name: todo
31+
env:
32+
- name: BIND_ADDRESS
3033
valueFrom:
3134
configMapKeyRef:
3235
name: todo-configmap
3336
key: bindAddress
34-
- name: mongodbUrl
37+
- name: MONGODB_URL
3538
valueFrom:
3639
configMapKeyRef:
3740
name: todo-configmap
3841
key: mongodbUrl
39-
image: k3d.localhost:5000/todo
40-
name: todo
4142
ports:
4243
- containerPort: 2010
43-
restartPolicy: Always
4444

4545
---
4646

@@ -57,7 +57,9 @@ metadata:
5757
type: backend
5858

5959
spec:
60+
type: NodePort
6061
ports:
61-
- port: 3090
62+
- port: 2010
63+
nodePort: 31080
6264
selector:
6365
io.kompose.service: todo

native.dockerfile

-9
This file was deleted.

src/main/kotlin/Application.kt

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.hexagontk.todo.backend
22

33
import com.hexagonkt.converters.ConvertersManager
4-
import com.hexagonkt.core.ALL_INTERFACES
5-
import com.hexagonkt.core.getPath
6-
import com.hexagonkt.core.requirePath
4+
import com.hexagonkt.core.*
75
import com.hexagonkt.http.server.HttpServer
86
import com.hexagonkt.http.server.HttpServerSettings
97
import com.hexagonkt.http.server.jetty.JettyServletAdapter
@@ -13,8 +11,11 @@ import com.hexagontk.todo.backend.stores.MongoDbTaskStore
1311
internal class Application {
1412
private val server: HttpServer = HttpServer(
1513
adapter = JettyServletAdapter(minThreads = 4),
16-
settings = HttpServerSettings(bindPort = 2010, bindAddress = ALL_INTERFACES),
17-
handler = Router(store = MongoDbTaskStore()).handler
14+
handler = Router(store = MongoDbTaskStore()).handler,
15+
settings = HttpServerSettings(
16+
bindPort = 2010,
17+
bindAddress = Jvm.systemSetting("BIND_ADDRESS", LOOPBACK_INTERFACE)
18+
),
1819
)
1920

2021
fun start() {

src/main/kotlin/entities/Task.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ data class Task(
66
val order: Int? = null,
77
val completed: Boolean? = false
88
) {
9-
val url = "${System.getenv("SERVICE_serviceURL")}/tasks/$id"
9+
val url = "${System.getenv("SERVICE_URL")}/tasks/$id"
1010
}

src/main/kotlin/stores/MongoDbTaskStore.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.hexagontk.todo.backend.stores
22

3+
import com.hexagonkt.core.Jvm
34
import com.hexagonkt.store.mongodb.MongoDbStore
45
import com.hexagontk.todo.backend.entities.Task
56

67
class MongoDbTaskStore: TaskStore {
78
private val store = MongoDbStore(
89
Task::class,
910
Task::id,
10-
System.getenv("MONGODB_URL") ?: "mongodb://root:password@localhost/todo?authSource=admin",
11+
Jvm.systemSetting("MONGODB_URL", "mongodb://root:password@localhost/todo?authSource=admin"),
1112
)
1213

1314
override fun findAll(): List<Task> {

src/main/resources/META-INF/native-image/com.hexagontk/todo_backend/native-image.properties

-4
This file was deleted.

src/test/resources/META-INF/native-image/com.hexagontk/todo_backend/native-image.properties

-3
This file was deleted.

0 commit comments

Comments
 (0)