###############################################################################
# Copyright (c) 2020 Red Hat, Inc.
###############################################################################

FROM openshift/origin-release:golang-1.15 as builder

ARG REMOTE_SOURCE
ARG REMOTE_SOURCE_DIR
ARG GITHUB_TOKEN
ARG COMPONENT_VERSION

ENV USE_VENDORIZED_BUILD_HARNESS=true
ENV COMPONENT_NAME=klusterlet-addon-lease-controller
ENV COMPONENT_VERSION="${COMPONENT_VERSION}"

# Install ChromeDriver in build image
# ENV CHROMEDRIVER_VERSION "80.0.3987.16"
# RUN mkdir -p $REMOTE_SOURCE_DIR/chromedriver
# ENV CHROMEDRIVER_VERSION "81.0.4044.69"
# RUN wget --no-check-certificate -N https://chromedriver.storage.googleapis.com/${CHROMEDRIVER_VERSION}/chromedriver_linux64.zip -P $REMOTE_SOURCE_DIR/chromedriver

# RUN mkdir -p $REMOTE_SOURCE_DIR/app/build/bin
# RUN unzip $REMOTE_SOURCE_DIR/chromedriver/chromedriver_linux64.zip -d $REMOTE_SOURCE_DIR/app/build/bin

COPY $REMOTE_SOURCE $REMOTE_SOURCE_DIR/app/
WORKDIR $REMOTE_SOURCE_DIR/app
# RUN git config --global url."https://${GITHUB_TOKEN}:x-oauth-basic@github.com/".insteadOf "https://github.com/"

# compile go tests in build image
RUN GOFLAGS="" go get -u github.com/onsi/ginkgo/ginkgo && GOFLAGS="" ginkgo build -tags e2e test/e2e

FROM registry.access.redhat.com/ubi8/ubi-minimal:latest

ARG VCS_REF
ARG VCS_URL
ARG IMAGE_NAME
ARG IMAGE_DESCRIPTION
ARG ARCH_TYPE
ARG REMOTE_SOURCE_DIR

RUN microdnf update

# expose env vars for runtime
ENV KUBECONFIG "/opt/.kube/config"
ENV OPTIONS "/resources/options.yaml"
ENV REPORT_FILE "/results/results.xml"
ENV GINKGO_DEFAULT_FLAGS "-slowSpecThreshold=120 -timeout 7200s"
ENV GINKGO_NODES "1"
ENV GINKGO_FLAGS=""
ENV GINKGO_FOCUS=""
ENV GINKGO_SKIP=""

ENV E2E_BIN=/usr/local/bin/e2e.test \
    USER_UID=1001 \
    USER_NAME=klusterlet-addon-lease-controller

# install operator binary
COPY --from=builder $REMOTE_SOURCE_DIR/app/test/e2e/e2e.test ${E2E_BIN}
COPY --from=builder $REMOTE_SOURCE_DIR/app/build/bin /usr/local/bin

# Copy resources which contains the options.yaml
COPY --from=builder $REMOTE_SOURCE_DIR/app/test/e2e/resources /test/e2e/resources
RUN  /usr/local/bin/user_setup

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

USER ${USER_UID}

LABEL com.redhat.component="klusterlet-addon-lease-controller-container" \
      name="rhacm1-tech-preview/klusterlet-addon-lease-controller-rhel8" \
      version="${CI_CONTAINER_VERSION}" \
      release="${CI_CONTAINER_RELEASE}" \
      summary="klusterlet-addon-lease-controller" \
      io.openshift.expose-services="" \
      io.openshift.tags="data,images" \
      io.k8s.display-name="klusterlet-addon-lease-controller" \
      maintainer="['eisraeli@redhat.com', 'jbieren@redhat.com']" \
      description="klusterlet-addon-lease-controller"
