# ──────────────────────────────────────────────────────────────────────────────
# App-of-Apps: each child Application receives global.* values from the
# bootstrap Application via the per-environment values file
# (environments/dev.yaml, environments/qa.yaml, …).
#
# For charts with env-specific static values, overrides are injected via
# helm.valuesObject.  For charts with env-specific templates, the global
# values are passed through so templates can reference .Values.global.*.
# ──────────────────────────────────────────────────────────────────────────────

# Install PingCAP CRDs directly from the upstream public repo so that
# tidb-operator and tidb-cluster can sync without manual steps on new clusters.
# Using Replace=true + ServerSideApply=true handles large CRD schema updates.
# prune: false ensures CRDs are never deleted by ArgoCD.
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: tidb-crds
  namespace: argocd
  annotations:
    argocd.argoproj.io/sync-wave: "-2"
spec:
  project: default
  source:
    repoURL: https://github.com/pingcap/tidb-operator
    targetRevision: v1.6.5
    path: manifests/crd/v1
  destination:
    server: https://kubernetes.default.svc
    namespace: tidb-admin
  syncPolicy:
    automated:
      prune: false
      selfHeal: true
    syncOptions:
      - CreateNamespace=true
      - Replace=true
      - ServerSideApply=true
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: storage-classes
  namespace: argocd
  annotations:
    argocd.argoproj.io/sync-wave: "-1"
spec:
  project: default
  source:
    repoURL: git@github.com:theorchard/stripo.git
    targetRevision: HEAD
    path: argocd-bootstrap/storage-classes
  destination:
    server: https://kubernetes.default.svc
    namespace: kube-system
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: tidb-operator
  namespace: argocd
  annotations:
    argocd.argoproj.io/sync-wave: "0"
spec:
  project: default
  source:
    repoURL: git@github.com:theorchard/stripo.git
    targetRevision: HEAD
    path: argocd-bootstrap/tidb-operator
  destination:
    server: https://kubernetes.default.svc
    namespace: tidb-admin
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: tidb-cluster
  namespace: argocd
  annotations:
    argocd.argoproj.io/sync-wave: "1"
spec:
  project: default
  source:
    repoURL: git@github.com:theorchard/stripo.git
    targetRevision: HEAD
    path: argocd-bootstrap/tidb-cluster
    helm:
      valuesObject:
        {{- if eq .Values.global.environment "dev" }}
        pd:
          requests:
            cpu: "100m"
            memory: "256Mi"
          limits:
            cpu: "500m"
            memory: "512Mi"
        tikv:
          requests:
            cpu: "500m"
            memory: "1Gi"
            storage: "20Gi"
          limits:
            cpu: "1"
            memory: "2Gi"
        tidb:
          requests:
            cpu: "250m"
            memory: "512Mi"
          limits:
            cpu: "1"
            memory: "1Gi"
        {{- end }}
  destination:
    server: https://kubernetes.default.svc
    namespace: tidb-cluster
  ignoreDifferences:
    - group: pingcap.com
      kind: TidbCluster
      jqPathExpressions:
        - .spec
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true
      - RespectIgnoreDifferences=true
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: external-secrets
  namespace: argocd
spec:
  project: default
  source:
    repoURL: git@github.com:theorchard/stripo.git
    targetRevision: HEAD
    path: argocd-bootstrap/external-secrets
    helm:
      valuesObject:
        external-secrets:
          serviceAccount:
            annotations:
              eks.amazonaws.com/role-arn: {{ .Values.global.irsaRoles.externalSecrets }}
  destination:
    server: https://kubernetes.default.svc
    namespace: external-secrets
  ignoreDifferences:
  - group: admissionregistration.k8s.io
    kind: ValidatingWebhookConfiguration
    jsonPointers:
    - /webhooks/0/clientConfig/caBundle
  - group: admissionregistration.k8s.io
    kind: MutatingWebhookConfiguration
    jsonPointers:
    - /webhooks/0/clientConfig/caBundle
  - group: apiextensions.k8s.io
    kind: CustomResourceDefinition
    jsonPointers:
    - /spec/conversion/webhook/clientConfig/caBundle
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true
      - ServerSideApply=true
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: argocd
  namespace: argocd
spec:
  project: default
  source:
    repoURL: git@github.com:theorchard/stripo.git
    targetRevision: HEAD
    path: argocd-bootstrap/argocd
    helm:
      valuesObject:
        global:
          {{- toYaml .Values.global | nindent 10 }}
  destination:
    server: https://kubernetes.default.svc
    namespace: argocd
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true
      - ServerSideApply=true
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: datadog
  namespace: argocd
  annotations:
    argocd.argoproj.io/sync-wave: "1"
