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

ARG cdi_version=${CI_UPSTREAM_VERSION}
ARG cdi_commit=${CI_UPSTREAM_COMMIT}
ENV GOPATH=/go

# Catch US Bazel changes that need to be propagated DS
ENV US_BAZEL_MD5=786569a3b852f5b80e539e34ad533a2f

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-apiserver/BUILD.bazel" > .md5sums && \
    md5sum -c .md5sums && rm .md5sums && \
    CDI_VERSION=${cdi_version} \
    CDI_SOURCE_DATE_EPOCH="$(date +%s)" \
    CDI_GIT_COMMIT=${cdi_commit} \
    CDI_GIT_VERSION=v${cdi_version} \
    CDI_GIT_TREE_STATE=clean \
    GOFLAGS=-mod=vendor \
    ./hack/build/build-go.sh build cmd/cdi-apiserver

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

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

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

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