# grafana_charts_deployment-ruler.yaml
{{- if and (eq .Values.ruler.kind "Deployment") .Values.ruler.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "loki.rulerFullname" . }}
  labels:
    {{- include "loki.rulerLabels" . | nindent 4 }}
    app.kubernetes.io/part-of: memberlist
  {{- with .Values.loki.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.ruler.replicas }}
  strategy:
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
  revisionHistoryLimit: {{ .Values.loki.revisionHistoryLimit }}
  selector:
    matchLabels:
      {{- include "loki.rulerSelectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        {{- include "loki.config.checksum" . | nindent 8 }}
        {{- with .Values.loki.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.ruler.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "loki.rulerSelectorLabels" . | nindent 8 }}
        app.kubernetes.io/part-of: memberlist
        {{- with .Values.loki.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.ruler.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ include "loki.serviceAccountName" . }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.ruler.hostAliases }}
      hostAliases:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- include "loki.rulerPriorityClassName" . | nindent 6 }}
      securityContext:
        {{- toYaml .Values.loki.podSecurityContext | nindent 8 }}
      terminationGracePeriodSeconds: {{ .Values.ruler.terminationGracePeriodSeconds }}
      {{- with .Values.ruler.initContainers }}
      initContainers:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: ruler
          image: {{ include "loki.rulerImage" . }}
          imagePullPolicy: {{ .Values.loki.image.pullPolicy }}
          {{- if or .Values.loki.command .Values.ruler.command }}
          command:
            - {{ coalesce .Values.ruler.command .Values.loki.command | quote }}
          {{- end }}
          args:
            - -config.file=/etc/loki/config/config.yaml
            - -target=ruler
            {{- with .Values.ruler.extraArgs }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          ports:
            - name: http
              containerPort: 3100
              protocol: TCP
            - name: grpc
              containerPort: 9095
              protocol: TCP
            - name: http-memberlist
              containerPort: 7946
              protocol: TCP
          {{- with .Values.ruler.extraEnv }}
          env:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.ruler.extraEnvFrom }}
          envFrom:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          securityContext:
            {{- toYaml .Values.loki.containerSecurityContext | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.loki.readinessProbe | nindent 12 }}
          livenessProbe:
            {{- toYaml .Values.loki.livenessProbe | nindent 12 }}
          volumeMounts:
            - name: config
              mountPath: /etc/loki/config
            - name: runtime-config
              mountPath: /var/{{ include "loki.name" . }}-runtime
            - name: data
              mountPath: /var/loki
            - name: tmp
              mountPath: /tmp/loki
            {{- range $dir, $_ := .Values.ruler.directories }}
            - name: {{ include "loki.rulerRulesDirName" $dir }}
              mountPath: /etc/loki/rules/{{ $dir }}
            {{- end }}
            {{- with .Values.ruler.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          resources:
            {{- toYaml .Values.ruler.resources | nindent 12 }}
        {{- if .Values.ruler.extraContainers }}
        {{- toYaml .Values.ruler.extraContainers | nindent 8}}
        {{- end }}
      {{- with .Values.ruler.affinity }}
      affinity:
        {{- tpl . $ | nindent 8 }}
      {{- end }}
      {{- with .Values.ruler.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.ruler.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.ruler.dnsConfig }}
      dnsConfig:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        - name: config
          {{- if .Values.loki.existingSecretForConfig }}
          secret:
            secretName: {{ .Values.loki.existingSecretForConfig }}
          {{- else if .Values.loki.configAsSecret }}
          secret:
            secretName: {{ include "loki.fullname" . }}-config
          {{- else }}
          configMap:
            name: {{ include "loki.fullname" . }}
          {{- end }}
        - name: runtime-config
          configMap:
            name: {{ template "loki.fullname" . }}-runtime
        {{- range $dir, $_ := .Values.ruler.directories }}
        - name: {{ include "loki.rulerRulesDirName" $dir }}
          configMap:
            name: {{ include "loki.rulerFullname" $ }}-{{ include "loki.rulerRulesDirName" $dir }}
        {{- end }}
        - name: tmp
          emptyDir: {}
        - name: data
          {{- if .Values.ruler.persistence.enabled }}
          persistentVolumeClaim:
            claimName: data-{{ include "loki.rulerFullname" . }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        {{- with .Values.ruler.extraVolumes }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
{{- end }}