From 879bd06259088d07fa78ae19d6b1f4101c67849a Mon Sep 17 00:00:00 2001 From: Mathieu Benoit Date: Wed, 3 Jul 2024 23:12:57 +0000 Subject: [PATCH] score-k8s for node-service --- templates/node-service/content/.gitignore | 4 +- templates/node-service/content/Makefile | 57 ++++++++++++++++++- templates/node-service/content/README.md | 33 +++++++++-- .../content/scripts/setup-kind-cluster.sh | 35 ++++++++++++ templates/podinfo-example/content/Makefile | 9 ++- templates/podinfo-example/content/README.md | 33 ++++++++++- 6 files changed, 162 insertions(+), 9 deletions(-) create mode 100755 templates/node-service/content/scripts/setup-kind-cluster.sh diff --git a/templates/node-service/content/.gitignore b/templates/node-service/content/.gitignore index 446a26c..f5757b0 100644 --- a/templates/node-service/content/.gitignore +++ b/templates/node-service/content/.gitignore @@ -1,3 +1,5 @@ node_modules/ .score-compose/ -compose.yaml \ No newline at end of file +compose.yaml +.score-k8s/ +manifests.yaml \ No newline at end of file diff --git a/templates/node-service/content/Makefile b/templates/node-service/content/Makefile index 8beb9af..c7850f7 100644 --- a/templates/node-service/content/Makefile +++ b/templates/node-service/content/Makefile @@ -38,4 +38,59 @@ compose-test: compose-up ## Delete the containers running via compose down. .PHONY: compose-down compose-down: - docker compose down -v --remove-orphans || true \ No newline at end of file + docker compose down -v --remove-orphans || true + +.score-k8s/state.yaml: + score-k8s init \ + --no-sample + +manifests.yaml: score.yaml .score-k8s/state.yaml Makefile + score-k8s generate score.yaml \ + --image ${CONTAINER_IMAGE} + +## Create a local Kind cluster. +.PHONY: kind-create-cluster +kind-create-cluster: + ./scripts/setup-kind-cluster.sh + +## Load the local container image in the current Kind cluster. +.PHONY: kind-load-image +kind-load-image: + kind load docker-image ${CONTAINER_IMAGE} + +NAMESPACE ?= default +## Generate a manifests.yaml file from the score spec, deploy it to Kubernetes and wait for the Pods to be Ready. +.PHONY: k8s-up +k8s-up: manifests.yaml + kubectl apply \ + -f manifests.yaml \ + -n ${NAMESPACE} + kubectl wait deployments/${WORKLOAD_NAME} \ + -n ${NAMESPACE} \ + --for condition=Available \ + --timeout=90s + kubectl wait pods \ + -n ${NAMESPACE} \ + -l app.kubernetes.io/name=${WORKLOAD_NAME} \ + --for condition=Ready \ + --timeout=90s + sleep 5 + +## Expose the container deployed in Kubernetes via port-forward. +.PHONY: k8s-test +k8s-test: k8s-up + curl $$(score-k8s resources get-outputs dns.default#${WORKLOAD_NAME}.dns --format '{{ .host }}') + +## Delete the deployment of the local container in Kubernetes. +.PHONY: k8s-down +k8s-down: + kubectl delete \ + -f manifests.yaml \ + -n ${NAMESPACE} + +## Deploy the workload to Humanitec. +.PHONY: htc-up +htc-up: + humctl score deploy \ + -f score.yaml \ + --wait \ No newline at end of file diff --git a/templates/node-service/content/README.md b/templates/node-service/content/README.md index de190e9..a46fcd3 100644 --- a/templates/node-service/content/README.md +++ b/templates/node-service/content/README.md @@ -1,11 +1,36 @@ # Score - ${{ values.name | capitalize }} -A simple score project deploying a hello world app. +The workload is a simple containerized NodeJS app talking to a MySQL database. + +[Score](https://score.dev/) is used to deploy the workload locally with `score-compose` in Docker, with `score-k8s` in Kubernetes or with `humctl` in Humanitec. See [Makefile](Makefile) for more details. ## Deploying -[Score](https://score.dev/) is used to deploy the workload to humanitec. +Locally: +```bash +make compose-up + +make compose-test +``` + +In Kubernetes (`Kind`): +```bash +make kind-create-cluster + +make kind-load-image + +make k8s-up + +make k8s-test +``` + +In Humanitec: +```bash +humctl login -## The Workload +humctl config set org FIXME +humctl config set app ${{ values.name }} +humctl config set app development -The workload is a simple nodejs app which is dockerized. +make htc-up +``` \ No newline at end of file diff --git a/templates/node-service/content/scripts/setup-kind-cluster.sh b/templates/node-service/content/scripts/setup-kind-cluster.sh new file mode 100755 index 0000000..7e9bb72 --- /dev/null +++ b/templates/node-service/content/scripts/setup-kind-cluster.sh @@ -0,0 +1,35 @@ +#!/bin/bash +set -o errexit + +cat <