# Postgres StatefulSet.
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: '{{ meta.name }}-postgres'
  namespace: '{{ meta.namespace }}'
  labels:
    app.kubernetes.io/name: 'postgres'
    app.kubernetes.io/instance: 'postgres-{{ meta.name }}'
    app.kubernetes.io/component: 'database'
    app.kubernetes.io/part-of: '{{ meta.name }}'
    app.kubernetes.io/managed-by: '{{ deployment_type }}-operator'
    app.kubernetes.io/operator-version: '{{ lookup("env", "OPERATOR_VERSION") }}'
    app.kubernetes.io/component: database
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: 'postgres'
      app.kubernetes.io/instance: 'postgres-{{ meta.name }}'
      app.kubernetes.io/component: 'database'
      app.kubernetes.io/managed-by: '{{ deployment_type }}-operator'
  serviceName: '{{ meta.name }}'
  replicas: 1
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app.kubernetes.io/name: 'postgres'
        app.kubernetes.io/instance: 'postgres-{{ meta.name }}'
        app.kubernetes.io/component: 'database'
        app.kubernetes.io/part-of: '{{ meta.name }}'
        app.kubernetes.io/managed-by: '{{ deployment_type }}-operator' 
    spec:
{% if image_pull_secret %}
      imagePullSecrets:
        - name: {{ image_pull_secret }}
{% endif %}
      containers:
        - image: '{{ postgres_image }}:{{ postgres_image_version }}'
          imagePullPolicy: '{{ image_pull_policy }}'
          name: postgres
          env:
            # For postgres_image based on rhel8/postgresql-12
            - name: POSTGRESQL_DATABASE
              valueFrom:
                secretKeyRef:
                  name: '{{ __postgres_configuration_secret }}'
                  key: database
            - name: POSTGRESQL_USER
              valueFrom:
                secretKeyRef:
                  name: '{{ __postgres_configuration_secret }}'
                  key: username
            - name: POSTGRESQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: '{{ __postgres_configuration_secret }}'
                  key: password

            # For postgres_image based on postgres
            - name: POSTGRES_DB
              valueFrom:
                secretKeyRef:
                  name: '{{ __postgres_configuration_secret }}'
                  key: database
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: '{{ __postgres_configuration_secret }}'
                  key: username
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: '{{ __postgres_configuration_secret }}'
                  key: password
            - name: PGDATA
              value: '{{ postgres_data_path }}'
            - name: POSTGRES_INITDB_ARGS
              value: '{{ postgres_initdb_args }}'
            - name: POSTGRES_HOST_AUTH_METHOD
              value: '{{ postgres_host_auth_method }}'
          ports:
            - containerPort: {{ awx_postgres_port | default('5432')}}
              name: postgres
          volumeMounts:
            - name: postgres
              mountPath: '{{ postgres_data_path | dirname }}'
              subPath: '{{ postgres_data_path | dirname | basename }}'
          resources: {{ postgres_resource_requirements }}
{% if postgres_selector %}
      nodeSelector:
        {{ postgres_selector | indent(width=8) }}
{% endif %}
{% if postgres_tolerations %}
      tolerations:
        {{ postgres_tolerations | indent(width=8) }}
{% endif %}
  volumeClaimTemplates:
    - metadata:
        name: postgres
      spec:
        accessModes:
          - ReadWriteOnce
{% if postgres_storage_class is defined %}
        storageClassName: '{{ postgres_storage_class }}'
{% endif %}
        resources: {{ postgres_storage_requirements }}

# Postgres Service.
---
apiVersion: v1
kind: Service
metadata:
  name: '{{ meta.name }}-postgres'
  namespace: '{{ meta.namespace }}'
  labels:
    app.kubernetes.io/name: 'postgres'
    app.kubernetes.io/instance: 'postgres-{{ meta.name }}'
    app.kubernetes.io/component: 'database'
    app.kubernetes.io/part-of: '{{ meta.name }}'
    app.kubernetes.io/managed-by: '{{ deployment_type }}-operator'
    app.kubernetes.io/operator-version: '{{ lookup("env", "OPERATOR_VERSION") }}'
    app.kubernetes.io/component: database
spec:
  ports:
    - port: 5432
  clusterIP: None
  selector:
    app.kubernetes.io/name: 'postgres'
    app.kubernetes.io/instance: 'postgres-{{ meta.name }}'
    app.kubernetes.io/component: 'database'
    app.kubernetes.io/part-of: '{{ meta.name }}'
    app.kubernetes.io/managed-by: '{{ deployment_type }}-operator'
