From eb01191f5431a1de6b2cc970d9f55fa9d2aa22c7 Mon Sep 17 00:00:00 2001 From: Alberto Martino Date: Mon, 16 Jun 2025 00:24:25 +0200 Subject: [PATCH 1/4] Add Taskfile for checking required variables --- variables/Taskfile.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 variables/Taskfile.yml diff --git a/variables/Taskfile.yml b/variables/Taskfile.yml new file mode 100644 index 0000000..90f7774 --- /dev/null +++ b/variables/Taskfile.yml @@ -0,0 +1,15 @@ +version: 3 + +tasks: + check-var: + desc: "Checks if a required variable is set" + cmds: + - | + VAR_NAME="{{.VAR_NAME}}" + VAR_VALUE="{{.VAR_VALUE}}" + + if [ -z "$VAR_VALUE" ]; then + printf "Error: required variable '%s' is not set or empty.\n" "$VAR_NAME" >&2 + exit 1 + fi + silent: true \ No newline at end of file From 149cbb9c20dccecf8ed81898a4a518d07366060e Mon Sep 17 00:00:00 2001 From: Alberto Martino Date: Mon, 16 Jun 2025 00:26:49 +0200 Subject: [PATCH 2/4] Add Taskfile for managing CouchDB container operations bonus tasks: - start: useful when the container was already created before - listdbs: executes the curl command to list the available database - createdb: executes curl to create a database (implemented to study variable concepts in tasks) --- docker-couchdb/Taskfile.yml | 94 +++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 docker-couchdb/Taskfile.yml diff --git a/docker-couchdb/Taskfile.yml b/docker-couchdb/Taskfile.yml new file mode 100644 index 0000000..a8e40c9 --- /dev/null +++ b/docker-couchdb/Taskfile.yml @@ -0,0 +1,94 @@ +version: 3 + +vars: + IMAGE: couchdb:latest + CONTAINER_NAME: couchdb-server + PORT: "5984" + COUCHDB_USER: admin + COUCHDB_PASSWORD: admin + ENDPOINT: http://{{.COUCHDB_USER}}:{{.COUCHDB_PASSWORD}}@localhost:{{.PORT}} + +includes: + variables: + taskfile: ../variables/Taskfile.yml + dir: ../variables + +tasks: + default: task --list-all + + dbcliprereq: + silent: true + cmds: + - test -x "$(which curl)" || { echo "curl not available"; exit 1; } + + create: + desc: Run the Couchdb container + cmds: + - docker run -d --name {{.CONTAINER_NAME}} -e COUCHDB_USER={{.COUCHDB_USER}} -e COUCHDB_PASSWORD={{.COUCHDB_PASSWORD}} -p {{.PORT}}:{{.PORT}} {{.IMAGE}} + silent: true + + stop: + desc: Stop the Couchdb container + cmds: + - docker stop {{.CONTAINER_NAME}} + silent: true + + remove: + desc: Remove the Couchdb container + cmds: + - docker rm -f {{.CONTAINER_NAME}} + silent: true + + logs: + desc: View the logs of the Couchdb container + cmds: + - docker logs {{.CONTAINER_NAME}} + silent: false + + status: + desc: Check the status of the Couchdb container + cmds: + - docker ps -a --filter "name={{.CONTAINER_NAME}}" + silent: true + + start: + desc: Start the Couchdb container + cmds: + - | + if docker ps -a --format '{{`{{.Names}}`}}' | grep -q '^{{.CONTAINER_NAME}}$'; then + if [ "$(docker inspect -f '{{`{{.State.Status}}`}}' {{.CONTAINER_NAME}})" = "exited" ]; then + docker start {{.CONTAINER_NAME}} + else + echo "Container is already running or in different state" + fi + else + echo "Container does not exist. Use 'task create' first" + fi + silent: true + + listdbs: + desc: List the Couchdb databases + cmds: + - task: dbcliprereq + - curl '{{.ENDPOINT}}/_all_dbs' + silent: true + + createdb: + desc: Create a couchdb database + vars: + DB_NAME: '{{.DB_NAME | default ""}}' + cmds: + - task: dbcliprereq + - task: variables:check-var + vars: { VAR_NAME: "DB_NAME", VAR_VALUE: "{{.DB_NAME}}" } + - curl -XPUT '{{.ENDPOINT}}/{{.DB_NAME}}' + + test: + desc: Run a test command in the Couchdb container + cmds: + - task: create + - task: status + - task: logs + - task: remove + - task: status + silent: false \ No newline at end of file From 84f8ec00dd17340240766a8fb5ea6008fa683ecc Mon Sep 17 00:00:00 2001 From: Alberto Martino Date: Mon, 16 Jun 2025 00:28:50 +0200 Subject: [PATCH 3/4] Add Taskfile for managing CouchDB deployment in k8s alongside its k8s templates Includes tasks for secret generation, deployment rendering, pod management, and related utilities. The two templates for rendering secret and deployment were created to study in deep env substitution. --- .gitignore | 1 + kube-couchdb/Taskfile.yml | 98 +++++++++++++++++++++++++++ kube-couchdb/couchdb-secret.tmpl.yaml | 9 +++ kube-couchdb/couchdb-secret.yaml | 9 +++ 4 files changed, 117 insertions(+) create mode 100644 .gitignore create mode 100644 kube-couchdb/Taskfile.yml create mode 100644 kube-couchdb/couchdb-secret.tmpl.yaml create mode 100644 kube-couchdb/couchdb-secret.yaml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/kube-couchdb/Taskfile.yml b/kube-couchdb/Taskfile.yml new file mode 100644 index 0000000..b1f2ed0 --- /dev/null +++ b/kube-couchdb/Taskfile.yml @@ -0,0 +1,98 @@ +version: 3 + +vars: + COUCHDB_USER: admin + COUCHDB_PASSWORD: admin + COUCHDB_USER_B64: + sh: 'echo -n "{{.COUCHDB_USER}}" | base64' + COUCHDB_PASSWORD_B64: + sh: 'echo -n "{{.COUCHDB_PASSWORD}}" | base64' + IMAGE: couchdb:latest + POD_NAME: couchdb-server + NAMESPACE: default + +tasks: + + default: task --list-all + + render-secret: + desc: Generates couchdb secret + env: + COUCHDB_USER: "{{.COUCHDB_USER_B64}}" + COUCHDB_PASSWORD: "{{.COUCHDB_PASSWORD_B64}}" + POD_NAME: "{{.POD_NAME}}" + NAMESPACE: "{{.NAMESPACE}}" + cmds: + - envsubst < couchdb-secret.tmpl.yaml > couchdb-secret.yaml + internal: true + + render-deployment: + desc: Render couchdb deployment manifest + env: + COUCHDB_USER: "{{.COUCHDB_USER_B64}}" + COUCHDB_PASSWORD: "{{.COUCHDB_PASSWORD_B64}}" + POD_NAME: "{{.POD_NAME}}" + NAMESPACE: "{{.NAMESPACE}}" + IMAGE: "{{.IMAGE}}" + cmds: + - echo "Rendering manifest..." + - envsubst < couchdb.tmpl.yaml > couchdb.yaml + internal: true + + render-templates: + internal: true + cmds: + - task: render-secret + - task: render-deployment + + create: + desc: Run the Couchdb pod + cmds: + - task: render-templates + - kubectl --dry-run=server apply -f couchdb-secret.yaml + - kubectl --dry-run=server apply -f couchdb.yaml + silent: true + + status: + desc: Check the status of the Couchdb pod + cmds: + - kubectl get pods -l app=couchdb + silent: true + + wait: + desc: Wait for the Couchdb pod to be ready + cmds: + - kubectl wait --for=condition=Ready pod -l app=couchdb --timeout=60s + silent: true + + remove: + desc: Remove the Couchdb pod + cmds: + - kubectl delete -f couchdb.tmpl.yaml + silent: true + + logs: + desc: View the logs of the Couchdb pod + cmds: + - kubectl logs -l app=couchdb + silent: false + + cli: + vars: + POD: + sh: | + kubectl get pods -l app=couchdb -o jsonpath='{.items[0].metadata.name}' + desc: Open a shell in the Couchdb pod + cmds: + - kubectl exec -it {{.POD}} -- couchdb-cli + silent: false + + test: + desc: Run a test command in the Couchdb pod + cmds: + - task: create + - task: wait + - task: status + - task: logs + - task: remove + silent: false \ No newline at end of file diff --git a/kube-couchdb/couchdb-secret.tmpl.yaml b/kube-couchdb/couchdb-secret.tmpl.yaml new file mode 100644 index 0000000..a29b08e --- /dev/null +++ b/kube-couchdb/couchdb-secret.tmpl.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Secret +metadata: + name: ${POD_NAME}-secret + namespace: ${NAMESPACE} +type: Opaque +data: + username: ${COUCHDB_USER} + password: ${COUCHDB_PASSWORD} diff --git a/kube-couchdb/couchdb-secret.yaml b/kube-couchdb/couchdb-secret.yaml new file mode 100644 index 0000000..74a0b40 --- /dev/null +++ b/kube-couchdb/couchdb-secret.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Secret +metadata: + name: couchdb-server-secret + namespace: default +type: Opaque +data: + username: YWRtaW4= + password: YWRtaW4= From ed267a320b7c87cd029bd106615e84520285e436 Mon Sep 17 00:00:00 2001 From: Alberto Martino Date: Mon, 16 Jun 2025 00:33:02 +0200 Subject: [PATCH 4/4] Add CouchDB k8s integration and container management to Taskfile hierarchy closes #1 --- Taskfile.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Taskfile.yml b/Taskfile.yml index 3aac17e..51427e0 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -4,12 +4,18 @@ includes: docker-redis: taskfile: ./docker-redis/Taskfile.yml dir: ./docker-redis + docker-couchdb: + taskfile: ./docker-couchdb/Taskfile.yml + dir: ./docker-couchdb kube-kind: taskfile: ./kube-kind/Taskfile.yml dir: ./kube-kind kube-redis: taskfile: ./kube-redis/Taskfile.yml dir: ./kube-redis + kube-couchdb: + taskfile: ./kube-couchdb/Taskfile.yml + dir: ./kube-couchdb tasks: default: task --list-all \ No newline at end of file