# Postgres StatefulSet.
---
apiVersion: v1
kind: StatefulSet
metadata:
  name: '{{ ansible_operator_meta.name }}-postgres'
  namespace: '{{ ansible_operator_meta.namespace }}'
  annotations: # About kube-linter checks: https://docs.kubelinter.io/#/generated/checks
    email: pulp-dev@redhat.com
    ignore-check.kube-linter.io/unset-cpu-requirements: "Temporarily disabled"
    ignore-check.kube-linter.io/unset-memory-requirements: "Temporarily disabled"
    ignore-check.kube-linter.io/read-secret-from-env-var: "Temporarily disabled"
  labels:
    app.kubernetes.io/name: 'postgres'
    app.kubernetes.io/instance: 'postgres-{{ ansible_operator_meta.name }}'
    app.kubernetes.io/component: database
    app.kubernetes.io/version: '{{ postgres_version }}'
    app.kubernetes.io/part-of: '{{ deployment_type }}'
    app.kubernetes.io/managed-by: '{{ deployment_type }}-operator'
    owner: pulp-dev
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: 'postgres'
      app.kubernetes.io/instance: 'postgres-{{ ansible_operator_meta.name }}'
      app.kubernetes.io/component: database
      app.kubernetes.io/version: '{{ postgres_version }}'
      app.kubernetes.io/part-of: '{{ deployment_type }}'
      app.kubernetes.io/managed-by: '{{ deployment_type }}-operator'
  serviceName: '{{ ansible_operator_meta.name }}-postgres-svc'
  replicas: 1
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app.kubernetes.io/name: 'postgres'
        app.kubernetes.io/instance: 'postgres-{{ ansible_operator_meta.name }}'
        app.kubernetes.io/component: database
        app.kubernetes.io/version: '{{ postgres_version }}'
        app.kubernetes.io/part-of: '{{ deployment_type }}'
        app.kubernetes.io/managed-by: '{{ deployment_type }}-operator'
    spec:
{% if image_pull_secret %}
      imagePullSecrets:
        - name: {{ image_pull_secret }}
{% endif %}
{% if affinity is defined and affinity.node_affinity is defined %}
      affinity:
        nodeAffinity: {{ affinity.node_affinity }}
{% endif %}
      serviceAccountName: '{{ ansible_operator_meta.name }}'
      containers:
        - image: '{{ _postgres_image }}'
          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: {{ postgres_port | default('5432')}}
              name: postgres
          livenessProbe:
            exec:
              command:
                - /bin/sh
                - -i
                - -c
                - pg_isready -h 127.0.0.1 -p 5432
            enabled: true
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 6
            successThreshold: 1
          readinessProbe:
            exec:
              command:
                - /bin/sh
                - -i
                - -c
                - pg_isready -h 127.0.0.1 -p 5432
            enabled: true
            initialDelaySeconds: 5
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 6
            successThreshold: 1
          volumeMounts:
            - name: postgres
              mountPath: '{{ postgres_data_path | dirname }}'
              subPath: '{{ postgres_data_path | dirname | basename }}'
          resources: {{ postgres_resource_requirements }}
  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: '{{ ansible_operator_meta.name }}-postgres-svc'
  namespace: '{{ ansible_operator_meta.namespace }}'
  labels:
    app.kubernetes.io/name: 'postgres'
    app.kubernetes.io/instance: 'postgres-{{ ansible_operator_meta.name }}'
    app.kubernetes.io/component: database
    app.kubernetes.io/version: '{{ postgres_version }}'
    app.kubernetes.io/part-of: '{{ deployment_type }}'
    app.kubernetes.io/managed-by: '{{ deployment_type }}-operator'
spec:
  ports:
    - port: 5432
  clusterIP: None
  selector:
    app.kubernetes.io/name: 'postgres'
    app.kubernetes.io/instance: 'postgres-{{ ansible_operator_meta.name }}'
    app.kubernetes.io/component: database
    app.kubernetes.io/version: '{{ postgres_version }}'
    app.kubernetes.io/part-of: '{{ deployment_type }}'
    app.kubernetes.io/managed-by: '{{ deployment_type }}-operator'
