##################################################
#
# go backend build
FROM openshift/golang-builder:1.10 AS gobuilder
RUN mkdir -p /go/src/github.com/openshift/console/
ADD upstream/ /go/src/github.com/openshift/console/
WORKDIR /go/src/github.com/openshift/console/
RUN ./build-backend.sh


##################################################
#
# nodejs frontend build
FROM rhscl/nodejs-8-rhel7:1 AS nodebuilder

# deviation from openshift-enterprise-console: 
# - upstream kubevirt/web-ui sources are pushed to dist-git for every build
# - upstream sources live under "upstream" sub-directory
# - kubevirt/web-ui npm dependencies are changed regularly (kubevirt/web-ui-components)
ADD upstream/ .
COPY yarn-offline.tar ./
COPY node-v8.9.4-headers.tar.gz ./

USER 0
# extract the yarn dependencies that must be provided in the dist-git lookaside cache
RUN tar fx yarn-offline.tar

# bootstrap yarn so we can install and run the other tools.
RUN container-entrypoint npm install ./npm-packages-offline-cache/yarn-1.9.4.tgz

# prevent download of chromedriver, sass binary, and node headers as part of module installs
ENV CHROMEDRIVER_FILEPATH=$HOME/fake_chromedriver.zip \
    SKIP_SASS_BINARY_DOWNLOAD_FOR_CI=true \
    NPM_CONFIG_TARBALL=$HOME/node-v8.9.4-headers.tar.gz

# run the build
RUN container-entrypoint ./build-frontend.sh

##################################################
#
# actual base image for final product
# TODO: Workaround using rhel-7 instead of ubi8
# Requires: https://jira.coreos.com/browse/ART-502
#FROM openshift/ose-base:v4.0.17.20190305.221051
#FROM openshift/ose-base:v4.0.22-201903220117
#FROM openshift3/ose-base:v3.11.98.20190320.105509
FROM ubi8-minimal:8-released
RUN mkdir -p /opt/bridge/bin
COPY --from=gobuilder /go/src/github.com/openshift/console/bin/bridge /opt/bridge/bin
COPY --from=nodebuilder /opt/app-root/src/frontend/public/dist /opt/bridge/static

WORKDIR /
# doesn't require a root user.
USER 1001

# following is overriden by kubevirt-ansible or kubevirt-web-ui-operator
# Hint for setting branding: use ConfigMap or kubevirt-web-ui operator's CR
CMD [ "/opt/bridge/bin/bridge", "--public-dir=/opt/bridge/static" ]

LABEL \
        io.k8s.description="This is a component of OpenShift Container Platform and provides Kubevirt Web User Interface." \
        summary="This is a component of OpenShift Container Platform and provides Kubevirt Web User Interface." \
        com.redhat.component="kubevirt-web-ui-container" \
        maintainer="Marek Libra <mlibra@redhat.com>" \
        name="container-native-virtualization/kubevirt-web-ui" \
        License="GPLv2+" \
        io.k8s.display-name="Kubevirt Web UI" \
        version="v2.0.0" \
        release="14.8" \
        io.openshift.tags="openshift,console,kubevirt,cnv"

