{{- if .Values.prometheus.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  name: {{ template "kube-prometheus.prometheus.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "kube-prometheus.prometheus.labels" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.prometheus.replicaCount }}
  shards: {{ .Values.prometheus.shards }}
  serviceAccountName: {{ template "kube-prometheus.prometheus.serviceAccountName" . }}
  {{- if .Values.prometheus.serviceMonitorSelector }}
  serviceMonitorSelector: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.serviceMonitorSelector "context" $) | nindent 4 }}
  {{- else }}
  serviceMonitorSelector: {}
  {{- end }}
  {{- if .Values.prometheus.podMonitorSelector }}
  podMonitorSelector: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.podMonitorSelector "context" $) | nindent 4 }}
  {{- else }}
  podMonitorSelector: {}
  {{- end }}
  {{- if .Values.prometheus.probeSelector }}
  probeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.probeSelector "context" $) | nindent 4 }}
  {{- else }}
  probeSelector: {}
  {{- end }}
  alerting:
    alertmanagers:
    {{- if .Values.prometheus.alertingEndpoints }}
    {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.alertingEndpoints "context" $) | nindent 6 }}
    {{- else if .Values.alertmanager.enabled }}
      - namespace: {{ include "common.names.namespace" . | quote }}
        name: {{ template "kube-prometheus.alertmanager.fullname" . }}
        port: http
        pathPrefix: "{{ .Values.alertmanager.routePrefix }}"
    {{- else }}
      []
    {{- end }}
  {{- if .Values.prometheus.image }}
  image: {{ template "kube-prometheus.prometheus.image" . }}
  {{- end }}
  {{- if .Values.prometheus.externalLabels }}
  externalLabels: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.externalLabels "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.prometheus.prometheusExternalLabelNameClear }}
  prometheusExternalLabelName: ""
  {{- else if .Values.prometheus.prometheusExternalLabelName }}
  prometheusExternalLabelName: "{{ .Values.prometheus.prometheusExternalLabelName }}"
  {{- end }}
  {{- if .Values.prometheus.replicaExternalLabelNameClear }}
  replicaExternalLabelName: ""
  {{- else if .Values.prometheus.replicaExternalLabelName }}
  replicaExternalLabelName: "{{ .Values.prometheus.replicaExternalLabelName }}"
  {{- end }}
  {{- if index .Values.prometheus "externalUrl" }}
  externalUrl: "{{ .Values.prometheus.externalUrl }}"
  {{- else if and .Values.prometheus.ingress.enabled }}
  externalUrl: {{ printf "%s://%s%s" (ternary "https" "http" .Values.prometheus.ingress.tls) .Values.prometheus.ingress.hostname .Values.prometheus.routePrefix }}
  {{- else }}
  externalUrl: http://{{ template "kube-prometheus.prometheus.fullname" . }}.{{ include "common.names.namespace" . }}:9090{{ .Values.prometheus.routePrefix }}
  {{- end }}
  paused: {{ .Values.prometheus.paused }}
  logLevel: {{ .Values.prometheus.logLevel }}
  logFormat: {{ .Values.prometheus.logFormat }}
  listenLocal: {{ .Values.prometheus.listenLocal }}
  enableAdminAPI: {{ .Values.prometheus.enableAdminAPI }}
  {{- if .Values.prometheus.enableFeatures }}
  enableFeatures:
    {{- range .Values.prometheus.enableFeatures }}
    - {{ . | quote }}
    {{- end }}
  {{- end }}
  {{- if .Values.prometheus.scrapeInterval }}
  scrapeInterval: {{ .Values.prometheus.scrapeInterval }}
  {{- end }}
  {{- if .Values.prometheus.evaluationInterval }}
  evaluationInterval: {{ .Values.prometheus.evaluationInterval }}
  {{- end }}
  {{- if .Values.prometheus.resources }}
  resources: {{- toYaml .Values.prometheus.resources | nindent 4 }}
  {{- end }}
  retention: {{ .Values.prometheus.retention }}
  {{- if .Values.prometheus.retentionSize }}
  retentionSize: {{ .Values.prometheus.retentionSize }}
  {{- end }}
  {{- if .Values.prometheus.disableCompaction }}
  disableCompaction: {{ .Values.prometheus.disableCompaction }}
  {{- end }}
  {{- if .Values.prometheus.walCompression }}
  walCompression: {{ .Values.prometheus.walCompression }}
  {{- end }}
  portName: "{{ .Values.prometheus.portName }}"
  routePrefix: "{{ .Values.prometheus.routePrefix }}"
  {{- if .Values.prometheus.secrets }}
  secrets: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.secrets "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.prometheus.configMaps }}
  configMaps: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.configMaps "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.prometheus.serviceMonitorNamespaceSelector }}
  serviceMonitorNamespaceSelector: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.serviceMonitorNamespaceSelector "context" $) | nindent 4 }}
  {{- else }}
  serviceMonitorNamespaceSelector: {}
  {{- end }}
  {{- if .Values.prometheus.podMonitorNamespaceSelector }}
  podMonitorNamespaceSelector: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.podMonitorNamespaceSelector "context" $) | nindent 4 }}
  {{- else }}
  podMonitorNamespaceSelector: {}
  {{- end }}
  {{- if .Values.prometheus.probeNamespaceSelector }}
  probeNamespaceSelector: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.probeNamespaceSelector "context" $) | nindent 4 }}
  {{- else }}
  probeNamespaceSelector: {}
  {{- end }}
  {{- if .Values.prometheus.remoteRead }}
  remoteRead: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.remoteRead "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.prometheus.remoteWrite }}
  remoteWrite: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.remoteWrite "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.prometheus.podSecurityContext.enabled }}
  securityContext: {{- omit .Values.prometheus.podSecurityContext "enabled" | toYaml | nindent 4 }}
  {{- end }}
  {{- if .Values.prometheus.ruleNamespaceSelector }}
  ruleNamespaceSelector: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.ruleNamespaceSelector "context" $) | nindent 4 }}
  {{- else }}
  ruleNamespaceSelector: {}
  {{- end }}
  {{- if .Values.prometheus.ruleSelector }}
  ruleSelector: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.ruleSelector "context" $) | nindent 4 }}
  {{- else }}
  ruleSelector: {}
  {{- end }}
  {{- if .Values.prometheus.storageSpec }}
  storage: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.storageSpec "context" $) | nindent 4 }}
  {{- else if .Values.prometheus.persistence.enabled }}
  storage:
    volumeClaimTemplate:
      metadata:
        {{- if or .Values.prometheus.persistence.annotations .Values.commonAnnotations }}
        annotations:
          {{- if .Values.prometheus.persistence.annotations }}
          {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.persistence.annotations "context" $) | nindent 10 }}
          {{- end }}
          {{- if .Values.commonAnnotations }}
          {{- include "common.tplvalues.render" (dict "value" .Values.commonAnnotations "context" $) | nindent 10 }}
          {{- end }}
        {{- end }}
        {{- if .Values.commonLabels }}
        labels: {{- include "common.tplvalues.render" (dict "value" .Values.commonLabels "context" $) | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
          {{- range .Values.prometheus.persistence.accessModes }}
          - {{ . | quote }}
          {{- end }}
        resources:
          requests:
            storage: {{ .Values.prometheus.persistence.size | quote }}
        {{- include "common.storage.class" (dict "persistence" .Values.prometheus.persistence "global" .Values.global) | nindent 8 }}
  {{- end }}
  {{- if or .Values.prometheus.podMetadata.labels .Values.prometheus.podMetadata.annotations (eq .Values.prometheus.podAntiAffinityPreset "soft") (eq .Values.prometheus.podAntiAffinityPreset "hard") }}
  podMetadata:
    labels:
    {{- if .Values.prometheus.podMetadata.labels }}
    {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.podMetadata.labels "context" $) | nindent 6 }}
    {{- end }}
    {{- if or (eq .Values.prometheus.podAntiAffinityPreset "soft") (eq .Values.prometheus.podAntiAffinityPreset "hard") }}
    {{- include "kube-prometheus.prometheus.matchLabels" . | nindent 6 }}
    {{- end }}
    {{- if .Values.prometheus.podMetadata.annotations }}
    annotations:
    {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.podMetadata.annotations "context" $) | nindent 6 }}
    {{- end }}
  {{- end }}
  {{- if .Values.prometheus.querySpec }}
  query: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.querySpec "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.prometheus.affinity }}
  affinity: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.affinity "context" $) | nindent 4 }}
  {{- else }}
  affinity:
    {{- if not (empty .Values.prometheus.podAffinityPreset) }}
    podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.prometheus.podAffinityPreset "component" "prometheus" "context" $) | nindent 6 }}
    {{- end }}
    {{- if not (empty .Values.prometheus.podAntiAffinityPreset) }}
    podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.prometheus.podAntiAffinityPreset "component" "prometheus" "context" $) | nindent 6 }}
    {{- end }}
    {{- if not (empty .Values.prometheus.nodeAffinityPreset.values) }}
    nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.prometheus.nodeAffinityPreset.type "key" .Values.prometheus.nodeAffinityPreset.key "values" .Values.prometheus.nodeAffinityPreset.values) | nindent 6 }}
    {{- end }}
  {{- end }}
  {{- if .Values.prometheus.nodeSelector }}
  nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.nodeSelector "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.prometheus.tolerations }}
  tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.tolerations "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.prometheus.volumes }}
  volumes: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.volumes "context" $) | nindent 4 }}
  {{- end }}
  {{- if .Values.prometheus.volumeMounts }}
  volumeMounts: {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.volumeMounts "context" $) | nindent 4 }}
  {{- end }}
  {{- if or .Values.prometheus.additionalScrapeConfigs.enabled .Values.prometheus.additionalScrapeConfigsExternal.enabled }}
  additionalScrapeConfigs:
    {{- if and .Values.prometheus.additionalScrapeConfigs.enabled (eq .Values.prometheus.additionalScrapeConfigs.type "external") }}
    name: {{ .Values.prometheus.additionalScrapeConfigs.external.name }}
    key: {{ .Values.prometheus.additionalScrapeConfigs.external.key }}
    {{- else if and .Values.prometheus.additionalScrapeConfigs.enabled (eq .Values.prometheus.additionalScrapeConfigs.type "internal") }}
    name: additional-scrape-jobs-{{ template "kube-prometheus.prometheus.fullname" . }}
    key: scrape-jobs.yaml
    {{- else if and (not .Values.prometheus.additionalScrapeConfigs.enabled) .Values.prometheus.additionalScrapeConfigsExternal.enabled }}
    name: {{ .Values.prometheus.additionalScrapeConfigsExternal.name }}
    key: {{ .Values.prometheus.additionalScrapeConfigsExternal.key }}
    {{- end }}
  {{- end }}
  {{- if .Values.prometheus.additionalAlertRelabelConfigsExternal.enabled }}
  additionalAlertRelabelConfigs:
    name: {{ .Values.prometheus.additionalAlertRelabelConfigsExternal.name }}
    key: {{ .Values.prometheus.additionalAlertRelabelConfigsExternal.key }}
  {{- end }}
{{- include "kube-prometheus.imagePullSecrets" . | indent 2 }}
  {{- if or .Values.prometheus.containers .Values.prometheus.thanos.create .Values.prometheus.containerSecurityContext.enabled .Values.prometheus.containerSecurityContext.enabled .Values.operator.prometheusConfigReloader.containerSecurityContext.enabled }}
  {{- if .Values.prometheus.initContainers }}
  initContainers:
    {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.initContainers "context" $) | nindent 4 }}
  {{- end }}
  containers:
    {{- if .Values.prometheus.thanos.create }}
    - name: thanos-sidecar
      image: {{ template "kube-prometheus.prometheus.thanosImage" . }}
      imagePullPolicy: {{ .Values.prometheus.thanos.image.pullPolicy }}
      args:
        - sidecar
        - --prometheus.url={{ default "http://localhost:9090" .Values.prometheus.thanos.prometheusUrl }}
        - --grpc-address=0.0.0.0:10901
        - --http-address=0.0.0.0:10902
        - --tsdb.path=/prometheus/
        {{- if .Values.prometheus.thanos.objectStorageConfig }}
        - --objstore.config=$(OBJSTORE_CONFIG)
        {{- end }}
        {{- if .Values.prometheus.thanos.extraArgs }}
        {{ toYaml .Values.prometheus.thanos.extraArgs | indent 8  | trim }}
        {{- end }}
      {{- if .Values.prometheus.thanos.objectStorageConfig }}
      env:
        - name: OBJSTORE_CONFIG
          valueFrom:
            secretKeyRef:
              name: {{ .Values.prometheus.thanos.objectStorageConfig.secretName }}
              key: {{ .Values.prometheus.thanos.objectStorageConfig.secretKey | default "thanos.yaml" }}
      {{- end }}
      {{- if .Values.prometheus.thanos.resources }}
      resources: {{- toYaml .Values.prometheus.thanos.resources | nindent 8 }}
      {{- end }}
      ports:
        - name: grpc
          containerPort: 10901
          protocol: TCP
        - name: http
          containerPort: 10902
          protocol: TCP
      volumeMounts:
        - mountPath: /prometheus
          name: prometheus-{{ template "kube-prometheus.prometheus.fullname" . }}-db
          {{- if not (.Values.prometheus.storageSpec.disableMountSubPath | default (not .Values.prometheus.persistence.enabled)) }}
          subPath: prometheus-db
          {{- end }}
        {{- if .Values.prometheus.thanos.extraVolumeMounts }}
        {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.thanos.extraVolumeMounts "context" $) | nindent 8 }}
        {{- end }}
      {{- if .Values.prometheus.thanos.containerSecurityContext.enabled }}
      # yamllint disable rule:indentation
      securityContext: {{- omit .Values.prometheus.thanos.containerSecurityContext "enabled" | toYaml | nindent 8 }}
      # yamllint enable rule:indentation
      {{- end }}
      {{- if .Values.prometheus.thanos.livenessProbe.enabled }}
      livenessProbe:
        httpGet:
          path: {{ .Values.prometheus.thanos.livenessProbe.path }}
          port: http
          scheme: HTTP
        initialDelaySeconds: {{ .Values.prometheus.thanos.livenessProbe.initialDelaySeconds }}
        periodSeconds: {{ .Values.prometheus.thanos.livenessProbe.periodSeconds }}
        timeoutSeconds: {{ .Values.prometheus.thanos.livenessProbe.timeoutSeconds }}
        failureThreshold: {{ .Values.prometheus.thanos.livenessProbe.failureThreshold }}
        successThreshold: {{ .Values.prometheus.thanos.livenessProbe.successThreshold }}
      {{- end }}
      {{- if .Values.prometheus.thanos.readinessProbe.enabled }}
      readinessProbe:
        httpGet:
          path: {{ .Values.prometheus.thanos.readinessProbe.path }}
          port: http
          scheme: HTTP
        initialDelaySeconds: {{ .Values.prometheus.thanos.readinessProbe.initialDelaySeconds }}
        periodSeconds: {{ .Values.prometheus.thanos.readinessProbe.periodSeconds }}
        timeoutSeconds: {{ .Values.prometheus.thanos.readinessProbe.timeoutSeconds }}
        failureThreshold: {{ .Values.prometheus.thanos.readinessProbe.failureThreshold }}
        successThreshold: {{ .Values.prometheus.thanos.readinessProbe.successThreshold }}
      {{- end }}
    {{- end }}
    {{- if or .Values.prometheus.containerSecurityContext.enabled .Values.prometheus.livenessProbe.enabled .Values.prometheus.readinessProbe.enabled }}
    ## This monkey patching is needed until the securityContexts are
    ## directly patchable via the CRD.
    ## ref: https://github.com/prometheus-operator/prometheus-operator/issues/3947
    ## currently implemented with strategic merge
    ## ref: https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/user-guides/strategic-merge-patch.md
    - name: prometheus
      {{- if .Values.prometheus.containerSecurityContext.enabled }}
      securityContext: {{- omit .Values.prometheus.containerSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.prometheus.livenessProbe.enabled }}
      livenessProbe:
        httpGet:
          path: {{ .Values.prometheus.livenessProbe.path }}
          port: web
          scheme: HTTP
        initialDelaySeconds: {{ .Values.prometheus.livenessProbe.initialDelaySeconds }}
        periodSeconds: {{ .Values.prometheus.livenessProbe.periodSeconds }}
        timeoutSeconds: {{ .Values.prometheus.livenessProbe.timeoutSeconds }}
        failureThreshold: {{ .Values.prometheus.livenessProbe.failureThreshold }}
        successThreshold: {{ .Values.prometheus.livenessProbe.successThreshold }}
      {{- end }}
      {{- if .Values.prometheus.readinessProbe.enabled }}
      readinessProbe:
        httpGet:
          path: {{ .Values.prometheus.readinessProbe.path }}
          port: web
          scheme: HTTP
        initialDelaySeconds: {{ .Values.prometheus.readinessProbe.initialDelaySeconds }}
        periodSeconds: {{ .Values.prometheus.readinessProbe.periodSeconds }}
        timeoutSeconds: {{ .Values.prometheus.readinessProbe.timeoutSeconds }}
        failureThreshold: {{ .Values.prometheus.readinessProbe.failureThreshold }}
        successThreshold: {{ .Values.prometheus.readinessProbe.successThreshold }}
      {{- end }}
      {{- if .Values.prometheus.startupProbe.enabled }}
      startupProbe:
        httpGet:
          path: {{ .Values.prometheus.startupProbe.path }}
          port: web
          scheme: HTTP
        initialDelaySeconds: {{ .Values.prometheus.startupProbe.initialDelaySeconds }}
        periodSeconds: {{ .Values.prometheus.startupProbe.periodSeconds }}
        timeoutSeconds: {{ .Values.prometheus.startupProbe.timeoutSeconds }}
        failureThreshold: {{ .Values.prometheus.startupProbe.failureThreshold }}
        successThreshold: {{ .Values.prometheus.startupProbe.successThreshold }}
      {{- end }}
    {{- end }}
    {{- if or .Values.operator.prometheusConfigReloader.containerSecurityContext.enabled .Values.operator.prometheusConfigReloader.livenessProbe.enabled .Values.operator.prometheusConfigReloader.readinessProbe.enabled }}
    ## This monkey patching is needed until the securityContexts are
    ## directly patchable via the CRD.
    ## ref: https://github.com/prometheus-operator/prometheus-operator/issues/3947
    ## currently implemented with strategic merge
    ## ref: https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/user-guides/strategic-merge-patch.md
    - name: config-reloader
      {{- if .Values.operator.prometheusConfigReloader.containerSecurityContext.enabled }}
      securityContext: {{- omit .Values.operator.prometheusConfigReloader.containerSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.operator.prometheusConfigReloader.livenessProbe.enabled }}
      livenessProbe:
        tcpSocket:
          port: reloader-web
        initialDelaySeconds: {{ .Values.operator.prometheusConfigReloader.livenessProbe.initialDelaySeconds }}
        periodSeconds: {{ .Values.operator.prometheusConfigReloader.livenessProbe.periodSeconds }}
        timeoutSeconds: {{ .Values.operator.prometheusConfigReloader.livenessProbe.timeoutSeconds }}
        failureThreshold: {{ .Values.operator.prometheusConfigReloader.livenessProbe.failureThreshold }}
        successThreshold: {{ .Values.operator.prometheusConfigReloader.livenessProbe.successThreshold }}
      {{- end }}
      {{- if .Values.operator.prometheusConfigReloader.readinessProbe.enabled }}
      readinessProbe:
        tcpSocket:
          port: reloader-web
        initialDelaySeconds: {{ .Values.operator.prometheusConfigReloader.readinessProbe.initialDelaySeconds }}
        periodSeconds: {{ .Values.operator.prometheusConfigReloader.readinessProbe.periodSeconds }}
        timeoutSeconds: {{ .Values.operator.prometheusConfigReloader.readinessProbe.timeoutSeconds }}
        failureThreshold: {{ .Values.operator.prometheusConfigReloader.readinessProbe.failureThreshold }}
        successThreshold: {{ .Values.operator.prometheusConfigReloader.readinessProbe.successThreshold }}
      {{- end }}
    {{- end }}
    {{- if .Values.prometheus.containers }}
    {{- include "common.tplvalues.render" (dict "value" .Values.prometheus.containers "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
  {{- if .Values.prometheus.priorityClassName }}
  priorityClassName: {{ .Values.prometheus.priorityClassName }}
  {{- end }}
{{- end }}
