Skip to content

Commit

Permalink
Migrate the k8s deployment file generation to PKL
Browse files Browse the repository at this point in the history
  • Loading branch information
thesocialdev committed Aug 25, 2024
1 parent 1826e9a commit cfc2917
Show file tree
Hide file tree
Showing 7 changed files with 462 additions and 219 deletions.
414 changes: 195 additions & 219 deletions .github/workflows/aws.yml

Large diffs are not rendered by default.

92 changes: 92 additions & 0 deletions deployment/k8s/app.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/K8sResource.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/api/apps/v1/Deployment.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/api/core/v1/Service.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/api/networking/v1/Ingress.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/api/core/v1/PodSpec.pkl"
import "./modules/ingress.pkl" as ingress

hidden appNamespace = "development"
hidden podPort = 3000

hidden IngressOptions = new {
rules = new {}
}
hidden ServiceOptions = new {
spec = new Service.ServiceSpec {
type = "NodePort"
selector {
["app"] = "aletheia"
}
ports {
new {
name = "aletheia"
targetPort = trace(podPort)
port = 80
}
}
}
}
hidden DeploymentOptions = new {
replicas = 1
containers = new Listing<PodSpec.Container> {}
}


resources: Listing<K8sResource> = new {
new Ingress {
metadata {
name = "ingress-aletheia"
namespace = appNamespace
annotations {
["kubernetes.io/ingress.class"] = "traefik"
}
}
spec {
rules = new {
for (_rule in IngressOptions.rules) {
_rule
}
}
}
}

new Service {
metadata {
name = "aletheia"
namespace = appNamespace
}
spec = ServiceOptions.spec
}

new Deployment {
metadata {
name = "aletheia"
namespace = appNamespace
}
spec {
replicas = DeploymentOptions.replicas
selector {
matchLabels {
["app"] = "aletheia"
}
}
template {
metadata {
labels {
["app"] = "aletheia"
}
}
spec {
containers = DeploymentOptions.containers
}
}
}
}
}

output {
value = resources
renderer = (K8sResource.output.renderer as YamlRenderer) {
isStream = true
}
}
28 changes: 28 additions & 0 deletions deployment/k8s/development.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
amends "./app.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/K8sResource.pkl"
import "./modules/ingress.pkl" as ingress
import "./modules/aletheia.pkl"

appNamespace = "development"
podPort = 3000

local newAletheia = new (aletheia) {
ns = appNamespace
p = podPort
}

IngressOptions {
rules {
(ingress.rule) {
host = "test.aletheiafact.org"
}
}
}

DeploymentOptions {
containers {
(newAletheia.pod.container) {
name = "aletheia"
}
}
}
11 changes: 11 additions & 0 deletions deployment/k8s/modules/aletheia.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import "./container.pkl" as containerConfig
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/api/core/v1/PodSpec.pkl"

hidden ns = ""
hidden p = 3000

pod = (containerConfig) {
namespace = ns
imagePath = "134187360702.dkr.ecr.us-east-1.amazonaws.com/aletheiafact-production" + ":" + read("env:TAG")
podPort = p
}
79 changes: 79 additions & 0 deletions deployment/k8s/modules/container.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/api/core/v1/EnvVar.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/api/core/v1/ResourceRequirements.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/api/core/v1/PodSpec.pkl"

hidden namespace = ""
hidden imagePath = ""
hidden podPort = ""

container: PodSpec.Container = new {
name = ""
image = imagePath
imagePullPolicy = "Always"
env = new {
new {
name = "NEXT_PUBLIC_UMAMI_SITE_ID"
value = read("env:UMAMI_SITE_ID")
}
new {
name = "NEXT_PUBLIC_RECAPTCHA_SITEKEY"
value = read("env:RECAPTCHA_SITEKEY")
}
new {
name = "ORY_SDK_URL"
value = read("env:ORY_SDK_URL")
}
new {
name = "ORY_ACCESS_TOKEN"
value = read("env:ORY_ACCESS_TOKEN")
}
new {
name = "NEW_RELIC_LICENSE_KEY"
value = read("env:NEW_RELIC_LICENSE_KEY")
}
new {
name = "NEW_RELIC_APP_NAME"
value = "aletheia-" + namespace
}
new {
name = "NEXT_PUBLIC_ORY_SDK_URL"
value = read("env:NEXT_PUBLIC_ORYSDKURL")
}
new {
name = "OPENAI_API_KEY"
value = read("env:OPENAI_API_KEY")
}
new {
name = "ENV_NAME"
value = namespace
}
}

readinessProbe {
httpGet {
path = "/api/health"
port = podPort
}
initialDelaySeconds = 50
timeoutSeconds = 5
}
livenessProbe {
httpGet {
path = "/api/health"
port = podPort
}
initialDelaySeconds = 50
timeoutSeconds = 10
failureThreshold = 10
}
resources {
requests {
["cpu"] = "300m"
["memory"] = 512.mib
}
limits {
["cpu"] = "400m"
["memory"] = 1024.mib
}
}
}
25 changes: 25 additions & 0 deletions deployment/k8s/modules/ingress.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/api/networking/v1/Ingress.pkl" as Ingress
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/api/apps/v1/Deployment.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/api/core/v1/Service.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/K8sResource.pkl"

rule: Ingress.IngressRule = new {
host = "www.test.aletheiafact.org"
http {
paths {
new {
path = "/"
pathType = "Prefix"
backend {
service {
name = "aletheia"
port {
name = "aletheia"
}
}
}
}
}
}
}

32 changes: 32 additions & 0 deletions deployment/k8s/production.pkl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
amends "./app.pkl"
import "package://pkg.pkl-lang.org/pkl-k8s/[email protected]#/K8sResource.pkl"
import "./modules/ingress.pkl" as ingress
import "./modules/aletheia.pkl"

appNamespace = "production"
podPort = 3000

local newAletheia = new (aletheia) {
ns = appNamespace
p = podPort
}

IngressOptions {
rules {
(ingress.rule) {
host = "aletheiafact.org"
}

(ingress.rule) {
host = "www.aletheiafact.org"
}
}
}

DeploymentOptions {
containers {
(newAletheia.pod.container) {
name = "aletheia"
}
}
}

0 comments on commit cfc2917

Please sign in to comment.