# headless service to avoid deletion of patronidemo-config endpoint
apiVersion: v1
kind: Service
metadata:
  name: patronidemo-config
  labels:
    application: patroni
    cluster-name: patronidemo
spec:
  clusterIP: None

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: &cluster_name patronidemo
  labels:
    application: patroni
    cluster-name: *cluster_name
spec:
  replicas: 3
  serviceName: *cluster_name
  selector:
    matchLabels:
      application: patroni
      cluster-name: *cluster_name
  template:
    metadata:
      labels:
        application: patroni
        cluster-name: *cluster_name
    spec:
      serviceAccountName: patronidemo
      containers:
      - name: *cluster_name
        image: patroni  # docker build -t patroni .
        imagePullPolicy: IfNotPresent
        readinessProbe:
          httpGet:
            scheme: HTTP
            path: /readiness
            port: 8008
          initialDelaySeconds: 3
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
        ports:
        - containerPort: 8008
          protocol: TCP
        - containerPort: 5432
          protocol: TCP
        volumeMounts:
        - mountPath: /home/postgres/pgdata
          name: pgdata
        env:
        - name: PATRONI_KUBERNETES_POD_IP
          valueFrom:
            fieldRef:
              fieldPath: status.podIP
        - name: PATRONI_KUBERNETES_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: PATRONI_KUBERNETES_BYPASS_API_SERVICE
          value: 'true'
        - name: PATRONI_KUBERNETES_USE_ENDPOINTS
          value: 'true'
        - name: PATRONI_KUBERNETES_LABELS
          value: '{application: patroni, cluster-name: patronidemo}'
        - name: PATRONI_SUPERUSER_USERNAME
          value: postgres
        - name: PATRONI_SUPERUSER_PASSWORD
          valueFrom:
            secretKeyRef:
              name: *cluster_name
              key: superuser-password
        - name: PATRONI_REPLICATION_USERNAME
          value: standby
        - name: PATRONI_REPLICATION_PASSWORD
          valueFrom:
            secretKeyRef:
              name: *cluster_name
              key: replication-password
        - name: PATRONI_SCOPE
          value: *cluster_name
        - name: PATRONI_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: PATRONI_POSTGRESQL_DATA_DIR
          value: /home/postgres/pgdata/pgroot/data
        - name: PATRONI_POSTGRESQL_PGPASS
          value: /tmp/pgpass
        - name: PATRONI_POSTGRESQL_LISTEN
          value: '0.0.0.0:5432'
        - name: PATRONI_RESTAPI_LISTEN
          value: '0.0.0.0:8008'
      terminationGracePeriodSeconds: 0
      volumes:
      - name: pgdata
        emptyDir: {}
#  volumeClaimTemplates:
#  - metadata:
#      labels:
#        application: spilo
#        spilo-cluster: *cluster_name
#      annotations:
#        volume.alpha.kubernetes.io/storage-class: anything
#      name: pgdata
#    spec:
#      accessModes:
#      - ReadWriteOnce
#      resources:
#        requests:
#          storage: 5Gi

---
apiVersion: v1
kind: Endpoints
metadata:
  name: &cluster_name patronidemo
  labels:
    application: patroni
    cluster-name: *cluster_name
subsets: []

---
apiVersion: v1
kind: Service
metadata:
  name: &cluster_name patronidemo
  labels:
    application: patroni
    cluster-name: *cluster_name
spec:
  type: ClusterIP
  ports:
  - port: 5432
    targetPort: 5432

---
apiVersion: v1
kind: Service
metadata:
  name: patronidemo-repl
  labels:
    application: patroni
    cluster-name:  &cluster_name patronidemo
    role: replica
spec:
  type: ClusterIP
  selector:
    application: patroni
    cluster-name: *cluster_name
    role: replica
  ports:
  - port: 5432
    targetPort: 5432

---
apiVersion: v1
kind: Secret
metadata:
  name: &cluster_name patronidemo
  labels:
    application: patroni
    cluster-name: *cluster_name
type: Opaque
data:
  superuser-password: emFsYW5kbw==
  replication-password: cmVwLXBhc3M=

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: patronidemo

---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: patronidemo
rules:
- apiGroups:
  - ""
  resources:
  - configmaps
  verbs:
  - create
  - get
  - list
  - patch
  - update
  - watch
  # delete and deletecollection are required only for 'patronictl remove'
  - delete
  - deletecollection
- apiGroups:
  - ""
  resources:
  - endpoints
  verbs:
  - get
  - patch
  - update
  # the following three privileges are necessary only when using endpoints
  - create
  - list
  - watch
  # delete and deletecollection are required only for for 'patronictl remove'
  - delete
  - deletecollection
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
  - list
  - patch
  - update
  - watch
# The following privilege is only necessary for creation of headless service
# for patronidemo-config endpoint, in order to prevent cleaning it up by the
# k8s master. You can avoid giving this privilege by explicitly creating the
# service like it is done in this manifest (lines 2..10)
- apiGroups:
  - ""
  resources:
  - services
  verbs:
  - create

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: patronidemo
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: patronidemo
subjects:
- kind: ServiceAccount
  name: patronidemo

# Following privileges are only required if deployed not in the "default"
# namespace and you want Patroni to bypass kubernetes service
# (PATRONI_KUBERNETES_BYPASS_API_SERVICE=true)
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: patroni-k8s-ep-access
rules:
- apiGroups:
  - ""
  resources:
  - endpoints
  resourceNames:
  - kubernetes
  verbs:
  - get

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: patroni-k8s-ep-access
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: patroni-k8s-ep-access
subjects:
- kind: ServiceAccount
  name: patronidemo
# The namespace must be specified explicitly.
# If deploying to the different namespace you have to change it.
  namespace: default
