Light, fluffy, and always free - The AWS Local Emulator alternative
refactor(docker): share the network-helper image build instead of copying it (#4406)
Priamo Germosen6f75f68aea36
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/LocallyBuiltHelperImage.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/LocallyBuiltHelperImage.javanew file mode 100644index 0000000000..e2bd7a80d9--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/LocallyBuiltHelperImage.java@@ -0,0 +1,72 @@+package io.github.hectorvent.floci.core.common.docker;++import com.github.dockerjava.api.DockerClient;+import com.github.dockerjava.api.command.BuildImageResultCallback;+import com.github.dockerjava.api.exception.NotFoundException;+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;+import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;++import java.io.ByteArrayInputStream;+import java.io.ByteArrayOutputStream;+import java.io.IOException;+import java.io.InputStream;+import java.util.Set;++/**+ * Builds a Floci-recipe helper image in the workload daemon the first time something needs it,+ * instead of trying to pull an image that only exists as a Dockerfile checked into Floci itself.+ * Security-group enforcement is the caller today; this exists as a shared utility because a second+ * one is coming (an ECS task-role credentials proxy needs the same image), and the alternative was+ * a verbatim copy of the build alongside the original. The Dockerfile-to-image build handling, and+ * the guard that only builds when the caller is still pointed at Floci's own default, live here.+ */+public final class LocallyBuiltHelperImage {++ private LocallyBuiltHelperImage() {+ }++ /**+ * @param configuredImage the image the caller is actually configured to use+ * @param defaultImage the caller's own built-in default; a caller pointed anywhere else is+ * responsible for that image being pullable, so nothing is built for it here+ * @param dockerfileResourcePath classpath location of the Dockerfile to build, e.g.+ * {@code "/docker/network-helper.Dockerfile"}+ */+ public static synchronized void ensureBuilt(DockerClient dockerClient, ContainerBuilder containerBuilder,+ String configuredImage, String defaultImage,+ String dockerfileResourcePath) {+ if (!defaultImage.equals(configuredImage)) {+ return;+ }+ String image = containerBuilder.resolveImage(configuredImage);+ try {+ dockerClient.inspectImageCmd(image).exec();+ return;+ } catch (NotFoundException missing) {+ // Build the versioned Floci recipe in the daemon used for workloads.+ }+ try (InputStream dockerfile = LocallyBuiltHelperImage.class.getResourceAsStream(dockerfileResourcePath)) {+ if (dockerfile == null) {+ throw new IllegalStateException("Floci helper Dockerfile is missing: " + dockerfileResourcePath);+ }+ byte[] content = dockerfile.readAllBytes();+ ByteArrayOutputStream archive = new ByteArrayOutputStream(content.length + 1024);+ try (TarArchiveOutputStream tar = new TarArchiveOutputStream(archive)) {+ TarArchiveEntry entry = new TarArchiveEntry("Dockerfile");+ entry.setSize(content.length);+ tar.putArchiveEntry(entry);+ tar.write(content);+ tar.closeArchiveEntry();+ }+ try (BuildImageResultCallback callback = new BuildImageResultCallback()) {+ String built = dockerClient.buildImageCmd(new ByteArrayInputStream(archive.toByteArray()))+ .withTags(Set.of(image)).exec(callback).awaitImageId();+ if (built == null || built.isBlank()) {+ throw new IllegalStateException("Docker did not build the Floci helper image: " + image);+ }+ }+ } catch (IOException e) {+ throw new IllegalStateException("Cannot build the Floci helper image: " + image, e);+ }+ }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/SecurityGroupFirewallManager.java b/src/main/java/io/github/hectorvent/floci/services/ec2/SecurityGroupFirewallManager.javaindex 3640121257..3f8776347a 100644--- a/src/main/java/io/github/hectorvent/floci/services/ec2/SecurityGroupFirewallManager.java+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/SecurityGroupFirewallManager.java@@ -2,7 +2,6 @@ package io.github.hectorvent.floci.services.ec2;import com.github.dockerjava.api.DockerClient;import com.github.dockerjava.api.async.ResultCallback;-import com.github.dockerjava.api.command.BuildImageResultCallback;import com.github.dockerjava.api.command.InspectContainerResponse;import com.github.dockerjava.api.exception.NotFoundException;import com.github.dockerjava.api.model.Container;@@ -15,6 +14,7 @@ import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;import io.github.hectorvent.floci.core.common.docker.ContainerSpec;import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper;+import io.github.hectorvent.floci.core.common.docker.LocallyBuiltHelperImage;import io.github.hectorvent.floci.services.ec2.model.SecurityGroup;import jakarta.annotation.PostConstruct;import jakarta.enterprise.context.ApplicationScoped;@@ -23,8 +23,6 @@ import org.jboss.logging.Logger;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;-import java.io.IOException;-import java.io.InputStream;import java.nio.charset.StandardCharsets;import java.util.ArrayList;import java.util.HashMap;@@ -134,41 +132,10 @@ public class SecurityGroupFirewallManager {}}- private synchronized void ensureHelperImage() {- String configured = config.network().securityGroupEnforcement().helperImage();- if (!"floci/network-helper:local".equals(configured)) {- return;- }- String image = containerBuilder.resolveImage(configured);- try {- dockerClient.inspectImageCmd(image).exec();- return;- } catch (NotFoundException missing) {- // Build the versioned Floci recipe in the daemon used for workloads.- }- try (InputStream dockerfile = getClass().getResourceAsStream("/docker/network-helper.Dockerfile")) {- if (dockerfile == null) {- throw new IllegalStateException("Floci network helper Dockerfile is missing");- }- byte[] content = dockerfile.readAllBytes();- ByteArrayOutputStream archive = new ByteArrayOutputStream(content.length + 1024);- try (TarArchiveOutputStream tar = new TarArchiveOutputStream(archive)) {- TarArchiveEntry entry = new TarArchiveEntry("Dockerfile");- entry.setSize(content.length);- tar.putArchiveEntry(entry);- tar.write(content);- tar.closeArchiveEntry();- }- try (BuildImageResultCallback callback = new BuildImageResultCallback()) {- String built = dockerClient.buildImageCmd(new ByteArrayInputStream(archive.toByteArray()))- .withTags(Set.of(image)).exec(callback).awaitImageId();- if (built == null || built.isBlank()) {- throw new IllegalStateException("Docker did not build the Floci network helper image");- }- }- } catch (IOException e) {- throw new IllegalStateException("Cannot build the Floci network helper image", e);- }+ private void ensureHelperImage() {+ LocallyBuiltHelperImage.ensureBuilt(dockerClient, containerBuilder,+ config.network().securityGroupEnforcement().helperImage(), "floci/network-helper:local",+ "/docker/network-helper.Dockerfile");}/** Registers only after the default-deny table exists, before workload startup. */diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/LocallyBuiltHelperImageTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/LocallyBuiltHelperImageTest.javanew file mode 100644index 0000000000..e9aa3d0919--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/LocallyBuiltHelperImageTest.java@@ -0,0 +1,126 @@+package io.github.hectorvent.floci.core.common.docker;++import com.github.dockerjava.api.DockerClient;+import com.github.dockerjava.api.command.BuildImageCmd;+import com.github.dockerjava.api.command.BuildImageResultCallback;+import com.github.dockerjava.api.command.InspectImageCmd;+import com.github.dockerjava.api.command.InspectImageResponse;+import com.github.dockerjava.api.exception.NotFoundException;+import org.junit.jupiter.api.BeforeEach;+import org.junit.jupiter.api.Test;++import java.io.InputStream;++import static org.junit.jupiter.api.Assertions.assertThrows;+import static org.junit.jupiter.api.Assertions.assertTrue;+import static org.mockito.ArgumentMatchers.any;+import static org.mockito.Mockito.RETURNS_SELF;+import static org.mockito.Mockito.mock;+import static org.mockito.Mockito.never;+import static org.mockito.Mockito.verify;+import static org.mockito.Mockito.when;++/**+ * Covers the decisions this helper makes before it ever touches a Dockerfile: that a caller pointed+ * at something other than Floci's own default is left to pull that image itself, that an image+ * already in the daemon is not rebuilt, and that a build which produces no image id fails instead+ * of reporting success. All are branches the callers depend on and none needs a real daemon, so+ * they are pinned here rather than only through the EC2 security-group path, which uses the default+ * image and is skipped without Docker.+ */+class LocallyBuiltHelperImageTest {++ private static final String DEFAULT_IMAGE = "floci/network-helper:local";+ private static final String DOCKERFILE = "/docker/network-helper.Dockerfile";++ private DockerClient dockerClient;+ private ContainerBuilder containerBuilder;++ @BeforeEach+ void setUp() {+ dockerClient = mock(DockerClient.class);+ containerBuilder = mock(ContainerBuilder.class);+ }++ @Test+ void buildsNothingWhenTheCallerIsPointedAtSomeOtherImage() {+ LocallyBuiltHelperImage.ensureBuilt(dockerClient, containerBuilder,+ "ghcr.io/someone/their-helper:2", DEFAULT_IMAGE, DOCKERFILE);++ // Not even an inspect: that image is the caller's own to make pullable.+ verify(dockerClient, never()).inspectImageCmd(any());+ verify(dockerClient, never()).buildImageCmd(any(InputStream.class));+ verify(containerBuilder, never()).resolveImage(any());+ }++ @Test+ void buildsNothingWhenTheImageIsAlreadyInTheDaemon() {+ stubResolvedImage();+ InspectImageCmd inspectCmd = mock(InspectImageCmd.class);+ when(dockerClient.inspectImageCmd(DEFAULT_IMAGE)).thenReturn(inspectCmd);+ when(inspectCmd.exec()).thenReturn(mock(InspectImageResponse.class));++ LocallyBuiltHelperImage.ensureBuilt(dockerClient, containerBuilder,+ DEFAULT_IMAGE, DEFAULT_IMAGE, DOCKERFILE);++ verify(dockerClient, never()).buildImageCmd(any(InputStream.class));+ }++ @Test+ void buildsFromTheDockerfileWhenTheImageIsMissing() {+ stubMissingImage();+ stubBuildReturning("sha256:built");++ LocallyBuiltHelperImage.ensureBuilt(dockerClient, containerBuilder,+ DEFAULT_IMAGE, DEFAULT_IMAGE, DOCKERFILE);++ verify(dockerClient).buildImageCmd(any(InputStream.class));+ }++ @Test+ void failsWhenTheBuildProducesNoImageId() {+ stubMissingImage();+ stubBuildReturning(null);++ IllegalStateException thrown = assertThrows(IllegalStateException.class,+ () -> LocallyBuiltHelperImage.ensureBuilt(dockerClient, containerBuilder,+ DEFAULT_IMAGE, DEFAULT_IMAGE, DOCKERFILE));++ assertTrue(thrown.getMessage().contains(DEFAULT_IMAGE), thrown.getMessage());+ }++ @Test+ void failsLoudlyWhenTheDockerfileIsNotOnTheClasspath() {+ stubMissingImage();++ IllegalStateException thrown = assertThrows(IllegalStateException.class,+ () -> LocallyBuiltHelperImage.ensureBuilt(dockerClient, containerBuilder,+ DEFAULT_IMAGE, DEFAULT_IMAGE, "/docker/does-not-exist.Dockerfile"));++ assertTrue(thrown.getMessage().contains("/docker/does-not-exist.Dockerfile"), thrown.getMessage());+ verify(dockerClient, never()).buildImageCmd(any(InputStream.class));+ }++ private void stubResolvedImage() {+ when(containerBuilder.resolveImage(DEFAULT_IMAGE)).thenReturn(DEFAULT_IMAGE);+ }++ private void stubMissingImage() {+ stubResolvedImage();+ InspectImageCmd inspectCmd = mock(InspectImageCmd.class);+ when(dockerClient.inspectImageCmd(DEFAULT_IMAGE)).thenReturn(inspectCmd);+ when(inspectCmd.exec()).thenThrow(new NotFoundException("no such image"));+ }++ /**+ * Returns a stub callback rather than the one the helper passes in: awaiting the real+ * {@link BuildImageResultCallback} would block, since no daemon ever feeds it events.+ */+ private void stubBuildReturning(String imageId) {+ BuildImageCmd buildCmd = mock(BuildImageCmd.class, RETURNS_SELF);+ when(dockerClient.buildImageCmd(any(InputStream.class))).thenReturn(buildCmd);+ BuildImageResultCallback result = mock(BuildImageResultCallback.class);+ when(result.awaitImageId()).thenReturn(imageId);+ when(buildCmd.exec(any())).thenReturn(result);+ }+}