#@follow_tag(registry.redhat.io/rhel8-2-els/rhel)
FROM registry.redhat.io/rhel8-2-els/rhel:8.2-7 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-66b7b001be5b03dde561b03e6db3dbd5872b8589.tar.gz /
RUN mkdir -p "$SRC_DIR" \
    && tar -xzf "hyperconverged-cluster-operator-66b7b001be5b03dde561b03e6db3dbd5872b8589.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-7

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.3" \
      release="3" \
      upstream-version="v1.2.0" \
      upstream-vcs-ref="66b7b001be5b03dde561b03e6db3dbd5872b8589" \
      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"
