{{ if .Values.loki.enabled }}
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: loki
  namespace: argocd
  annotations:
    argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
  finalizers:
  - resources-finalizer.argocd.argoproj.io
spec:
  destination:
    namespace: loki
    server: 'https://kubernetes.default.svc'
  sources:
  - repoURL: {{ .Values.cluster_config.manifests }}
    path: {{ .Values.cluster_config.policies }}/loki
    targetRevision: HEAD
  - repoURL: 'https://grafana.github.io/helm-charts'
    targetRevision: {{ .Values.loki.version }}
    chart: loki
    helm:
      values: |
        loki:
          auth_enabled: false
          storage:
            bucketNames:
              chunks: {{ .Values.loki.buckets.chunks }}
              ruler: {{ .Values.loki.buckets.ruler }}
              admin: {{ .Values.loki.buckets.admin }}
            s3:
              endpoint: {{ .Values.loki.s3.endpoint | default "https://s3.production.itpartner.no" }}
              region: {{ .Values.loki.s3.region | default "us-east-1" }}
              secretAccessKey: ${S3SECRET}
              accessKeyId: ${S3KEY}
              s3ForcePathStyle: true
              {{- if .Values.loki.s3.insecure_skip_verify }}
              http_config:
                insecure_skip_verify: true
              {{- end }}
          schemaConfig:
            configs:
            - from: "2022-09-28"
              index:
                period: 24h
                prefix: loki_index_
              object_store: s3
              schema: v13
              store: tsdb
          {{- if .Values.loki.compactor }}
          compactor:
            compaction_interval: 10m
            working_directory: /tmp/loki/compactor
            retention_enabled: true
            retention_delete_delay: 2h
            retention_delete_worker_count: 150
            delete_request_store: s3
          {{- end }}
        write:
          extraArgs:
            - -config.expand-env=true
          extraEnv:
          - name: S3KEY
            valueFrom:
              secretKeyRef:
                name: {{ .Values.loki.secret.name | default "s3-credentials"}}
                key: {{ .Values.loki.secret.access_key | default "access_key" }}
          - name: S3SECRET
            valueFrom:
              secretKeyRef:
                name: {{ .Values.loki.secret.name | default "s3-credentials"}}
                key: {{ .Values.loki.secret.access_key | default "access_secret" }}
          tolerations:
          - effect: "NoSchedule"
            operator: "Equal"
            key: "unschedulable"
            value: "true"
        read:
          extraArgs:
            - -config.expand-env=true
          extraEnv:
          - name: S3KEY
            valueFrom:
              secretKeyRef:
                name: {{ .Values.loki.secret.name | default "s3-credentials"}}
                key: {{ .Values.loki.secret.access_key | default "access_key" }}
          - name: S3SECRET
            valueFrom:
              secretKeyRef:
                name: {{ .Values.loki.secret.name | default "s3-credentials"}}
                key: {{ .Values.loki.secret.access_key | default "access_secret" }}
          tolerations:
          - effect: "NoSchedule"
            operator: "Equal"
            key: "unschedulable"
            value: "true"
        {{- if .Values.loki.compactor }}
        compactor:
          extraArgs:
            - -config.expand-env=true
          extraEnv:
          - name: S3KEY
            valueFrom:
              secretKeyRef:
                name: {{ .Values.loki.secret.name | default "s3-credentials"}}
                key: {{ .Values.loki.secret.access_key | default "access_key" }}
          - name: S3SECRET
            valueFrom:
              secretKeyRef:
                name: {{ .Values.loki.secret.name | default "s3-credentials"}}
                key: {{ .Values.loki.secret.access_key | default "access_secret" }}
        {{- end }}
        backend:
          extraArgs:
            - -config.expand-env=true
          extraEnv:
          - name: S3KEY
            valueFrom:
              secretKeyRef:
                name: {{ .Values.loki.secret.name | default "s3-credentials"}}
                key: {{ .Values.loki.secret.access_key | default "access_key" }}
          - name: S3SECRET
            valueFrom:
              secretKeyRef:
                name: {{ .Values.loki.secret.name | default "s3-credentials"}}
                key: {{ .Values.loki.secret.access_key | default "access_secret" }}
  project: sys
  ignoreDifferences:
  - group: apps
    kind: StatefulSet
    jsonPointers:
    - /spec/persistentVolumeClaimRetentionPolicy
  syncPolicy:
    managedNamespaceMetadata:
      labels:
        component: sys
    syncOptions:
      - CreateNamespace=true
      - ApplyOutOfSyncOnly=true
    {{- if .Values.loki.autosync }}
    automated:
      prune: true
      # selfHeal: false
    {{- end }}
{{ end }}
