Skip to content

Commit

Permalink
Pick up buildah base build arguments for container builds
Browse files Browse the repository at this point in the history
Ensure that buildah is called for kubevirt with the same base arguments
like for the oscontainer.

Signed-off-by: Roman Mohr <[email protected]>
  • Loading branch information
rmohr authored and jlebon committed Apr 12, 2022
1 parent a23c64c commit 6b50db1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
16 changes: 16 additions & 0 deletions src/cosalib/buildah.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os

# https://access.redhat.com/documentation/en-us/openshift_container_platform/4.1/html/builds/custom-builds-buildah
NESTED_BUILD_ARGS = ['--storage-driver', 'vfs']


def buildah_base_args(containers_storage=None):
buildah_base_argv = ['buildah']
if containers_storage is not None:
buildah_base_argv.append(f"--root={containers_storage}")
if os.environ.get('container') is not None:
print("Using nested container mode due to container environment variable")
buildah_base_argv.extend(NESTED_BUILD_ARGS)
else:
print("Skipping nested container mode")
return buildah_base_argv
19 changes: 14 additions & 5 deletions src/cosalib/kubevirt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import logging as log

from cosalib.cmdlib import (
run_verbose
run_verbose,
)

from cosalib.buildah import (
buildah_base_args
)

from cosalib.qemuvariants import QemuVariantImage
Expand All @@ -26,12 +30,13 @@ def write_oci(self, image_name):
"""
Take the qcow2 base image and convert it to an oci-archive.
"""
buildah_base_argv = buildah_base_args()
final_img = os.path.join(os.path.abspath(self.build_dir),
self.image_name)
buildah_img = run_verbose(["buildah", "from", "scratch"], stdout=subprocess.PIPE).stdout.decode("utf-8").strip()
run_verbose(["buildah", "add", "--chmod", "0555", buildah_img, image_name, "/disk/coreos.img"])
digest = run_verbose(["buildah", "commit", buildah_img], stdout=subprocess.PIPE).stdout.decode("utf-8").strip()
run_verbose(["buildah", "push", "--format", "oci", digest, f"oci-archive:{final_img}"])
buildah_img = run_verbose_collect_stdout(buildah_base_argv + ["from", "scratch"])
run_verbose(buildah_base_argv + ["add", "--chmod", "0555", buildah_img, image_name, "/disk/coreos.img"])
digest = run_verbose_collect_stdout(buildah_base_argv + ["commit", buildah_img])
run_verbose(buildah_base_argv + ["push", "--format", "oci", digest, f"oci-archive:{final_img}"])


def kubevirt_run_ore(build, args):
Expand Down Expand Up @@ -83,3 +88,7 @@ def get_kubevirt_variant(variant, parser, kwargs={}):
arch=parser.arch,
compress=parser.compress,
**kwargs)


def run_verbose_collect_stdout(args):
return run_verbose(args, stdout=subprocess.PIPE).stdout.decode("utf-8").strip()
15 changes: 4 additions & 11 deletions src/oscontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import shutil
import subprocess
from cosalib import cmdlib
from cosalib.buildah import (
buildah_base_args
)

OSCONTAINER_COMMIT_LABEL = 'com.coreos.ostree-commit'

# https://access.redhat.com/documentation/en-us/openshift_container_platform/4.1/html/builds/custom-builds-buildah
NESTED_BUILD_ARGS = ['--storage-driver', 'vfs']


def run_get_json(args):
return json.loads(subprocess.check_output(args))
Expand Down Expand Up @@ -113,14 +113,7 @@ def oscontainer_build(containers_storage, tmpdir, src, ref, image_name_and_tag,
else:
ostree_version = None

buildah_base_argv = ['buildah']
if containers_storage is not None:
buildah_base_argv.append(f"--root={containers_storage}")
if os.environ.get('container') is not None:
print("Using nested container mode due to container environment variable")
buildah_base_argv.extend(NESTED_BUILD_ARGS)
else:
print("Skipping nested container mode")
buildah_base_argv = buildah_base_args(containers_storage)

# In general, we just stick with the default tmpdir set up. But if a
# workdir is provided, then we want to be sure that all the heavy I/O work
Expand Down

0 comments on commit 6b50db1

Please sign in to comment.