#@follow_tag(registry.redhat.io/ubi8/ubi:latest)
FROM registry.redhat.io/ubi8/ubi:8.4-211 AS builder

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

ENV GOPATH=/go

RUN source /etc/os-release && \
    dnf module enable -y \
    go-toolset:rhel8 \
    virt:av \
    mercurial:4.8 \
    python27:2.7 \
    llvm-toolset:rhel8 \
    perl:5.26 \
    && dnf install -y \
        tar \
        gzip \
        go-compilers-golang-compiler \
        rsync \
        sed \
        libvirt-devel \
        make \
        git \
        mercurial \
        sudo \
        gcc \
        findutils \
        qemu-img \
        protobuf \
        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

COPY ${libvirt_tar} /
RUN tar xf /${libvirt_tar} && \
    rm /${libvirt_tar} && \
    mv /cnv-libvirt-* /libvirt

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-launcher


#@follow_tag(registry.redhat.io/ubi8/ubi-minimal:latest)
FROM registry.redhat.io/ubi8/ubi-minimal:8.4-210

ARG kubevirt_version=${CI_UPSTREAM_VERSION}

COPY --from=builder /etc/dnf/modules.d/virt.module /etc/dnf/modules.d/virt.module
COPY --from=builder /etc/dnf/modules.d/llvm-toolset.module /etc/dnf/modules.d/llvm-toolset.module

RUN microdnf module enable python36:3.6
RUN PKGS="nettle qemu-kvm libvirt-daemon-driver-qemu libvirt-daemon-driver-storage-core libvirt-client selinux-policy selinux-policy-targeted augeas socat xorriso sudo edk2-ovmf iptables" && \
    microdnf -y --setopt=tsflags=nodocs install $PKGS && \
    microdnf clean all && \
    rpm -V $PKGS && \
    test $(id -u qemu) = 107 # make sure that the qemu user really is 107 && \
    mkdir -p /usr/share/kubevirt/virt-launcher

COPY --from=builder /go/src/kubevirt.io/kubevirt/_out/cmd/virt-launcher/virt-launcher-${kubevirt_version}-linux-amd64 /usr/bin/virt-launcher
COPY --from=builder /libvirt/augconf /tmp/augconf
COPY --from=builder /go/src/kubevirt.io/kubevirt/_out/cmd/container-disk-v2alpha/container-disk /usr/bin/container-disk

RUN augtool -f /tmp/augconf
RUN echo "remember_owner = 0" >> /etc/libvirt/qemu.conf

ENTRYPOINT [ "/usr/bin/virt-launcher" ]

LABEL com.redhat.component="virt-launcher-container" \
    name="container-native-virtualization/virt-launcher" \
    version="${CI_CONTAINER_VERSION}" \
    release="${CI_CONTAINER_RELEASE}" \
    upstream-version="${CI_UPSTREAM_VERSION}" \
    upstream-vcs-ref="${CI_UPSTREAM_COMMIT}" \
    upstream-vcs-type="git" \
    summary="Hosts VM processes" \
    io.openshift.expose-services="" \
    io.openshift.tags="cnv,kubevirt,VM" \
    io.k8s.display-name="virt-launcher" \
    maintainer="sgott@redhat.com,ibezukh@redhat.com,dhiller@redhat.com,jlejosne@redhat.com" \
    description="Create cgroups and namespaced and host VM process"
