# GitOps Infrastructure Rebuild Plan **Goal:** Bootstrap cluster from local source + GitOps-managed future state with zero resource duplication. --- ## ๐Ÿ“Š Current vs Proposed Architecture ### Current Issues โŒ 1. **Circular dependencies** - CNPG operator, ddb-cluster, Forgejo scattered between bootstrap/, data/, argocd/bootstrap/ 2. **Resource duplication** - ddb-cluster.yaml in k8s/data/ deployed by wave 4 app, but should be bootstrap-only 3. **Manual bootstrap steps** - Scattered across BOOTSTRAP.md, requires copying secrets between namespaces 4. **SOPS secrets** - db-role-secrets.enc.yaml excluded from kustomization (out-of-band) 5. **No single source of truth** - Same resources defined in multiple places ### Proposed Architecture โœ… ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Phase 0: Local Bootstrap (One-time, k8s/bootstrap-local/) โ”‚ โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚ โ”‚ โ€ข ArgoCD + SOPS plugin โ”‚ โ”‚ โ€ข CloudNativePG operator โ”‚ โ”‚ โ€ข ddb-cluster (PostgreSQL 3 replicas) โ”‚ โ”‚ โ€ข Forgejo + dependencies (DB, Redis) โ”‚ โ”‚ โ”‚ โ”‚ Script: ./bootstrap.sh (automated) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ†“ git push to Forgejo โ†“ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Phase 1: GitOps (100% automated, ArgoCD app-of-apps) โ”‚ โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚ โ”‚ Wave 0: Substrate (cert-manager, nginx, reloader) โ”‚ โ”‚ Wave 1: Networking (Cilium policies, CoreDNS) โ”‚ โ”‚ Wave 2: Storage/Observability (MinIO, Longhorn, Prometheus) โ”‚ โ”‚ Wave 3: Logging (Loki, Grafana, Promtail) โ”‚ โ”‚ Wave 4: Secrets (ALL *.enc.yaml via SOPS) โ”‚ โ”‚ Wave 5: IAM (Vault, Authentik, Forgejo runner) โ”‚ โ”‚ Wave 6: Data Schemas (DB init, per-app databases) โ”‚ โ”‚ Wave 7: Messaging (Kafka, Redis, SQS) โ”‚ โ”‚ Wave 8: Applications (Temporal, Portainer, etc.) โ”‚ โ”‚ โ”‚ โ”‚ All changes: git commit โ†’ push โ†’ ArgoCD auto-sync โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` --- ## ๐Ÿ—‚๏ธ Directory Structure (Single Source of Truth) ``` homelab/ โ”œโ”€โ”€ bootstrap.sh # Automated bootstrap script โ”œโ”€โ”€ GITOPS-REBUILD-PLAN.md # This file โ”œโ”€โ”€ DDB-REVIEW.md # DDB configuration review โ”‚ โ”œโ”€โ”€ k8s/ โ”‚ โ”œโ”€โ”€ bootstrap-local/ # ๐Ÿ”ด Phase 0: Apply once, never touched by ArgoCD โ”‚ โ”‚ โ”œโ”€โ”€ kustomization.yaml # Orchestrates 00-04 โ”‚ โ”‚ โ”œโ”€โ”€ 00-namespaces.yaml # All namespaces with PodSecurity labels โ”‚ โ”‚ โ”œโ”€โ”€ 01-argocd.yaml # ArgoCD ConfigMaps โ”‚ โ”‚ โ”œโ”€โ”€ 02-cnpg-operator.yaml # CNPG operator Application โ”‚ โ”‚ โ”œโ”€โ”€ 03-ddb-bootstrap.yaml # PostgreSQL cluster + Forgejo DB + Redis โ”‚ โ”‚ โ””โ”€โ”€ 04-forgejo.yaml # Forgejo Application (inline values) โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ argocd/ โ”‚ โ”‚ โ”œโ”€โ”€ root/ โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ kustomization.yaml โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ homelab-root.yaml # ๐ŸŸข App-of-apps (Phase 1 entry point) โ”‚ โ”‚ โ”œโ”€โ”€ projects/ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ homelab-project.yaml โ”‚ โ”‚ โ””โ”€โ”€ apps/ # Wave-based applications โ”‚ โ”‚ โ”œโ”€โ”€ 00-substrate.yaml # cert-manager, nginx, reloader โ”‚ โ”‚ โ”œโ”€โ”€ 01-networking.yaml # RENAMED from 05-networking โ”‚ โ”‚ โ”œโ”€โ”€ 02-storage-obs.yaml # RENAMED from 10-storage-observability โ”‚ โ”‚ โ”œโ”€โ”€ 03-logging.yaml # RENAMED from 20-logging โ”‚ โ”‚ โ”œโ”€โ”€ 04-secrets.yaml # RENAMED from 00-secrets (after logging for Grafana) โ”‚ โ”‚ โ”œโ”€โ”€ 05-iam.yaml # RENAMED from 30-security โ”‚ โ”‚ โ”œโ”€โ”€ 06-data.yaml # RENAMED from 40-data (schemas ONLY, not cluster) โ”‚ โ”‚ โ”œโ”€โ”€ 07-messaging.yaml # RENAMED from 50-messaging โ”‚ โ”‚ โ””โ”€โ”€ 08-applications.yaml # RENAMED from 60-applications โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ data/ โ”‚ โ”‚ โ”œโ”€โ”€ cluster/ # ๐Ÿ”ด Bootstrap-only (duplicated in bootstrap-local/) โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ ddb-cluster.yaml # NOT deployed by ArgoCD โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ forgejo-database.yaml # NOT deployed by ArgoCD โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ kustomization.yaml # Reference only โ”‚ โ”‚ โ””โ”€โ”€ schemas/ # ๐ŸŸข GitOps-managed (wave 6) โ”‚ โ”‚ โ”œโ”€โ”€ authentik-database.yaml โ”‚ โ”‚ โ”œโ”€โ”€ temporal-database.yaml โ”‚ โ”‚ โ”œโ”€โ”€ temporal-visibility-database.yaml โ”‚ โ”‚ โ”œโ”€โ”€ schemas.yaml โ”‚ โ”‚ โ”œโ”€โ”€ db-init-job.yaml โ”‚ โ”‚ โ””โ”€โ”€ kustomization.yaml โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ security/ โ”‚ โ”‚ โ”œโ”€โ”€ sops-secrets/ โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ (all *.enc.yaml handled by wave 4 SOPS app) โ”‚ โ”‚ โ””โ”€โ”€ ... โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ [other directories unchanged] โ”‚ โ””โ”€โ”€ terraform/ # Talos configs ONLY (no k8s resources) โ”œโ”€โ”€ main.tf โ”œโ”€โ”€ variables.tf โ””โ”€โ”€ provider.tf ``` --- ## ๐Ÿš€ Execution Plan ### **Step 1: Prepare Local Environment** ```bash # 1. Ensure prerequisites export KUBECONFIG=~/.kube/homelab-config kubectl cluster-info # Verify cluster reachable # 2. Verify SOPS age key exists ls ~/.sops/homelab-age.key # 3. Install ArgoCD CLI (if not present) brew install argocd # macOS # or: curl -sSL -o argocd https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64 ``` ### **Step 2: Backup Current State (Safety)** ```bash # Export all current resources for rollback kubectl get applications -n argocd -o yaml > backup-argocd-apps.yaml kubectl get cluster ddb-cluster -n ddb -o yaml > backup-ddb-cluster.yaml kubectl get all -n cicd -o yaml > backup-forgejo.yaml ``` ### **Step 3: Clean Shutdown (Optional - for fresh rebuild)** โš ๏ธ **ONLY if doing a complete rebuild**. Skip if incrementally migrating. ```bash # Delete all ArgoCD Applications (keeps resources, just removes ArgoCD management) kubectl delete applications --all -n argocd # Optionally delete the entire cluster and re-provision (nuclear option) # talosctl reset --nodes --graceful --reboot ``` ### **Step 4: Run Bootstrap Script** ```bash # Execute automated bootstrap (creates ArgoCD, CNPG, DDB, Forgejo) ./bootstrap.sh # Expected output: # โœ… ArgoCD installed # โœ… SOPS age secret created # โœ… CNPG operator ready # โœ… DDB cluster healthy # โœ… Forgejo ready at https://forgejo.riotpiao.com ``` ### **Step 5: Push to Forgejo** ```bash # Add Forgejo remote (if not already added) git remote add forgejo https://forgejo.riotpiao.com/riotpiao.com/homelab.git # Push entire repo git push forgejo main # Verify via Forgejo UI: https://forgejo.riotpiao.com ``` ### **Step 6: Deploy App-of-Apps Root** ```bash # Apply homelab-project + homelab-root kubectl apply -f k8s/argocd/projects/homelab-project.yaml kubectl apply -k k8s/argocd/root # Sync everything (waves 0-8) argocd app sync homelab-root --prune # Watch the sync (Ctrl+C to exit) watch -n 2 'kubectl get applications -n argocd' ``` ### **Step 7: Verify All Applications Healthy** ```bash # Check ArgoCD Applications argocd app list # Expected output (all Synced + Healthy): # NAME CLUSTER NAMESPACE PROJECT STATUS HEALTH # homelab-root https://kubernetes.default.svc argocd homelab Synced Healthy # cert-manager https://kubernetes.default.svc cert-manager homelab Synced Healthy # ingress-nginx https://kubernetes.default.svc ingress-nginx homelab Synced Healthy # data-schemas https://kubernetes.default.svc ddb homelab Synced Healthy # temporal https://kubernetes.default.svc temporal homelab Synced Healthy # ... (all other apps) # Check all pods kubectl get pods --all-namespaces | grep -v Running # (should be empty or only Completed jobs) ``` --- ## ๐Ÿ” DDB Cluster - ArgoCD Application **Status:** DDB cluster is **bootstrap-only** (NOT managed by ArgoCD). **Reason:** Circular dependency - Forgejo (which hosts the git repo) depends on ddb-cluster โ†’ ddb-cluster cannot be in the git repo Forgejo hosts. โš ๏ธ **CRITICAL: Storage HA Status Unknown** - CLAUDE.md claims single-node storage (cp-1 only) = NO HA - Longhorn manifests show 3-node config (cp-1, cp-2, cp-3) = HA - **VERIFY BEFORE PROCEEDING:** See `STORAGE-ARCHITECTURE-CLARIFICATION.md` - If single-node: Deploy `k8s/infrastructure/longhorn/` first **Current approach (recommended):** - ddb-cluster deployed via `k8s/bootstrap-local/03-ddb-bootstrap.yaml` - Applied once during bootstrap, never touched afterward - Schema changes (Database CRs) are GitOps-managed via wave 6 `data-schemas` app **Alternative (if Forgejo is decoupled):** If you later move Forgejo to an external git host (GitHub, GitLab), you could create: ```yaml # k8s/argocd/apps/03-database.yaml (NOT currently used) apiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: ddb-cluster namespace: argocd annotations: argocd.argoproj.io/sync-wave: "3" spec: project: homelab source: repoURL: https://forgejo.riotpiao.com/riotpiao.com/homelab.git targetRevision: main path: k8s/data/cluster destination: server: https://kubernetes.default.svc namespace: ddb syncPolicy: automated: prune: false # Safety: never auto-delete database selfHeal: true ``` **DDB Configuration Review:** See `DDB-REVIEW.md` for: - Current vs recommended configuration - Resource limits, connection pooling, backups - Monitoring and performance tuning --- ## ๐Ÿ“‹ Resource Mapping (No Duplication) | Resource | Bootstrap (k8s/bootstrap-local/) | GitOps (ArgoCD apps/) | Terraform | |----------|----------------------------------|-----------------------|-----------| | ArgoCD | โœ… 01-argocd.yaml | โŒ Not managed | โŒ | | CNPG operator | โœ… 02-cnpg-operator.yaml (as Application) | โŒ | โŒ | | ddb-cluster | โœ… 03-ddb-bootstrap.yaml | โŒ | โŒ | | forgejo-database | โœ… 03-ddb-bootstrap.yaml | โŒ | โŒ | | Forgejo | โœ… 04-forgejo.yaml | โŒ (manual sync only) | โŒ | | Forgejo Redis | โœ… 03-ddb-bootstrap.yaml | โŒ | โŒ | | cert-manager | โŒ | โœ… Wave 0 (00-substrate) | โŒ | | ingress-nginx | โŒ | โœ… Wave 0 (00-substrate) | โŒ | | Prometheus | โŒ | โœ… Wave 2 (02-storage-obs) | โŒ | | Loki | โŒ | โœ… Wave 3 (03-logging) | โŒ | | SOPS secrets | โŒ | โœ… Wave 4 (04-secrets) | โŒ | | Authentik | โŒ | โœ… Wave 5 (05-iam) | โŒ | | authentik-database | โŒ | โœ… Wave 6 (06-data schemas) | โŒ | | temporal-database | โŒ | โœ… Wave 6 (06-data schemas) | โŒ | | Temporal | โŒ | โœ… Wave 8 (08-applications) | โŒ | | Talos configs | โŒ | โŒ | โœ… terraform/ | **Rule:** Each resource has exactly ONE source of truth. No overlap. --- ## ๐Ÿงช Testing the Rebuild ### **Dry-Run (Safe)** ```bash # 1. Test bootstrap script without actually applying ./bootstrap.sh --dry-run # (modify script to support this flag) # 2. Test kustomization builds kubectl kustomize k8s/bootstrap-local/ kubectl kustomize k8s/data/schemas/ # 3. Test ArgoCD app rendering argocd app diff homelab-root --local k8s/argocd/apps/ ``` ### **Incremental Migration (Safer than Full Rebuild)** If cluster is currently running, migrate incrementally: ```bash # 1. Add bootstrap-local/ resources alongside existing (won't conflict) kubectl apply -k k8s/bootstrap-local/ --dry-run=client # 2. Update ArgoCD apps one wave at a time kubectl apply -f k8s/argocd/apps/00-substrate.yaml argocd app sync cert-manager ingress-nginx reloader # (verify healthy, then proceed to next wave) # 3. Delete old bootstrap Applications kubectl delete application cnpg-operator -n argocd # Now in bootstrap-local kubectl delete application forgejo -n argocd # Now manual-sync-only ``` --- ## ๐ŸŽฏ Success Criteria โœ… **Bootstrap completes in <10 minutes** (`./bootstrap.sh`) โœ… **All namespaces pre-created with correct PodSecurity labels** โœ… **ddb-cluster healthy (3/3 replicas)** โœ… **Forgejo accessible at https://forgejo.riotpiao.com** โœ… **ArgoCD app-of-apps sync completes (all waves)** โœ… **All Applications show Synced + Healthy** โœ… **No duplicate resources** (each resource managed by exactly one mechanism) โœ… **SOPS secrets decrypted successfully** (all *.enc.yaml) โœ… **Temporal connects to PostgreSQL** (via temporal-database) โœ… **Authentik connects to PostgreSQL** (via authentik-database) โœ… **Future changes via git push only** (no manual kubectl apply) --- ## ๐Ÿ”„ Day-2 Operations (Post-Bootstrap) ### **Making Changes** ```bash # 1. Edit manifests locally vim k8s/applications/temporal/temporal-values.yaml # 2. Commit + push git add -A git commit -m "fix(temporal): increase replicas to 3" git push forgejo main # 3. ArgoCD auto-syncs within 3 minutes (or manual) argocd app sync temporal ``` ### **Adding a New Service** ```bash # 1. Create manifests mkdir k8s/applications/myapp cat > k8s/applications/myapp/kustomization.yaml << EOF apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization namespace: myapp resources: - deployment.yaml - service.yaml EOF # 2. Create ArgoCD Application cat > k8s/argocd/apps/08-applications.yaml << 'EOF' # (append to existing file) --- apiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: myapp namespace: argocd annotations: argocd.argoproj.io/sync-wave: "8" spec: project: homelab source: repoURL: https://forgejo.riotpiao.com/riotpiao.com/homelab.git targetRevision: main path: k8s/applications/myapp destination: server: https://kubernetes.default.svc namespace: myapp syncPolicy: automated: prune: true selfHeal: true syncOptions: - CreateNamespace=true EOF # 3. Push git add -A && git commit -m "feat(myapp): add new application" && git push # ArgoCD picks it up automatically ``` ### **Updating DDB Cluster (Bootstrap Resources)** Since ddb-cluster is bootstrap-only, changes require manual apply: ```bash # 1. Edit k8s/bootstrap-local/03-ddb-bootstrap.yaml vim k8s/bootstrap-local/03-ddb-bootstrap.yaml # 2. Apply changes (will patch existing cluster) kubectl apply -f k8s/bootstrap-local/03-ddb-bootstrap.yaml # 3. Commit to git (for record-keeping) git add k8s/bootstrap-local/03-ddb-bootstrap.yaml git commit -m "chore(ddb): increase shared_buffers to 512MB" git push ``` --- ## ๐Ÿ“š Related Documents - **CLAUDE.md** - Cluster topology, hard rules, GitOps gotchas - **DDB-REVIEW.md** - PostgreSQL configuration review and recommendations - **USAGE.md** - (STALE - helmfile era, ignore deployment procedures) - **TROUBLESHOOTING.md** - Generic Kubernetes debugging methodology - **k8s/argocd/bootstrap/BOOTSTRAP.md** - (OLD - replaced by this plan) --- ## โ“ FAQ **Q: Why is ddb-cluster not managed by ArgoCD?** A: Circular dependency - Forgejo needs DDB, ArgoCD syncs from Forgejo. Breaking the cycle requires one manual bootstrap step. **Q: Can I move Forgejo to ArgoCD management?** A: Only if you move the git repo to an external host (GitHub, GitLab). The `04-forgejo.yaml` Application already exists, just change `syncPolicy.automated` to enable it. **Q: What if bootstrap.sh fails halfway?** A: Re-run it. All commands are idempotent (`kubectl apply`, `--dry-run=client -o yaml | kubectl apply`). **Q: How do I rollback a bad GitOps change?** A: `git revert ` โ†’ `git push`. ArgoCD auto-syncs the rollback. **Q: Why rename wave numbers (00 โ†’ 0, 05 โ†’ 1, etc.)?** A: Cleaner sequential naming (0-8). Old helmfile convention used gaps (00, 05, 10, ...) which are unnecessary in pure ArgoCD. **Q: Can I delete k8s/argocd/bootstrap/ after migration?** A: Yes, once bootstrap-local/ is working. Keep BOOTSTRAP.md for historical reference if needed. --- **Last Updated:** 2025-01-XX **Status:** Ready for implementation