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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ template "grafana-tempo.metrics-generator.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: metrics-generator
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.metricsGenerator.replicaCount }}
  {{- if .Values.metricsGenerator.updateStrategy }}
  updateStrategy: {{- toYaml .Values.metricsGenerator.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.metricsGenerator.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: metrics-generator
  serviceName: {{ template "grafana-tempo.metrics-generator.fullname" . }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/tempo-configmap.yaml") . | sha256sum }}
        {{- if .Values.metricsGenerator.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: metrics-generator
        tempo-gossip-member: "true"
    spec:
      serviceAccountName: {{ template "grafana-tempo.serviceAccountName" . }}
      {{- include "grafana-tempo.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.metricsGenerator.automountServiceAccountToken }}
      {{- if .Values.metricsGenerator.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.metricsGenerator.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.metricsGenerator.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.metricsGenerator.podAffinityPreset "component" "metrics-generator" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.metricsGenerator.podAntiAffinityPreset "component" "metrics-generator" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.metricsGenerator.nodeAffinityPreset.type "key" .Values.metricsGenerator.nodeAffinityPreset.key "values" .Values.metricsGenerator.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.metricsGenerator.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.metricsGenerator.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.metricsGenerator.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.metricsGenerator.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.metricsGenerator.priorityClassName }}
      priorityClassName: {{ .Values.metricsGenerator.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.metricsGenerator.schedulerName }}
      schedulerName: {{ .Values.metricsGenerator.schedulerName }}
      {{- end }}
      {{- if .Values.metricsGenerator.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.metricsGenerator.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      enableServiceLinks: {{ .Values.metricsGenerator.enableServiceLinks }}
      initContainers:
        {{- if .Values.metricsGenerator.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.initContainers "context" $) | nindent 8 }}
        {{- end }}
        {{- if and .Values.volumePermissions.enabled .Values.metricsGenerator.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "grafana-tempo.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              mkdir -p {{ .Values.tempo.dataDir }}{{- if .Values.metricsGenerator.persistence.subPath }}/{{ .Values.metricsGenerator.persistence.subPath }}{{- end }}
              {{- if and .Values.metricsGenerator.podSecurityContext.enabled .Values.metricsGenerator.containerSecurityContext.enabled }}
              find {{ .Values.tempo.dataDir }}{{- if .Values.metricsGenerator.persistence.subPath }}/{{ .Values.metricsGenerator.persistence.subPath }}{{- end }} -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | xargs -r chown -R {{ .Values.metricsGenerator.containerSecurityContext.runAsUser }}:{{ .Values.metricsGenerator.podSecurityContext.fsGroup }}
              {{- end }}
          {{- if eq ( toString ( .Values.volumePermissions.containerSecurityContext.runAsUser )) "auto" }}
          securityContext: {{- omit .Values.volumePermissions.containerSecurityContext "runAsUser" | toYaml | nindent 12 }}
          {{- else }}
          securityContext: {{- .Values.volumePermissions.containerSecurityContext | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.volumePermissions.resources }}
          resources: {{- include "common.tplvalues.render" (dict "value" .Values.volumePermissions.resources "context" $) | nindent 12 }}
          {{- else if ne .Values.volumePermissions.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.volumePermissions.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: data
              mountPath: {{ .Values.tempo.dataDir }}
        {{- end }}
      containers:
        - name: grafana-tempo-metrics-generator
          image: {{ template "grafana-tempo.image" . }}
          imagePullPolicy: {{ .Values.tempo.image.pullPolicy }}
          {{- if .Values.metricsGenerator.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.metricsGenerator.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.metricsGenerator.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.metricsGenerator.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - -target=metrics-generator
            - -config.file=/bitnami/grafana-tempo/conf/tempo.yaml
            - -mem-ballast-size-mbs={{ .Values.tempo.memBallastSizeMbs }}
          {{- end }}
          {{- if .Values.metricsGenerator.extraEnvVars }}
          env: {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.extraEnvVars "context" $) | nindent 12 }}
          {{- end }}
          envFrom:
            {{- if .Values.metricsGenerator.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.metricsGenerator.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - containerPort: {{ .Values.tempo.containerPorts.web }}
              name: http
            - containerPort: {{ .Values.tempo.containerPorts.gossipRing }}
              name: http-memberlist
            - containerPort: {{ .Values.tempo.containerPorts.grpc }}
              name: grpc
          {{- if .Values.metricsGenerator.resources }}
          resources: {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.resources "context" $) | nindent 12 }}
          {{- else if ne .Values.metricsGenerator.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.metricsGenerator.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.metricsGenerator.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.metricsGenerator.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.metricsGenerator.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: {{ .Values.tempo.containerPorts.web }}
          {{- end }}
          {{- if .Values.metricsGenerator.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.metricsGenerator.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.metricsGenerator.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /ready
              port: {{ .Values.tempo.containerPorts.web }}
          {{- end }}
          {{- if .Values.metricsGenerator.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.metricsGenerator.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.metricsGenerator.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- end }}
          {{- if .Values.metricsGenerator.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: tempo-config
              mountPath: /bitnami/grafana-tempo/conf/tempo.yaml
              subPath: tempo.yaml
            - name: overrides-config
              mountPath: /bitnami/grafana-tempo/conf/overrides.yaml
              subPath: overrides.yaml
            - name: data
              mountPath: {{ .Values.tempo.dataDir }}
          {{- if .Values.metricsGenerator.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.metricsGenerator.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.metricsGenerator.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: tempo-config
          configMap:
            name: {{ template "grafana-tempo.tempoConfigmapName" . }}
        - name: overrides-config
          configMap:
            name: {{ template "grafana-tempo.overridesConfigmapName" . }}
        {{- if .Values.metricsGenerator.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
  {{- if not .Values.metricsGenerator.persistence.enabled }}
        - name: data
          emptyDir: {}
  {{- else }}
  volumeClaimTemplates:
    - metadata:
        name: data
        {{- if or .Values.metricsGenerator.persistence.annotations .Values.commonAnnotations }}
        {{- $claimAnnotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.metricsGenerator.persistence.annotations .Values.commonAnnotations ) "context" . ) }}
        annotations: {{- include "common.tplvalues.render" ( dict "value" $claimAnnotations "context" $) | nindent 10 }}
        {{- end }}
        {{- if .Values.commonLabels }}
        labels: {{- include "common.tplvalues.render" (dict "value" .Values.commonLabels "context" $) | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
        {{- range .Values.metricsGenerator.persistence.accessModes }}
          - {{ . | quote }}
        {{- end }}
        resources:
          requests:
            storage: {{ .Values.metricsGenerator.persistence.size | quote }}
        {{- if .Values.metricsGenerator.persistence.selector }}
        selector: {{- include "common.tplvalues.render" (dict "value" .Values.metricsGenerator.persistence.selector "context" $) | nindent 10 }}
        {{- end }}
        {{- include "common.storage.class" (dict "persistence" .Values.metricsGenerator.persistence "global" .Values.global) | nindent 8 }}
  {{- end }}
