# pod to connect automatically to cos buckets in a docker with the docker and s3 secrets
apiVersion: v1
kind: Pod
metadata:
  name: test-als-all-in-one-pod
spec:
  automountServiceAccountToken: false
  containers:
  - name: test
    command:
    - /bin/bash
    args:
    - -c
    - |
      bash /usr/local/bin/dockerd-entrypoint.sh &
      echo "Waiting for dockerd to start"
      TIMEOUT=10
      DAEMON="dockerd"
      i=0
      while [ $i -lt $TIMEOUT ] && ! /usr/bin/pgrep $DAEMON
      do
        i=$((i+1))
        sleep 2
      done
      pid=`/usr/bin/pgrep $DAEMON`
      if [ -z "$pid" ]
      then
        echo "$DAEMON did not started after $(($TIMEOUT*2)) seconds"
        exit 1
      else
        echo "Found $DAEMON pid:$pid"
        ps -aef | grep docker | grep -v grep
        sleep 10
        ps -aef
        echo "Launching docker info"
        docker info
        if ! test -d /root/.docker 
        then
          mkdir /root/.docker
          echo "$SECRET_AUTH" > /root/.docker/config.json
        fi
        if grep -Fq "index.docker.io" /root/.docker/config.json
        then
          echo "Docker login"
          docker run hello-world

          CONT_NAME=docker_s3_copy
          docker run --env SECRET_AUTH --env SECRET_S3 -it --privileged --name $CONT_NAME debian:bullseye bash
          docker exec -it $CONT_NAME /bin/bash
          if ! test -d /root/.docker 
          then
            mkdir /root/.docker
            echo "$SECRET_AUTH" > /root/.docker/config.json
          fi

          echo :${S3_SECRET_AUTH} > .passwd-s3fs
          chmod 600 .passwd-s3fs
          
          mkdir -p /mnt/s3_ppc64le-docker
          s3fs ppc64le-docker /mnt/s3_ppc64le-docker -o url=https://s3.us-south.cloud-object-storage.appdomain.cloud -o passwd_file=.passwd-s3fs -o ibm_iam_auth
          ls /mnt/s3_ppc64le-docker
        fi
      fi
    image: quay.io/powercloud/docker-ce-build
    resources: {}
    terminationMessagePolicy: FallbackToLogsOnError
    securityContext:
      privileged: true
    volumeMounts:
    - name: docker-graph-storage
      mountPath: /var/lib/docker
    env:
      - name: SECRET_AUTH
        valueFrom:
          secretKeyRef:
            name: docker-token
            key: .dockerconfigjson
      - name: S3_SECRET_AUTH
        valueFrom:
          secretKeyRef:
            name: secret-s3
            key: password
  restartPolicy: Never
  terminationGracePeriodSeconds: 18
  volumes:
  - name: docker-graph-storage
    emptyDir: {}
status: {}  
