Skip to content

Commit

Permalink
Switch S2I builds to --as-dockerfile
Browse files Browse the repository at this point in the history
Adjust `s2i_inner`, the main method used for invoking the s2i binary,
such that it uses the "--as-dockerfile" build parameter. This causes
it to generate the image sources required to perform the S2I build,
but not to perform the build itself. There are two reasons to do
this:

1. It's closer to what OpenShift's internal source-to-image process
   does. We've hit some bugs which are triggered in an OpenShift
   deployment that are not being exercised by s2i in standalone
   mode (see some of the discussion in e.g. OPENJDK-2824).

2. Doing the build stage ourselves allows us to vary parameters of
   the build -- such as the container tooling to use -- in the
   future, smoothing progress towards running tests with Podman.

One known caveat: using s2i end-to-end, the scripts-url value is
determined in the following priority order

1. --scripts-url argument passed to s2i
2. application source .s2i/bin directory
3. path from builder image's io.openshift.s2i.scripts-url label

This commit currently maps (3) to (1), so in a scenario where the
label is specified in the builder image, and the application source
contains a .s2i/bin binary, before, the application copy would take
precedence, but now it will be ignored. This will break some tests.

Signed-off-by: Jonathan Dowland <[email protected]>
  • Loading branch information
jmtd committed Nov 21, 2024
1 parent bad1c72 commit 33cd56e
Showing 1 changed file with 49 additions and 10 deletions.
59 changes: 49 additions & 10 deletions steps/s2i_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
import re
import os
import tempfile
import docker

from container import Container
from steps import _execute

# A future version of Cekit will expose this to us, for now we hard-code
# XXX There's now three of these spread across files. Singleton?
DOCKER_API_VERSION = "1.35"
DOCKER_CLIENT = docker.APIClient(version=DOCKER_API_VERSION)

logger = logging.getLogger("cekit")

Expand All @@ -29,16 +34,50 @@ def s2i_inner(context, application, path='.', env="", incremental=False, tag="ma
mirror = "-e 'MAVEN_MIRROR_URL=%s'" % os.getenv("MAVEN_MIRROR_URL")

image_id = "integ-" + context.image
command = "s2i build --loglevel=5 --pull-policy if-not-present %s --context-dir=%s -r=%s %s %s %s %s %s %s" % (
mirror, path, tag, env, application, context.image, image_id, "--incremental" if incremental else "",
"--runtime-image="+runtime_image if runtime_image else ""
)
logger.info("Executing new S2I build with the command [%s]..." % command)

output = _execute(command)
if output:
context.config.userdata['s2i_build_log'] = output
return output

# Inspect the builder image for some S2I parameters.
labels = DOCKER_CLIENT.inspect_image(context.image).get("Config", {}).get("Labels",{})
assemble_user = labels.get("io.openshift.s2i.assemble-user", False)
scripts_url = labels.get("io.openshift.s2i.scripts-url", False)

with tempfile.TemporaryDirectory(prefix="behave-test-steps.") as workdir:
dockerfile = os.path.join(workdir, "Dockerfile")

# Perform S2I Dockerfile/source generation
command = f"""s2i build --loglevel=5 --pull-policy if-not-present\
{mirror}\
--context-dir={path}\
-r={tag}\
{env}\
{application}\
{context.image}\
{image_id}\
{"--incremental" if incremental else ""}\
{"--runtime-image="+runtime_image if runtime_image else ""}\
{"--assemble-user="+assemble_user if assemble_user else ""}\
{"--scripts-url="+scripts_url if scripts_url else ""}\
--as-dockerfile {dockerfile} \
"""
logger.info("Executing S2I with the command [%s]..." % command)
output = _execute(command)
if output:
context.config.userdata['s2i_build_log'] = output
else:
# s2i likely failed. Bail out
return False

# perform the S2I build
command = f"""docker build -t {image_id} {workdir}"""
logger.info("Executing S2I build with the command [%s]..." % command)
output = _execute(command)
if output:
oldlog = context.config.userdata.get('s2i_build_log',"")
context.config.userdata['s2i_build_log'] = oldlog + "\n" + output
else:
# S2I build failed. Bail out
return False

return output


@given(u's2i build {application} from {path} without running')
Expand Down

0 comments on commit 33cd56e

Please sign in to comment.