1
0
Fork 0

Added build with buildah and deploy to kubernetes

This commit is contained in:
Julien Dessaux 2022-10-19 23:20:56 +02:00
parent 08da31f2e7
commit 28424a589d
Signed by: adyxax
GPG key ID: F92E51B86E07177E
4 changed files with 148 additions and 4 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
kcov-output/
node_modules/
zig-cache
zig-out

View file

@ -1,20 +1,34 @@
SHELL:=bash
REVISION=$(shell git rev-parse HEAD)
.PHONY: build
build: ## make build # Builds a cartridge
zig build
build: ## make build # Builds a cartridge
zig build -Drelease-small=true
.PHONY: buildah
buildah: ## make buildah # Builds the container image
deploy/build-image.sh
.PHONY: deploy
deploy: ## make deploy # deploy the cartridge the active kubernetes context
sed -i deploy/kubernetes.yaml -e 's/^\(\s*image:[^:]*:\).*$$/\1$(REVISION)/'
kubectl apply -f deploy/kubernetes.yaml
.PHONY: help
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
.PHONY: init
init: ## make init # initialize project dependencies
init: ## make init # initialize project dependencies
npm install wasm4
rm package.json package-lock.json # w4 will think it is an AssemblyScript game if we leave these files
.PHONY: push
push: ## make push # push the built image to quay.io
buildah push adyxax/grenade-brothers quay.io/adyxax/grenade-brothers:$(REVISION)
.PHONY: serve
serve: ## make serve # run a nodejs web server
serve: ## make serve # run a nodejs web server
node_modules/.bin/w4 watch --no-open --no-qr
.DEFAULT_GOAL := help

45
deploy/build-image.sh Executable file
View file

@ -0,0 +1,45 @@
#!/usr/bin/env bash
set -euo pipefail
zig build -Drelease-small=true
ret=0; buildah images adyxax/alpine &>/dev/null || ret=$?
if [[ "${ret}" != 0 ]]; then
buildah rmi --all
ALPINE_LATEST=$(curl --silent https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/ |
perl -lane '$latest = $1 if $_ =~ /^<a href="(alpine-minirootfs-\d+\.\d+\.\d+-x86_64\.tar\.gz)">/; END {print $latest}'
)
if [ ! -e "./${ALPINE_LATEST}" ]; then
echo "Fetching ${ALPINE_LATEST}..."
curl --silent "https://dl-cdn.alpinelinux.org/alpine/latest-stable/releases/x86_64/${ALPINE_LATEST}" \
--output "./${ALPINE_LATEST}"
fi
ctr=$(buildah from scratch)
buildah add "${ctr}" "${ALPINE_LATEST}" /
buildah run "${ctr}" /bin/sh -c 'apk upgrade --no-cache'
buildah run "${ctr}" /bin/sh -c 'apk add --no-cache pcre sqlite-libs'
buildah commit "${ctr}" adyxax/alpine
buildah rm "${ctr}"
fi
ret=0; buildah images adyxax/wasm4 &>/dev/null || ret=$?
if [[ "${ret}" != 0 ]]; then
wasm4=$(buildah from adyxax/alpine)
buildah run "${wasm4}" /bin/sh -c 'apk add --no-cache nodejs-current npm'
buildah run "${wasm4}" /bin/sh -c 'npm install -g wasm4'
buildah commit "${wasm4}" adyxax/wasm4
else
wasm4=$(buildah from adyxax/wasm4)
fi
buildah copy "${wasm4}" zig-out/lib/cart.wasm /
buildah config \
--author 'Julien Dessaux' \
--cmd "w4 run --port 80 --no-open --no-qr /cart.wasm" \
--port 80 \
"${wasm4}"
buildah commit "${wasm4}" adyxax/grenade-brothers
buildah rm "${wasm4}"

84
deploy/kubernetes.yaml Normal file
View file

@ -0,0 +1,84 @@
apiVersion: v1
kind: Namespace
metadata:
name: grenade-brothers
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: grenade-brothers
name: grenade-brothers
labels:
app: grenade-brothers
spec:
replicas: 1
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
selector:
matchLabels:
app: grenade-brothers
template:
metadata:
labels:
app: grenade-brothers
spec:
containers:
- name: grenade-brothers
image: quay.io/adyxax/grenade-brothers:08da31f2e79f87a93281afe8f13eaa700d57708a
ports:
- containerPort: 80
readinessProbe:
httpGet:
path: '/'
port: 80
initialDelaySeconds: 1
timeoutSeconds: 1
livenessProbe:
httpGet:
path: '/'
port: 80
initialDelaySeconds: 1
timeoutSeconds: 1
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "sleep 10"]
---
apiVersion: v1
kind: Service
metadata:
namespace: grenade-brothers
name: grenade-brothers
spec:
type: ClusterIP
selector:
app: grenade-brothers
ports:
- protocol: TCP
port: 80
targetPort: 80
name: grenade-brothers
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: grenade-brothers
name: grenade-brothers
spec:
ingressClassName: nginx
tls:
- secretName: wildcard-adyxax-org
rules:
- host: grenade-brothers.adyxax.org
http:
paths:
- path: '/'
pathType: Prefix
backend:
service:
name: grenade-brothers
port:
number: 80