---
# Flux GitRepository + Kustomizations for the primary cluster.
#
# URL is templated at project-create time via substitution of
# {{GITHUB_OWNER}}/{{PROJECT_NAME}} (see src/create.js PLACEHOLDERS).
# Applied once by the GitHub Actions deploy workflow at
# .github/workflows/deploy.yml — after that, Flux reconciles the repo's
# k8s/ directory every minute (GitRepository interval) and the
# Kustomizations every 10 min.
#
# Layout in the customer's repo:
#   k8s/base/                                # db + app + traefik + backup + etc.
#   k8s/overlays/production-<region>/        # per-env overlay on top of base
apiVersion: source.toolkit.fluxcd.io/v1
kind: GitRepository
metadata:
  name: vibecarbon
  namespace: flux-system
spec:
  # 1-min poll keeps the "git push → reconcile" feedback loop tight.
  interval: 1m
  url: https://github.com/{{GITHUB_OWNER}}/{{PROJECT_NAME}}
  ref:
    branch: main
  # Secret populated by the deploy workflow at first run, containing a
  # `username` + `password` (PAT or GitHub App token with repo:read).
  secretRef:
    name: flux-system
---
# Base manifests (namespace, network policies, db, app, traefik, backup).
# The db StatefulSet lives here too (k8s/base/db/), but see `patches` below.
# Hetzner CCM and CSI are installed via cloud-init, not as Kubernetes manifests.
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
  name: vibecarbon-base
  namespace: flux-system
spec:
  interval: 10m
  targetNamespace: vibecarbon
  sourceRef:
    kind: GitRepository
    name: vibecarbon
  path: ./k8s/base
  prune: true
  # No `wait: true`: Flux ignores `healthChecks` when wait is on, and the
  # db StatefulSet below is not in this Kustomization's own inventory (see
  # `patches`), so `wait` alone would never look at the database. The
  # explicit list covers every workload base runs on this cluster, plus the
  # CLI-applied cluster-autoscaler.
  timeout: 10m
  # The db StatefulSet stays with the vibecarbon CLI's deploy, which renders
  # its per-cluster values (image, StorageClass, S3 endpoint and region,
  # wal-g prefix, the wal-archive ConfigMap sha; see
  # k8s/base/db/statefulset.yaml). Flux would apply the
  # unrendered placeholders, so it drops the object from its build here and
  # only health-checks the live one. Its Services and NetworkPolicy carry no
  # placeholders and stay in Flux's hands.
  patches:
    - patch: |
        $patch: delete
        apiVersion: apps/v1
        kind: StatefulSet
        metadata:
          name: db
      target:
        kind: StatefulSet
        name: db
    # The backup CronJob is DEPLOY-owned: its schedule and retention are per
    # environment (the CLI deploy and deploy.yml fill them in from the
    # environment's backup config, scripts/render-backup-settings.mjs), while
    # this k8s/base is shared by every environment on the branch. The live
    # object carries kustomize.toolkit.fluxcd.io/prune: disabled.
    - patch: |
        $patch: delete
        apiVersion: batch/v1
        kind: CronJob
        metadata:
          name: backup
      target:
        kind: CronJob
        name: backup
    # The TLS Certificate is DEPLOY-owned too: the CLI deploy renders it with
    # the environment's names and issuer (renderK8sBase), while this
    # k8s/base only carries an app.example.com placeholder, which Flux would
    # otherwise put back on every reconcile. The live object carries
    # kustomize.toolkit.fluxcd.io/prune: disabled.
    - patch: |
        $patch: delete
        apiVersion: cert-manager.io/v1
        kind: Certificate
        metadata:
          name: vibecarbon-tls
      target:
        kind: Certificate
        name: vibecarbon-tls
    # The registry's hostPort binds {{MASTER_PRIVATE_IP}}, rendered per
    # cluster by the CLI's deploy (renderK8sBase), so the CLI owns this
    # Deployment like the db. Its Service and NetworkPolicy stay with Flux.
    - patch: |
        $patch: delete
        apiVersion: apps/v1
        kind: Deployment
        metadata:
          name: local-registry
      target:
        kind: Deployment
        name: local-registry
    # Traefik's image comes from the version set this environment resolved
    # ({{TRAEFIK_IMAGE}}), rendered by the CLI's deploy, so the CLI owns this
    # Deployment like the registry. Its Service and RBAC stay with Flux.
    - patch: |
        $patch: delete
        apiVersion: apps/v1
        kind: Deployment
        metadata:
          name: traefik
      target:
        kind: Deployment
        name: traefik
    # Volumes are the CLI's too. An add-on's PersistentVolumeClaim names the
    # provider's StorageClass ({{K8S_STORAGE_CLASS}}), rendered per cluster by
    # the CLI's deploy, and a claim's class can't change once it exists. A
    # target with no name takes every claim; kustomize still wants a name in
    # the patch itself. Each claim carries prune: disabled.
    - patch: |
        $patch: delete
        apiVersion: v1
        kind: PersistentVolumeClaim
        metadata:
          name: every-claim
      target:
        kind: PersistentVolumeClaim
  healthChecks:
    - apiVersion: apps/v1
      kind: StatefulSet
      name: db
      namespace: vibecarbon
    - apiVersion: apps/v1
      kind: Deployment
      name: app
      namespace: vibecarbon
    - apiVersion: apps/v1
      kind: Deployment
      name: traefik
      namespace: vibecarbon
    - apiVersion: apps/v1
      kind: Deployment
      name: local-registry
      namespace: vibecarbon
    # Applied by the CLI (kube-system, outside this path), checked here like
    # the db: the autoscaler is what adds workers under load.
    - apiVersion: apps/v1
      kind: Deployment
      name: cluster-autoscaler
      namespace: kube-system
---
# Production overlay (region-specific patches on top of ./k8s/base).
# Applied only when the region matches; the deploy workflow renders the
# correct overlay name (production-nbg1, production-hel1, etc.) into a
# Kustomization name specific to the cluster at apply time.
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
  name: vibecarbon-production-nbg1
  namespace: flux-system
spec:
  interval: 10m
  targetNamespace: vibecarbon
  sourceRef:
    kind: GitRepository
    name: vibecarbon
  path: ./k8s/overlays/production-nbg1
  prune: true
  wait: true
  timeout: 10m
  dependsOn:
    - name: vibecarbon-base
