Skip to content

Commit 0ae3b52

Browse files
committed
Migrate the k8s deployment file generation to PKL
1 parent 1826e9a commit 0ae3b52

File tree

7 files changed

+462
-219
lines changed

7 files changed

+462
-219
lines changed

.github/workflows/aws.yml

Lines changed: 195 additions & 219 deletions
Large diffs are not rendered by default.

deployment/k8s/app.pkl

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/K8sResource.pkl"
2+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/api/apps/v1/Deployment.pkl"
3+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/api/core/v1/Service.pkl"
4+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/api/networking/v1/Ingress.pkl"
5+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/api/core/v1/PodSpec.pkl"
6+
import "./modules/ingress.pkl" as ingress
7+
8+
hidden appNamespace = "development"
9+
hidden podPort = 3000
10+
11+
hidden IngressOptions = new {
12+
rules = new {}
13+
}
14+
hidden ServiceOptions = new {
15+
spec = new Service.ServiceSpec {
16+
type = "NodePort"
17+
selector {
18+
["app"] = "aletheia"
19+
}
20+
ports {
21+
new {
22+
name = "aletheia"
23+
targetPort = trace(podPort)
24+
port = 80
25+
}
26+
}
27+
}
28+
}
29+
hidden DeploymentOptions = new {
30+
replicas = 1
31+
containers = new Listing<PodSpec.Container> {}
32+
}
33+
34+
35+
resources: Listing<K8sResource> = new {
36+
new Ingress {
37+
metadata {
38+
name = "ingress-aletheia"
39+
namespace = appNamespace
40+
annotations {
41+
["kubernetes.io/ingress.class"] = "traefik"
42+
}
43+
}
44+
spec {
45+
rules = new {
46+
for (_rule in IngressOptions.rules) {
47+
_rule
48+
}
49+
}
50+
}
51+
}
52+
53+
new Service {
54+
metadata {
55+
name = "aletheia"
56+
namespace = appNamespace
57+
}
58+
spec = ServiceOptions.spec
59+
}
60+
61+
new Deployment {
62+
metadata {
63+
name = "aletheia"
64+
namespace = appNamespace
65+
}
66+
spec {
67+
replicas = DeploymentOptions.replicas
68+
selector {
69+
matchLabels {
70+
["app"] = "aletheia"
71+
}
72+
}
73+
template {
74+
metadata {
75+
labels {
76+
["app"] = "aletheia"
77+
}
78+
}
79+
spec {
80+
containers = DeploymentOptions.containers
81+
}
82+
}
83+
}
84+
}
85+
}
86+
87+
output {
88+
value = resources
89+
renderer = (K8sResource.output.renderer as YamlRenderer) {
90+
isStream = true
91+
}
92+
}

