{{ if .Values.oncall.enabled }}
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: oncall
  namespace: argocd
  annotations:
    argocd.argoproj.io/sync-options: SkipDryRunOnMissingResource=true
  finalizers:
  - resources-finalizer.argocd.argoproj.io
spec:
  destination:
    namespace: prometheus
    server: 'https://kubernetes.default.svc'
  project: sys
  syncPolicy:
    managedNamespaceMetadata:
      labels:
        component: sys
    syncOptions:
      - CreateNamespace=true
      - ApplyOutOfSyncOnly=true
    {{ if .Values.oncall.autosync }}
    automated:
      prune: true
      # selfHeal: false
    {{ end }}
  sources:
#  - repoURL: {{ .Values.cluster_config.manifests }}
#    path: {{ .Values.cluster_config.policies }}/oncall
#    targetRevision: HEAD
  - repoURL: 'https://grafana.github.io/helm-charts'
    targetRevision: '{{ .Values.oncall.version }}'
    chart: oncall
    helm:
      values: |
        base_url: oncall.{{ .Values.cluster_config.domain }}
        base_url_protocol: https
        image:
          repository: grafana/oncall
          pullPolicy: IfNotPresent
        ingress-nginx:
           enabled: false
        cert-manager:
           enabled: false
           installCRDs: false
        ingress:
          enabled: true
          ingressClassName: nginx
          annotations:
            cert-manager.io/cluster-issuer: {{ .Values.cluster_config.ingress_clusterissuer }}
            nginx.ingress.kubernetes.io/ssl-redirect: "true"
            {{- with .Values.cluster_config.ingress_whitelist_ips }}
            nginx.ingress.kubernetes.io/whitelist-source-range: {{ join "," . }}
            {{- end }}
          path: /
          pathType: Prefix
          hosts:
            - oncall.{{ .Values.cluster_config.domain }}
          tls:
            - secretName: oncall-tls
              hosts:
                - oncall.{{ .Values.cluster_config.domain }}

        {{- if .Values.oncall.slack }}
        oncall:
          slack:
            enabled: true
            clientId: {{ .Values.oncall.slack.clientId }}
            clientSecret: {{ .Values.oncall.slack.clientSecret }}
            signingSecret: {{ .Values.oncall.slack.signingSecret }}
            redirectHost: https://oncall.{{ .Values.cluster_config.domain }}
        {{- end }}

        {{- if .Values.oncall.smtp }}
        oncall:
          smtp:
            enabled: true
            host: {{ .Values.oncall.smtp.host }}
            port: {{ .Values.oncall.smtp.port }}
            username: {{ .Values.oncall.smtp.username }}
            password: {{ .Values.oncall.smtp.password }}
            tls: {{ .Values.oncall.smtp.tls }}
            fromEmail: {{ .Values.oncall.smtp.fromEmail }}
        {{- end }}
        database:
          type: postgresql
        postgresql:
          enabled: true
          auth:
            database: oncall
        mariadb:
          enabled: false
        redis:
          enabled: true
        broker:
           type: rabbitmq
        rabbitmq:
          enabled: true
        grafana:
          enabled: false
        externalGrafana:
          url: {{ .Values.oncall.externalGrafana.url }}
{{- end }}
