{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if .Values.ruler.enabled }}
apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ include "thanos.ruler.fullname" . }}
  namespace: {{ include "common.names.namespace" . }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: ruler
    {{- if .Values.ruler.statefulsetLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.ruler.statefulsetLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if or .Values.commonAnnotations .Values.ruler.statefulsetAnnotations }}
  annotations:
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
    {{- if .Values.ruler.statefulsetAnnotations }}
    {{- include "common.tplvalues.render" (dict "value" .Values.ruler.statefulsetAnnotations "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  {{- if not (or .Values.ruler.autoscaling.enabled .Values.ruler.externalAutoscaler) }}
  replicas: {{ .Values.ruler.replicaCount }}
  {{- end }}
  revisionHistoryLimit: {{ .Values.ruler.revisionHistoryLimit }}
  podManagementPolicy: {{ .Values.ruler.podManagementPolicy }}
  serviceName: {{ include "thanos.ruler.fullname" . }}-headless
  {{- if .Values.ruler.updateStrategy }}
  updateStrategy: {{- toYaml .Values.ruler.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.ruler.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: ruler
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: ruler
      {{- if or .Values.commonAnnotations .Values.ruler.podAnnotations (include "thanos.ruler.createConfigmap" .) (include "thanos.createObjstoreSecret" .) }}
      annotations:
        {{- if .Values.commonAnnotations }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if (include "thanos.createObjstoreSecret" .) }}
        checksum/objstore-configuration: {{ include "thanos.objstoreConfig" . | sha256sum }}
        {{- end }}
        {{- if (include "thanos.ruler.createConfigmap" .) }}
        checksum/ruler-configuration: {{ include "thanos.rulerConfigMap" . | sha256sum }}
        {{- end }}
        {{- if .Values.ruler.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.ruler.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
      {{- include "thanos.imagePullSecrets" . | nindent 6 }}
      serviceAccountName: {{ include "thanos.ruler.serviceAccountName" . }}
      automountServiceAccountToken: {{ .Values.ruler.automountServiceAccountToken }}
      {{- if .Values.ruler.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.ruler.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.ruler.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.ruler.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.ruler.podAffinityPreset "component" "ruler" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.ruler.podAntiAffinityPreset "component" "ruler" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.ruler.nodeAffinityPreset.type "key" .Values.ruler.nodeAffinityPreset.key "values" .Values.ruler.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.ruler.dnsConfig }}
      dnsConfig: {{- include "common.tplvalues.render" (dict "value" .Values.ruler.dnsConfig "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.ruler.dnsPolicy }}
      dnsPolicy: {{ .Values.ruler.dnsPolicy | quote }}
      {{- end }}
      {{- if .Values.ruler.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.ruler.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.ruler.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.ruler.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.ruler.priorityClassName }}
      priorityClassName: {{ .Values.ruler.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.ruler.schedulerName }}
      schedulerName: {{ .Values.ruler.schedulerName }}
      {{- end }}
      {{- if .Values.ruler.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.ruler.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.ruler.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.ruler.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if or .Values.ruler.initContainers (and .Values.volumePermissions.enabled .Values.ruler.persistence.enabled) }}
      initContainers:
        {{- if and .Values.volumePermissions.enabled .Values.ruler.persistence.enabled }}
        - name: init-chmod-data
          image: {{ include "thanos.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - sh
            - -c
            - |
              mkdir -p /data
              chown -R "{{ .Values.ruler.containerSecurityContext.runAsUser }}:{{ .Values.ruler.podSecurityContext.fsGroup }}" /data
          securityContext:
            runAsUser: 0
          volumeMounts:
            - name: data
              mountPath: /data
        {{- end }}
        {{- if .Values.ruler.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.ruler.initContainers "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
      containers:
        {{- if .Values.ruler.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.ruler.sidecars  "context" $) | nindent 8 }}
        {{- end }}
        - name: ruler
          image: {{ include "thanos.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.ruler.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.ruler.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.ruler.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.ruler.command "context" $) | nindent 12 }}
          {{- end }}
          args:
            {{- if .Values.ruler.args }}
            {{- include "common.tplvalues.render" (dict "value" .Values.ruler.args "context" $) | nindent 12 }}
            {{- else }}
            - rule
            - --log.level={{ .Values.ruler.logLevel }}
            - --log.format={{ .Values.ruler.logFormat }}
            - --grpc-address=0.0.0.0:{{ .Values.ruler.containerPorts.grpc }}
            - --http-address=0.0.0.0:{{ .Values.ruler.containerPorts.http }}
            {{- if .Values.ruler.dataPath }}
            - --data-dir={{ .Values.ruler.dataPath }}
            {{- else }}
            - --data-dir=/data
            {{- end }}
            - --eval-interval={{ .Values.ruler.evalInterval }}
            {{- if (include "thanos.httpConfigEnabled" .) }}
            - --http.config=/conf/http/http-config.yml
            {{- end }}
            {{- range .Values.ruler.alertmanagers }}
            - --alertmanagers.url={{ . }}
            {{- end }}
            {{- if .Values.ruler.alertmanagersConfig }}
            - --alertmanagers.config-file=/conf/alertmanagers/alertmanagers_config.yml
            {{- end }}
            {{- if and .Values.query.enabled .Values.ruler.dnsDiscovery.enabled }}
            - --query=dnssrv+_http._tcp.{{ include "thanos.query.fullname" . }}.{{ .Release.Namespace }}.svc.{{ .Values.clusterDomain }}
            {{- end }}
            - --alert.query-url={{- template "thanos.ruler.queryURL" .}}
            - --label={{ .Values.ruler.replicaLabel }}="$(POD_NAME)"
            - --label=ruler_cluster="{{ .Values.ruler.clusterName }}"
            - --alert.label-drop={{ .Values.ruler.replicaLabel }}
            - --objstore.config-file=/conf/objstore/objstore.yml
            - --rule-file={{ .Values.ruler.ruleFile }}
            {{- range .Values.ruler.queries }}
            - --query={{ . }}
            {{- end }}
            {{- if .Values.ruler.extraFlags }}
            {{- include "common.tplvalues.render" (dict "value" .Values.ruler.extraFlags "context" $) | nindent 12 }}
            {{- end }}
            {{- end }}
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            {{- if .Values.ruler.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.ruler.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.ruler.extraEnvVarsCM .Values.ruler.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.ruler.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.ruler.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.ruler.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.ruler.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.ruler.containerPorts.http }}
              protocol: TCP
            - name: grpc
              containerPort: {{ .Values.ruler.containerPorts.grpc }}
              protocol: TCP
          {{- if .Values.ruler.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.ruler.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.ruler.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.ruler.livenessProbe "enabled") "context" $) | nindent 12 }}
            {{- if not .Values.auth.basicAuthUsers }}
            httpGet:
              path: /-/healthy
              port: http
              scheme: {{ ternary "HTTPS" "HTTP" .Values.https.enabled }}
            {{- else }}
            tcpSocket:
              port: http
            {{- end }}
          {{- end }}
          {{- if .Values.ruler.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.ruler.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.ruler.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.ruler.readinessProbe "enabled") "context" $) | nindent 12 }}
            {{- if not .Values.auth.basicAuthUsers }}
            httpGet:
              path: /-/ready
              port: http
              scheme: {{ ternary "HTTPS" "HTTP" .Values.https.enabled }}
            {{- else }}
            tcpSocket:
              port: http
            {{- end }}
          {{- end }}
          {{- if .Values.ruler.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.ruler.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.ruler.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.ruler.startupProbe "enabled") "context" $) | nindent 12 }}
            {{- if not .Values.auth.basicAuthUsers }}
            httpGet:
              path: /-/ready
              port: http
              scheme: {{ ternary "HTTPS" "HTTP" .Values.https.enabled }}
            {{- else }}
            tcpSocket:
              port: http
            {{- end }}
          {{- end }}
          {{- if .Values.ruler.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.ruler.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.ruler.resources }}
          resources: {{- toYaml .Values.ruler.resources | nindent 12 }}
          {{- else if ne .Values.ruler.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.ruler.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: ruler-config
              mountPath: /conf/rules
            - name: objstore-config
              mountPath: /conf/objstore
            {{- if (include "thanos.httpConfigEnabled" .) }}
            - name: http-config
              mountPath: /conf/http
            {{- if .Values.https.enabled }}
            - name: http-certs
              mountPath: /certs
            {{- end }}
            {{- end }}
            - name: data
              mountPath: /data
            {{- if .Values.ruler.alertmanagersConfig }}
            - name: alertmanagers-config
              mountPath: /conf/alertmanagers
            {{- end }}
            {{- if .Values.ruler.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.ruler.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
      volumes:
        - name: ruler-config
          configMap:
            name: {{ include "thanos.ruler.configmapName" . }}
        - name: objstore-config
          secret:
            secretName: {{ include "thanos.objstoreSecretName" . }}
            {{- if .Values.existingObjstoreSecretItems }}
            items: {{- toYaml .Values.existingObjstoreSecretItems | nindent 14 }}
            {{- end }}
        {{- if (include "thanos.httpConfigEnabled" .) }}
        - name: http-config
          secret:
            secretName: {{ include "thanos.httpConfigSecretName" . }}
        {{- if .Values.https.enabled }}
        - name: http-certs
          secret:
            secretName: {{ include "thanos.httpCertsSecretName" . }}
        {{- end }}
        {{- end }}
        {{- if .Values.ruler.alertmanagersConfig }}
        - name: alertmanagers-config
          secret:
            secretName: {{ include "thanos.ruler.fullname" . }}-alertmanagers-config
        {{- end }}
        {{- if .Values.ruler.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.ruler.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
  {{- if and .Values.ruler.persistence.enabled .Values.ruler.persistence.existingClaim }}
        - name: data
          persistentVolumeClaim:
            claimName: {{ .Values.ruler.persistence.existingClaim }}
  {{- else if not .Values.ruler.persistence.enabled }}
        - name: data
          emptyDir: {}
  {{- else if and .Values.ruler.persistence.enabled (not .Values.ruler.persistence.existingClaim) }}
  {{- if .Values.ruler.persistentVolumeClaimRetentionPolicy.enabled }}
  persistentVolumeClaimRetentionPolicy:
    whenDeleted: {{ .Values.ruler.persistentVolumeClaimRetentionPolicy.whenDeleted }}
    whenScaled: {{ .Values.ruler.persistentVolumeClaimRetentionPolicy.whenScaled }}
  {{- end }}
  volumeClaimTemplates:
    - apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: data
        {{- if .Values.ruler.persistence.annotations }}
        annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.ruler.persistence.annotations "context" $) | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
        {{- range .Values.ruler.persistence.accessModes }}
          - {{ . | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.ruler.persistence.size | quote }}
        {{- include "common.storage.class" (dict "persistence" .Values.ruler.persistence "global" .Values.global) | nindent 8 }}
  {{- end }}
{{- end }}
