ARG VERSION

# --------------------------------------------------------------------------------------------------
# Builder Image
# --------------------------------------------------------------------------------------------------
FROM cytopia/ansible:${VERSION}-aws as builder

# Required tools for building Python packages
RUN set -eux \
	&& apk add --no-cache \
		# build tools
		coreutils \
		g++ \
		gcc \
		make \
		musl-dev \
		openssl-dev \
		python3-dev \
		# misc tools
		curl

# Python packages (copied to final image)
RUN set -eux \
	&& pip3 install --no-cache-dir --no-compile \
		openshift \
	&& find /usr/lib/ -name '__pycache__' -print0 | xargs -0 -n1 rm -rf \
	&& find /usr/lib/ -name '*.pyc' -print0 | xargs -0 -n1 rm -rf

# kubectl (copied to final image)
RUN set -eux \
	&& if [ "$(uname -m)" = "aarch64" ]; then \
		ARCH="arm64"; \
	elif [ "$(uname -m)" = "x86_64" ]; then \
		ARCH="amd64"; \
	else \
		fail; \
	fi \
	\
	&& curl -sS -L --fail -o /usr/bin/kubectl \
		https://storage.googleapis.com/kubernetes-release/release/$(curl -sS --fail https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/${ARCH}/kubectl \
	&& chmod +x /usr/bin/kubectl \
	&& kubectl version --client --short=true 2>&1 | grep -E 'v[.0-9]+'

# openshift client (copied to final image)
RUN set -eux \
	&& if [ "$(uname -m)" != "x86_64" ]; then \
		echo -e '#!/bin/sh\necho "Not available for this platform"' > /usr/bin/oc \
		&& chmod +x /usr/bin/oc \
		&& exit 0; \
	fi \
	\
	&& curl -sS -L --fail -o /tmp/openshift-client-linux.tar.gz https://mirror.openshift.com/pub/openshift-v4/clients/ocp/stable/openshift-client-linux.tar.gz \
	&& mkdir /tmp/openshift-client-linux/ \
	&& tar -xzf /tmp/openshift-client-linux.tar.gz -C /tmp/openshift-client-linux/ \
	&& mv /tmp/openshift-client-linux/oc /usr/bin/oc-dynamically-linked \
	&& echo -e '#!/bin/sh\n/lib/ld-musl-$(uname -m).so.1 --library-path /lib /usr/bin/oc-dynamically-linked "$@"' > /usr/bin/oc \
	&& chmod +x /usr/bin/oc \
	&& oc version --client


# --------------------------------------------------------------------------------------------------
# Final Image
# --------------------------------------------------------------------------------------------------
FROM cytopia/ansible:${VERSION}-aws as production
ARG VERSION
# https://github.com/opencontainers/image-spec/blob/master/annotations.md
#LABEL "org.opencontainers.image.created"=""
#LABEL "org.opencontainers.image.version"=""
#LABEL "org.opencontainers.image.revision"=""
LABEL "maintainer"="cytopia <cytopia@everythingcli.org>"
LABEL "org.opencontainers.image.authors"="cytopia <cytopia@everythingcli.org>"
LABEL "org.opencontainers.image.vendor"="cytopia"
LABEL "org.opencontainers.image.licenses"="MIT"
LABEL "org.opencontainers.image.url"="https://github.com/cytopia/docker-ansible"
LABEL "org.opencontainers.image.documentation"="https://github.com/cytopia/docker-ansible"
LABEL "org.opencontainers.image.source"="https://github.com/cytopia/docker-ansible"
LABEL "org.opencontainers.image.ref.name"="Ansible ${VERSION} awsk8s"
LABEL "org.opencontainers.image.title"="Ansible ${VERSION} awsk8s"
LABEL "org.opencontainers.image.description"="Ansible ${VERSION} awsk8s"

COPY --from=builder /usr/lib/python3.10/site-packages/ /usr/lib/python3.10/site-packages/
COPY --from=builder /usr/bin/kubectl /usr/bin/kubectl
COPY --from=builder /usr/bin/oc* /usr/bin/

WORKDIR /data
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["/bin/bash"]
