FROM ubi8-minimal:8.2-339 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 microdnf install -y tar gzip rsync sed go-compilers-golang-compiler make

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

FROM ubi8-minimal:8.2-339

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

RUN microdnf 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

# 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.4.0" \
      release="70" \
      upstream-version="v0.4.0-216-g39e9c8d" \
      upstream-vcs-ref="39e9c8d49f944f53debc644f99d5326c08daeae8" \
      upstream-vcs-type="git" \
      summary="CNV HyperConverged Cluster Operator" \
      io.openshift.expose-services="" \
      io.openshift.tags="cnv,kubevirt,operator" \
      io.k8s.display-name="hyperconverged-cluster-operator" \
      maintainer="Ryan Hallisey <rhallise@redhat.com>" \
      description="CNV HyperConverged Cluster Operator"
