From ce45abbdb2517e74c2d25cb86d889c0c13476e49 Mon Sep 17 00:00:00 2001 From: Vladimir Nachev Date: Tue, 2 Apr 2024 16:49:33 +0300 Subject: [PATCH] Build multi-arch OCI image (#113) * Build multiplatform docker image * Add multiarch support in install config and scripts * Address review feedback * Ignore __pycache__ * Fix tests Co-authored-by: Plamen Kokanov --------- Co-authored-by: Plamen Kokanov --- .ci/pipeline_definitions | 8 ++++++++ .gitignore | 4 +++- dockerfile-configs/common-components.yaml | 6 +++--- generator/lib/dockerfile.py | 1 + generator/validate-tools.py | 6 +++++- hacks/install_etcdctl | 6 ++++-- hacks/install_k9s | 5 +++-- 7 files changed, 27 insertions(+), 9 deletions(-) diff --git a/.ci/pipeline_definitions b/.ci/pipeline_definitions index 3305015..95c9df6 100644 --- a/.ci/pipeline_definitions +++ b/.ci/pipeline_definitions @@ -10,6 +10,10 @@ ops-toolbelt: ocm_repository_mappings: - repository: europe-docker.pkg.dev/gardener-project/releases publish: + oci-builder: docker-buildx + platforms: + - linux/amd64 + - linux/arm64 dockerimages: ops-toolbelt: image: europe-docker.pkg.dev/gardener-project/snapshots/gardener/ops-toolbelt @@ -51,6 +55,10 @@ ops-toolbelt: channel_name: 'C9CEBQPGE' #internal gardener channel slack_cfg_name: 'scp_workspace' publish: + oci-builder: docker-buildx + platforms: + - linux/amd64 + - linux/arm64 dockerimages: ops-toolbelt: image: europe-docker.pkg.dev/gardener-project/releases/gardener/ops-toolbelt diff --git a/.gitignore b/.gitignore index 98f042e..ae69d49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .vscode .idea .DS_Store -generated_dockerfiles \ No newline at end of file +generated_dockerfiles + +**/__pycache__/ diff --git a/dockerfile-configs/common-components.yaml b/dockerfile-configs/common-components.yaml index 08a4d80..12cfdc9 100644 --- a/dockerfile-configs/common-components.yaml +++ b/dockerfile-configs/common-components.yaml @@ -46,7 +46,7 @@ - name: yaml2json # renovate: datasource=github-releases depName=bronze1man/yaml2json version: v1.3 - from: https://github.com/bronze1man/yaml2json/releases/download/{version}/yaml2json_linux_amd64 + from: https://github.com/bronze1man/yaml2json/releases/download/{version}/yaml2json_linux_$(echo ${{TARGETARCH}} |sed 's/x86_64/amd64/;s/arm64/arm/') info: transform yaml string to json string without the type infomation. - name: kubetail from: https://raw.githubusercontent.com/johanhaleby/kubetail/master/kubetail @@ -54,7 +54,7 @@ - name: nerdctl # renovate: datasource=github-releases depName=containerd/nerdctl version: 1.7.5 - from: https://github.com/containerd/nerdctl/releases/download/v{version}/nerdctl-{version}-linux-amd64.tar.gz + from: https://github.com/containerd/nerdctl/releases/download/v{version}/nerdctl-{version}-linux-${{TARGETARCH}}.tar.gz to: /nerdctl.tar.gz command: | tar Cxzvvf /usr/local/bin nerdctl.tar.gz &&\ @@ -66,7 +66,7 @@ - name: kubectl # renovate: datasource=github-releases depName=kubernetes/kubernetes version: v1.28.8 - from: https://dl.k8s.io/release/{version}/bin/linux/amd64/kubectl + from: https://dl.k8s.io/release/{version}/bin/linux/${{TARGETARCH}}/kubectl info: command line tool for controlling Kubernetes clusters. - bash: diff --git a/generator/lib/dockerfile.py b/generator/lib/dockerfile.py index 69a9659..d90b4b2 100644 --- a/generator/lib/dockerfile.py +++ b/generator/lib/dockerfile.py @@ -26,6 +26,7 @@ def create(self): def to_string(self): output = self.from_image + output += "ARG TARGETARCH\n" for layer in self.layers: output += layer.get_layer_as_string() return output diff --git a/generator/validate-tools.py b/generator/validate-tools.py index b71bebe..726b65f 100755 --- a/generator/validate-tools.py +++ b/generator/validate-tools.py @@ -7,6 +7,7 @@ import argparse import subprocess import shlex +import re from lib import dockerfile, commands, config_parser @@ -15,9 +16,12 @@ def validate_tools(commands_list): for command in commands_list: if isinstance(command, commands.Curl): for tool in command.get_tools(): + cmd_line = tool.get_from() + cmd_line = re.sub(r"\$\(echo \${TARGETARCH}.*\)", "amd64", cmd_line) + cmd_line = re.sub(r"\${TARGETARCH}", "amd64", cmd_line) try: subprocess.run( - ["curl", "-sLf", shlex.quote(tool.get_from())], + ["curl", "-sLf", shlex.quote(cmd_line)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True) diff --git a/hacks/install_etcdctl b/hacks/install_etcdctl index 3a39d98..290558b 100755 --- a/hacks/install_etcdctl +++ b/hacks/install_etcdctl @@ -25,10 +25,12 @@ function install () { fi fi + arch=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/') + echo "installing etcdctl version ${version}" - download_url="https://github.com/coreos/etcd/releases/download/${version}/etcd-${version}-linux-amd64.tar.gz" + download_url="https://github.com/coreos/etcd/releases/download/${version}/etcd-${version}-linux-${arch}.tar.gz" - curl -sL ${download_url} -o etcd-${version}-linux-amd64.tar.gz && tar -zxvf etcd-${version}-linux-amd64.tar.gz && mv etcd-${version}-linux-amd64/etcdctl . && rm etcd-${version}-linux-amd64.tar.gz && rm -r etcd-${version}-linux-amd64 + curl -sL ${download_url} -o etcd-${version}-linux-${arch}.tar.gz && tar -zxvf etcd-${version}-linux-${arch}.tar.gz && mv etcd-${version}-linux-${arch}/etcdctl . && rm etcd-${version}-linux-${arch}.tar.gz && rm -r etcd-${version}-linux-${arch} echo "You can now start using etcdctl. Just execute \"etcdctl\" to use it. See https://etcd.io/docs/v3.4/dev-guide/interacting_v3/ for more details." diff --git a/hacks/install_k9s b/hacks/install_k9s index 6aa8880..e373471 100755 --- a/hacks/install_k9s +++ b/hacks/install_k9s @@ -14,20 +14,21 @@ function show_help () { function install () { local version=$1 local download_url + local arch=$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/') if [ -z "$version" ] then # fetch latest local latest_release=$(curl -sL https://api.github.com/repos/derailed/k9s/releases/latest) version=$(echo "${latest_release}" | jq -r '.tag_name') echo "installing latest version ${version}" - download_url=$(echo "${latest_release}" | jq -r '.assets[] | select(.name == "k9s_Linux_amd64.tar.gz") | .browser_download_url') + download_url=$(echo "${latest_release}" | jq -r --arg filename "k9s_Linux_${arch}.tar.gz" '.assets[] | select(.name == $filename) | .browser_download_url') else if [[ ! $version == v* ]] then version="v${version}" fi echo "installing k9s version ${version}" - download_url="https://github.com/derailed/k9s/releases/download/${version}/k9s_Linux_amd64.tar.gz" + download_url="https://github.com/derailed/k9s/releases/download/${version}/k9s_Linux_${arch}.tar.gz" fi curl -sL ${download_url} -o k9s.tar.gz && tar -zxvf k9s.tar.gz k9s && mv k9s /usr/local/bin/k9s && chmod 755 /usr/local/bin/k9s && rm k9s.tar.gz