Files
k3s-cluster/argocd/gen-apps.sh

142 lines
4.3 KiB
Bash
Raw Permalink Normal View History

2026-06-23 01:03:06 +02:00
#!/usr/bin/env bash
# Generates ArgoCD Application manifests (one per app folder) + an AppProject.
#
# Layout produced:
# argocd/apps/project.yaml -> AppProject "k3s-cluster" (sync-wave -1)
# argocd/apps/<app>.yaml -> Application for that app folder
# argocd-bootstrap.yaml (repo root) -> app-of-apps: syncs everything in argocd/apps/
#
# Bootstrap (one-time, after ArgoCD + cert-manager are installed):
# kubectl apply -f argocd-bootstrap.yaml
#
# Re-run this script after adding/removing an app folder to regenerate the manifests.
set -euo pipefail
cd "$(dirname "$0")/.." # repo root
REPO="${REPO:-https://git.rogi.casa/roger/k3s-cluster.git}"
REV="${REV:-main}"
APPS_DIR="argocd/apps"
mkdir -p "$APPS_DIR"
# app-name | namespace | path | recurse | validate
APPS=(
"argocd|argocd|argocd|false|true"
"cert-manager|cert-manager|cert-manager|true|true"
"fava|fava|fava|true|true"
"gitea|gitea|gitea|true|true"
"glance|glance|glance|true|true"
"gym-tracker|gym-tracker|gym-tracker|true|true"
"homeassistant|home-assistant|homeassistant|true|true"
"jellyfin|jellyfin|jellyfin|true|true"
"litellm|litellm|litellm|true|true"
"minecraft-server|minecraft|minecraft-server|true|true"
"monitoring|monitoring|monitoring|true|true"
"myorg-assistant|myorg-assistant|myorg-assistant|true|true"
"n8n|n8n|n8n|true|true"
"nas|nas-proxy|nas|true|true"
"openwebui|openwebui|openwebui|true|true"
"phoenix|phoenix|phoenix|true|false"
"pihole|pihole|pihole|true|true"
2026-06-27 00:09:39 +02:00
"platform-engineer|platform-engineer|platform-engineer|true|true"
2026-06-23 01:03:06 +02:00
"qbittorrent|qbittorrent|qbittorrent|true|true"
"vaultwarden|vaultwarden|vaultwarden|true|true"
)
# ---------------------------------------------------------------------------
# AppProject
# ---------------------------------------------------------------------------
cat > "$APPS_DIR/project.yaml" <<EOF
apiVersion: argoproj.io/v1alpha1
kind: AppProject
metadata:
name: k3s-cluster
namespace: argocd
annotations:
argocd.argoproj.io/sync-wave: "-1"
spec:
description: Applications for the rogi.casa K3s cluster (managed in Git)
sourceRepos:
- ${REPO}
destinations:
- server: https://kubernetes.default.svc
namespace: "*"
clusterResourceWhitelist:
- group: "*"
kind: "*"
EOF
# ---------------------------------------------------------------------------
# One Application per app folder
# ---------------------------------------------------------------------------
gen_app() {
local name="$1" ns="$2" path="$3" recurse="$4" validate="$5"
local recurse_yaml validate_opts=""
[ "$recurse" = "true" ] && recurse_yaml=" recurse: true" || recurse_yaml=" recurse: false"
[ "$validate" = "false" ] && validate_opts=$'\n - Validate=false'
cat > "$APPS_DIR/${name}.yaml" <<EOF
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: ${name}
namespace: argocd
annotations:
argocd.argoproj.io/sync-wave: "0"
spec:
project: k3s-cluster
source:
repoURL: ${REPO}
targetRevision: ${REV}
path: ${path}
directory:
${recurse_yaml}
destination:
server: https://kubernetes.default.svc
namespace: ${ns}
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=false${validate_opts}
EOF
}
for line in "${APPS[@]}"; do
IFS='|' read -r name ns path recurse validate <<< "$line"
gen_app "$name" "$ns" "$path" "$recurse" "$validate"
done
# ---------------------------------------------------------------------------
# Root "app-of-apps" bootstrap Application (uses the built-in default project)
# ---------------------------------------------------------------------------
cat > argocd-bootstrap.yaml <<EOF
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: k3s-cluster-root
namespace: argocd
annotations:
argocd.argoproj.io/sync-wave: "-1"
spec:
project: default
source:
repoURL: ${REPO}
targetRevision: ${REV}
path: argocd/apps
directory:
recurse: true
destination:
server: https://kubernetes.default.svc
namespace: argocd
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=false
EOF
echo "Generated $(find "$APPS_DIR" -name '*.yaml' | wc -l) files in $APPS_DIR/ and argocd-bootstrap.yaml"