apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
    app.kubernetes.io/name: "central-storage"
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
{{- if .Values.logIngest.enabled }}
  annotations:
      opentelemetry.io/config-checksum: {{ include (print $.Template.BasePath "/central-storage/opentelemetry.yaml") . | sha256sum }}
{{- end }}
  name: "central-storage"
  namespace: {{ .Release.Namespace | quote }}
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: "central-storage"
      app.kubernetes.io/instance: {{ .Release.Name }}
      app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
      app.kubernetes.io/managed-by: {{ .Release.Service }}
  replicas: {{ .Values.centralStorage.replicas }}
  template:
    metadata:
      labels:
        app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
        app.kubernetes.io/name: "central-storage"
        app.kubernetes.io/managed-by: {{ .Release.Service }}
        app.kubernetes.io/instance: {{ .Release.Name }}
      annotations:
        app.kubernetes.io/config-checksum: {{ include (print $.Template.BasePath "/central-storage/central-storage-config.yaml") . | sha256sum }}
{{- if .Values.logIngest.enabled }}
        opentelemetry.io/config-checksum: {{ include (print $.Template.BasePath "/central-storage/opentelemetry.yaml") . | sha256sum }}
        sidecar.opentelemetry.io/inject: "{{ .Release.Namespace}}/{{ .Release.Name }}-opentelemetry-collector"
{{- end }}
    spec:
{{- if .Values.logIngest.enabled }}
      serviceAccountName: "{{ .Release.Name }}-otel-collector"
{{- end }}
      containers:
        - name: "central-storage"
          image: "{{ coalesce .Values.centralStorage.image.registry .Values.global.registry "docker.io"}}/{{ coalesce .Values.centralStorage.image.repository .Values.global.repository }}/{{ .Values.centralStorage.image.name }}:{{ .Values.centralStorage.image.tag | default "latest" }}"
          imagePullPolicy: {{ .Values.centralStorage.image.imagePullPolicy | default "IfNotPresent" }}
          ports:
            {{- range $port := .Values.centralStorage.ports }}
            - containerPort: {{ $port.containerPort }}
              name: {{ $port.name }}
              protocol: {{ $port.protocol }}
            {{- end }}
          {{- if .Values.centralStorage.resources }}  
          resources:
            {{- toYaml .Values.centralStorage.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.centralStorage.probes }}
          {{- if and (.Values.centralStorage.probes.liveness) (eq .Values.centralStorage.probes.liveness.type "httpGet") }}
          livenessProbe:
            httpGet:
              path: {{ .Values.centralStorage.probes.liveness.config.path }}
              port: {{ .Values.centralStorage.probes.liveness.config.port }}
            {{- if .Values.centralStorage.probes.liveness.config.initialDelaySeconds }}
            initialDelaySeconds: {{ .Values.centralStorage.probes.liveness.config.initialDelaySeconds }}
            {{- end }}
            {{- if .Values.centralStorage.probes.liveness.config.periodSeconds }}
            periodSeconds: {{ .Values.centralStorage.probes.liveness.config.periodSeconds }}
            {{- end }}
            {{- if .Values.centralStorage.probes.liveness.config.timeoutSeconds }}
            timeoutSeconds: {{ .Values.centralStorage.probes.liveness.config.timeoutSeconds }}
            {{- end }}
            {{- if .Values.centralStorage.probes.liveness.config.successThreshold }}
            successThreshold: {{ .Values.centralStorage.probes.liveness.config.successThreshold }}
            {{- end }}
            {{- if .Values.centralStorage.probes.liveness.config.failureThreshold }}
            failureThreshold: {{ .Values.centralStorage.probes.liveness.config.failureThreshold }}
            {{- end }}
            {{- if .Values.centralStorage.probes.liveness.config.terminationGracePeriodSeconds }}
            terminationGracePeriodSeconds: {{ .Values.centralStorage.probes.liveness.config.terminationGracePeriodSeconds }}
            {{- end }}
          {{- end }}
          {{- if and (.Values.centralStorage.probes.readiness) (eq .Values.centralStorage.probes.readiness.type "httpGet") }}
          readinessProbe:
            httpGet:
              path: {{ .Values.centralStorage.probes.readiness.config.path }}
              port: {{ .Values.centralStorage.probes.readiness.config.port }}
            {{- if .Values.centralStorage.probes.readiness.config.initialDelaySeconds }}
            initialDelaySeconds: {{ .Values.centralStorage.probes.readiness.config.initialDelaySeconds }}
            {{- end }}
            {{- if .Values.centralStorage.probes.readiness.config.periodSeconds }}
            periodSeconds: {{ .Values.centralStorage.probes.readiness.config.periodSeconds }}
            {{- end }}
            {{- if .Values.centralStorage.probes.readiness.config.timeoutSeconds }}
            timeoutSeconds: {{ .Values.centralStorage.probes.readiness.config.timeoutSeconds }}
            {{- end }}
            {{- if .Values.centralStorage.probes.readiness.config.successThreshold }}
            successThreshold: {{ .Values.centralStorage.probes.readiness.config.successThreshold }}
            {{- end }}
            {{- if .Values.centralStorage.probes.readiness.config.failureThreshold }}
            failureThreshold: {{ .Values.centralStorage.probes.readiness.config.failureThreshold }}
            {{- end }}
          {{- end }}
          {{- end}}
          env:
            {{- range $key, $value := .Values.centralStorage.configs }}
            - name: {{ $key | snakecase | upper }}
              valueFrom:
                secretKeyRef:
                  name: central-storage-config
                  key: {{ $key | snakecase | upper }}
            {{- end }}
{{- if .Values.logIngest.enabled }}
          volumeMounts:
            - name: app-logs
              mountPath: {{ .Values.logIngest.logPath }}
{{- end }}
{{- if .Values.logIngest.enabled }}
      volumes:
        - name: app-logs
          {{- if and (hasKey .Values.logIngest "persistence") (.Values.logIngest.persistence.enabled) }}
          persistentVolumeClaim:
            claimName: {{ .Release.Name }}-logs-pvc
          {{- else }}
          emptyDir: {}
          {{- end }}
{{- end }}