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

ENV OPERATOR=/usr/local/bin/hyperconverged-cluster-operator \
    USER_UID=1001 \
    USER_NAME=hyperconverged-cluster-operator

ENV GOPATH=/go
ENV SRC_DIR=${GOPATH}/src/github.com/kubevirt/hyperconverged-cluster-operator

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

COPY hyperconverged-cluster-operator-786439628759cbf0bf80b6e1066cdb4a2cda8c5a.tar.gz /
RUN mkdir -p "$SRC_DIR" \
    && tar -xzf "hyperconverged-cluster-operator-786439628759cbf0bf80b6e1066cdb4a2cda8c5a.tar.gz" \
        --strip-components 1 \
        -C "$SRC_DIR" \
    && cd "$SRC_DIR" \
    && GOFLAGS=-mod=vendor make build

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

ENV OPERATOR=/usr/local/bin/hyperconverged-cluster-operator \
    USER_UID=1001 \
    USER_NAME=hyperconverged-cluster-operator \
    CSV_MERGER=/usr/local/bin/csv-merger

RUN dnf install -y shadow-utils

COPY --from=builder /go/src/github.com/kubevirt/hyperconverged-cluster-operator/_out/hyperconverged-cluster-operator ${OPERATOR}
COPY --from=builder /go/src/github.com/kubevirt/hyperconverged-cluster-operator/build/bin /usr/local/bin
COPY --from=builder /go/src/github.com/kubevirt/hyperconverged-cluster-operator/_out/csv-merger $CSV_MERGER
COPY --from=builder /go/src/github.com/kubevirt/hyperconverged-cluster-operator/deploy/crds/hco*.yaml /usr/local/share/info/
RUN echo -e "#!/bin/bash\ncat /usr/local/share/info/hco*.yaml\n" >> /usr/local/bin/crd-generator && chmod +x /usr/local/bin/crd-generator


# ensure $HOME exists and is accessible by group 0 (we don't know what the runtime UID will be)
RUN mkdir -p ${HOME} && \
    chown ${USER_UID}:0 ${HOME} && \
    chmod ug+rwx ${HOME} && \
    # runtime user will need to be able to self-insert in /etc/passwd
    chmod g+rw /etc/passwd

ENTRYPOINT ["/usr/local/bin/entrypoint"]

USER ${USER_UID}

LABEL com.redhat.component="hyperconverged-cluster-operator-container" \
      name="container-native-virtualization/hyperconverged-cluster-operator" \
      version="v2.5.7" \
      release="2" \
      upstream-version="v1.2.0-2-g78643962" \
      upstream-vcs-ref="786439628759cbf0bf80b6e1066cdb4a2cda8c5a" \
      upstream-vcs-type="git" \
      summary="OpenShift Virtualization HyperConverged Cluster Operator" \
      io.openshift.expose-services="" \
      io.openshift.tags="virtualization,kubevirt,operator" \
      io.k8s.display-name="hyperconverged-cluster-operator" \
      maintainer="Simone Tiraboschi <stirabos@redhat.com>" \
      description="OpenShift Virtualization HyperConverged Cluster Operator"
