# Copyright (c) 2018-2020 Tigera, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

ARG GIT_VERSION=unknown
ARG UBI_IMAGE

FROM ${UBI_IMAGE} as ubi

# Since our binary isn't designed to run as PID 1, run it via the tini init daemon.
ENV TINI_VERSION v0.18.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-static-amd64 /sbin/tini
RUN chmod +x /sbin/tini

# Add in top-level license file
RUN mkdir /licenses
ADD licenses /licenses
COPY LICENSE /licenses

FROM scratch
ARG GIT_VERSION
LABEL name="Calico Typha" \
      vendor="Project Calico" \
      version=$GIT_VERSION \
      release="1" \
      summary="Calico Typha is a fan-out datastore proxy" \
      description="Calico Typha is a fan-out datastore proxy" \
      maintainer="Shaun Crampton <shaun@tigera.io>"

COPY --from=ubi /sbin/tini /sbin/tini
COPY --from=ubi /licenses /licenses

COPY --from=ubi /usr/include /usr/include

# lib dependencies for typha
COPY --from=ubi /lib64/libpthread.so.0 /lib64/libpthread.so.0
COPY --from=ubi /lib64/libc.so.6 /lib64/libc.so.6
COPY --from=ubi /lib64/ld-linux-x86-64.so.2 /lib64/ld-linux-x86-64.so.2
COPY --from=ubi /lib64/libresolv.so.2 /lib64/libresolv.so.2
# These two libnss_* files are referenced in our local nsswitch.conf
# They depend on some of the previous libs
COPY --from=ubi /lib64/libnss_dns.so.2 /lib64/libnss_dns.so.2
COPY --from=ubi /lib64/libnss_files.so.2 /lib64/libnss_files.so.2

# Copy hostname configuration files so glibc hostname lookups work.
COPY --from=ubi /etc/host.conf /etc/host.conf
# But we skip 'myhostname' because it depends on too many libraries.
ADD nsswitch.conf /etc/nsswitch.conf

# Put our binary in /code rather than directly in /usr/bin.  This allows the downstream builds
# to more easily extract the build artefacts from the container.
ADD bin/calico-typha-amd64 /code/calico-typha
ADD typha.cfg /etc/calico/typha.cfg

WORKDIR /code
ENV PATH="$PATH:/code"

USER 999
# Run Typha by default
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["calico-typha"]
