Skip to content

Commit 72593b0

Browse files
committed
hack: rework container_build function
Depending on the container engine and the target architecture, multiple commands need to be executed to prepare the environment, build the image, and clean up artifacts afterward. To streamline this process, a tasks array (an array of lambdas) has been introduced, its elements are executed sequencally at the end of the container_build function. Signed-off-by: Alexander Bachmann <[email protected]>
1 parent 46aa7ba commit 72593b0

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

hack/build-image

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -205,34 +205,41 @@ def container_engine(cli):
205205
def container_build(cli, target):
206206
"""Construct and execute a command to build the target container image."""
207207
eng = container_engine(cli)
208-
args = [eng, "build"]
209-
210-
if "docker" in args[0]:
211-
# if the target arch and the host_arch are not the same, we need to use buildx
212-
if target.arch != host_arch():
213-
args = [
214-
eng,
215-
"buildx",
216-
"build",
217-
f"--builder={_BUILDX_BUILDER_NAME}",
218-
f"--platform=linux/{target.arch}",
219-
"--load"
220-
]
221-
# Docker's default builder only supports the host architecture.
222-
# Therefore, we need to create a new builder to support other
223-
# architectures. Errors are suppressed to prevent issues when
224-
# the builder is already available - this can be improved later.
225-
run(cli, [eng, "buildx", "create", f"--name={_BUILDX_BUILDER_NAME}"], check=False)
226-
elif target.arch != host_arch() or FORCE_ARCH_FLAG:
227-
# We've noticed a few small quirks when using podman with the --arch
228-
# option. The main issue is that building the client image works
229-
# but then the toolbox image fails because it somehow doesn't see
230-
# the image we just built as usable. This doesn't happen when
231-
# --arch is not provided. So if the target arch and the host_arch
232-
# are the same, skip passing the extra argument.
233-
args.append(f"--arch={target.arch}")
234-
235-
run(cli, args + create_common_container_engine_args(cli, target), check=True)
208+
tasks = []
209+
210+
# For docker cross-builds we need to use buildx
211+
if "docker" in eng and target.arch != host_arch():
212+
args = [eng, "buildx"]
213+
214+
# Docker's default builder only supports the host architecture.
215+
# Therefore, we need to create a new builder to support other
216+
# architectures, and we must ensure we start with a fresh builder
217+
# that does not contain any images from previous builds.
218+
tasks.append(lambda : run(cli, args + ["rm", _BUILDX_BUILDER_NAME], check=False))
219+
tasks.append(lambda : run(cli, args + ["create", f"--name={_BUILDX_BUILDER_NAME}"], check=True))
220+
221+
tasks.append(lambda : run(cli, args + [
222+
"build",
223+
f"--builder={_BUILDX_BUILDER_NAME}",
224+
f"--platform=linux/{target.arch}",
225+
"--load"] + create_common_container_engine_args(cli, target), check=True))
226+
227+
tasks.append(lambda : run(cli, args + ["rm", _BUILDX_BUILDER_NAME], check=True))
228+
else:
229+
args = [eng, "build"]
230+
if target.arch != host_arch() or FORCE_ARCH_FLAG:
231+
# We've noticed a few small quirks when using podman with the --arch
232+
# option. The main issue is that building the client image works
233+
# but then the toolbox image fails because it somehow doesn't see
234+
# the image we just built as usable. This doesn't happen when
235+
# --arch is not provided. So if the target arch and the host_arch
236+
# are the same, skip passing the extra argument.
237+
args += [f"--arch={target.arch}"]
238+
239+
tasks.append(lambda : run(cli, args + create_common_container_engine_args(cli, target), check=True))
240+
241+
for task in tasks:
242+
task()
236243

237244
def create_common_container_engine_args(cli, target):
238245
args = []

0 commit comments

Comments
 (0)