FROM registry.redhat.io/ubi8/ubi-minimal AS build

USER root

ENV GOPATH=/go
ENV PATH=${PATH}:${GOPATH}/bin

WORKDIR "${GOPATH}/src/github.com/3scale"

ARG SRC_TAG=v2.0.1
ARG TARBALL=3scale-istio-adapter-${SRC_TAG}.tar.gz
COPY ${TARBALL} /tmp/

ARG BUILD_PKGS="tar gzip findutils git go-toolset make"

RUN microdnf update --nodocs -y \
 && microdnf install --nodocs -y ${BUILD_PKGS} \
 && microdnf clean all -y \
 && rm -rf /var/cache/yum \
 && mkdir -p "${GOPATH}/bin" \
 # just create an empty dep binary because the build process runs it
 # (we don't require it as all dependencies are vendored)
 && touch "${GOPATH}/bin/dep" \
 && chmod +x "${GOPATH}/bin/dep" \
 && tar xzpf "/tmp/${TARBALL}" --checkpoint=1000 \
 && PROJECT_DIR="$(find . -maxdepth 1 -mindepth 1 -type d)" \
 && if test "${PROJECT_DIR}" != "3scale-istio-adapter"; then \
      mv "${PROJECT_DIR}" 3scale-istio-adapter ; \
    fi \
 && cd 3scale-istio-adapter \
 && VERSION="$(echo ${SRC_TAG} | sed -E -e 's/^v([0-9])/\1/')" \
 && echo "Building adapter version ${VERSION}" \
 && make VERSION="${VERSION}" build-adapter \
 && echo "Building CLI tool version ${VERSION}" \
 && make VERSION="${VERSION}" build-cli \
 && mkdir -p /app \
 && cp _output/* /app/ \
 && cd /app \
 && rm -rf "${GOPATH}" "/tmp/${TARBALL}"

FROM registry.redhat.io/ubi8/ubi-minimal

# Labels consumed by Red Hat build service
# Please keep the version label first as it is looked up by the Makefile - also, it is meant to be the image tag.
LABEL version="2.0.0" \
      com.redhat.component="3scale-istio-adapter-rhel8-container" \
      name="openshift-service-mesh/3scale-istio-adapter-rhel8" \
      summary="3scale Istio Mixer Adapter." \
      description="The 3scale Istio Mixer Adapter provides API Management capabilities based on Red Hat 3scale API Management to the Istio Service Mesh. This image contains the out-of-process Mixer adapter, which takes care of applying rate limits, authorization, and reporting of HTTP(s) requests to configured services." \
      io.openshift.expose-services="3333:tcp,8080:http" \
      io.k8s.display-name="3scale Istio Mixer Adapter" \
      io.openshift.tags="istio, 3scale, 3scale-amp, service-mesh, api, api-management" \
      maintainer="amr@redhat.com"

# Ensure we pick up updates from repos
RUN microdnf update --nodocs -y

WORKDIR /opt/app

COPY --from=build "/app/*" /opt/app/

EXPOSE 3333
EXPOSE 8080

USER 1001

# Set TZ to avoid glibc wasting time with unneeded syscalls
ENV TZ=:/etc/localtime \
    HOME=/tmp/ \
    # App-specific env
    THREESCALE_LISTEN_ADDR=3333

CMD ["./3scale-istio-adapter"]
