{{- if .Values.thanosRuler.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ThanosRuler
metadata:
  name: {{ template "kube-prometheus-stack.thanosRuler.crname" . }}
  namespace: {{ template "kube-prometheus-stack.namespace" . }}
  labels:
    app: {{ include "kube-prometheus-stack.thanosRuler.name" . }}
{{- include "kube-prometheus-stack.labels" . | indent 4 -}}
{{- if .Values.thanosRuler.annotations }}
  annotations:
{{ toYaml .Values.thanosRuler.annotations | indent 4 }}
{{- end }}
spec:
{{- if .Values.thanosRuler.thanosRulerSpec.image }}
  {{- $registry := .Values.global.imageRegistry | default .Values.thanosRuler.thanosRulerSpec.image.registry -}}
  {{- if and .Values.thanosRuler.thanosRulerSpec.image.tag .Values.thanosRuler.thanosRulerSpec.image.sha }}
  image: "{{ $registry }}/{{ .Values.thanosRuler.thanosRulerSpec.image.repository }}:{{ .Values.thanosRuler.thanosRulerSpec.image.tag }}@sha256:{{ .Values.thanosRuler.thanosRulerSpec.image.sha }}"
  {{- else if .Values.thanosRuler.thanosRulerSpec.image.sha }}
  image: "{{ $registry }}/{{ .Values.thanosRuler.thanosRulerSpec.image.repository }}@sha256:{{ .Values.thanosRuler.thanosRulerSpec.image.sha }}"
  {{- else if .Values.thanosRuler.thanosRulerSpec.image.tag }}
  image: "{{ $registry }}/{{ .Values.thanosRuler.thanosRulerSpec.image.repository }}:{{ .Values.thanosRuler.thanosRulerSpec.image.tag }}"
  {{- else }}
  image: "{{ $registry }}/{{ .Values.thanosRuler.thanosRulerSpec.image.repository }}"
  {{- end }}
  {{- if .Values.thanosRuler.thanosRulerSpec.image.sha }}
  sha: {{ .Values.thanosRuler.thanosRulerSpec.image.sha }}
  {{- end }}
{{- end }}
  replicas: {{ .Values.thanosRuler.thanosRulerSpec.replicas }}
  listenLocal: {{ .Values.thanosRuler.thanosRulerSpec.listenLocal }}
  {{- if .Values.thanosRuler.thanosRulerSpec.serviceName }}
  serviceName: {{ tpl .Values.thanosRuler.thanosRulerSpec.serviceName . }}
  {{- end }}
  serviceAccountName: {{ template "kube-prometheus-stack.thanosRuler.serviceAccountName" . }}
{{- if .Values.thanosRuler.thanosRulerSpec.externalPrefix }}
  externalPrefix: "{{ tpl .Values.thanosRuler.thanosRulerSpec.externalPrefix . }}"
{{- else if and .Values.thanosRuler.ingress.enabled .Values.thanosRuler.ingress.hosts }}
  externalPrefix: "http://{{ tpl (index .Values.thanosRuler.ingress.hosts 0) . }}{{ .Values.thanosRuler.thanosRulerSpec.routePrefix }}"
{{- else if .Values.thanosRuler.thanosRulerSpec.externalPrefixNilUsesHelmValues }}
  externalPrefix: "http://{{ template "kube-prometheus-stack.thanosRuler.name" . }}.{{ template "kube-prometheus-stack.namespace" . }}:{{ .Values.thanosRuler.service.port }}"
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.additionalArgs }}
  additionalArgs:
{{ tpl (toYaml .Values.thanosRuler.thanosRulerSpec.additionalArgs) $ | indent 4 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.nodeSelector }}
  nodeSelector:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.nodeSelector | indent 4 }}
{{- end }}
  paused: {{ .Values.thanosRuler.thanosRulerSpec.paused }}
  logFormat: {{ .Values.thanosRuler.thanosRulerSpec.logFormat | quote  }}
  logLevel:  {{ .Values.thanosRuler.thanosRulerSpec.logLevel | quote  }}
  retention: {{ .Values.thanosRuler.thanosRulerSpec.retention | quote  }}
{{- if .Values.thanosRuler.thanosRulerSpec.evaluationInterval }}
  evaluationInterval: {{ .Values.thanosRuler.thanosRulerSpec.evaluationInterval }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.ruleNamespaceSelector }}
  ruleNamespaceSelector:
{{ tpl (toYaml .Values.thanosRuler.thanosRulerSpec.ruleNamespaceSelector | indent 4) . }}
{{ else }}
  ruleNamespaceSelector: {}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.ruleSelector }}
  ruleSelector:
{{ tpl (toYaml .Values.thanosRuler.thanosRulerSpec.ruleSelector | indent 4) .}}
{{- else if .Values.thanosRuler.thanosRulerSpec.ruleSelectorNilUsesHelmValues }}
  ruleSelector:
    matchLabels:
      release: {{ $.Release.Name | quote }}
{{ else }}
  ruleSelector: {}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.alertQueryUrl }}
  alertQueryUrl: "{{ .Values.thanosRuler.thanosRulerSpec.alertQueryUrl }}"
{{- end}}
{{- if .Values.thanosRuler.thanosRulerSpec.alertmanagersUrl }}
  alertmanagersUrl:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.alertmanagersUrl | indent 4 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.alertmanagersConfig.existingSecret }}
  alertmanagersConfig:
    key: "{{.Values.thanosRuler.thanosRulerSpec.alertmanagersConfig.existingSecret.key }}"
    name: "{{.Values.thanosRuler.thanosRulerSpec.alertmanagersConfig.existingSecret.name }}"
{{- else if .Values.thanosRuler.thanosRulerSpec.alertmanagersConfig.secret }}
  alertmanagersConfig:
    key: alertmanager-configs.yaml
    name: {{ template "kube-prometheus-stack.thanosRuler.name" . }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.queryEndpoints }}
  queryEndpoints:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.queryEndpoints | indent 4 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.queryConfig.existingSecret }}
  queryConfig:
    key: "{{.Values.thanosRuler.thanosRulerSpec.queryConfig.existingSecret.key }}"
    name: "{{.Values.thanosRuler.thanosRulerSpec.queryConfig.existingSecret.name }}"
{{- else if .Values.thanosRuler.thanosRulerSpec.queryConfig.secret }}
  queryConfig:
    key: query-configs.yaml
    name: {{ template "kube-prometheus-stack.thanosRuler.name" . }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.resources }}
  resources:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.resources | indent 4 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.routePrefix }}
  routePrefix: "{{ .Values.thanosRuler.thanosRulerSpec.routePrefix }}"
{{- end }}
{{- if kindIs "bool" .Values.thanosRuler.thanosRulerSpec.hostUsers }}
  hostUsers: {{ .Values.thanosRuler.thanosRulerSpec.hostUsers }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.securityContext }}
  securityContext:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.securityContext | indent 4 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.storage }}
  storage:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.storage | indent 4 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.objectStorageConfig.existingSecret }}
  objectStorageConfig:
    key: "{{.Values.thanosRuler.thanosRulerSpec.objectStorageConfig.existingSecret.key }}"
    name: "{{.Values.thanosRuler.thanosRulerSpec.objectStorageConfig.existingSecret.name }}"
{{- else if .Values.thanosRuler.thanosRulerSpec.objectStorageConfig.secret }}
  objectStorageConfig:
    key: object-storage-configs.yaml
    name: {{ template "kube-prometheus-stack.thanosRuler.name" . }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.labels }}
  labels:
{{ tpl (toYaml .Values.thanosRuler.thanosRulerSpec.labels) $ | indent 4 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.podMetadata }}
  podMetadata:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.podMetadata | indent 4 }}
{{- end }}
{{- if or .Values.thanosRuler.thanosRulerSpec.podAntiAffinity .Values.thanosRuler.thanosRulerSpec.affinity }}
  affinity:
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.affinity }}
{{ toYaml .Values.thanosRuler.thanosRulerSpec.affinity | indent 4 }}
{{- end }}
{{- if eq .Values.thanosRuler.thanosRulerSpec.podAntiAffinity "hard" }}
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - topologyKey: {{ .Values.thanosRuler.thanosRulerSpec.podAntiAffinityTopologyKey }}
        labelSelector:
          matchExpressions:
            - {key: app.kubernetes.io/name, operator: In, values: [thanos-ruler]}
            - {key: thanos-ruler, operator: In, values: [{{ template "kube-prometheus-stack.thanosRuler.crname" . }}]}
{{- else if eq .Values.thanosRuler.thanosRulerSpec.podAntiAffinity "soft" }}
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 100
        podAffinityTerm:
          topologyKey: {{ .Values.thanosRuler.thanosRulerSpec.podAntiAffinityTopologyKey }}
          labelSelector:
            matchExpressions:
              - {key: app.kubernetes.io/name, operator: In, values: [thanos-ruler]}
              - {key: thanos-ruler, operator: In, values: [{{ template "kube-prometheus-stack.thanosRuler.crname" . }}]}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.tolerations }}
  tolerations:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.tolerations | indent 4 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.topologySpreadConstraints }}
  topologySpreadConstraints:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.topologySpreadConstraints | indent 4 }}
{{- end }}
{{- if .Values.global.imagePullSecrets }}
  imagePullSecrets:
{{ toYaml .Values.global.imagePullSecrets | indent 4 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.containers }}
  containers:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.containers | indent 4 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.initContainers }}
  initContainers:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.initContainers | indent 4 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.priorityClassName }}
  priorityClassName: {{.Values.thanosRuler.thanosRulerSpec.priorityClassName }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.volumes }}
  volumes:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.volumes | indent 4 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.volumeMounts }}
  volumeMounts:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.volumeMounts | indent 4 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.alertDropLabels }}
  alertDropLabels:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.alertDropLabels | indent 4 }}
{{- end }}
  portName: {{ .Values.thanosRuler.thanosRulerSpec.portName }}
{{- if .Values.thanosRuler.thanosRulerSpec.podManagementPolicy }}
  podManagementPolicy: {{ .Values.thanosRuler.thanosRulerSpec.podManagementPolicy }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.updateStrategy }}
  updateStrategy:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.updateStrategy | indent 4 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.terminationGracePeriodSeconds }}
  terminationGracePeriodSeconds: {{ .Values.thanosRuler.thanosRulerSpec.terminationGracePeriodSeconds }}
{{- end }}
{{- with .Values.thanosRuler.thanosRulerSpec.additionalConfig }}
  {{- tpl (toYaml .) $ | nindent 2 }}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.web }}
  web:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.web | indent 4 }}
{{- end }}
{{- with .Values.thanosRuler.thanosRulerSpec.additionalConfigString }}
  {{- tpl . $ | nindent 2 }}
{{- end }}
{{- end }}
