{{- range .Values.agentWorkers }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "attune.fullname" $ }}-agent-worker-{{ .name }}
  labels:
    {{- include "attune.labels" $ | nindent 4 }}
    app.kubernetes.io/component: agent-worker-{{ .name }}
spec:
  replicas: {{ .replicas | default 1 }}
  selector:
    matchLabels:
      {{- include "attune.selectorLabels" $ | nindent 6 }}
      app.kubernetes.io/component: agent-worker-{{ .name }}
  template:
    metadata:
      labels:
        {{- include "attune.selectorLabels" $ | nindent 8 }}
        app.kubernetes.io/component: agent-worker-{{ .name }}
    spec:
      {{- if $.Values.global.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml $.Values.global.imagePullSecrets | nindent 8 }}
      {{- end }}
      {{- if .runtimeClassName }}
      runtimeClassName: {{ .runtimeClassName }}
      {{- end }}
      {{- if .nodeSelector }}
      nodeSelector:
        {{- toYaml .nodeSelector | nindent 8 }}
      {{- end }}
      {{- if .tolerations }}
      tolerations:
        {{- toYaml .tolerations | nindent 8 }}
      {{- end }}
      {{- if .stopGracePeriod }}
      terminationGracePeriodSeconds: {{ .stopGracePeriod }}
      {{- else }}
      terminationGracePeriodSeconds: 45
      {{- end }}
      initContainers:
        - name: agent-loader
          image: {{ include "attune.image" (dict "root" $ "image" $.Values.images.agent) }}
          imagePullPolicy: {{ $.Values.images.agent.pullPolicy }}
          command: ["cp", "/usr/local/bin/attune-agent", "/opt/attune/agent/attune-agent"]
          volumeMounts:
            - name: agent-bin
              mountPath: /opt/attune/agent
        - name: wait-for-schema
          image: postgres:16-alpine
          command: ["/bin/sh", "-ec"]
          args:
            - |
              until PGPASSWORD="$DB_PASSWORD" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -tAc "SELECT to_regclass('${DB_SCHEMA}.identity')" | grep -q identity; do
                echo "waiting for schema";
                sleep 2;
              done
          envFrom:
            - secretRef:
                name: {{ include "attune.secretName" $ }}
        - name: wait-for-packs
          image: busybox:1.36
          command: ["/bin/sh", "-ec"]
          args:
            - |
              until [ -f /opt/attune/packs/core/pack.yaml ]; do
                echo "waiting for packs";
                sleep 2;
              done
          volumeMounts:
            - name: packs
              mountPath: /opt/attune/packs
      containers:
        - name: worker
          image: {{ .image }}
          {{- if .imagePullPolicy }}
          imagePullPolicy: {{ .imagePullPolicy }}
          {{- end }}
          command: ["/opt/attune/agent/attune-agent"]
          envFrom:
            - secretRef:
                name: {{ include "attune.secretName" $ }}
          env:
            - name: ATTUNE_CONFIG
              value: /opt/attune/config.yaml
            - name: ATTUNE__DATABASE__SCHEMA
              value: {{ $.Values.database.schema | quote }}
            - name: ATTUNE_WORKER_TYPE
              value: container
            - name: ATTUNE_WORKER_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: ATTUNE_API_URL
              value: http://{{ include "attune.apiServiceName" $ }}:{{ $.Values.api.service.port }}
            - name: RUST_LOG
              value: {{ .logLevel | default "info" }}
            {{- if .runtimes }}
            - name: ATTUNE_WORKER_RUNTIMES
              value: {{ join "," .runtimes | quote }}
            {{- end }}
            {{- if .env }}
            {{- toYaml .env | nindent 12 }}
            {{- end }}
          resources:
            {{- toYaml (.resources | default dict) | nindent 12 }}
          volumeMounts:
            - name: agent-bin
              mountPath: /opt/attune/agent
              readOnly: true
            - name: config
              mountPath: /opt/attune/config.yaml
              subPath: config.yaml
            - name: packs
              mountPath: /opt/attune/packs
              readOnly: true
            - name: runtime-envs
              mountPath: /opt/attune/runtime_envs
            - name: artifacts
              mountPath: /opt/attune/artifacts
      volumes:
        - name: agent-bin
          emptyDir: {}
        - name: config
          configMap:
            name: {{ include "attune.fullname" $ }}-config
        - name: packs
          persistentVolumeClaim:
            claimName: {{ include "attune.fullname" $ }}-packs
        - name: runtime-envs
          persistentVolumeClaim:
            claimName: {{ include "attune.fullname" $ }}-runtime-envs
        - name: artifacts
          persistentVolumeClaim:
            claimName: {{ include "attune.fullname" $ }}-artifacts
{{- end }}
