ARG VERSION

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

# Required tools for building Python packages
RUN set -eux \
	&& apk add --no-cache \
		# misc tools
		coreutils \
		curl \
		git

# Install Helm by major and minor version
ARG HELM
RUN set -eux \
	&& if [ "$(uname -m)" = "aarch64" ]; then \
		ARCH="arm64"; \
	elif [ "$(uname -m)" = "x86_64" ]; then \
		ARCH="amd64"; \
	else \
		fail; \
	fi \
	\
	&& git clone https://github.com/helm/helm \
	&& cd helm \
	# Sort by unique minor versions (without patch) \
	&& version="$(git tag | grep -E "^v${HELM}\.[0-9]{1,2}$" | sort -V | tail -1)" \
	&& echo "${version}" \
	&& curl -sS -L --fail -O https://get.helm.sh/helm-${version}-linux-${ARCH}.tar.gz \
	&& tar xvfz helm-${version}-linux-${ARCH}.tar.gz \
	&& mv linux-${ARCH}/helm /usr/bin/helm \
	&& chmod +x /usr/bin/helm \
	&& /usr/bin/helm version | grep -E "${HELM}" | grep -E "${version}"


# --------------------------------------------------------------------------------------------------
# Final Image
# --------------------------------------------------------------------------------------------------
FROM cytopia/ansible:${VERSION}-awsk8s 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} awshelm"
LABEL "org.opencontainers.image.title"="Ansible ${VERSION} awshelm"
LABEL "org.opencontainers.image.description"="Ansible ${VERSION} awshelm"

COPY --from=builder /usr/bin/helm /usr/bin/helm

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