-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskfile.yaml
More file actions
442 lines (373 loc) · 14.9 KB
/
Taskfile.yaml
File metadata and controls
442 lines (373 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
---
version: 3
includes:
git:
taskfile: https://raw.githubusercontent.com/stuttgart-things/platform-engineering-showcase/refs/heads/main/taskfiles/git.yaml
vars:
PROJECT:
sh: echo ${PROJECT}
BRANCH:
sh: if [ $(git rev-parse --abbrev-ref HEAD) != "main" ]; then echo $(git rev-parse --abbrev-ref HEAD); else echo main ; fi
CROSSPLANE_PACKAGE_REGISTRY: ghcr.io
DAGGER_CROSSPLANE_MODULE: github.com/stuttgart-things/dagger/crossplane
DAGGER_CROSSPLANE_MODULE_VERSION: v0.0.2
TEST_RELEASE_TEMPLATES: tests/releases.yaml
TEST_DIR: /tmp
TEST_DEFAULTS: tests/defaults.yaml
CLUSTER_NAME: helm-test
PATH_KUBE_CONFIG: /home/sthings/.kube/kind-{{ .CLUSTER_NAME }}
PATH_CLUSTER_TEMPLATE: tests/cluster.yaml
DEV_CLUSTER_DIR: /tmp/helmfiles/cluster
KIND_CONFIG_PATH: tests/cluster.yaml
CLUSTER_INFRA_HELMFILE: /tmp/kind-infra.yaml
TEST_FILES: tests/releases.yaml
POST_CONFIG_FILES: tests/post-release.yaml
DATE:
sh: date +"%Y.%m%d.%H%M"
tasks:
helmfile-action:
desc: Get app name
cmds:
- task: "{{ .OPERATION }}"
vars:
HELMFILE_PATH: $(dirname "{{ .APP }}")
HELMFILE: $(basename "{{ .APP }}")
vars:
OPERATION:
sh: gum choose "render" "deploy"
FOLDER:
sh: gum choose "database" "monitoring" "cicd" "infra"
APP:
sh: |
find "{{ .FOLDER }}" -maxdepth 1 -type f -name "*.yaml.gotmpl" | gum choose
create-kind-cluster:
desc: Create, Start & Configure kind cluster
env:
KUBECONFIG: "{{ .PATH_KUBE_CONFIG }}"
cmds:
- task: destroy-kind-cluster
- |
kind create cluster \
--config {{ .KIND_CONFIG_PATH }} \
--name {{ .CLUSTER_NAME }} \
--kubeconfig {{ .PATH_KUBE_CONFIG }}
kubectl get nodes --kubeconfig {{ .PATH_KUBE_CONFIG }}
# - task: execute-base-cluster-setup
# - task: output-ingress-nipio
# create-vcluster-cluster:
generate-helmfile:
desc: Generate Helmfile configuration + values
cmds:
- |
k2n gen \
--examples-dirs "{{ .EXAMPLE_DIRS }}" \
--ruleset-env-dir {{ .RULESET_ENV_DIR }} \
--usecase {{ .USECASE }} \
--example-file-ext ".yaml,.tf,.gotmpl,.md" \
--verbose=true \
--prompt-to-ai={{ .PROMPT }} \
--instruction "Give me a helmfile + values for {{ .CHART }}. I deployed like this w/ helm cli: {{ .HELM_CLI_INSTALL_CMD }} .See examples for schema. Do not invent values - just use the information from the helm install command" \
--destination "" # stdout
vars:
USECASE: helmfile
EXAMPLE_DIRS: .k2n/helmfile-examples
RULESET_ENV_DIR: .k2n/env-ruleset
CHART:
sh: gum input --placeholder "Enter helm chart name"
HELM_CLI_INSTALL_CMD:
sh: gum input --placeholder "Enter helm cli install command"
PROMPT:
sh: gum choose "true" "false" --header="Prompt to ai?"
generate-helmfile-include:
desc: Generate Helmfile include config for dev cluster
cmds:
- |
k2n gen \
--examples-dirs "{{ .EXAMPLE_DIRS }},./{{ .KIND }}" \
--ruleset-env-dir {{ .RULESET_ENV_DIR }} \
--usecase {{ .USECASE }} \
--example-file-ext ".yaml,.tf,.gotmpl,.md" \
--verbose=true \
--prompt-to-ai={{ .PROMPT }} \
--instruction "give me a {{ .USECASE }}-config for {{ .APP }} w/ {{ .INCLUDE }} include for cluster {{ .CLUSTER_NAME }}. {{ .USER_WORKING_DIR }}/{{ .KIND }}/{{ .APP }}.yaml.gotmpl is local path no description. see examples for schema" \
--destination "" # stdout
vars:
USECASE: helmfile
EXAMPLE_DIRS: .k2n/include-examples
RULESET_ENV_DIR: .k2n/env-ruleset
RULESET_USECASE_DIR: .k2n/rulesets/terraformvm
PROMPT:
sh: gum choose "true" "false" --header="Prompt to ai?"
APP:
sh: gum input --placeholder "Enter app name"
KIND:
sh: gum choose "infra" "database" "monitoring" "apps" "cicd"
INCLUDE:
sh: gum choose "local" "git" --header="Local or git include?"
CLUSTER_NAME:
sh: gum choose "dev"
branch:
desc: Create branch from main
cmds:
- git checkout main
- git branch
- git pull
- |
echo "Enter to be created (remote) branch:"
read BRANCH_NAME;
git checkout -b ${BRANCH_NAME}
git push origin ${BRANCH_NAME}
- git branch
- git branch --set-upstream-to=origin/main ${BRANCH_NAME}
release:
deps:
- check
desc: push new version
cmds:
- task: pr
- npx semantic-release --dry-run
- npx semantic-release --debug --no-ci
- echo released version $(git describe --tags --abbrev=0)
install-release:
desc: Test a release
cmds:
- rm -rf {{ .TEST_DIR }}/* && mkdir {{ .TEST_DIR }} || true
- |
release=$(yq '.template | keys | .[]' {{ .TEST_RELEASE_TEMPLATES }} | gum choose)
folder=$(find {{ .SEARCH_FOLDERS }} -type f -name "${release}.yaml" -exec dirname {} \; | sort -u)
domain=$(kubectl get nodes -o json | jq -r '.items[] | select(.metadata.labels."ingress-ready" == "true") | .status.addresses[] | select(.type == "InternalIP") | .address').nip.io
clusterIssuer=$(kubectl get clusterissuer -o jsonpath='{.items[0].metadata.name}')
storageClass=standard
machineshop render \
--source local \
--template {{ .TEST_RELEASE_TEMPLATES }} \
--output file \
--kind multikey \
--key ${release} \
--destination {{ .TEST_DIR }}/${release}.yaml \
--values "source={{ .USER_WORKING_DIR }}/${folder}/${release}.yaml, domain=${domain}, clusterIssuer=${clusterIssuer}, storageClass=${storageClass}"
helmfile template -f {{ .TEST_DIR }}/${release}.yaml
SELECTED_KUBECONFIG=$(gum choose {{ .ALL_KUBECONFIGS }})
echo SWITCHING TO ${SELECTED_KUBECONFIG//\"/}
printf "\n\nexport KUBECONFIG={{ .KUBECONFIG_FOLDER }}/${SELECTED_KUBECONFIG//\"/}\n\n"
export KUBECONFIG={{ .KUBECONFIG_FOLDER }}/${SELECTED_KUBECONFIG//\"/}
kubectl get nodes
code {{ .TEST_DIR }}/${release}.yaml
gum confirm "Do you want to apply ${release}?" || exit 0
echo "Apply: helmfile apply -f {{ .TEST_DIR }}/${release}.yaml"
helmfile apply -f {{ .TEST_DIR }}/${release}.yaml
for i in {1..20}; do
gum spin --spinner dot --title "Waiting for all pods to be running..." -- sleep 5
kubectl get po -A | grep -v "kube-system" | grep -v "Completed"
kubectl get ingress -A
choice=$(gum choose "SKIP WAITING" "CONTINUE WAITING")
if [[ "$choice" == "SKIP WAITING" ]]; then
echo "Skipping loop."
break
fi
done
# POST CONFIG
#if $(yq e ".template.${release}" {{ .POST_CONFIG_FILES }}) != "null"; then
# echo Key ${release} found
#fi
gum confirm "Do you want to destroy ${release}?" || exit 0
helmfile destroy -f {{ .TEST_DIR }}/${release}.yaml
vars:
KUBECONFIG_FOLDER: ~/.kube
SEARCH_FOLDERS: "infra apps database cicd"
ALL_KUBECONFIGS:
sh: ls {{ .KUBECONFIG_FOLDER }} | grep -v "^cache$" | xargs -n1 printf '"%s" '
uninstall-release:
desc: Uninstall a release
cmds:
- |
SELECTED_KUBECONFIG=$(gum choose {{ .ALL_KUBECONFIGS }})
echo SWITCHING TO ${SELECTED_KUBECONFIG//\"/}
printf "\n\nexport KUBECONFIG={{ .KUBECONFIG_FOLDER }}/${SELECTED_KUBECONFIG//\"/}\n\n"
export KUBECONFIG={{ .KUBECONFIG_FOLDER }}/${SELECTED_KUBECONFIG//\"/}
kubectl get nodes
ALL_HELM_RELEASES=$(helm ls -A | awk 'NR>1 {print $1 ":" $2}')
echo ${ALL_HELM_RELEASES}
SELECTED_RELEASE=$(echo "$ALL_HELM_RELEASES" | gum choose)
# Extract release name and namespace
RELEASE_NAME=$(echo "$SELECTED_RELEASE" | cut -d':' -f1)
NAMESPACE=$(echo "$SELECTED_RELEASE" | cut -d':' -f2)
# Uninstall the Helm release
helm uninstall "$RELEASE_NAME" -n "$NAMESPACE"
vars:
KUBECONFIG_FOLDER: ~/.kube
ALL_KUBECONFIGS:
sh: ls {{ .KUBECONFIG_FOLDER }} | grep -v "^cache$" | xargs -n1 printf '"%s" '
render:
desc: Render helmfile
vars:
DAGGER_MODULE: github.com/stuttgart-things/dagger/helm
DAGGER_MODULE_VERSION: v0.25.0
cmds:
- |
echo HELMFILE_PATH: {{ .HELMFILE_PATH }}
echo HELMFILE: {{ .HELMFILE }}
- |
dagger call -m {{ .DAGGER_MODULE }}@{{ .DAGGER_MODULE_VERSION }} \
render-helmfile \
--src {{ .HELMFILE_PATH }} \
--helmfileName {{ .HELMFILE }} \
--registry-secret file://~/.docker/config.json \
-vv --progress plain
execute-base-cluster-setup:
desc: Execute base setup on kind cluster
vars:
RANDOM:
sh: |
echo $RANDOM % 5555 + 1 | bc
env:
HELMFILE_CACHE_HOME: /tmp/helmfile/cache/{{ .DATE }}/{{ .RANDOM }}
KUBECONFIG: "{{ .PATH_KUBE_CONFIG }}"
cmds:
- |
gum confirm "Do you want to execute base cluster setup on {{ .CLUSTER_NAME }}?" || exit 0
machineshop render \
--source local \
--template {{ .PATH_CLUSTER_TEMPLATE }} \
--output file \
--kind multikey \
--key kind-infra \
--destination {{ .CLUSTER_INFRA_HELMFILE }} \
--values "source={{ .USER_WORKING_DIR }},clusterName={{ .CLUSTER_NAME }}"
START_TIME=$(date +%s)
TIMEOUT=180 # seconds
helmfile init
while true; do
echo "RUNNING HELMFILE APPLY..."
helmfile apply -f {{ .CLUSTER_INFRA_HELMFILE }} --kubeconfig {{ .PATH_KUBE_CONFIG }} && \
echo "RUNNING HELMFILE SYNC..." && \
helmfile sync -f {{ .CLUSTER_INFRA_HELMFILE }} --kubeconfig {{ .PATH_KUBE_CONFIG }} && \
{ echo "Commands succeeded. Exiting loop."; break; }
CURRENT_TIME=$(date +%s)
ELAPSED=$(( CURRENT_TIME - START_TIME ))
if [ $ELAPSED -ge ${TIMEOUT} ]; then
echo "COMMANDS ARE STILL FAILING AFTER ONE MINUTE. EXITING WITH ERROR."
exit 1
fi
echo "RETRYING IN 5 SECONDS..."
sleep 5
done
kubectl get po -A --kubeconfig {{ .PATH_KUBE_CONFIG }}
destroy-kind-cluster:
desc: Destroy kind cluster
env:
KUBECONFIG: "{{ .PATH_KUBE_CONFIG }}"
cmds:
- |
if kind get clusters | grep -wq "{{ .CLUSTER_NAME }}"; then
kubectl get nodes -o wide --kubeconfig {{ .PATH_KUBE_CONFIG }}
# DELETE EXISTING CLUSTER
gum confirm "Do you want to destroy existing cluster {{ .CLUSTER_NAME }}?" || exit 0
echo "Cluster '{{ .CLUSTER_NAME }}' exists (already). Deleting it..."
kind delete clusters "{{ .CLUSTER_NAME }}"
fi
output-ingress-nipio:
desc: Output ingress domain
env:
KUBECONFIG: "{{ .PATH_KUBE_CONFIG }}"
cmds:
- |
echo *.$(kubectl get nodes -o json | jq -r '.items[] | select(.metadata.labels."ingress-ready" == "true") | .status.addresses[] | select(.type == "InternalIP") | .address').nip.io
tests-create-includes:
desc: Create test files
cmds:
- rm -rf {{ .TEST_DIR }}/* && mkdir {{ .TEST_DIR }} || true
- |
mkdir -p {{ .RENDER_DIR }}
for release in $(yq '.template | keys | .[]' {{ .TEST_FILES }}); do
echo "Creating: ${release} include"
folder=$(find {{ .SEARCH_FOLDERS }} -type f -name "${release}.yaml" -exec dirname {} \; | sort -u)
clusterIssuer=$(kubectl get clusterissuer -o jsonpath='{.items[0].metadata.name}')
storageClass=standard
machineshop render \
--source local \
--template {{ .TEST_RELEASE_TEMPLATES }} \
--output file \
--kind multikey \
--key ${release} \
--destination {{ .RENDER_DIR }}/${release}.yaml \
--defaults {{ .TEST_DEFAULTS }} \
--values "source={{ .USER_WORKING_DIR }}/${folder}/${release}.yaml"
helmfile template -f {{ .RENDER_DIR }}/${release}.yaml
done
vars:
RENDER_DIR: "/tmp/{{ .DATE }}"
tests-render-includes:
desc: Test render includes
cmds:
- task: tests-create-includes
- |
for release in $(yq '.template | keys | .[]' {{ .TEST_FILES }}); do
echo "Rendering: ${release}"
export HELMFILE_CACHE_HOME={{ .TEST_DIR }}/cache
helmfile deps -f {{ .TEST_DIR }}/${release}.yaml
helmfile template -f {{ .TEST_DIR }}/${release}.yaml
done
run-pre-commit-hook:
deps:
- check
desc: "Run the pre-commit hook script to replace .example.com with .example.com"
cmds:
- |
# Find all YAML files in the repository recursively, excluding Taskfile.yaml
files=$(find . -type f \( -name "*.yaml" -o -name "*.yml" -o -name "*.yml" -o -name "*.md" \) ! -name "Taskfile.yaml")
# Loop through each file
for file in $files; do
# Skip binary files
if file "$file" | grep -q "text"; then
# Replace the string and update the file
sed -i 's/\.sva\.de/\.example\.com/g' "$file"
fi
done
# Add all modified YAML files back to staging
git add $(git ls-files --modified | grep -E '\.ya?ml$')
exit 0
silent: false
do:
desc: Select a task to run
cmds:
- |
# Extract task names (keep internal colons, remove only trailing colon)
task_name=$(task -l | awk '/^\*/ {print $2}' | sed 's/:$//' | gum choose)
# Run the selected task
[ -n "$task_name" ] && task "$task_name"
render-helmfile-local:
desc: Render helmfile locally using dagger helm module
cmds:
- |
set -e
# Find helmfiles in specific directories
HELMFILES=$(find apps cicd database infra monitoring -maxdepth 1 -type f \( -name "*.yaml.gotmpl" -o -name "*.yaml" \) 2>/dev/null | sort)
if [ -z "$HELMFILES" ]; then
echo "❌ No helmfiles found in apps, cicd, database, infra, or monitoring directories"
exit 1
fi
# Let user choose helmfile
HELMFILE=$(echo "$HELMFILES" | gum choose --header "Select helmfile to render:")
if [ -z "$HELMFILE" ]; then
echo "❌ No helmfile selected"
exit 1
fi
# Extract directory and filename
HELMFILE_DIR=$(dirname "$HELMFILE")
HELMFILE_NAME=$(basename "$HELMFILE")
# Normalize path (remove ./ prefix)
SRC="${HELMFILE_DIR#./}"
[ "$SRC" = "." ] && SRC="."
echo ""
echo "📋 Rendering helmfile:"
echo " Source : $SRC"
echo " File : $HELMFILE_NAME"
echo ""
# Render using dagger
dagger call -m github.com/stuttgart-things/dagger/helm@v0.57.0 \
helmfile-operation \
--operation template \
--src "$SRC" \
--helmfile-ref "$HELMFILE_NAME" \
--progress plain -vv