# Build stage 1 -- build rook

FROM openshift/golang-builder:1.11 AS builder

ENV GOPATH=/go
ENV IMPORT_PATH=github.com/rook/rook
ENV VERSION=4.2-279.f6bdfe9d.ocs_4.2

RUN rm -rf /go && mkdir -p /go/
COPY rook-ocs_4.2-f6bdfe9d.tar.gz /rook.tar.gz
COPY rook-vendor-ocs_4.2-f6bdfe9d.tar.gz /rook-vendor.tar.gz
RUN \
    tar -C /go/ -xzmf /rook.tar.gz && \
    tar -C /go/ -xzmf /rook-vendor.tar.gz && \
    cd /go/ && \
    mv -f vendor src && \
    mkdir -p "src/$IMPORT_PATH" && \
    rm -rf "src/$IMPORT_PATH" && \
    ln -s /go/ "src/$IMPORT_PATH"

WORKDIR /go
RUN CGO_ENABLED=0 GOOS=linux go build -compiler gc -ldflags "-v -n -X github.com/rook/rook/pkg/version.Version=${VERSION} -extldflags '-static'" -a -v -x -o bin/rook ${IMPORT_PATH}/cmd/rook
RUN CGO_ENABLED=0 GOOS=linux go build -compiler gc -ldflags "-v -n -X github.com/rook/rook/pkg/version.Version=${VERSION} -extldflags '-static'" -a -v -x -o bin/rookflex ${IMPORT_PATH}/cmd/rookflex

# Build stage 2 -- build tini
FROM ubi8:8-released AS tinibuilder
RUN dnf install -y make cmake gcc glibc-static

RUN rm -rf /tini && mkdir -p /tini/
COPY tini-v0.18.0.tar.gz /tini.tar.gz
RUN \
    tar -C /tini/ -xzmf /tini.tar.gz && \
    cd /tini/ && \
    cmake . && \
    make

# Build stage 3 -- create rook container
FROM rhceph:4-7

COPY --from=tinibuilder /tini/tini /tini
COPY --from=builder /go/bin/rook /go/bin/rookflex /go/images/ceph/toolbox.sh /usr/local/bin/
COPY --from=builder /go/cluster/examples/kubernetes/ceph/csi/template /etc/ceph-csi
COPY --from=builder /go/cluster/examples/kubernetes/ceph/monitoring /etc/ceph-monitoring
COPY --from=builder /go/cluster/olm/ceph/templates /etc/ceph-csv-templates

LABEL maintainer="Boris Ranto <branto@redhat.com>"
LABEL com.redhat.component="rook-ceph-operator-container"
LABEL name="rook-ceph"
LABEL version="4.2"
LABEL release="279.f6bdfe9d.ocs_4.2"
LABEL description="OpenShift Container Storage 4.2 Rook container"
LABEL summary="Provides the latest Rook package for OpenShift Container Storage 4.2."
LABEL io.k8s.display-name="Rook Ceph Orchestrator"
LABEL io.openshift.tags="rhceph ceph rook"
LABEL upstream-vcs-ref="f6bdfe9d7f0439b00863e0f723eb0cfe1ca4a2af"

ENTRYPOINT ["/tini", "--", "/usr/local/bin/rook"]
CMD [""]
