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

{{- if .Values.tableManager.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "grafana-loki.table-manager.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: grafana-loki
    app.kubernetes.io/component: table-manager
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.tableManager.replicaCount }}
  {{- if .Values.tableManager.updateStrategy }}
  strategy: {{- toYaml .Values.tableManager.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.tableManager.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: grafana-loki
      app.kubernetes.io/component: table-manager
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/loki-configmap.yaml") . | sha256sum }}
        {{- if .Values.tableManager.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: grafana-loki
        app.kubernetes.io/component: table-manager
        loki-gossip-member: "true"
    spec:
      serviceAccountName: {{ template "grafana-loki.serviceAccountName" . }}
      {{- include "grafana-loki.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.tableManager.automountServiceAccountToken }}
      {{- if .Values.tableManager.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tableManager.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.tableManager.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.tableManager.podAffinityPreset "component" "tableManager" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.tableManager.podAntiAffinityPreset "component" "tableManager" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.tableManager.nodeAffinityPreset.type "key" .Values.tableManager.nodeAffinityPreset.key "values" .Values.tableManager.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.tableManager.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.tableManager.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tableManager.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.tableManager.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.tableManager.priorityClassName }}
      priorityClassName: {{ .Values.tableManager.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.tableManager.schedulerName }}
      schedulerName: {{ .Values.tableManager.schedulerName }}
      {{- end }}
      {{- if .Values.tableManager.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.tableManager.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      enableServiceLinks: {{ .Values.tableManager.enableServiceLinks }}
      initContainers:
        {{- if .Values.tableManager.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: grafana-loki-table-manager
          image: {{ template "grafana-loki.image" . }}
          imagePullPolicy: {{ .Values.loki.image.pullPolicy }}
          {{- if .Values.tableManager.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.tableManager.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.tableManager.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.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.tableManager.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - -target=table-manager
            - -config.file={{ .Values.loki.dataDir }}/conf/loki.yaml
          {{- if .Values.tableManager.extraArgs }}
          {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.extraArgs "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.tableManager.extraEnvVars }}
          env: {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.extraEnvVars "context" $) | nindent 12 }}
          {{- end }}
          envFrom:
            {{- if .Values.tableManager.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.tableManager.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.tableManager.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.tableManager.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - containerPort: {{ .Values.loki.containerPorts.http }}
              name: http
            - containerPort: {{ .Values.loki.containerPorts.grpc }}
              name: grpc
          {{- if .Values.tableManager.resources }}
          resources: {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.resources "context" $) | nindent 12 }}
          {{- else if ne .Values.tableManager.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.tableManager.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.tableManager.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.tableManager.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.tableManager.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.tableManager.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.tableManager.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.tableManager.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /ready
              port: http
          {{- end }}
          {{- if .Values.tableManager.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.tableManager.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.tableManager.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- end }}
          {{- if .Values.tableManager.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: loki-config
              mountPath: {{ .Values.loki.dataDir }}/conf/loki.yaml
              subPath: loki.yaml
          {{- if .Values.tableManager.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.tableManager.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.tableManager.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: loki-config
          configMap:
            name: {{ template "grafana-loki.loki.configmapName" . }}
        {{- if .Values.tableManager.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.tableManager.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
