Skip to content

Commit cfde2c8

Browse files
authored
feat: add run-k3d (#5)
1 parent e083fd7 commit cfde2c8

File tree

14 files changed

+178
-66
lines changed

14 files changed

+178
-66
lines changed

.github/workflows/run-tests.yml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ name: run-tests
22

33
on:
44
workflow_dispatch:
5+
push:
6+
branches-ignore:
7+
- main
8+
paths:
9+
- ".github/workflows/run-tests.yml"
10+
- "**kind**"
11+
- "**k3s**"
512

613
permissions: read-all
714

@@ -15,16 +22,17 @@ jobs:
1522
runs-on: ${{ matrix.os }}
1623
strategy:
1724
matrix:
18-
os: [ubuntu-24.04, ubuntu-24.04-arm]
19-
timeout-minutes: 30
25+
# os: [ubuntu-24.04, ubuntu-24.04-arm]
26+
os: [ubuntu-24.04]
27+
timeout-minutes: 10
2028
steps:
2129
- name: 🛠️ Checkout Code
2230
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
2331

2432
- name: 🚀 Run mise
2533
run: |
2634
docker run --rm -i \
27-
--env SOPS_AGE_KEY --env MISE_SOPS_AGE_KEY \
35+
--env SOPS_AGE_KEY --env MISE_SOPS_AGE_KEY --env CLICOLOR_FORCE=1 \
2836
-v "$PWD:/mnt" \
2937
-v "/var/run/docker.sock:/var/run/docker.sock" \
3038
--workdir /mnt \
@@ -40,8 +48,9 @@ jobs:
4048
runs-on: ${{ matrix.os }}
4149
strategy:
4250
matrix:
43-
os: [ubuntu-24.04, ubuntu-24.04-arm]
44-
timeout-minutes: 30
51+
# os: [ubuntu-24.04, ubuntu-24.04-arm]
52+
os: [ubuntu-24.04]
53+
timeout-minutes: 10
4554
steps:
4655
- name: 🛠️ Checkout Code
4756
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

.jscpd.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"ignore": ["**/scripts/**", "**/.git/**", ".github/workflows/run-tests.yml"]
3+
}

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,25 @@
33
Infrastructure as Code for provisioning multiple Kubernetes clusters, managed
44
using GitOps with ArgoCD
55

6-
Tests:
6+
Create all "kind" clusters:
7+
8+
```bash
9+
mise task run "create:kind:*"
10+
mise task run "delete:kind:*"
11+
```
12+
13+
Create all "k3s" clusters:
14+
15+
```bash
16+
mise task run "create:k3d:*"
17+
mise task run "delete:k3d:*"
18+
```
19+
20+
> Same for eksctl, az, terraform-aws, terraform-az, ... clusters
21+
22+
---
23+
24+
## Tests
725

