apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app: gpu-operator-ci
  name: operator-image-builder-script
  namespace: gpu-operator-ci
data:
  run_operator_image_builder.sh: |
    #! /bin/bash
    set -ex

    IMAGE_STREAM=image-registry.openshift-image-registry.svc:5000/${IMAGESTREAM_NAME}:${IMAGESTREAM_TAG}
    CONTAINER_FILE=./Dockerfile

    mkdir /work && cd /work

    git clone ${OPERATOR_GIT_REPO} gpu-operator -b ${OPERATOR_GIT_REF} --depth 1

    cd gpu-operator

    git show --quiet

    if [ "${BUILDER_FROM_IMAGE}" ]; then
      # if provided, use custom 'FROM ... as builder' image
      sed "s|FROM golang:.* as builder|FROM ${BUILDER_FROM_IMAGE} as builder|" -i ${CONTAINER_FILE}
    fi

    # avoid docker.io quotas ...
    sed -i 's|FROM nvidia/cuda:|FROM nvcr.io/nvidia/cuda:|' ${CONTAINER_FILE}

    cat <<EOF > /usr/local/bin/docker
    #! /bin/sh
    exec podman --cgroup-manager=cgroupfs "\$@"
    EOF
    chmod u+x /usr/local/bin/docker

    IMAGE_NAME="gpu-operator:latest"
    make docker-build IMG=${IMAGE_NAME}

    # push the image locally

    AUTH="--tls-verify=false --authfile /tmp/.dockercfg"
    cp /var/run/secrets/openshift.io/push/.dockercfg /tmp
    (echo "{ \"auths\": " ; cat /var/run/secrets/openshift.io/push/.dockercfg ; echo "}") > /tmp/.dockercfg

    podman push $AUTH $IMAGE_NAME $IMAGE_STREAM

    echo "GPU Operator built from $OPERATOR_GIT_REPO / $OPERATOR_GIT_REF and pushed to $IMAGE_STREAM"
