apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "agentlens.fullname" . }}
  labels:
    {{- include "agentlens.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "agentlens.selectorLabels" . | nindent 6 }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "agentlens.labels" . | nindent 8 }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "agentlens.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: {{ include "agentlens.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.config.port }}
              protocol: TCP
          envFrom:
            - configMapRef:
                name: {{ include "agentlens.fullname" . }}
            - secretRef:
                name: {{ include "agentlens.secretName" . }}
                optional: true
          env:
            {{- if or .Values.postgresql.enabled .Values.externalDatabase.enabled }}
            {{- if and .Values.externalDatabase.enabled .Values.externalDatabase.existingSecret }}
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.externalDatabase.existingSecret }}
                  key: {{ .Values.externalDatabase.secretKey | default "database-url" }}
            {{- else if and .Values.externalDatabase.enabled .Values.externalDatabase.url }}
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: {{ include "agentlens.fullname" . }}
                  key: database-url
            {{- else if .Values.postgresql.enabled }}
            - name: DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ default (printf "%s-postgresql" .Release.Name) .Values.postgresql.auth.existingSecret }}
                  key: password
            - name: DATABASE_URL
              value: "postgresql://{{ .Values.postgresql.auth.username }}:$(DATABASE_PASSWORD)@{{ .Release.Name }}-postgresql:5432/{{ .Values.postgresql.auth.database }}"
            {{- end }}
            {{- end }}
            {{- if and .Values.externalRedis.enabled .Values.externalRedis.existingSecret }}
            - name: REDIS_URL
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.externalRedis.existingSecret }}
                  key: {{ .Values.externalRedis.secretKey | default "redis-url" }}
            {{- else if and .Values.externalRedis.enabled .Values.externalRedis.url }}
            - name: REDIS_URL
              valueFrom:
                secretKeyRef:
                  name: {{ include "agentlens.fullname" . }}
                  key: redis-url
            {{- else if .Values.redis.enabled }}
            - name: REDIS_URL
              value: "redis://{{ .Release.Name }}-redis-master:6379"
            {{- end }}
          livenessProbe:
            {{- toYaml .Values.probes.liveness | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.probes.readiness | nindent 12 }}
          startupProbe:
            {{- toYaml .Values.probes.startup | nindent 12 }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- if .Values.persistence.enabled }}
          volumeMounts:
            - name: data
              mountPath: /app/data
          {{- end }}
      {{- if .Values.persistence.enabled }}
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (printf "%s-data" (include "agentlens.fullname" .)) }}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml . | nindent 8 }}
      {{- end }}
