apiVersion: v1
kind: Service
metadata:
  name: {{ include "attune.apiServiceName" . }}
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  type: {{ .Values.api.service.type }}
  selector:
    {{- include "attune.componentLabels" (dict "root" . "component" "api") | nindent 4 }}
  ports:
    - name: http
      port: {{ .Values.api.service.port }}
      targetPort: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "attune.apiServiceName" . }}
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.api.replicaCount }}
  selector:
    matchLabels:
      {{- include "attune.componentLabels" (dict "root" . "component" "api") | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "attune.componentLabels" (dict "root" . "component" "api") | nindent 8 }}
    spec:
      {{- if .Values.global.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml .Values.global.imagePullSecrets | nindent 8 }}
      {{- end }}
      initContainers:
        - 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: api
          image: {{ include "attune.image" (dict "root" . "image" .Values.images.api) }}
          imagePullPolicy: {{ .Values.images.api.pullPolicy }}
          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__WORKER_TYPE
              value: container
          ports:
            - name: http
              containerPort: 8080
          readinessProbe:
            httpGet:
              path: /health
              port: http
            initialDelaySeconds: 10
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /health
              port: http
            initialDelaySeconds: 20
            periodSeconds: 15
          resources:
            {{- toYaml .Values.api.resources | nindent 12 }}
          volumeMounts:
            - name: config
              mountPath: /opt/attune/config.yaml
              subPath: config.yaml
            - name: packs
              mountPath: /opt/attune/packs
            - name: runtime-envs
              mountPath: /opt/attune/runtime_envs
            - name: artifacts
              mountPath: /opt/attune/artifacts
      volumes:
        - 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
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "attune.fullname" . }}-executor
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.executor.replicaCount }}
  selector:
    matchLabels:
      {{- include "attune.componentLabels" (dict "root" . "component" "executor") | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "attune.componentLabels" (dict "root" . "component" "executor") | nindent 8 }}
    spec:
      {{- if .Values.global.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml .Values.global.imagePullSecrets | nindent 8 }}
      {{- end }}
      initContainers:
        - 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: executor
          image: {{ include "attune.image" (dict "root" . "image" .Values.images.executor) }}
          imagePullPolicy: {{ .Values.images.executor.pullPolicy }}
          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__WORKER_TYPE
              value: container
          resources:
            {{- toYaml .Values.executor.resources | nindent 12 }}
          volumeMounts:
            - name: config
              mountPath: /opt/attune/config.yaml
              subPath: config.yaml
            - name: packs
              mountPath: /opt/attune/packs
            - name: artifacts
              mountPath: /opt/attune/artifacts
      volumes:
        - name: config
          configMap:
            name: {{ include "attune.fullname" . }}-config
        - name: packs
          persistentVolumeClaim:
            claimName: {{ include "attune.fullname" . }}-packs
        - name: artifacts
          persistentVolumeClaim:
            claimName: {{ include "attune.fullname" . }}-artifacts
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "attune.fullname" . }}-worker
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.worker.replicaCount }}
  selector:
    matchLabels:
      {{- include "attune.componentLabels" (dict "root" . "component" "worker") | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "attune.componentLabels" (dict "root" . "component" "worker") | nindent 8 }}
    spec:
      {{- if .Values.global.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml .Values.global.imagePullSecrets | nindent 8 }}
      {{- end }}
      initContainers:
        - 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: {{ include "attune.image" (dict "root" . "image" .Values.images.worker) }}
          imagePullPolicy: {{ .Values.images.worker.pullPolicy }}
          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_RUNTIMES
              value: {{ .Values.worker.runtimes | quote }}
            - name: ATTUNE_WORKER_TYPE
              value: container
            - name: ATTUNE_WORKER_NAME
              value: {{ .Values.worker.name | quote }}
            - name: ATTUNE_API_URL
              value: http://{{ include "attune.apiServiceName" . }}:{{ .Values.api.service.port }}
          resources:
            {{- toYaml .Values.worker.resources | nindent 12 }}
          volumeMounts:
            - name: config
              mountPath: /opt/attune/config.yaml
              subPath: config.yaml
            - name: packs
              mountPath: /opt/attune/packs
            - name: runtime-envs
              mountPath: /opt/attune/runtime_envs
            - name: artifacts
              mountPath: /opt/attune/artifacts
      volumes:
        - 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
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "attune.fullname" . }}-sensor
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.sensor.replicaCount }}
  selector:
    matchLabels:
      {{- include "attune.componentLabels" (dict "root" . "component" "sensor") | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "attune.componentLabels" (dict "root" . "component" "sensor") | nindent 8 }}
    spec:
      {{- if .Values.global.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml .Values.global.imagePullSecrets | nindent 8 }}
      {{- end }}
      terminationGracePeriodSeconds: 45
      initContainers:
        - name: sensor-agent-loader
          image: {{ include "attune.image" (dict "root" . "image" .Values.images.agent) }}
          imagePullPolicy: {{ .Values.images.agent.pullPolicy }}
          command: ["cp", "/usr/local/bin/attune-sensor-agent", "/opt/attune/agent/attune-sensor-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: sensor
          image: {{ include "attune.image" (dict "root" . "image" .Values.images.sensor) }}
          imagePullPolicy: {{ .Values.images.sensor.pullPolicy }}
          command: ["/opt/attune/agent/attune-sensor-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__WORKER_TYPE
              value: container
            - name: ATTUNE_SENSOR_RUNTIMES
              value: {{ .Values.sensor.runtimes | quote }}
            - name: ATTUNE_API_URL
              value: http://{{ include "attune.apiServiceName" . }}:{{ .Values.api.service.port }}
            - name: ATTUNE_MQ_URL
              value: {{ include "attune.rabbitmqUrl" . | quote }}
            - name: ATTUNE_PACKS_BASE_DIR
              value: /opt/attune/packs
            - name: RUST_LOG
              value: {{ .Values.sensor.logLevel | quote }}
          resources:
            {{- toYaml .Values.sensor.resources | 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
      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
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "attune.notifierServiceName" . }}
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  type: {{ .Values.notifier.service.type }}
  selector:
    {{- include "attune.componentLabels" (dict "root" . "component" "notifier") | nindent 4 }}
  ports:
    - name: ws
      port: {{ .Values.notifier.service.port }}
      targetPort: ws
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "attune.notifierServiceName" . }}
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.notifier.replicaCount }}
  selector:
    matchLabels:
      {{- include "attune.componentLabels" (dict "root" . "component" "notifier") | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "attune.componentLabels" (dict "root" . "component" "notifier") | nindent 8 }}
    spec:
      {{- if .Values.global.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml .Values.global.imagePullSecrets | nindent 8 }}
      {{- end }}
      initContainers:
        - 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" . }}
      containers:
        - name: notifier
          image: {{ include "attune.image" (dict "root" . "image" .Values.images.notifier) }}
          imagePullPolicy: {{ .Values.images.notifier.pullPolicy }}
          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__WORKER_TYPE
              value: container
          ports:
            - name: ws
              containerPort: 8081
          readinessProbe:
            httpGet:
              path: /health
              port: ws
            initialDelaySeconds: 10
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /health
              port: ws
            initialDelaySeconds: 20
            periodSeconds: 15
          resources:
            {{- toYaml .Values.notifier.resources | nindent 12 }}
          volumeMounts:
            - name: config
              mountPath: /opt/attune/config.yaml
              subPath: config.yaml
      volumes:
        - name: config
          configMap:
            name: {{ include "attune.fullname" . }}-config
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "attune.fullname" . }}-web
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  type: {{ .Values.web.service.type }}
  selector:
    {{- include "attune.componentLabels" (dict "root" . "component" "web") | nindent 4 }}
  ports:
    - name: http
      port: {{ .Values.web.service.port }}
      targetPort: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "attune.fullname" . }}-web
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.web.replicaCount }}
  selector:
    matchLabels:
      {{- include "attune.componentLabels" (dict "root" . "component" "web") | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "attune.componentLabels" (dict "root" . "component" "web") | nindent 8 }}
    spec:
      {{- if .Values.global.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml .Values.global.imagePullSecrets | nindent 8 }}
      {{- end }}
      containers:
        - name: web
          image: {{ include "attune.image" (dict "root" . "image" .Values.images.web) }}
          imagePullPolicy: {{ .Values.images.web.pullPolicy }}
          env:
            - name: API_URL
              value: {{ .Values.web.config.apiUrl | quote }}
            - name: WS_URL
              value: {{ .Values.web.config.wsUrl | quote }}
            - name: ENVIRONMENT
              value: {{ .Values.web.config.environment | quote }}
          ports:
            - name: http
              containerPort: 80
          readinessProbe:
            httpGet:
              path: /health
              port: http
            initialDelaySeconds: 10
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /health
              port: http
            initialDelaySeconds: 20
            periodSeconds: 15
          resources:
            {{- toYaml .Values.web.resources | nindent 12 }}
