## Dumped via:
##   kubectl edit configmap gitlab-gitlab-runner -n gitlab-ce -o yaml
##
## Final config.toml is located at /home/gitlab-runner/.gitlab-runner/config.toml
##
## After apply, need to restrart runner (possibly - manually)
## ssh root@k2.ku.suggest.io
## docker ps | grep runner | grep -v "task-" | grep -v "/pause:"
## docker kill ...
##
## Install secrets for mount /root/.ssh-mount via
##   ~/projects/sio/-doc/ku.s.io/30-gitlab-runner-ssh-deploy
##
apiVersion: v1
data:
  check-live: |
    #!/bin/bash
    if /usr/bin/pgrep -f .*register-the-runner; then
      exit 0
    elif /usr/bin/pgrep gitlab.*runner; then
      exit 0
    else
      exit 1
    fi
  config.template.toml: |
    [[runners]]
    ## To suppress SSL pain, use direct in-cluster connection to gitlab WORKHORSE.
    ## Do not use webservice port 8080, because it needs JWT Token.
    clone_url = "http://gitlab-webservice-default.gitlab-ce.svc.cluster.local:8181/"

    [runners.kubernetes]
    image = "ubuntu:18.04"
    
    #[[runners.kubernetes.volumes.host_path]]
    #  name = "docker"
    #  host_path = "/var/run/docker.sock"
    #  mount_path = "/var/run/docker.sock"
    #  read_only = false

    ## custom mounts for faster scala compiling:
    [[runners.kubernetes.volumes.host_path]]
    name = "home-cache"
    host_path = "/var/lib/containers/gitlab-ce-runner-home-cache"
    mount_path = "/root/.cache"
    read_only = false
    
    [[runners.kubernetes.volumes.host_path]]
    name = "ivy2-cache"
    host_path = "/var/lib/containers/ivy2-cache"
    mount_path = "/root/.ivy2/cache"
    read_only = false
    
    [[runners.kubernetes.volumes.host_path]]
    name = "sbt-zinc"
    host_path = "/var/lib/containers/sbt-home"
    mount_path = "/root/.sbt"
    read_only = false

    [[runners.kubernetes.volumes.host_path]]
    name = "yarn-cache"
    host_path = "/var/lib/containers/yarn-cache"
    ## root -> /usr/local/share/.cache/yarn,  user -> ~/.cache/yarn
    mount_path = "/usr/local/share/.cache/yarn"
    read_only = false
    
    [[runners.kubernetes.volumes.secret]]
    name = "deploy-ssh-keys"
    mount_path = "/root/.ssh-mount"
    read_only = true
    
    [runners.kubernetes.node_selector]
    "io.suggest.sio2.build.here" = "true"
  config.toml: |
    concurrent = 1
    check_interval = 5
    log_level = "info"
    listen_address = ':9252'
  configure: |
    set -e
    cp /init-secrets/* /secrets
  entrypoint: |
    #!/bin/bash
    set -e
    mkdir -p /home/gitlab-runner/.gitlab-runner/
    cp /configmaps/config.toml /home/gitlab-runner/.gitlab-runner/

    if [[ -f /secrets/runner-registration-token ]]; then
      export REGISTRATION_TOKEN=$(cat /secrets/runner-registration-token)
    fi

    if [[ -f /secrets/runner-token ]]; then
      export CI_SERVER_TOKEN=$(cat /secrets/runner-token)
    fi

    # Register the runner
    if ! sh /configmaps/register-the-runner; then
      exit 1
    fi

    # Run pre-entrypoint-script
    if ! bash /configmaps/pre-entrypoint-script; then
      exit 1
    fi
    
    # Start the runner
    exec /entrypoint run --user=gitlab-runner \
      --working-directory=/home/gitlab-runner
  pre-entrypoint-script: ""
  register-the-runner: |
    #!/bin/bash
    MAX_REGISTER_ATTEMPTS=30

    for i in $(seq 1 "${MAX_REGISTER_ATTEMPTS}"); do
      echo "Registration attempt ${i} of ${MAX_REGISTER_ATTEMPTS}"
      /entrypoint register \
        --template-config /configmaps/config.template.toml \
        --non-interactive

      retval=$?

      if [ ${retval} = 0 ]; then
        break
      elif [ ${i} = ${MAX_REGISTER_ATTEMPTS} ]; then
        exit 1
      fi

      sleep 5
    done

    exit 0
kind: ConfigMap
metadata:
  annotations:
    meta.helm.sh/release-name: gitlab
    meta.helm.sh/release-namespace: gitlab-ce
  labels:
    app: gitlab-gitlab-runner
    app.kubernetes.io/managed-by: Helm
    chart: gitlab-runner-0.30.0
    heritage: Helm
    release: gitlab
  name: gitlab-gitlab-runner
  namespace: gitlab-ce
