FROM openshift/ose-base:v4.8.0.20220215.234309
ENV __doozer=update BUILD_RELEASE=202202152218.p0.g076a920.assembly.stream BUILD_VERSION=v4.8.0 OS_GIT_MAJOR=4 OS_GIT_MINOR=8 OS_GIT_PATCH=0 OS_GIT_TREE_STATE=clean OS_GIT_VERSION=4.8.0-202202152218.p0.g076a920.assembly.stream SOURCE_GIT_TREE_STATE=clean 
ENV __doozer=merge OS_GIT_COMMIT=076a920 OS_GIT_VERSION=4.8.0-202202152218.p0.g076a920.assembly.stream-076a920 SOURCE_DATE_EPOCH=1639441476 SOURCE_GIT_COMMIT=076a9209048c7d478f59084409c2dca5e3a6f2d8 SOURCE_GIT_TAG=076a920 SOURCE_GIT_URL=https://github.com/openshift/driver-toolkit 

ARG KERNEL_VERSION=''
ARG RT_KERNEL_VERSION=''
ARG RHEL_VERSION='8.4'

RUN echo ${RHEL_VERSION} > /etc/yum/vars/releasever \
    && yum config-manager --best --setopt=install_weak_deps=False --save

# kernel packages needed to build drivers / kmods 
RUN yum -y install \
    kernel-core${KERNEL_VERSION:+-}${KERNEL_VERSION} \
    kernel-devel${KERNEL_VERSION:+-}${KERNEL_VERSION} \
    kernel-headers${KERNEL_VERSION:+-}${KERNEL_VERSION} \
    kernel-modules${KERNEL_VERSION:+-}${KERNEL_VERSION} \
    kernel-modules-extra${KERNEL_VERSION:+-}${KERNEL_VERSION} \
    && yum clean all

# real-time kernel packages
RUN if [ $(arch) = x86_64 ]; then \
    yum -y install \
    kernel-rt-core${RT_KERNEL_VERSION:+-}${RT_KERNEL_VERSION} \
    kernel-rt-devel${RT_KERNEL_VERSION:+-}${RT_KERNEL_VERSION} \
    kernel-rt-modules${RT_KERNEL_VERSION:+-}${RT_KERNEL_VERSION} \
    kernel-rt-modules-extra${RT_KERNEL_VERSION:+-}${RT_KERNEL_VERSION} \
    && yum clean all ; fi

# Additional packages that are mandatory for driver-containers
RUN yum -y install elfutils-libelf-devel kmod binutils kabi-dw kernel-abi-whitelists \
    && yum clean all
    
# Additional packages that are needed for a subset (e.g DPDK) of driver-containers
RUN yum -y install xz diffutils \
    && yum clean all
    
# Find and install the GCC version used to compile the kernel
# If it cannot be found (fails on some architecutres), install the default gcc
RUN export INSTALLED_KERNEL=$(rpm -q --qf "%{VERSION}-%{RELEASE}.%{ARCH}"  kernel-core) \
&& GCC_VERSION=$(cat /lib/modules/${INSTALLED_KERNEL}/config | grep -Eo "Compiler: gcc \(GCC\) ([0-9\.]+)" | grep -Eo "([0-9\.]+)") \
&& yum -y install gcc-${GCC_VERSION} \
|| yum -y install gcc && \
yum clean all

# Packages needed to build kmods-via-containers and likely needed for driver-containers
RUN yum -y install git make \
    && yum clean all

# Packages needed to sign and run externally build kernel modules
RUN if [ $(arch) == "x86_64" ] || [ $(arch) == "aarch64" ]; then \
    ARCH_DEP_PKGS="mokutil"; fi \
    && yum -y install openssl keyutils $ARCH_DEP_PKGS \
    && yum clean all

# Add and build kmods-via-containers
COPY kmods-via-containers /tmp/kmods-via-containers

WORKDIR /tmp/kmods-via-containers

RUN make install DESTDIR=/usr/local CONFDIR=/etc/

COPY manifests /manifests


# Last layer for metadata for mapping the driver-toolkit to a specific kernel version
RUN export INSTALLED_KERNEL=$(rpm -q --qf "%{VERSION}-%{RELEASE}.%{ARCH}"  kernel-core); \
    export INSTALLED_RT_KERNEL=$(rpm -q --qf "%{VERSION}-%{RELEASE}.%{ARCH}"  kernel-rt-core); \
    echo "{ \"KERNEL_VERSION\": \"${INSTALLED_KERNEL}\", \"RT_KERNEL_VERSION\": \"${INSTALLED_RT_KERNEL}\", \"RHEL_VERSION\": \"${RHEL_VERSION}\" }" > /etc/driver-toolkit-release.json

LABEL \
        io.k8s.description="driver-toolkit is a container with the kernel packages necessary for building driver containers for deploying kernel modules/drivers on OpenShift" \
        name="openshift/ose-driver-toolkit" \
        io.openshift.release.operator="true" \
        version="v4.8.0" \
        com.redhat.component="driver-toolkit-container" \
        io.openshift.maintainer.product="OpenShift Container Platform" \
        io.openshift.maintainer.component="Driver Toolkit" \
        release="202202152218.p0.g076a920.assembly.stream" \
        io.openshift.build.commit.id="076a9209048c7d478f59084409c2dca5e3a6f2d8" \
        io.openshift.build.source-location="https://github.com/openshift/driver-toolkit" \
        io.openshift.build.commit.url="https://github.com/openshift/driver-toolkit/commit/076a9209048c7d478f59084409c2dca5e3a6f2d8"