826
```bash
927
SOPS_AGE_KEY="$(grep -v ^# ~/Documents/secrets/age.txt)"
@@ -16,7 +34,7 @@ docker run --rm -it \
1634
-v "$PWD:/mnt" \
1735
-v "/var/run/docker.sock:/var/run/docker.sock" \
1836
--workdir /mnt \
19-
bash bash -c 'set -euxo pipefail && \
37+
bash bash -c 'set -euo pipefail && \
2038
apk add docker && \
2139
wget -q https://mise.run -O - | sh && \
2240
eval "$(~/.local/bin/mise activate bash)" && \

clusters/k3d01-internal/.env.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This file should contains the secrets and should be encrypted using SOPS

clusters/k3d01-internal/mise.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[tools]
2+
k3d = "5.8.3"
3+
4+
[env]
5+
_.file = ".env.yaml"
6+
# keep-sorted start
7+
CLUSTER_FQDN = "k3d01.internal"
8+
CLUSTER_NAME = "k3d01"
9+
# keep-sorted end
10+
11+
[tasks."create:k3d01-internal"]
12+
description = 'Create K8s cluster'
13+
run = '${CLUSTERS_RUN_SCRIPT_DIRECTORY}/run-k3d.sh create'
14+
15+
[tasks."delete:k3d01-internal"]
16+
description = 'Delete K8s cluster'
17+
run = '${CLUSTERS_RUN_SCRIPT_DIRECTORY}/run-k3d.sh delete'

clusters/k3d02-internal/.env.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This file should contains the secrets and should be encrypted using SOPS

clusters/k3d02-internal/mise.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[tools]
2+
k3d = "5.8.3"
3+
4+
[env]
5+
_.file = ".env.yaml"
6+
# keep-sorted start
7+
CLUSTER_FQDN = "k3d02.internal"
8+
CLUSTER_NAME = "k3d02"
9+
# keep-sorted end
10+
11+
[tasks."create:k3d02-internal"]
12+
description = 'Create K8s cluster'
13+
run = '${CLUSTERS_RUN_SCRIPT_DIRECTORY}/run-k3d.sh create'
14+
15+
[tasks."delete:k3d02-internal"]
16+
description = 'Delete K8s cluster'
17+
run = '${CLUSTERS_RUN_SCRIPT_DIRECTORY}/run-k3d.sh delete'

clusters/kind01-internal/.env.yaml

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1 @@
1-
SECRET: ENC[AES256_GCM,data:QIOd+nLFzAQ=,iv:6o+BhThDyGAQ1jwjG04bS95c55xdUCdB6XhezYrKv90=,tag:tnwKw9zZjc7OLDi7YbiYnw==,type:str]
2-
sops:
3-
kms: []
4-
gcp_kms: []
5-
azure_kv: []
6-
hc_vault: []
7-
age:
8-
- recipient: age1jjuamrdk3vrk6g8qhrjnqtt4x2yvvxw7fz2nkvf78398dj7vav7s74z4zz
9-
enc: |
10-
-----BEGIN AGE ENCRYPTED FILE-----
11-
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBTNXVyZWd4bTFWekVKbjdn
12-
dzlPcWZnWUxsME9UWlN2NjgzMGhoQ1VIV0drCnlScGZTYitqcTYyR291eVArYWVs
13-
c1JyU2FTVWJBajdWdFVGRFN5TXFsMWMKLS0tIEt2WjdNN3FWaU5UQnNwRDNseTVy
14-
TXhtbU94bVFjOTJiUmQyQ2U5VWU0WlkK5Ur8KSyl5/4/AJrADYLRF/27r7hEZzY1
15-
0Qo5LZDZOLXoJ6RBEAYi7WNj/hYbgoBd6maB93fMUaEW6MfC5zK6DA==
16-
-----END AGE ENCRYPTED FILE-----
17-
lastmodified: "2025-03-14T09:14:06Z"
18-
mac: ENC[AES256_GCM,data:kDFNXFwtCNtUISMdYQG0yu38ORYdwsp5v02KrzFZS/zj3mPSNU71RGRPn44Vp9noztBRzz0C6kwql7BaWC2twDVAlR9FIrQDl+jf9PQiO9kBmrQo60TJq4gFe0jesWaBFyKDAG3ffi/oELJs8NZVkbBXwpshNoLs0C06nwc989g=,iv:fPVkLhox+xd5KXFmC6t+CJJ8c4Nsjh8u0DSCvnanYKM=,tag:2isNCgC8yxDaZOojyqgEjA==,type:str]
19-
pgp: []
20-
unencrypted_suffix: _unencrypted
21-
version: 3.9.4
1+
# This file should contains the secrets and should be encrypted using SOPS

clusters/kind01-internal/mise.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
[tools]
2+
kind = "0.27.0"
3+
14
[env]
25
_.file = ".env.yaml"
36
# keep-sorted start
47
CLUSTER_FQDN = "kind01.internal"
58
CLUSTER_NAME = "kind01"
69
# keep-sorted end
710

8-
[tasks."create"]
11+
[tasks."create:kind01-internal"]
912
description = 'Create K8s cluster'
1013
run = '${CLUSTERS_RUN_SCRIPT_DIRECTORY}/run-kind.sh create'
1114

12-
[tasks."delete"]
15+
[tasks."delete:kind01-internal"]
1316
description = 'Delete K8s cluster'
1417
run = '${CLUSTERS_RUN_SCRIPT_DIRECTORY}/run-kind.sh delete'

clusters/kind02-internal/.env.yaml

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1 @@
1-
SECRET: ENC[AES256_GCM,data:QIOd+nLFzAQ=,iv:6o+BhThDyGAQ1jwjG04bS95c55xdUCdB6XhezYrKv90=,tag:tnwKw9zZjc7OLDi7YbiYnw==,type:str]
2-
sops:
3-
kms: []
4-
gcp_kms: []
5-
azure_kv: []
6-
hc_vault: []
7-
age:
8-
- recipient: age1jjuamrdk3vrk6g8qhrjnqtt4x2yvvxw7fz2nkvf78398dj7vav7s74z4zz
9-
enc: |
10-
-----BEGIN AGE ENCRYPTED FILE-----
11-
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBTNXVyZWd4bTFWekVKbjdn
12-
dzlPcWZnWUxsME9UWlN2NjgzMGhoQ1VIV0drCnlScGZTYitqcTYyR291eVArYWVs
13-
c1JyU2FTVWJBajdWdFVGRFN5TXFsMWMKLS0tIEt2WjdNN3FWaU5UQnNwRDNseTVy
14-
TXhtbU94bVFjOTJiUmQyQ2U5VWU0WlkK5Ur8KSyl5/4/AJrADYLRF/27r7hEZzY1
15-
0Qo5LZDZOLXoJ6RBEAYi7WNj/hYbgoBd6maB93fMUaEW6MfC5zK6DA==
16-
-----END AGE ENCRYPTED FILE-----
17-
lastmodified: "2025-03-14T09:14:06Z"
18-
mac: ENC[AES256_GCM,data:kDFNXFwtCNtUISMdYQG0yu38ORYdwsp5v02KrzFZS/zj3mPSNU71RGRPn44Vp9noztBRzz0C6kwql7BaWC2twDVAlR9FIrQDl+jf9PQiO9kBmrQo60TJq4gFe0jesWaBFyKDAG3ffi/oELJs8NZVkbBXwpshNoLs0C06nwc989g=,iv:fPVkLhox+xd5KXFmC6t+CJJ8c4Nsjh8u0DSCvnanYKM=,tag:2isNCgC8yxDaZOojyqgEjA==,type:str]
19-
pgp: []
20-
unencrypted_suffix: _unencrypted
21-
version: 3.9.4
1+
# This file should contains the secrets and should be encrypted using SOPS

clusters/kind02-internal/mise.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
[tools]
2+
kind = "0.27.0"
3+
14
[env]
25
_.file = ".env.yaml"
36
# keep-sorted start
47
CLUSTER_FQDN = "kind02.internal"
58
CLUSTER_NAME = "kind02"
69
# keep-sorted end
710

8-
[tasks."create"]
11+
[tasks."create:kind02-internal"]
912
description = 'Create K8s cluster'
1013
run = '${CLUSTERS_RUN_SCRIPT_DIRECTORY}/run-kind.sh create'
1114

12-
[tasks."delete"]
15+
[tasks."delete:kind02-internal"]
1316
description = 'Delete K8s cluster'
1417
run = '${CLUSTERS_RUN_SCRIPT_DIRECTORY}/run-kind.sh delete'

mise.toml

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
[tools]
22
# keep-sorted start
3-
eksctl = "0.205.0"
3+
k3d = "5.8.3"
44
kind = "0.27.0"
5-
kubectl = "1.32.3"
6-
opentofu = "1.9.0"
75
sops = "3.9.4"
86
# keep-sorted end
97

@@ -22,19 +20,44 @@ CLUSTERS_KUBECONFIG_DIRECTORY = "{{ config_root }}/clusters/.kubeconfig"
2220
# Directory which contains the scripts to create and delete the clusters
2321
CLUSTERS_RUN_SCRIPT_DIRECTORY = "{{ config_root }}/scripts"
2422

23+
#######################################
24+
# Kind
25+
#######################################
26+
2527
[tasks."create:kind:kind01-internal"]
2628
description = 'Create kind01.internal K8s cluster'
2729
# Run mise again due to missing support for SOPS-encrypted environment variables in tasks: https://github.com/jdx/mise/discussions/4593
28-
run = 'cd "${CLUSTERS_DIRECTORY}/${MISE_TASK_NAME##*:}" && mise run create'
30+
run = 'cd "${CLUSTERS_DIRECTORY}/${MISE_TASK_NAME##*:}" && mise run create:kind01-internal'
2931

3032
[tasks."delete:kind:kind01-internal"]
3133
description = 'Delete kind01.internal K8s cluster'
32-
run = 'cd "${CLUSTERS_DIRECTORY}/${MISE_TASK_NAME##*:}" && mise run delete'
34+
run = 'cd "${CLUSTERS_DIRECTORY}/${MISE_TASK_NAME##*:}" && mise run delete:kind01-internal'
3335

3436
[tasks."create:kind:kind02-internal"]
3537
description = 'Create kind02.internal K8s cluster'
36-
run = 'cd "${CLUSTERS_DIRECTORY}/${MISE_TASK_NAME##*:}" && mise run create'
38+
run = 'cd "${CLUSTERS_DIRECTORY}/${MISE_TASK_NAME##*:}" && mise run create:kind02-internal'
3739

3840
[tasks."delete:kind:kind02-internal"]
3941
description = 'Delete kind02.internal K8s cluster'
40-
run = 'cd "${CLUSTERS_DIRECTORY}/${MISE_TASK_NAME##*:}" && mise run delete'
42+
run = 'cd "${CLUSTERS_DIRECTORY}/${MISE_TASK_NAME##*:}" && mise run delete:kind02-internal'
43+
44+
#######################################
45+
# K3d
46+
#######################################
47+
48+
[tasks."create:k3d:k3d01-internal"]
49+
description = 'Create k3d01.internal K8s cluster'
50+
# Run mise again due to missing support for SOPS-encrypted environment variables in tasks: https://github.com/jdx/mise/discussions/4593
51+
run = 'cd "${CLUSTERS_DIRECTORY}/${MISE_TASK_NAME##*:}" && mise run create:k3d01-internal'
52+
53+
[tasks."delete:k3d:k3d01-internal"]
54+
description = 'Delete k3d01.internal K8s cluster'
55+
run = 'cd "${CLUSTERS_DIRECTORY}/${MISE_TASK_NAME##*:}" && mise run delete:k3d01-internal'
56+
57+
[tasks."create:k3d:k3d02-internal"]
58+
description = 'Create k3d02.internal K8s cluster'
59+
run = 'cd "${CLUSTERS_DIRECTORY}/${MISE_TASK_NAME##*:}" && mise run create:k3d02-internal'
60+
61+
[tasks."delete:k3d:k3d02-internal"]
62+
description = 'Delete k3d02.internal K8s cluster'
63+
run = 'cd "${CLUSTERS_DIRECTORY}/${MISE_TASK_NAME##*:}" && mise run delete:k3d02-internal'

scripts/run-k3d.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env bash
2+
3+
# The "create" needs to be idempotent !
4+
create() {
5+
if k3d cluster list --no-headers | grep -q "^${CLUSTER_FQDN} "; then
6+
echo "*** Cluster \"${CLUSTER_FQDN}\" already exists...."
7+
else
8+
mkdir -p "${CLUSTERS_KUBECONFIG_DIRECTORY}"
9+
k3d cluster create "${CLUSTER_FQDN}" --kubeconfig-update-default=false \
10+
--k3s-arg "--disable=traefik@all" \
11+
--k3s-arg "--disable=local-storage@all" \
12+
--k3s-arg "--disable=metrics-server@all"
13+
k3d kubeconfig write "${CLUSTER_FQDN}" --overwrite --output "${CLUSTERS_KUBECONFIG_DIRECTORY}/kubeconfig_${CLUSTER_FQDN}.yml"
14+
fi
15+
}
16+
17+
delete() {
18+
if k3d cluster list --no-headers | grep -q "^${CLUSTER_FQDN} "; then
19+
k3d cluster delete "${CLUSTER_FQDN}"
20+
if [[ -f "${CLUSTERS_KUBECONFIG_DIRECTORY}/kubeconfig_${CLUSTER_FQDN}.yml" ]]; then
21+
echo "*** Deleting \"${CLUSTERS_KUBECONFIG_DIRECTORY}/kubeconfig_${CLUSTER_FQDN}.yml\" ..."
22+
rm "${CLUSTERS_KUBECONFIG_DIRECTORY}/kubeconfig_${CLUSTER_FQDN}.yml"
23+
fi
24+
if [[ -d "${CLUSTERS_KUBECONFIG_DIRECTORY}" && -z "$(ls -A "${CLUSTERS_KUBECONFIG_DIRECTORY}")" ]]; then
25+
echo "*** Deleting empty \"${CLUSTERS_KUBECONFIG_DIRECTORY}\" ..."
26+
rmdir "${CLUSTERS_KUBECONFIG_DIRECTORY}" || true
27+
fi
28+
else
29+
echo "*** Cluster \"${CLUSTER_FQDN}\" does not exist..."
30+
fi
31+
}
32+
33+
usage() {
34+
echo "*** Usage: $0 {create|delete}"
35+
exit 1
36+
}
37+
38+
: "${CLUSTER_FQDN:?Error: CLUSTER_FQDN environment variable is not set!}"
39+
: "${CLUSTERS_KUBECONFIG_DIRECTORY:?Error: CLUSTERS_KUBECONFIG_DIRECTORY environment variable is not set!}"
40+
41+
if [[ $# -ne 1 ]]; then
42+
usage
43+
fi
44+
45+
case "$1" in
46+
create)
47+
echo "*** Creating K8s cluster..."
48+
create
49+
;;
50+
delete)
51+
echo "*** Deleting K8s cluster..."
52+
delete
53+
;;
54+
*)
55+
usage
56+
;;
57+
esac

scripts/run-kind.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ delete() {
2020
if kind get clusters | grep -q "^${CLUSTER_FQDN}$"; then
2121
kind delete cluster --name "${CLUSTER_FQDN}" --kubeconfig "${CLUSTERS_KUBECONFIG_DIRECTORY}/kubeconfig_${CLUSTER_FQDN}.yml"
2222
if [[ -f "${CLUSTERS_KUBECONFIG_DIRECTORY}/kubeconfig_${CLUSTER_FQDN}.yml" ]]; then
23-
echo "*** Deleting \"${CLUSTERS_KUBECONFIG_DIRECTORY}/kubeconfig_${CLUSTER_FQDN}.yml\" ..."
23+
echo "*** Deleting \"${CLUSTERS_KUBECONFIG_DIRECTORY}/kubeconfig_${CLUSTER_FQDN}.yml\""
2424
rm "${CLUSTERS_KUBECONFIG_DIRECTORY}/kubeconfig_${CLUSTER_FQDN}.yml"
2525
fi
2626
if [[ -d "${CLUSTERS_KUBECONFIG_DIRECTORY}" && -z "$(ls -A "${CLUSTERS_KUBECONFIG_DIRECTORY}")" ]]; then
27-
echo "*** Deleting empty \"${CLUSTERS_KUBECONFIG_DIRECTORY}\" ..."
28-
rmdir "${CLUSTERS_KUBECONFIG_DIRECTORY}"
27+
echo "*** Deleting empty \"${CLUSTERS_KUBECONFIG_DIRECTORY}\""
28+
rmdir "${CLUSTERS_KUBECONFIG_DIRECTORY}" || true
2929
fi
3030
else
31-
echo "*** Cluster \"${CLUSTER_FQDN}\" does not exist..."
31+
echo "*** Cluster \"${CLUSTER_FQDN}\" does not exist"
3232
fi
3333
}
3434

@@ -46,11 +46,11 @@ fi
4646

4747
case "$1" in
4848
create)
49-
echo "*** Creating K8s cluster..."
49+
echo "*** Creating K8s cluster"
5050
create
5151
;;
5252
delete)
53-
echo "*** Deleting K8s cluster..."
53+
echo "*** Deleting K8s cluster"
5454
delete
5555
;;
5656
*)

0 commit comments

Comments
 (0)