{{- if .Values.thanosRuler.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ThanosRuler
metadata:
  name: {{ template "kube-prometheus-stack.fullname" . }}-thanos-ruler
  namespace: {{ template "kube-prometheus-stack.namespace" . }}
  labels:
    app: {{ template "kube-prometheus-stack.name" . }}-thanos-ruler
{{ 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 }}
  {{- if and .Values.thanosRuler.thanosRulerSpec.image.tag .Values.thanosRuler.thanosRulerSpec.image.sha }}
  image: "{{ .Values.thanosRuler.thanosRulerSpec.image.repository }}:{{ .Values.thanosRuler.thanosRulerSpec.image.tag }}@sha256:{{ .Values.thanosRuler.thanosRulerSpec.image.sha }}"
  {{- else if .Values.thanosRuler.thanosRulerSpec.image.sha }}
  image: "{{ .Values.thanosRuler.thanosRulerSpec.image.repository }}@sha256:{{ .Values.thanosRuler.thanosRulerSpec.image.sha }}"
  {{- else if .Values.thanosRuler.thanosRulerSpec.image.tag }}
  image: "{{ .Values.thanosRuler.thanosRulerSpec.image.repository }}:{{ .Values.thanosRuler.thanosRulerSpec.image.tag }}"
  {{- else }}
  image: "{{ .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 }}
  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 }}
  externalPrefix: http://{{ template "kube-prometheus-stack.fullname" . }}-thanosRuler.{{ template "kube-prometheus-stack.namespace" . }}:{{ .Values.thanosRuler.service.port }}
{{- 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.ruleNamespaceSelector }}
  ruleNamespaceSelector:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.ruleNamespaceSelector | indent 4 }}
{{ else }}
  ruleNamespaceSelector: {}
{{- end }}
{{- if .Values.thanosRuler.thanosRulerSpec.ruleSelector }}
  ruleSelector:
{{ 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.queryEndpoints }}
  queryEndpoints:
{{ toYaml .Values.thanosRuler.thanosRulerSpec.queryEndpoints | indent 4 }}
{{- 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 .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.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.fullname" . }}-thanos-ruler]}
{{- 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.fullname" . }}-thanos-ruler]}
{{- 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 }}
  portName: {{ .Values.thanosRuler.thanosRulerSpec.portName }}
{{- end }}
