# ============================================================
# OmniCommerce Cloud — ArgoCD ApplicationSets
# GitOps Deployment Configuration
# ============================================================

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: omnicommerce
  namespace: argocd
spec:
  generators:
    - git:
        repoURL: https://github.com/omnicommerce/omnicommerce-cloud.git
        revision: HEAD
        directories:
          - path: infrastructure/kubernetes/overlays/*
  template:
    metadata:
      name: 'omnicommerce-{{path.basename}}'
      labels:
        app.kubernetes.io/part-of: omnicommerce
    spec:
      project: omnicommerce
      source:
        repoURL: https://github.com/omnicommerce/omnicommerce-cloud.git
        targetRevision: HEAD
        path: '{{path}}'
      destination:
        name: '{{cluster.name}}'
        namespace: omnicommerce
      syncPolicy:
        automated:
          prune: true
          selfHeal: true
          allowEmpty: false
        syncOptions:
          - CreateNamespace=true
          - PruneLast=true
          - ApplyOutOfSyncOnly=true
        retry:
          limit: 5
          backoff:
            duration: 5s
            factor: 2
            maxDuration: 3m
---
# ============================================================
# ArgoCD App of Apps Pattern
# ============================================================

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: omnicommerce-infra
  namespace: argocd
  finalizers:
    - resources-finalizer.argocd.argoproj.io
spec:
  project: omnicommerce
  source:
    repoURL: https://github.com/omnicommerce/omnicommerce-cloud.git
    targetRevision: HEAD
    path: infrastructure/argocd/apps
    directory:
      recurse: true
  destination:
    server: https://kubernetes.default.svc
    namespace: argocd
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true
---
# ============================================================
# Individual Application definitions
# ============================================================

# -------- Infrastructure Stack --------

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: omnicommerce-cert-manager
  namespace: argocd
spec:
  project: omnicommerce
  source:
    chart: cert-manager
    repoURL: https://charts.jetstack.io
    targetRevision: 1.14.0
    helm:
      values: |
        installCRDs: true
        ingressShim:
          defaultIssuerName: letsencrypt-prod
          defaultIssuerKind: ClusterIssuer
  destination:
    server: https://kubernetes.default.svc
    namespace: cert-manager
  syncPolicy:
    automated:
      prune: true
    syncOptions:
      - CreateNamespace=true
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: omnicommerce-ingress-nginx
  namespace: argocd
spec:
  project: omnicommerce
  source:
    chart: ingress-nginx
    repoURL: https://kubernetes.github.io/ingress-nginx
    targetRevision: 4.10.0
    helm:
      values: |
        controller:
          replicaCount: 3
          service:
            annotations:
              service.beta.kubernetes.io/aws-load-balancer-type: nlb
              service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled: "true"
          metrics:
            enabled: true
            serviceMonitor:
              enabled: true
          resources:
            requests:
              cpu: 200m
              memory: 256Mi
            limits:
              cpu: 500m
              memory: 512Mi
  destination:
    server: https://kubernetes.default.svc
    namespace: ingress-nginx
  syncPolicy:
    automated:
      prune: true
    syncOptions:
      - CreateNamespace=true
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: omnicommerce-prometheus-stack
  namespace: argocd
spec:
  project: omnicommerce
  source:
    chart: kube-prometheus-stack
    repoURL: https://prometheus-community.github.io/helm-charts
    targetRevision: 58.0.0
    helm:
      values: |
        prometheus:
          prometheusSpec:
            retention: 30d
            resources:
              requests:
                cpu: 500m
                memory: 2Gi
              limits:
                cpu: 2
                memory: 4Gi
        grafana:
          adminPassword: ${GRAFANA_ADMIN_PASSWORD}
          persistence:
            enabled: true
            size: 20Gi
        alertmanager:
          enabled: true
  destination:
    server: https://kubernetes.default.svc
    namespace: monitoring
  syncPolicy:
    automated:
      prune: true
    syncOptions:
      - CreateNamespace=true
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: omnicommerce-loki
  namespace: argocd
spec:
  project: omnicommerce
  source:
    chart: loki-stack
    repoURL: https://grafana.github.io/helm-charts
    targetRevision: 2.10.0
    helm:
      values: |
        loki:
          persistence:
            enabled: true
            size: 100Gi
        promtail:
          enabled: true
  destination:
    server: https://kubernetes.default.svc
    namespace: monitoring
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: omnicommerce-external-secrets
  namespace: argocd
spec:
  project: omnicommerce
  source:
    chart: external-secrets
    repoURL: https://charts.external-secrets.io
    targetRevision: 0.9.0
    helm:
      values: |
        serviceAccount:
          annotations:
            eks.amazonaws.com/role-arn: ${EXTERNAL_SECRETS_ROLE_ARN}
  destination:
    server: https://kubernetes.default.svc
    namespace: external-secrets
  syncPolicy:
    syncOptions:
      - CreateNamespace=true
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: omnicommerce-strimzi-kafka
  namespace: argocd
spec:
  project: omnicommerce
  source:
    chart: strimzi-kafka-operator
    repoURL: https://strimzi.io/charts/
    targetRevision: 0.40.0
    helm:
      values: |
        watchAnyNamespace: true
  destination:
    server: https://kubernetes.default.svc
    namespace: kafka
  syncPolicy:
    syncOptions:
      - CreateNamespace=true
---
# ============================================================
# STRIMZI KAFKA CLUSTER (Custom Resource)
# ============================================================

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: omnicommerce-kafka-cluster
  namespace: argocd
spec:
  project: omnicommerce
  source:
    repoURL: https://github.com/omnicommerce/omnicommerce-cloud.git
    targetRevision: HEAD
    path: infrastructure/kubernetes/strimzi
  destination:
    server: https://kubernetes.default.svc
    namespace: kafka
  syncPolicy:
    syncOptions:
      - CreateNamespace=true
---
# ============================================================
# TEMPORAL
# ============================================================

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: omnicommerce-temporal
  namespace: argocd
spec:
  project: omnicommerce
  source:
    repoURL: https://github.com/omnicommerce/omnicommerce-cloud.git
    targetRevision: HEAD
    path: infrastructure/kubernetes/temporal
  destination:
    server: https://kubernetes.default.svc
    namespace: temporal
  syncPolicy:
    syncOptions:
      - CreateNamespace=true
