version: '3.4'

services:
  host-password:
    image: lscr.io/linuxserver/openssh-server:latest
    environment:
      - PASSWORD_ACCESS=true
      - USER_PASSWORD=pass
      - USER_NAME=user
    labels:
      - sshpiper.username=pass
      - sshpiper.container_username=user
      - sshpiper.port=2222
      - sshpiper.network=e2e_default
    volumes:
      - shared:/shared
      - sshconfig_password:/config
    networks:
      - default
      - netdistract

  host-publickey:
    image: lscr.io/linuxserver/openssh-server:latest
    environment:
      - USER_NAME=user
    labels:
      - sshpiper.container_username=user
      - sshpiper.port=2222
      - sshpiper.authorized_keys=c3NoLWVkMjU1MTkgQUFBQUMzTnphQzFsWkRJMU5URTVBQUFBSU5SR1RIMzI1ckRVcDEydHBsd3VrSG1SOHl0YkM5VFBaODg2Z0NzdHluUDEK
      - sshpiper.private_key=LS0tLS1CRUdJTiBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0KYjNCbGJuTnphQzFyWlhrdGRqRUFBQUFBQkc1dmJtVUFBQUFFYm05dVpRQUFBQUFBQUFBQkFBQUFNd0FBQUF0emMyZ3RaVwpReU5UVXhPUUFBQUNEVVJreDk5dWF3MUtkZHJhWmNMcEI1a2ZNcld3dlV6MmZQT29BckxjcHo5UUFBQUpDK2owK1N2bzlQCmtnQUFBQXR6YzJndFpXUXlOVFV4T1FBQUFDRFVSa3g5OXVhdzFLZGRyYVpjTHBCNWtmTXJXd3ZVejJmUE9vQXJMY3B6OVEKQUFBRURjUWdkaDJ6MnIvNmJscTB6aUoxbDZzNklBWDhDKzlRSGZBSDkzMWNITk85UkdUSDMyNXJEVXAxMnRwbHd1a0htUgo4eXRiQzlUUFo4ODZnQ3N0eW5QMUFBQUFEV0p2YkdsaGJrQjFZblZ1ZEhVPQotLS0tLUVORCBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0K
    volumes:
      - shared:/shared      
      - sshconfig_publickey:/config
    
  host-k8s-proxy:
    build: ./kubetools
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ../plugin/kubernetes/crd.yaml:/kubernetes/crd.yaml:ro
      - ./k8sworkload.yaml:/kubernetes/workload.yaml:ro
    # networks:
    #   - kind
    #   - default
    command: 
      - bash
      - -c 
      - |
        # kind delete cluster -n sshpipertest
        (kind get kubeconfig -q -n sshpipertest || kind create cluster -n sshpipertest)
        docker network connect kind $$(hostname) # self contain
        docker network connect e2e_default sshpipertest-control-plane
        kind export kubeconfig -n sshpipertest --internal
        #kubectl delete po -l k8s-app=kube-dns -A
        kind load docker-image -n sshpipertest e2e_piper-imageonly 
        kubectl wait --for=condition=ready pod -A --all --timeout=2m
        kubectl delete -f /kubernetes/crd.yaml
        kubectl delete -f /kubernetes/workload.yaml
        kubectl apply -f /kubernetes/crd.yaml
        kubectl apply -f /kubernetes/workload.yaml
        #kubectl set image deployment/sshpiper-deployment sshpiper=e2e_piper-imageonly
        kubectl wait deployment --all --for condition=Available=True
        kubectl port-forward service/sshpiper --pod-running-timeout=2m --address 0.0.0.0 2222:2222 &
        kubectl logs -f deployment/sshpiper-deployment
    privileged: true
    depends_on:
      - host-publickey
      - host-password    
      - piper-imageonly

  testrunner:
    environment:
      - SSHPIPERD_LOG_LEVEL=trace
      - SSHPIPERD_E2E_TEST=1
      - SSHPIPERD_DEBUG=${SSHPIPERD_DEBUG}
    build: 
      context: ../
      target: builder
      args:
        - BUILDTAGS=e2e
    volumes:
      - ..:/src
      - shared:/shared
      - sshconfig_publickey:/sshconfig_publickey
      - sshconfig_password:/sshconfig_password
      - /var/run/docker.sock:/var/run/docker.sock
    command: ["./e2eentry.sh"]
    privileged: true
    working_dir: /src/e2e
    depends_on:
      - host-publickey
      - host-password
      - host-k8s-proxy

  # ensure sshpiperd image works
  piper-imageonly:
    environment:
      - SSHPIPERD_LOG_LEVEL=trace
    build: ../      

volumes:
  shared:
    driver_opts:
      type: tmpfs
      device: tmpfs

  sshconfig_publickey:

  sshconfig_password:

networks:
  netdistract: