# SHA256 digest of the base image
ARG BASE_IMAGE_SHA256
ARG BASE_IMAGE=docker.io/rockylinux

FROM $BASE_IMAGE@sha256:$BASE_IMAGE_SHA256

# Kubernetes version
ARG K8S_SHORT_VERSION
ARG K8S_VERSION
# Etcd version
ARG ETCD_VERSION

# Timestamp of the build, formatted as RFC3339
ARG BUILD_DATE
# Git revision o the tree at build time
ARG VCS_REF
# Version of the image
ARG VERSION
# Version of the project, e.g. `git describe --always --long --dirty --broken`
ARG METALK8S_VERSION

# These contain BUILD_DATE so should come 'late' for layer caching
LABEL maintainer="squad-metalk8s@scality.com" \
      # http://label-schema.org/rc1/
      org.label-schema.build-date="$BUILD_DATE" \
      org.label-schema.name="metalk8s-utils" \
      org.label-schema.description="Utilities container for MetalK8s" \
      org.label-schema.url="https://github.com/scality/metalk8s/" \
      org.label-schema.vcs-url="https://github.com/scality/metalk8s.git" \
      org.label-schema.vcs-ref="$VCS_REF" \
      org.label-schema.vendor="Scality" \
      org.label-schema.version="$VERSION" \
      org.label-schema.schema-version="1.0" \
      # https://github.com/opencontainers/image-spec/blob/master/annotations.md
      org.opencontainers.image.created="$BUILD_DATE" \
      org.opencontainers.image.authors="squad-metalk8s@scality.com" \
      org.opencontainers.image.url="https://github.com/scality/metalk8s/" \
      org.opencontainers.image.source="https://github.com/scality/metalk8s.git" \
      org.opencontainers.image.version="$VERSION" \
      org.opencontainers.image.revision="$VCS_REF" \
      org.opencontainers.image.vendor="Scality" \
      org.opencontainers.image.title="metalk8s-utils" \
      org.opencontainers.image.description="Utilities container for MetalK8s" \
      # https://docs.openshift.org/latest/creating_images/metadata.html
      io.openshift.tags="metalk8s,utils" \
      io.k8s.description="Utilities container for MetalK8s" \
      io.openshift.non-scalable="true" \
      # Various
      com.scality.metalk8s.version="$METALK8S_VERSION"

# Final layers, installing tooling
RUN printf "[kubernetes]\n\
name=Kubernetes\n\
baseurl=https://pkgs.k8s.io/core:/stable:/v%s/rpm/\n\
enabled=1\n\
gpgcheck=1\n\
gpgkey=https://pkgs.k8s.io/core:/stable:/v%s/rpm/repodata/repomd.xml.key\n" \
    ${K8S_SHORT_VERSION} ${K8S_SHORT_VERSION} >/etc/yum.repos.d/kubernetes.repo && \
    microdnf clean all && \
    microdnf update -y && \
    microdnf install -y epel-release && \
    microdnf install -y --enablerepo=crb \
        bash-completion \
        bind-utils \
        bzip2 \
        conntrack-tools \
        cri-tools \
        curl \
        e2fsprogs \
        ebtables \
        ethtool \
        gdb \
        git \
        htop \
        httpd-tools \
        httpie \
        iftop \
        iotop \
        iperf \
        iperf3 \
        iproute \
        ipset \
        iptables \
        ipvsadm \
        jq \
        "kubectl-${K8S_VERSION}" \
        less \
        lsof \
        ltrace \
        lvm2 \
        mtr \
        net-tools \
        nethogs \
        nmap \
        nmap-ncat \
        openssh-clients \
        openssh-server \
        openssl \
        parted \
        qperf \
        rsync \
        screen \
        socat \
        strace \
        sysstat \
        tar \
        tcpdump \
        telnet \
        tmux \
        traceroute \
        util-linux \
        vim-minimal \
        wget \
        wireshark-cli \
        xfsprogs \
        && \
    microdnf clean all

# etcd is not packaged in RHEL/Rocky/CentOS 9 Extras repository, so we download/install
# it manually
RUN mkdir /tmp/etcd-download && \
    curl -L https://github.com/etcd-io/etcd/releases/download/${ETCD_VERSION}/etcd-${ETCD_VERSION}-linux-amd64.tar.gz \
        -o /tmp/etcd-${ETCD_VERSION}-linux-amd64.tar.gz && \
    tar xzvf /tmp/etcd-${ETCD_VERSION}-linux-amd64.tar.gz -C /tmp/etcd-download --strip-components=1 && \
    rm -f /tmp/etcd-${ETCD_VERSION}-linux-amd64.tar.gz && \
    mv /tmp/etcd-download/etcdctl /usr/bin && \
    rm -rf /tmp/etcd-download

RUN kubectl completion bash > /etc/bash_completion.d/kubectl
