apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "baserow.fullname" . }}
  namespace: {{ include "baserow.namespace" . }}
  labels:
    {{- include "baserow.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
  selector:
    matchLabels:
      {{- include "baserow.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        rollme: {{ randAlphaNum 5 | quote }}
      {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "baserow.selectorLabels" . | nindent 8 }}

    spec:
      serviceAccountName: {{ include "baserow.serviceAccountName" . }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName }}
      {{- end -}}
      {{- if or .Values.global.baserow.securityContext.enabled .Values.securityContext.enabled }}
      securityContext: {{- include "baserow.podSecurityContext" . | nindent 8 }}
      {{- end }}
      {{- if or .Values.imagePullSecrets .Values.global.baserow.imagePullSecrets }}
      imagePullSecrets: {{ include "baserow.imagePullSecrets" . }}
      {{- end }}
      {{- with .Values.volumes }}
      volumes:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        - name: {{ include "baserow.fullname" . }}
          image: {{ include "baserow.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          workingDir: {{ .Values.workingDir }}
          {{- with .Values.args }}
          args:
          {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- if .Values.service.create }}
          ports:
            - name: http
              containerPort: {{ .Values.service.targetPort }}
              protocol: TCP
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          {{- with .Values.livenessProbe }}
          livenessProbe:
            {{ toYaml . | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          {{- with .Values.readinessProbe }}
          readinessProbe:
            {{ toYaml . | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if or .Values.global.baserow.containerSecurityContext.enabled .Values.containerSecurityContext.enabled }}
          securityContext: {{- include "baserow.containerSecurityContext" . | nindent 12 }}
          {{- end }}
          {{- with .Values.resources }}
          resources: {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.volumeMounts }}
          volumeMounts:
            {{ toYaml . | nindent 12 }}
          {{- end }}
          envFrom:
            {{ include "baserow.envFrom" . | nindent 12 }}
          env:
            - name: K8S_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: K8S_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: K8S_POD_UID
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: K8S_POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: OTEL_RESOURCE_ATTRIBUTES
              value: "k8s.container.name=$(K8S_POD_NAME),k8s.node.name=$(K8S_NODE_NAME),k8s.pod.name=$(K8S_POD_NAME),k8s.pod.uid=$(K8S_POD_UID),k8s.namespace.name=$(K8S_POD_NAMESPACE),k8s.resource.type=container"
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: OTEL_EXPORTER_OTLP_ENDPOINT
              value: "http://$(HOST_IP):4318"
            {{- range .Values.extraEnv }}
            {{- if and (.name) (.value) }}
            - name: {{ .name | quote }}
              value: {{ .value | quote }}
            {{- else if and (.name) (.valueFrom) }}
            - name: {{ .name | quote }}
              valueFrom:
                {{- toYaml .valueFrom | nindent 16 }}
            {{- end }}
            {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}