#@follow_tag(registry.redhat.io/rhel8-2-els/rhel)
FROM registry.redhat.io/rhel8-2-els/rhel:8.2-23 AS builder

ENV GOPATH=/go

# Catch US Bazel changes that need to be propagated DS
ENV US_BAZEL_MD5=23a49e6ddad4d276ed1f9a680e1cd0af

RUN echo -e "[go-toolset]\nname=go-toolset\nprofiles=\nstate=enabled\nstream=rhel8" > /etc/dnf/modules.d/go-toolset.module \
    && dnf install -y tar git sed go-compilers-golang-compiler

COPY containerized-data-importer-*.tar.gz /
RUN mkdir -p /go/src/kubevirt.io/containerized-data-importer && \
    tar -xzf containerized-data-importer-*.tar.gz --strip-components 1 \
        -C /go/src/kubevirt.io/containerized-data-importer && \
    cd /go/src/kubevirt.io/containerized-data-importer && \
    echo "$US_BAZEL_MD5 cmd/cdi-controller/BUILD.bazel" > .md5sums && \
    md5sum -c .md5sums && rm .md5sums && \
    GOFLAGS=-mod=vendor ./hack/build/build-go.sh build cmd/cdi-controller


#@follow_tag(registry.redhat.io/rhel8-2-els/rhel)
FROM registry.redhat.io/rhel8-2-els/rhel:8.2-23
COPY --from=builder /go/src/kubevirt.io/containerized-data-importer/_out/cmd/cdi-controller/cdi-controller /usr/bin/virt-cdi-controller

# Create non-root user
RUN dnf install -y shadow-utils
RUN useradd -u 1001 --create-home -s /bin/bash cdi-controller
RUN chgrp -R 0 /home/cdi-controller && \
    chmod -R g=u /home/cdi-controller
WORKDIR /home/cdi-controller
USER 1001

ENTRYPOINT [ "/usr/bin/virt-cdi-controller", "-alsologtostderr" ]

LABEL com.redhat.component="virt-cdi-controller-container" \
      name="container-native-virtualization/virt-cdi-controller" \
      version="${CI_CONTAINER_VERSION}" \
      release="${CI_CONTAINER_RELEASE}" \
      upstream-version="${CI_UPSTREAM_VERSION}" \
      upstream-vcs-ref="${CI_UPSTREAM_COMMIT}" \
      upstream-vcs-type="git" \
      summary="Controller for the data fetching service" \
      io.openshift.expose-services="" \
      io.openshift.tags="data,images" \
      io.k8s.display-name="virt-cdi-controller" \
      maintainer="The KubeVirt Project <kubevirt-dev@googlegroups.com" \
      description="Controller for the data fetching service for CNV images"
