#@follow_tag(registry.redhat.io/ubi8/ubi-minimal:8.2)
FROM registry.redhat.io/ubi8/ubi-minimal:8.2-349 AS builder

ARG kubevirt_version=${CI_UPSTREAM_VERSION}
ARG kubevirt_commit=${CI_UPSTREAM_COMMIT}
ARG kubevirt_tar=${CI_ARCHIVE_KUBEVIRT}

ENV GOPATH=/go

RUN echo -e "[go-toolset]\nname=go-toolset\nprofiles=\nstate=enabled\nstream=rhel8" > /etc/dnf/modules.d/go-toolset.module \
    && microdnf install -y \
        tar \
        gzip \
        rsync \
        sed \
        go-compilers-golang-compiler \
        glibc-static
COPY ${kubevirt_tar} /
RUN tar xf ${kubevirt_tar} && \
    mkdir -p /go/src/kubevirt.io/ && \
    mv kubevirt-${kubevirt_commit} /go/src/kubevirt.io/kubevirt
WORKDIR ${GOPATH}/src/kubevirt.io/kubevirt

RUN KUBEVIRT_VERSION=${kubevirt_version} \
    KUBEVIRT_SOURCE_DATE_EPOCH="$(date +%s)" \
    KUBEVIRT_GIT_COMMIT=${kubevirt_commit} \
    KUBEVIRT_GIT_VERSION=v${kubevirt_version} \
    KUBEVIRT_GIT_TREE_STATE=clean \
    GOFLAGS=-mod=vendor \
    bash -x ./hack/build-go.sh install cmd/virt-controller


#@follow_tag(registry.redhat.io/ubi8/ubi-minimal:8.2)
FROM registry.redhat.io/ubi8/ubi-minimal:8.2-349

ARG kubevirt_version=${CI_UPSTREAM_VERSION}

RUN microdnf install -y shadow-utils
# Create non-root user
RUN useradd -u 1001 --create-home -s /bin/bash virt-controller
COPY --from=builder /go/src/kubevirt.io/kubevirt/_out/cmd/virt-controller/virt-controller-${kubevirt_version}-linux-amd64 /usr/bin/virt-controller
WORKDIR /home/virt-controller
USER 1001
ENTRYPOINT [ "/usr/bin/virt-controller" ]

LABEL com.redhat.component="virt-controller-container" \
    name="${CI_CONF_RELEASE_REPO}/virt-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="Virtualization controller" \
    io.openshift.expose-services="" \
    io.openshift.tags="cnv,kubevirt" \
    io.k8s.display-name="virt-controller" \
    maintainer="sgott@redhat.com,ibezukh@redhat.com,dhiller@redhat.com,jlejosne@redhat.com" \
    description="Virtualization controller for CNV"
