apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app: gpu-operator-ci
  name: operator-bundle-image-builder-script
  namespace: gpu-operator-ci
data:
  run_operator_bundle_image_builder.sh: |
    #! /bin/bash
    set -o pipefail
    set -o errexit
    set -o nounset
    set -x

    echo "QUAY_BUNDLE_IMAGE_NAME=${QUAY_BUNDLE_IMAGE_NAME}"
    echo "OPERATOR_IMAGE_NAME=${OPERATOR_IMAGE_NAME}"

    CONTAINER_FILE=./bundle.Dockerfile
    if [ ! -f ${CONTAINER_FILE} ]; then
      # TODO: This branch allows for bundles to be built for the modified GPU operator make file.
      # This was merged in https://github.com/NVIDIA/gpu-operator/commit/b0e99d9e6ac8738ee335f6e866242b1c9977ea09
      # This can be set as the default once no older versions are built.
      CONTAINER_FILE=./docker/bundle.Dockerfile
    fi
    CONTEXT_LOCATION="."

    CSV_FILE=bundle/manifests/gpu-operator.clusterserviceversion.yaml

    mkdir /work && cd /work

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

    cd gpu-operator

    git show --quiet
    GIT_VERSION=$(git rev-parse --short HEAD)

    DATE_VERSION=$(date -d "@$(git log -1 ${GIT_VERSION} --format="%at")" +%y.%-m.%-d) # gives 21.9.29
    CSV_VERSION="${DATE_VERSION}-git.${GIT_VERSION}"

    # update operator image
    mv $CSV_FILE ${CSV_FILE}.orig
    cat ${CSV_FILE}.orig | yq \
      | jq .metadata.annotations.containerImage=\"${OPERATOR_IMAGE_NAME}\" \
      | jq .metadata.name=\"gpu-operator-certified-${CSV_VERSION}\" \
      | jq .spec.version=\"${CSV_VERSION}\" \
      | jq .spec.install.spec.deployments[0].spec.template.spec.containers[0].image=\"$OPERATOR_IMAGE_NAME\" \
      > $CSV_FILE

    rm ${CSV_FILE}.orig

    # update validator & node-status-exporter images in the ClusterPolicy

    cat ${CSV_FILE} | yq \
      | jq '.metadata.annotations."alm-examples"' -r \
      > clusterpolicy.json.orig

    cat clusterpolicy.json.orig \
      | jq '.[].spec.validator.image="gpu-operator-ci"' \
      | jq '.[].spec.validator.repository="image-registry.openshift-image-registry.svc:5000/gpu-operator-ci"' \
      | jq '.[].spec.validator.version="'${VALIDATOR_IMAGE_VERSION}'"' \
      | jq '.[].spec.nodeStatusExporter.image="gpu-operator-ci"' \
      | jq '.[].spec.nodeStatusExporter.repository="image-registry.openshift-image-registry.svc:5000/gpu-operator-ci"' \
      | jq '.[].spec.nodeStatusExporter.version="'${VALIDATOR_IMAGE_VERSION}'"' \
      > clusterpolicy.json
    rm  clusterpolicy.json.orig

    # update ClusterPolicy in the CSV

    mv "${CSV_FILE}" "${CSV_FILE}.orig"
    yq --yaml-roundtrip --rawfile alm_examples clusterpolicy.json '.metadata.annotations."alm-examples" = $alm_examples' "${CSV_FILE}.orig" > "${CSV_FILE}"
    rm "${CSV_FILE}.orig"
    rm clusterpolicy.json

    # build the bundle image

    podman build -f "${CONTAINER_FILE}" "${CONTEXT_LOCATION}" -t "${QUAY_BUNDLE_IMAGE_NAME}"

    # push the image to quay.io

    cat /var/run/secrets/openshift.io/push/.dockerconfigjson > /tmp/.dockercfg
    AUTH="--tls-verify=false --authfile /tmp/.dockercfg"

    podman push $AUTH "$QUAY_BUNDLE_IMAGE_NAME"
