---
# Source: sonarqube/charts/postgresql/templates/statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: ce-lts-postgresql
  labels:
    app: postgresql
    chart: postgresql-8.6.4
    release: "ce-lts"
    heritage: "Helm"
spec:
  serviceName: ce-lts-postgresql-headless
  replicas: 1
  updateStrategy:
    type: RollingUpdate
  selector:
    matchLabels:
      app: postgresql
      release: "ce-lts"
      role: master
  template:
    metadata:
      name: ce-lts-postgresql
      labels:
        app: postgresql
        chart: postgresql-8.6.4
        release: "ce-lts"
        heritage: "Helm"
        role: master
    spec:      
      securityContext:
        fsGroup: 1000
      initContainers:
        - name: init-chmod-data
          image: docker.io/bitnami/bitnami-shell:10
          imagePullPolicy: "IfNotPresent"
          command:
            - sh
            - -c
            - |
              mkdir -p /bitnami/postgresql/conf /bitnami/postgresql/data /bitnami/postgresql/lock
              chmod 700 /bitnami/postgresql/conf /bitnami/postgresql/data /bitnami/postgresql/lock
              find /bitnami/postgresql -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | \
                xargs chown -R 1000:1000
          securityContext: 
            runAsUser: 0
          volumeMounts:
            - name: data
              mountPath: /bitnami/postgresql
      containers:
        - name: ce-lts-postgresql
          image: docker.io/bitnami/postgresql:11.7.0-debian-10-r26
          imagePullPolicy: "IfNotPresent"
          resources:
            limits:
              cpu: 2
              memory: 2Gi
            requests:
              cpu: 100m
              memory: 200Mi
          securityContext:
            runAsUser: 1000
          env:
            - name: BITNAMI_DEBUG
              value: "false"
            - name: POSTGRESQL_PORT_NUMBER
              value: "5432"
            - name: POSTGRESQL_VOLUME_DIR
              value: "/bitnami/postgresql"
            - name: PGDATA
              value: "/bitnami/postgresql/data"
            - name: POSTGRES_USER
              value: "sonarUser"
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ce-lts-postgresql
                  key: postgresql-password
            - name: POSTGRES_DB
              value: "sonarDB"
            - name: POSTGRESQL_ENABLE_LDAP
              value: "no"
          ports:
            - name: tcp-postgresql
              containerPort: 5432
          livenessProbe:
            exec:
              command:
                - /bin/sh
                - -c
                - exec pg_isready -U "sonarUser" -d "sonarDB" -h 127.0.0.1 -p 5432
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 6
          readinessProbe:
            exec:
              command:
                - /bin/sh
                - -c
                - -e
                - |
                  exec pg_isready -U "sonarUser" -d "sonarDB" -h 127.0.0.1 -p 5432
                  [ -f /opt/bitnami/postgresql/tmp/.initialized ] || [ -f /bitnami/postgresql/.initialized ]
            initialDelaySeconds: 5
            periodSeconds: 10
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 6
          volumeMounts:
            - name: dshm
              mountPath: /dev/shm
            - name: data
              mountPath: /bitnami/postgresql
      volumes:
        - name: dshm
          emptyDir:
            medium: Memory
            sizeLimit: 1Gi
        - name: data
          persistentVolumeClaim:
            claimName: postgresql-data-claim
