mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 08:09:43 -05:00
refactor(docker): share the network-helper image build instead of copying it (#4406)
SecurityGroupFirewallManager built floci/network-helper:local from the Dockerfile checked into Floci itself, because that image is never pulled from a registry. A second caller needs the same thing, so the build moves to LocallyBuiltHelperImage in core/common/docker and the existing caller uses it rather than keeping its own copy: image resolution, the inspect-then-build check, the tar of the Dockerfile, and the guard that only builds when the caller is still pointed at Floci's own default all live in one place now. No behaviour change. Split out of #4293, where the second caller lives, since this stands on its own.
This commit is contained in:
1 parent
b66c433a9f
commit
6f75f68aea
3 files changed
+203
-38
No files matched your search
+72
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-38
@@ -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. */
|
||||
|
||||
Reference in new issue
Block a user