FROM openshift/golang-builder:1.14 AS builder

ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GO111MODULE=off
ENV installsuffix=cgo
ENV VERSION_PKG="github.com/jaegertracing/jaeger-operator/pkg/version"

ADD jaeger-operator-*.tar.gz /

RUN mkdir -p $GOPATH/src/github.com/jaegertracing
RUN tar -xf /jaeger-operator-*.tar.gz -C $GOPATH/src/github.com/jaegertracing

RUN export OPERATOR_VERSION=`ls $GOPATH/src/github.com/jaegertracing | sed s/jaeger-operator-v//g` && \
	export JAEGER_VERSION=`echo $OPERATOR_VERSION | sed s/.redhat[1-9]*//g` && \
	mv $GOPATH/src/github.com/jaegertracing/jaeger-operator-v${OPERATOR_VERSION} $GOPATH/src/github.com/jaegertracing/jaeger-operator && \
	export VERSION_DATE=`date -u +'%Y-%m-%dT%H:%M:%SZ'` && \
	cd $GOPATH/src/github.com/jaegertracing/jaeger-operator && \
	go build -o "build/_output/bin/jaeger-operator" -ldflags "-X ${VERSION_PKG}.version=${OPERATOR_VERSION} -X ${VERSION_PKG}.buildDate=${VERSION_DATE} -X ${VERSION_PKG}.defaultJaeger=${JAEGER_VERSION}"

FROM registry.redhat.io/ubi8/ubi

ENV OPERATOR=/usr/bin/jaeger-operator \
    USER_UID=1001 \
    USER_NAME=jaeger-operator

RUN yum upgrade -y
RUN yum install -y openssl
RUN yum clean all

COPY --from=builder /go/src/github.com/jaegertracing/jaeger-operator/build/_output/bin/jaeger-operator /usr/bin/jaeger-operator
COPY --from=builder /go/src/github.com/jaegertracing/jaeger-operator/scripts/cert_generation.sh /usr/bin/scripts/cert_generation.sh

RUN mkdir /tmp/_working_dir && \
    mkdir /scripts && \
    chmod og+w /tmp/_working_dir /scripts && \
    cp /usr/bin/scripts/cert_generation.sh /scripts

ENTRYPOINT ["/usr/bin/jaeger-operator"]

USER ${USER_UID}

LABEL com.redhat.component="jaeger-rhel8-operator-container" \
      name="distributed-tracing/jaeger-rhel8-operator" \
      summary="Jaeger Operator" \
      description="Operator for the Jaeger distributed tracing system" \
      io.openshift.expose-services="" \
      io.openshift.tags="tracing" \
      io.k8s.display-name="Jaeger Operator" \
      maintainer="Jaeger Team <jaeger-prod@redhat.com>" \
      version="2.0.0.tp.1"

