From 92b42c7f6d28a6912d6a84d95e50c767e540f5a1 Mon Sep 17 00:00:00 2001 From: Marco Vedovati Date: Fri, 17 May 2019 18:50:47 +0200 Subject: [PATCH 1/2] agent: detect required Go version from versions.yaml Detect the Go version required to build the agent from the versions.yaml file in the runtime repository. Fixes: #291 Signed-off-by: Marco Vedovati --- .ci/lib.sh | 1 - .ci/setup.sh | 7 ++++++- rootfs-builder/alpine/Dockerfile.in | 12 +++++++++++- rootfs-builder/centos/Dockerfile.in | 10 +++++++++- rootfs-builder/clearlinux/Dockerfile.in | 11 ++++++++++- rootfs-builder/euleros/Dockerfile.in | 9 ++++++++- rootfs-builder/fedora/Dockerfile.in | 12 +++++++++++- rootfs-builder/rootfs.sh | 18 +++++++++++++++--- rootfs-builder/versions.txt | 1 - 9 files changed, 70 insertions(+), 11 deletions(-) delete mode 100644 rootfs-builder/versions.txt diff --git a/.ci/lib.sh b/.ci/lib.sh index 5f0db45615..f9bfcbc5ca 100644 --- a/.ci/lib.sh +++ b/.ci/lib.sh @@ -20,6 +20,5 @@ clone_tests_repo() run_static_checks() { - clone_tests_repo bash "$tests_repo_dir/.ci/static-checks.sh" "github.com/kata-containers/osbuilder" } diff --git a/.ci/setup.sh b/.ci/setup.sh index 8ddded15bf..e6ddc587af 100755 --- a/.ci/setup.sh +++ b/.ci/setup.sh @@ -22,8 +22,13 @@ elif [ "$ID" == ubuntu ];then sudo apt-get -qq update sudo apt-get install -y -qq make automake qemu-utils python-pip coreutils moreutils bc sudo pip install yamllint -else +else echo "Linux distribution not supported" fi + +clone_tests_repo bash "${cidir}/static-checks.sh" +# yq needed to correctly parse runtime/versions.yaml +make -C ${tests_repo_dir} install-yq + diff --git a/rootfs-builder/alpine/Dockerfile.in b/rootfs-builder/alpine/Dockerfile.in index 5208043d1b..94752399bf 100644 --- a/rootfs-builder/alpine/Dockerfile.in +++ b/rootfs-builder/alpine/Dockerfile.in @@ -5,4 +5,14 @@ From golang:@GO_VERSION@-alpine -RUN apk update && apk add git make bash gcc musl-dev linux-headers apk-tools-static libseccomp libseccomp-dev +RUN apk update && apk add \ + git \ + make \ + bash \ + gcc \ + musl-dev \ + linux-headers \ + apk-tools-static \ + libseccomp \ + libseccomp-dev \ + curl diff --git a/rootfs-builder/centos/Dockerfile.in b/rootfs-builder/centos/Dockerfile.in index 4e89be128a..26026374f9 100644 --- a/rootfs-builder/centos/Dockerfile.in +++ b/rootfs-builder/centos/Dockerfile.in @@ -7,7 +7,15 @@ From centos:@OS_VERSION@ @SET_PROXY@ -RUN yum -y update && yum install -y git make gcc coreutils libseccomp libseccomp-devel chrony +RUN yum -y update && yum install -y \ +git \ +make \ +gcc \ +coreutils \ +libseccomp \ +libseccomp-devel \ +chrony \ +curl # This will install the proper golang to build Kata components @INSTALL_GO@ diff --git a/rootfs-builder/clearlinux/Dockerfile.in b/rootfs-builder/clearlinux/Dockerfile.in index ff5164b795..5dbdca74a3 100644 --- a/rootfs-builder/clearlinux/Dockerfile.in +++ b/rootfs-builder/clearlinux/Dockerfile.in @@ -7,7 +7,16 @@ From fedora:27 @SET_PROXY@ -RUN dnf -y update && dnf install -y git systemd pkgconfig gcc coreutils libseccomp libseccomp-devel chrony +RUN dnf -y update && dnf install -y \ +git \ +systemd \ +pkgconfig \ +gcc \ +coreutils \ +libseccomp \ +libseccomp-devel \ +chrony \ +curl # This will install the proper golang to build Kata components @INSTALL_GO@ diff --git a/rootfs-builder/euleros/Dockerfile.in b/rootfs-builder/euleros/Dockerfile.in index 4ff79ba280..285d66293f 100644 --- a/rootfs-builder/euleros/Dockerfile.in +++ b/rootfs-builder/euleros/Dockerfile.in @@ -7,7 +7,14 @@ FROM euleros:@OS_VERSION@ @SET_PROXY@ -RUN yum -y update && yum install -y yum git make gcc coreutils chrony +RUN yum -y update && yum install -y \ +yum \ +git \ +make \ +gcc \ +coreutils \ +chrony \ +curl # This will install the proper golang to build Kata components @INSTALL_GO@ diff --git a/rootfs-builder/fedora/Dockerfile.in b/rootfs-builder/fedora/Dockerfile.in index 19e10adde6..0cd3492463 100644 --- a/rootfs-builder/fedora/Dockerfile.in +++ b/rootfs-builder/fedora/Dockerfile.in @@ -7,7 +7,17 @@ From fedora:@OS_VERSION@ @SET_PROXY@ -RUN dnf -y update && dnf install -y git redhat-release systemd pkgconfig gcc make libseccomp libseccomp-devel chrony +RUN dnf -y update && dnf install -y \ +git \ +redhat-release \ +systemd \ +pkgconfig \ +gcc \ +make \ +libseccomp \ +libseccomp-devel \ +chrony \ +curl # This will install the proper golang to build Kata components @INSTALL_GO@ diff --git a/rootfs-builder/rootfs.sh b/rootfs-builder/rootfs.sh index 7e8baa6937..55c41e8b89 100755 --- a/rootfs-builder/rootfs.sh +++ b/rootfs-builder/rootfs.sh @@ -17,6 +17,7 @@ AGENT_INIT=${AGENT_INIT:-no} KERNEL_MODULES_DIR=${KERNEL_MODULES_DIR:-""} OSBUILDER_VERSION="unknown" DOCKER_RUNTIME=${DOCKER_RUNTIME:-runc} +GO_VERSION= export GOPATH=${GOPATH:-${HOME}/go} lib_file="${script_dir}/../scripts/lib.sh" @@ -25,9 +26,6 @@ source "$lib_file" # Default architecture ARCH=$(uname -m) -# Load default versions for golang and other componets -source "${script_dir}/versions.txt" - # distro-specific config file typeset -r CONFIG_SH="config.sh" @@ -247,6 +245,18 @@ error_handler() fi } +detect_go_version() +{ + typeset -r yq=$(command -v yq || command -v ${GOPATH}/bin/yq) + [ -z "$yq" ] && die "'yq' application not found (needed to parsing minimum Go version required)" + + typeset -r runtimeVersion="${AGENT_VERSION:-master}" + typeset -r runtimeVersionsURL="https://raw.githubusercontent.com/kata-containers/runtime/${runtimeVersion}/versions.yaml" + + GO_VERSION=$(curl -sSL "${runtimeVersionsURL}" | $yq r - languages.golang.version) + info "Detected Go version: $GO_VERSION" +} + while getopts a:hlo:r:t: opt do case $opt in @@ -311,6 +321,8 @@ fi mkdir -p ${ROOTFS_DIR} +detect_go_version + if [ -n "${USE_DOCKER}" ] ; then image_name="${distro}-rootfs-osbuilder" diff --git a/rootfs-builder/versions.txt b/rootfs-builder/versions.txt deleted file mode 100644 index a686225cc8..0000000000 --- a/rootfs-builder/versions.txt +++ /dev/null @@ -1 +0,0 @@ -GO_VERSION=1.11.1 From e770e2ad1bbc41f38aa9397989698ec29a065cc0 Mon Sep 17 00:00:00 2001 From: Marco Vedovati Date: Fri, 17 May 2019 19:42:12 +0200 Subject: [PATCH 2/2] rootfs: enforce minimum Go version when building locally When building locally (without Docker), the Go version installed on the system, needed to build the agent, must satisfy the minimum Go version requirement specified in runtime/versions.yaml. Signed-off-by: Marco Vedovati --- rootfs-builder/rootfs.sh | 56 +++++++++++++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/rootfs-builder/rootfs.sh b/rootfs-builder/rootfs.sh index 55c41e8b89..fa8c68a682 100755 --- a/rootfs-builder/rootfs.sh +++ b/rootfs-builder/rootfs.sh @@ -250,11 +250,46 @@ detect_go_version() typeset -r yq=$(command -v yq || command -v ${GOPATH}/bin/yq) [ -z "$yq" ] && die "'yq' application not found (needed to parsing minimum Go version required)" - typeset -r runtimeVersion="${AGENT_VERSION:-master}" - typeset -r runtimeVersionsURL="https://raw.githubusercontent.com/kata-containers/runtime/${runtimeVersion}/versions.yaml" + local runtimeRevision= - GO_VERSION=$(curl -sSL "${runtimeVersionsURL}" | $yq r - languages.golang.version) - info "Detected Go version: $GO_VERSION" + if [ "${AGENT_VERSION:-master}" == "master" ]; then + # This matches both AGENT_VERSION == "" and AGENT_VERSION == "master" + runtimeRevision="master" + else + # Detect runtime revision by fetching the agent's VERSION file + runtimeRevision="$(curl -fsSL https://raw.githubusercontent.com/kata-containers/agent/${AGENT_VERSION:-master}/VERSION)" + [ -z "$runtimeRevision" ] && die "Could not detect the agent version for the given AGENT_VERSION='${AGENT_VERSION:-master}'" + fi + + typeset -r runtimeVersionsURL="https://raw.githubusercontent.com/kata-containers/runtime/${runtimeRevision}/versions.yaml" + GO_VERSION="$(curl -fsSL "$runtimeVersionsURL" | $yq r - "languages.golang.version")" + + [ "$?" == "0" ] && [ "$GO_VERSION" != "null" ] +} + +# Compares two SEMVER-style versions passed as arguments, up to the MINOR version +# number. +# Returns a zero exit code if the version specified by the first argument is +# older OR equal than / to the version in the second argument, non-zero exit +# code otherwise. +compare_versions() +{ + typeset -i -a v1=($(echo "$1" | awk 'BEGIN {FS = "."} {print $1" "$2}')) + typeset -i -a v2=($(echo "$2" | awk 'BEGIN {FS = "."} {print $1" "$2}')) + + # Sanity check: first version can't be all zero + [ "${v1[0]}" -eq "0" ] && \ + [ "${v1[1]}" -eq "0" ] && \ + die "Failed to parse version number" + + # Major + [ "${v1[0]}" -gt "${v2[0]}" ] && { false; return; } + + # Minor + [ "${v1[0]}" -eq "${v2[0]}" ] && \ + [ "${v1[1]}" -gt "${v2[1]}" ] && { false; return; } + + true } while getopts a:hlo:r:t: opt @@ -321,9 +356,18 @@ fi mkdir -p ${ROOTFS_DIR} -detect_go_version +detect_go_version || + die "Could not detect the required Go version for AGENT_VERSION='${AGENT_VERSION:-master}'." -if [ -n "${USE_DOCKER}" ] ; then +echo "Required Go version: $GO_VERSION" + +if [ -z "${USE_DOCKER}" ] ; then + #Generate an error if the local Go version is too old + foundVersion=$(go version | sed -E "s/^.+([0-9]+\.[0-9]+\.[0-9]+).*$/\1/g") + + compare_versions "$GO_VERSION" $foundVersion || \ + die "Your Go version $foundVersion is older than the minimum expected Go version $GO_VERSION" +else image_name="${distro}-rootfs-osbuilder" generate_dockerfile "${distro_config_dir}"