# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
#   Red Hat, Inc. - initial API and implementation

# This Dockerfile and folder contents are manually synced from 
# https://github.com/eclipse/che-theia/tree/v7.3.2/dockerfiles/remote-plugin-kubernetes-tooling-1.0.4

# https://access.redhat.com/containers/?tab=tags#/registry.access.redhat.com/ubi8-minimal
FROM ubi8-minimal:8.2-301.1592810506

ENV \
    HELM_VERSION="v3.1.1" \
    KUBECTL_VERSION="v1.18.3" \
    KAMEL_VERSION="1.0.0" \
    HOME=/home/theia

ADD etc/storage.conf $HOME/.config/containers/storage.conf

RUN mkdir -p /home/theia /projects && \
    # Change permissions to let any arbitrary user
    for f in "${HOME}" "/etc/passwd" "/etc/group" "/projects"; do \
        echo "Changing permissions on ${f}" && chgrp -R 0 ${f} && \
        chmod -R g+rwX ${f}; \
    done

COPY etc/docker.sh /usr/local/bin/docker
COPY . /tmp/assets
# COPY repository /home/theia/.helm/repository

# NOTE: uncomment for local build. Must also set full registry path in FROM to registry.redhat.io or registry.access.redhat.com
# enable rhel 8 content sets (from Brew) to resolve buildah
# COPY content_sets_rhel8.repo /etc/yum.repos.d/

RUN microdnf install -y which dnf && dnf install -y buildah nodejs npm && \
    dnf update -y systemd buildah nodejs npm fuse-overlayfs slirp4netns gnutls && \
    dnf clean all && rm -rf /var/cache/yum && \
    echo "Installed Packages" && rpm -qa | sort -V && echo "End Of Installed Packages" && \
    cat /etc/passwd | \
    sed s#theia:x.*#theia:x:\${USER_ID}:\${GROUP_ID}::\${HOME}:/bin/bash#g \
    > ${HOME}/passwd.template && \
    cat /etc/group | \
    sed s#root:x:0:#root:x:0:0,\${USER_ID}:#g \
    > ${HOME}/group.template && \
    for d in helm kubectl kamel; do \
      if [[ -f /usr/local/bin/${d} ]]; then rm -f /usr/local/bin/${d}; fi; \
      if [[ -f /tmp/assets/asset-$(uname -m)-${d}.tar.gz ]]; then tar xzf /tmp/assets/asset-$(uname -m)-${d}.tar.gz -C /usr/local/bin/; fi; \
    done && chmod 755 /usr/local/bin/* && rm -fr /tmp/assets && \
    echo "========" && \
    echo -n "helm:    "; helm version --short --client; \
    echo -n "kubectl: "; kubectl version --short --client=true; \
    if [[ -f /usr/local/bin/kamel ]]; then \
      echo -n "kamel:   "; kamel version \
    else \
      echo "kamel: not available on $(uname -m)"; \
    fi; \
    echo "========"

# set up local Helm configuration skipping Tiller installation
# RUN mkdir -p ${HOME}/.helm/repository/cache/ && \
#     ln -s ${HOME}/.helm/repository/local/index.yaml ${HOME}/.helm/repository/cache/local-index.yaml && \
#     helm init --client-only

ADD etc/entrypoint.sh /entrypoint.sh

ENTRYPOINT [ "/entrypoint.sh" ]
CMD ${PLUGIN_REMOTE_ENDPOINT_EXECUTABLE}

ENV SUMMARY="Red Hat CodeReady Workspaces - Kubernetes plugin container" \
    DESCRIPTION="Red Hat CodeReady Workspaces - Kubernetes plugin container" \
    PRODNAME="codeready-workspaces" \
    COMPNAME="plugin-kubernetes-rhel8"

LABEL summary="$SUMMARY" \
      description="$DESCRIPTION" \
      io.k8s.description="$DESCRIPTION" \
      io.k8s.display-name="$DESCRIPTION" \
      io.openshift.tags="$PRODNAME,$COMPNAME" \
      com.redhat.component="$PRODNAME-$COMPNAME-container" \
      name="$PRODNAME/$COMPNAME" \
      version="2.2" \
      license="EPLv2" \
      maintainer="Nick Boldt <nboldt@redhat.com>" \
      io.openshift.expose-services="" \
      usage=""