spec:
  project: default
  source:
    repoURL: git@github.com:theorchard/stripo.git
    targetRevision: HEAD
    path: argocd-bootstrap/datadog
    helm:
      valuesObject:
        global:
          {{- toYaml .Values.global | nindent 10 }}
        datadog:
          datadog:
            clusterName: {{ .Values.global.clusterName }}
            tags:
              - env:{{ .Values.global.environment }}
              - service:stripo
  destination:
    server: https://kubernetes.default.svc
    namespace: datadog
  ignoreDifferences:
    - group: external-secrets.io
      kind: ExternalSecret
      jqPathExpressions:
        - .spec.data[].remoteRef.conversionStrategy
        - .spec.data[].remoteRef.decodingStrategy
        - .spec.data[].remoteRef.metadataPolicy
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true
      - ServerSideApply=true
      - RespectIgnoreDifferences=true
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: aws-load-balancer-controller
  namespace: argocd
spec:
  project: default
  source:
    repoURL: git@github.com:theorchard/stripo.git
    targetRevision: HEAD
    path: argocd-bootstrap/aws-load-balancer-controller
    helm:
      valuesObject:
        aws-load-balancer-controller:
          clusterName: {{ .Values.global.clusterName }}
          vpcId: {{ .Values.global.vpcId }}
          serviceAccount:
            annotations:
              eks.amazonaws.com/role-arn: {{ .Values.global.irsaRoles.lbController }}
  destination:
    server: https://kubernetes.default.svc
    namespace: kube-system
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true
      - ServerSideApply=true
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: nats
  namespace: argocd
  annotations:
    argocd.argoproj.io/sync-wave: "1"
spec:
  project: default
  source:
    repoURL: git@github.com:theorchard/stripo.git
    targetRevision: HEAD
    path: argocd-bootstrap/nats
    helm:
      valueFiles:
        - values.yaml
  destination:
    server: https://kubernetes.default.svc
    namespace: nats
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true
      - ServerSideApply=true
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: stripo
  namespace: argocd
  annotations:
    argocd.argoproj.io/sync-wave: "2"
spec:
  project: default
  source:
    repoURL: git@github.com:theorchard/stripo.git
    targetRevision: HEAD
    path: argocd-bootstrap/stripo
    helm:
      valueFiles:
        - values.yaml
        - values/ai-service.yaml
        - values/amp-validator-service.yaml
        - values/coediting-core-service.yaml
        - values/countdowntimer.yaml
        - values/emple-ui.yaml
        - values/env-adapter-service.yaml
        - values/merge-service.yaml
        - values/screenshot-service.yaml
        - values/stripe-html-cleaner-service.yaml
        - values/stripe-html-gen-service.yaml
        - values/stripo-plugin-api-gateway.yaml
        - values/stripo-plugin-custom-blocks-service.yaml
        - values/stripo-plugin-details-service.yaml
        - values/stripo-plugin-documents-service.yaml
        - values/stripo-plugin-image-bank-service.yaml
        - values/stripo-plugin-proxy-service.yaml
        - values/stripo-plugin-statistics-service.yaml
        - values/stripo-security-service.yaml
        - values/stripo-timer-api.yaml
        # Per-environment overrides – loaded last so they win
        - values-{{ .Values.global.environment }}.yaml
      valuesObject:
        global:
          {{- toYaml .Values.global | nindent 10 }}
  destination:
    server: https://kubernetes.default.svc
    namespace: stripo
  ignoreDifferences:
    - group: external-secrets.io
      kind: ExternalSecret
      jqPathExpressions:
        - .spec.data[].remoteRef.conversionStrategy
        - .spec.data[].remoteRef.decodingStrategy
        - .spec.data[].remoteRef.metadataPolicy
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true
      - ServerSideApply=true
      - RespectIgnoreDifferences=true
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: karpenter-nodepools
  namespace: argocd
  annotations:
    argocd.argoproj.io/sync-wave: "1"
spec:
  project: default
  source:
    repoURL: git@github.com:theorchard/stripo.git
    targetRevision: HEAD
    path: argocd-bootstrap/karpenter-nodepools
  destination:
    server: https://kubernetes.default.svc
    namespace: kube-system
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - ServerSideApply=true
