{{- if .Values.database.postgresql.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "attune.postgresqlServiceName" . }}
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  selector:
    {{- include "attune.componentLabels" (dict "root" . "component" "postgresql") | nindent 4 }}
  ports:
    - name: postgres
      port: {{ .Values.database.port }}
      targetPort: postgres
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "attune.postgresqlServiceName" . }}
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  serviceName: {{ include "attune.postgresqlServiceName" . }}
  replicas: 1
  selector:
    matchLabels:
      {{- include "attune.componentLabels" (dict "root" . "component" "postgresql") | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "attune.componentLabels" (dict "root" . "component" "postgresql") | nindent 8 }}
    spec:
      containers:
        - name: postgresql
          image: "{{ .Values.database.postgresql.image.repository }}:{{ .Values.database.postgresql.image.tag }}"
          imagePullPolicy: IfNotPresent
          env:
            - name: POSTGRES_USER
              value: {{ .Values.database.username | quote }}
            - name: POSTGRES_PASSWORD
              value: {{ .Values.database.password | quote }}
            - name: POSTGRES_DB
              value: {{ .Values.database.database | quote }}
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          ports:
            - name: postgres
              containerPort: 5432
          livenessProbe:
            exec:
              command: ["pg_isready", "-U", "{{ .Values.database.username }}"]
            initialDelaySeconds: 20
            periodSeconds: 10
          readinessProbe:
            exec:
              command: ["pg_isready", "-U", "{{ .Values.database.username }}"]
            initialDelaySeconds: 10
            periodSeconds: 10
          resources:
            {{- toYaml .Values.database.postgresql.resources | nindent 12 }}
          volumeMounts:
            - name: data
              mountPath: /var/lib/postgresql/data
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          {{- toYaml .Values.database.postgresql.persistence.accessModes | nindent 10 }}
        resources:
          requests:
            storage: {{ .Values.database.postgresql.persistence.size }}
        {{- if .Values.database.postgresql.persistence.storageClassName }}
        storageClassName: {{ .Values.database.postgresql.persistence.storageClassName }}
        {{- end }}
{{- end }}
{{- if .Values.rabbitmq.enabled }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "attune.rabbitmqServiceName" . }}
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  selector:
    {{- include "attune.componentLabels" (dict "root" . "component" "rabbitmq") | nindent 4 }}
  ports:
    - name: amqp
      port: {{ .Values.rabbitmq.port }}
      targetPort: amqp
    - name: management
      port: {{ .Values.rabbitmq.managementPort }}
      targetPort: management
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "attune.rabbitmqServiceName" . }}
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  serviceName: {{ include "attune.rabbitmqServiceName" . }}
  replicas: 1
  selector:
    matchLabels:
      {{- include "attune.componentLabels" (dict "root" . "component" "rabbitmq") | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "attune.componentLabels" (dict "root" . "component" "rabbitmq") | nindent 8 }}
    spec:
      containers:
        - name: rabbitmq
          image: "{{ .Values.rabbitmq.image.repository }}:{{ .Values.rabbitmq.image.tag }}"
          imagePullPolicy: IfNotPresent
          env:
            - name: RABBITMQ_DEFAULT_USER
              value: {{ .Values.rabbitmq.username | quote }}
            - name: RABBITMQ_DEFAULT_PASS
              value: {{ .Values.rabbitmq.password | quote }}
            - name: RABBITMQ_DEFAULT_VHOST
              value: /
          ports:
            - name: amqp
              containerPort: 5672
            - name: management
              containerPort: 15672
          livenessProbe:
            exec:
              command: ["rabbitmq-diagnostics", "-q", "ping"]
            initialDelaySeconds: 20
            periodSeconds: 15
          readinessProbe:
            exec:
              command: ["rabbitmq-diagnostics", "-q", "ping"]
            initialDelaySeconds: 10
            periodSeconds: 10
          resources:
            {{- toYaml .Values.rabbitmq.resources | nindent 12 }}
          volumeMounts:
            - name: data
              mountPath: /var/lib/rabbitmq
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          {{- toYaml .Values.rabbitmq.persistence.accessModes | nindent 10 }}
        resources:
          requests:
            storage: {{ .Values.rabbitmq.persistence.size }}
        {{- if .Values.rabbitmq.persistence.storageClassName }}
        storageClassName: {{ .Values.rabbitmq.persistence.storageClassName }}
        {{- end }}
{{- end }}
{{- if .Values.redis.enabled }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ include "attune.redisServiceName" . }}
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  selector:
    {{- include "attune.componentLabels" (dict "root" . "component" "redis") | nindent 4 }}
  ports:
    - name: redis
      port: {{ .Values.redis.port }}
      targetPort: redis
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "attune.redisServiceName" . }}
  labels:
    {{- include "attune.labels" . | nindent 4 }}
spec:
  serviceName: {{ include "attune.redisServiceName" . }}
  replicas: 1
  selector:
    matchLabels:
      {{- include "attune.componentLabels" (dict "root" . "component" "redis") | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "attune.componentLabels" (dict "root" . "component" "redis") | nindent 8 }}
    spec:
      containers:
        - name: redis
          image: "{{ .Values.redis.image.repository }}:{{ .Values.redis.image.tag }}"
          imagePullPolicy: IfNotPresent
          command: ["redis-server", "--appendonly", "yes"]
          ports:
            - name: redis
              containerPort: 6379
          livenessProbe:
            exec:
              command: ["redis-cli", "ping"]
            initialDelaySeconds: 15
            periodSeconds: 10
          readinessProbe:
            exec:
              command: ["redis-cli", "ping"]
            initialDelaySeconds: 10
            periodSeconds: 10
          resources:
            {{- toYaml .Values.redis.resources | nindent 12 }}
          volumeMounts:
            - name: data
              mountPath: /data
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes:
          {{- toYaml .Values.redis.persistence.accessModes | nindent 10 }}
        resources:
          requests:
            storage: {{ .Values.redis.persistence.size }}
        {{- if .Values.redis.persistence.storageClassName }}
        storageClassName: {{ .Values.redis.persistence.storageClassName }}
        {{- end }}
{{- end }}