deployment/k8s/development.pkl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
amends "./app.pkl"
2+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/K8sResource.pkl"
3+
import "./modules/ingress.pkl" as ingress
4+
import "./modules/aletheia.pkl"
5+
6+
appNamespace = "development"
7+
podPort = 3000
8+
9+
local newAletheia = new (aletheia) {
10+
ns = appNamespace
11+
p = podPort
12+
}
13+
14+
IngressOptions {
15+
rules {
16+
(ingress.rule) {
17+
host = "test.aletheiafact.org"
18+
}
19+
}
20+
}
21+
22+
DeploymentOptions {
23+
containers {
24+
(newAletheia.pod.container) {
25+
name = "aletheia"
26+
}
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import "./container.pkl" as containerConfig
2+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/api/core/v1/PodSpec.pkl"
3+
4+
hidden ns = ""
5+
hidden p = 3000
6+
7+
pod = (containerConfig) {
8+
namespace = ns
9+
imagePath = "134187360702.dkr.ecr.us-east-1.amazonaws.com/aletheiafact-production" + ":" + read("env:TAG")
10+
podPort = p
11+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/api/core/v1/EnvVar.pkl"
2+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/api/core/v1/ResourceRequirements.pkl"
3+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/api/core/v1/PodSpec.pkl"
4+
5+
hidden namespace = ""
6+
hidden imagePath = ""
7+
hidden podPort = ""
8+
9+
container: PodSpec.Container = new {
10+
name = ""
11+
image = imagePath
12+
imagePullPolicy = "Always"
13+
env = new {
14+
new {
15+
name = "NEXT_PUBLIC_UMAMI_SITE_ID"
16+
value = read("env:UMAMI_SITE_ID")
17+
}
18+
new {
19+
name = "NEXT_PUBLIC_RECAPTCHA_SITEKEY"
20+
value = read("env:RECAPTCHA_SITEKEY")
21+
}
22+
new {
23+
name = "ORY_SDK_URL"
24+
value = read("env:ORY_SDK_URL")
25+
}
26+
new {
27+
name = "ORY_ACCESS_TOKEN"
28+
value = read("env:ORY_ACCESS_TOKEN")
29+
}
30+
new {
31+
name = "NEW_RELIC_LICENSE_KEY"
32+
value = read("env:NEW_RELIC_LICENSE_KEY")
33+
}
34+
new {
35+
name = "NEW_RELIC_APP_NAME"
36+
value = "aletheia-" + namespace
37+
}
38+
new {
39+
name = "NEXT_PUBLIC_ORY_SDK_URL"
40+
value = read("env:NEXT_PUBLIC_ORYSDKURL")
41+
}
42+
new {
43+
name = "OPENAI_API_KEY"
44+
value = read("env:OPENAI_API_KEY")
45+
}
46+
new {
47+
name = "ENV_NAME"
48+
value = namespace
49+
}
50+
}
51+
52+
readinessProbe {
53+
httpGet {
54+
path = "/api/health"
55+
port = podPort
56+
}
57+
initialDelaySeconds = 50
58+
timeoutSeconds = 5
59+
}
60+
livenessProbe {
61+
httpGet {
62+
path = "/api/health"
63+
port = podPort
64+
}
65+
initialDelaySeconds = 50
66+
timeoutSeconds = 10
67+
failureThreshold = 10
68+
}
69+
resources {
70+
requests {
71+
["cpu"] = "300m"
72+
["memory"] = 512.mib
73+
}
74+
limits {
75+
["cpu"] = "400m"
76+
["memory"] = 1024.mib
77+
}
78+
}
79+
}

deployment/k8s/modules/ingress.pkl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/api/networking/v1/Ingress.pkl" as Ingress
2+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/api/apps/v1/Deployment.pkl"
3+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/api/core/v1/Service.pkl"
4+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/K8sResource.pkl"
5+
6+
rule: Ingress.IngressRule = new {
7+
host = "www.test.aletheiafact.org"
8+
http {
9+
paths {
10+
new {
11+
path = "/"
12+
pathType = "Prefix"
13+
backend {
14+
service {
15+
name = "aletheia"
16+
port {
17+
name = "aletheia"
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}
25+

deployment/k8s/production.pkl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
amends "./app.pkl"
2+
import "package://pkg.pkl-lang.org/pkl-k8s/k8s@1.0.1#/K8sResource.pkl"
3+
import "./modules/ingress.pkl" as ingress
4+
import "./modules/aletheia.pkl"
5+
6+
appNamespace = "production"
7+
podPort = 3000
8+
9+
local newAletheia = new (aletheia) {
10+
ns = appNamespace
11+
p = podPort
12+
}
13+
14+
IngressOptions {
15+
rules {
16+
(ingress.rule) {
17+
host = "aletheiafact.org"
18+
}
19+
20+
(ingress.rule) {
21+
host = "www.aletheiafact.org"
22+
}
23+
}
24+
}
25+
26+
DeploymentOptions {
27+
containers {
28+
(newAletheia.pod.container) {
29+
name = "aletheia"
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)