# PetSet was renamed to StatefulSet in k8s 1.5
# apiVersion: apps/v1alpha1
# kind: PetSet
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: stolon-keeper
  namespace: postgresql
spec:
  serviceName: "stolon-keeper"
  replicas: 3
  selector:
    matchLabels:
      component: stolon-keeper
      stolon-cluster: kube-stolon
  template:
    metadata:
      labels:
        component: stolon-keeper
        stolon-cluster: kube-stolon
      annotations:
        pod.alpha.kubernetes.io/initialized: "true"
        prometheus.io/scrape: "true"
        prometheus.io/port: "8080"
    spec:
      imagePullSecrets:
      - name: regcred
      terminationGracePeriodSeconds: 10
      containers:
        - name: stolon-keeper
          image: sorintlab/stolon:master-pg15
          command:
            - "/bin/bash"
            - "-ec"
            - |

              echo "COMECANDO com user 1013"
              useradd -u 1013 -r -s /usr/sbin/nologin fenixuser
              id
              id stolon
              # Generate our keeper uid using the pod index
              IFS='-' read -ra ADDR <<< "$(hostname)"
              export STKEEPER_UID="keeper${ADDR[-1]}"
              export POD_IP=$(hostname -i)
              export STKEEPER_PG_LISTEN_ADDRESS=$POD_IP
              
              which psql
              which postgres
              which pg_ctl


              export STOLON_DATA=/stolon-data/data
              mkdir -p $STOLON_DATA


              ls -la $STOLON_DATA

              exec gosu 1013 stolon-keeper --data-dir $STOLON_DATA



          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: STKEEPER_CLUSTER_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.labels['stolon-cluster']
            - name: STKEEPER_STORE_BACKEND
              value: "kubernetes"
            - name: STKEEPER_KUBE_RESOURCE_KIND
              value: "configmap"
            - name: STKEEPER_PG_REPL_USERNAME
              #value: "repluser"
              valueFrom:
                secretKeyRef:
                  name: stolon-repl-secret
                  key: username
            - name: STKEEPER_PG_REPL_PASSWORD
              #value: "replpassword"
              valueFrom:
                secretKeyRef:
                  name: stolon-repl-secret
                  key: password
            - name: STKEEPER_PG_SU_USERNAME
              #value: "stolon"
              valueFrom:
                secretKeyRef:
                  name: stolon-secret
                  key: username
            - name: STKEEPER_PG_SU_PASSWORDFILE
              value: "/etc/secrets/stolon/password"
            - name: STKEEPER_METRICS_LISTEN_ADDRESS
              value: "0.0.0.0:8080"
            # Uncomment this to enable debug logs
            #- name: STKEEPER_DEBUG
            #  value: "true"
          ports:
            - containerPort: 5432
            - containerPort: 8080
          volumeMounts:
            - mountPath: /stolon-data
              name: stolon-data
            - mountPath: /etc/secrets/stolon
              name: stolon
      volumes:
        - name: stolon
          secret:
            secretName: stolon-secret
  volumeClaimTemplates:
    - kind: PersistentVolumeClaim
      apiVersion: v1
      metadata:
        name: stolon-data
        namespace: postgresql
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 10Gi
        selector:
          matchLabels:
            postgres-id: "stolon-0" 