apiVersion: v1
kind: Pod
metadata:
  name: mongodb-pod
  labels:
    app: mongodb
spec:
  volumes:
    - name: mongodb-storage
      persistentVolumeClaim:
       claimName: mongodb-claim
  containers:
    - name: mongodb-container
      image: mongo:3.4
      args: ["--auth"]
      ports:
        - containerPort: 27017
          name: "mongodb-server"
      volumeMounts:
        - mountPath: "/data"
          name: mongodb-storage
      lifecycle:
            postStart:
              exec:
                command:  
                  - "/bin/sh"
                  - "-c"
                  - "/usr/bin/mongo admin --eval \"db.createUser({ user: '${MONGO_USER}', pwd: '${MONGO_PASSWORD}', roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ] });\" && /usr/bin/mongo --authenticationDatabase admin -u \"${MONGO_USER}\" -p \"${MONGO_PASSWORD}\" test --eval \"db.createUser({ user: '${MONGO_USER}', pwd: '${MONGO_PASSWORD}', roles: [ { role: 'readWrite', db: 'test' } ] });\""
      env:
        - name: MONGO_USER
          valueFrom:
            secretKeyRef:
              name: mongodb-secret
              key: username
        - name: MONGO_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mongodb-secret
              key: password