Skip to content

Commit 413279f

Browse files
hack: split some functions so that the original call is less sprawling
Split up the function setting up the docker buildx or podman build commands so that the higher level function is shorter overall. Best viewed with git diff -w Signed-off-by: John Mulligan <[email protected]>
1 parent 198e107 commit 413279f

File tree

1 file changed

+60
-52
lines changed

1 file changed

+60
-52
lines changed

hack/build-image

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -204,64 +204,72 @@ def container_engine(cli):
204204
return _DISCOVERED_CONTAINER_ENGINES[0]
205205

206206

207-
def container_build(cli, target):
208-
"""Construct and execute a command to build the target container image."""
207+
def _buildx_build_tasks(cli, target):
209208
eng = container_engine(cli)
210209
tasks = []
210+
args = [eng, "buildx"]
211+
212+
# Docker's default builder only supports the host architecture.
213+
# Therefore, we need to create a new builder to support other
214+
# architectures, and we must ensure we start with a fresh builder
215+
# that does not contain any images from previous builds.
216+
tasks.append({"cmd": args + ["rm", target.flat_name()], "check": False})
217+
tasks.append(
218+
{
219+
"cmd": args + ["create", f"--name={target.flat_name()}"],
220+
"check": True,
221+
}
222+
)
211223

212-
# For docker cross-builds we need to use buildx
213-
if "docker" in eng and target.arch != host_arch():
214-
args = [eng, "buildx"]
215-
216-
# Docker's default builder only supports the host architecture.
217-
# Therefore, we need to create a new builder to support other
218-
# architectures, and we must ensure we start with a fresh builder
219-
# that does not contain any images from previous builds.
220-
tasks.append(
221-
{"cmd": args + ["rm", target.flat_name()], "check": False}
222-
)
223-
tasks.append(
224-
{
225-
"cmd": args + ["create", f"--name={target.flat_name()}"],
226-
"check": True,
227-
}
228-
)
224+
tasks.append(
225+
{
226+
"cmd": args
227+
+ [
228+
"build",
229+
f"--builder={target.flat_name()}",
230+
f"--platform=linux/{target.arch}",
231+
"--load",
232+
]
233+
+ create_common_container_engine_args(cli, target),
234+
"check": True,
235+
}
236+
)
229237

230-
tasks.append(
231-
{
232-
"cmd": args
233-
+ [
234-
"build",
235-
f"--builder={target.flat_name()}",
236-
f"--platform=linux/{target.arch}",
237-
"--load",
238-
]
239-
+ create_common_container_engine_args(cli, target),
240-
"check": True,
241-
}
242-
)
238+
tasks.append({"cmd": args + ["rm", target.flat_name()], "check": True})
239+
return tasks
243240

244-
tasks.append(
245-
{"cmd": args + ["rm", target.flat_name()], "check": True}
246-
)
241+
242+
def _common_build_tasks(cli, target):
243+
# podman/common build tasks
244+
eng = container_engine(cli)
245+
tasks = []
246+
args = [eng, "build"]
247+
if target.arch != host_arch() or FORCE_ARCH_FLAG:
248+
# We've noticed a few small quirks when using podman with the
249+
# --arch option. The main issue is that building the client image
250+
# works but then the toolbox image fails because it somehow doesn't
251+
# see the image we just built as usable. This doesn't happen when
252+
# --arch is not provided. So if the target arch and the host_arch
253+
# are the same, skip passing the extra argument.
254+
args += [f"--arch={target.arch}"]
255+
256+
tasks.append(
257+
{
258+
"cmd": args + create_common_container_engine_args(cli, target),
259+
"check": True,
260+
}
261+
)
262+
return tasks
263+
264+
265+
def container_build(cli, target):
266+
"""Construct and execute a command to build the target container image."""
267+
eng = container_engine(cli)
268+
# For docker cross-builds we need to use buildx
269+
if "docker" in eng and target.arch != host_arch():
270+
tasks = _buildx_build_tasks(cli, target)
247271
else:
248-
args = [eng, "build"]
249-
if target.arch != host_arch() or FORCE_ARCH_FLAG:
250-
# We've noticed a few small quirks when using podman with the
251-
# --arch option. The main issue is that building the client image
252-
# works but then the toolbox image fails because it somehow doesn't
253-
# see the image we just built as usable. This doesn't happen when
254-
# --arch is not provided. So if the target arch and the host_arch
255-
# are the same, skip passing the extra argument.
256-
args += [f"--arch={target.arch}"]
257-
258-
tasks.append(
259-
{
260-
"cmd": args
261-
+ create_common_container_engine_args(cli, target),
262-
"check": True,
263-
}
264-
)
272+
tasks = _common_build_tasks(cli, target)
265273

266274
for task in tasks:
267275
run(cli, **task)

0 commit comments

Comments
 (0)