mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(docker): rebuild local helpers for daemon platform and recipe (#7222)
* fix(docker): rebuild local helpers for daemon platform and recipe Preserve the complete retained helper platform and recipe feature from fork #301 on current main. Rebuild foreign-platform or stale-recipe helpers, explicitly pull the native base, and retain the isolated Docker regression and existing workload launch-platform API. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> Signed-off-by: Andre Kurait <akurait@amazon.com> * test(docker): isolate helper platform and reuse checks Give foreign-platform fixtures the current recipe label and count real Docker build requests so cached image IDs cannot mask an unnecessary rebuild. Signed-off-by: Andre Kurait <AndreKurait@gmail.com> Signed-off-by: Andre Kurait <akurait@amazon.com> * test: share cached helper image fixtures Reuse one cached-image stub and recipe hashing helper across helper-image and ECS proxy fixtures. Gate native helper execution on the shared Docker availability probe instead of an opt-in property. Preserve platform and actual build-count assertions. Addresses upstream review 5729804211. Signed-off-by: Andre Kurait <akurait@amazon.com> * test: restore shared base image after helper platform checks Preserve the original base image tag through successful, skipped and failed helper-platform fixtures. Keep the requested Docker availability gate and the existing native-build and cache-reuse assertions. Signed-off-by: Andre Kurait <akurait@amazon.com> --------- Signed-off-by: Andre Kurait <AndreKurait@gmail.com> Signed-off-by: Andre Kurait <akurait@amazon.com>
This commit is contained in:
1 parent
3c5e8936f2
commit
7bae152c69
11 files changed
+436
-62
No files matched your search
@@ -0,0 +1,42 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.InspectImageResponse;
|
||||
import com.github.dockerjava.api.exception.DockerClientException;
|
||||
import com.github.dockerjava.api.model.Info;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/** Platform selection shared by native helper builds and workload image resolution. */
|
||||
public final class DockerPlatform {
|
||||
private DockerPlatform() {}
|
||||
|
||||
public static String daemon(DockerClient dockerClient) {
|
||||
Info info = dockerClient.infoCmd().exec();
|
||||
String architecture = info.getArchitecture();
|
||||
if (architecture == null || architecture.isBlank()) {
|
||||
throw new DockerClientException("Docker did not report its architecture");
|
||||
}
|
||||
String os = info.getOsType();
|
||||
if (os == null || os.isBlank()) {
|
||||
os = "linux";
|
||||
}
|
||||
String normalizedArchitecture = switch (architecture.toLowerCase(Locale.ROOT)) {
|
||||
case "aarch64" -> "arm64";
|
||||
case "x86_64" -> "amd64";
|
||||
default -> architecture.toLowerCase(Locale.ROOT);
|
||||
};
|
||||
return os.toLowerCase(Locale.ROOT) + "/" + normalizedArchitecture;
|
||||
}
|
||||
|
||||
public static boolean matches(InspectImageResponse image, String platform) {
|
||||
if (image == null) {
|
||||
return false;
|
||||
}
|
||||
if (platform == null) {
|
||||
return true;
|
||||
}
|
||||
String[] parts = platform.split("/", 2);
|
||||
return parts.length == 2 && parts[0].equals(image.getOs()) && parts[1].equals(image.getArch());
|
||||
}
|
||||
}
|
||||
+20
-8
@@ -2,7 +2,9 @@ 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.command.InspectImageResponse;
|
||||
import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import io.github.hectorvent.floci.core.common.Digests;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
||||
|
||||
@@ -10,10 +12,11 @@ import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Builds a Floci-recipe helper image in the workload daemon the first time something needs it,
|
||||
* Builds a Floci-recipe helper image for the workload daemon's platform when needed,
|
||||
* 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
|
||||
@@ -22,6 +25,8 @@ import java.util.Set;
|
||||
*/
|
||||
public final class LocallyBuiltHelperImage {
|
||||
|
||||
static final String RECIPE_LABEL = "io.floci.helper.recipe-sha256";
|
||||
|
||||
private LocallyBuiltHelperImage() {
|
||||
}
|
||||
|
||||
@@ -39,17 +44,23 @@ public final class LocallyBuiltHelperImage {
|
||||
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.
|
||||
}
|
||||
String platform = DockerPlatform.daemon(dockerClient);
|
||||
try (InputStream dockerfile = LocallyBuiltHelperImage.class.getResourceAsStream(dockerfileResourcePath)) {
|
||||
if (dockerfile == null) {
|
||||
throw new IllegalStateException("Floci helper Dockerfile is missing: " + dockerfileResourcePath);
|
||||
}
|
||||
byte[] content = dockerfile.readAllBytes();
|
||||
String recipe = Digests.sha256Hex(content);
|
||||
try {
|
||||
InspectImageResponse cached = dockerClient.inspectImageCmd(image).exec();
|
||||
if (DockerPlatform.matches(cached, platform) && cached.getConfig() != null
|
||||
&& cached.getConfig().getLabels() != null
|
||||
&& recipe.equals(cached.getConfig().getLabels().get(RECIPE_LABEL))) {
|
||||
return;
|
||||
}
|
||||
} catch (NotFoundException missing) {
|
||||
// Build the current Floci recipe in the daemon used for workloads.
|
||||
}
|
||||
ByteArrayOutputStream archive = new ByteArrayOutputStream(content.length + 1024);
|
||||
try (TarArchiveOutputStream tar = new TarArchiveOutputStream(archive)) {
|
||||
TarArchiveEntry entry = new TarArchiveEntry("Dockerfile");
|
||||
@@ -60,7 +71,8 @@ public final class LocallyBuiltHelperImage {
|
||||
}
|
||||
try (BuildImageResultCallback callback = new BuildImageResultCallback()) {
|
||||
String built = dockerClient.buildImageCmd(new ByteArrayInputStream(archive.toByteArray()))
|
||||
.withTags(Set.of(image)).exec(callback).awaitImageId();
|
||||
.withTags(Set.of(image)).withPlatform(platform).withPull(true)
|
||||
.withLabels(Map.of(RECIPE_LABEL, recipe)).exec(callback).awaitImageId();
|
||||
if (built == null || built.isBlank()) {
|
||||
throw new IllegalStateException("Docker did not build the Floci helper image: " + image);
|
||||
}
|
||||
|
||||
+4
-32
@@ -8,18 +8,17 @@ import com.github.dockerjava.api.exception.DockerClientException;
|
||||
import com.github.dockerjava.api.exception.InternalServerErrorException;
|
||||
import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import com.github.dockerjava.api.model.AuthConfig;
|
||||
import com.github.dockerjava.api.model.Info;
|
||||
import com.github.dockerjava.api.model.PullResponseItem;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig.EcsServiceConfig.ImagePullBehavior;
|
||||
import io.github.hectorvent.floci.core.common.docker.DockerClientProducer;
|
||||
import io.github.hectorvent.floci.core.common.docker.DockerPlatform;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
@@ -83,7 +82,7 @@ public class ImageCacheService {
|
||||
return resolvedImage;
|
||||
}
|
||||
InspectImageResponse localImage = inspectLocalImage(imageUri);
|
||||
if (matchesPlatform(localImage, requestedPlatform)) {
|
||||
if (DockerPlatform.matches(localImage, requestedPlatform)) {
|
||||
resolvedImage = resolvedImageReference(imageUri, localImage);
|
||||
resolvedImages.put(imageKey, resolvedImage);
|
||||
LOG.infov("Image already present locally, skipping pull: {0}", imageUri);
|
||||
@@ -178,7 +177,7 @@ public class ImageCacheService {
|
||||
|
||||
private InspectImageResponse cachedImage(ImageKey imageKey) {
|
||||
InspectImageResponse local = inspectLocalImage(imageKey.imageUri());
|
||||
return matchesPlatform(local, imageKey.platform()) ? local : null;
|
||||
return DockerPlatform.matches(local, imageKey.platform()) ? local : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -344,38 +343,11 @@ public class ImageCacheService {
|
||||
if (resolvedPlatform != null) {
|
||||
return resolvedPlatform;
|
||||
}
|
||||
Info info = dockerClient.infoCmd().exec();
|
||||
String architecture = info.getArchitecture();
|
||||
if (architecture == null || architecture.isBlank()) {
|
||||
throw new DockerClientException("Docker did not report its architecture");
|
||||
}
|
||||
String os = info.getOsType();
|
||||
if (os == null || os.isBlank()) {
|
||||
os = "linux";
|
||||
}
|
||||
String normalizedArchitecture = switch (architecture.toLowerCase(Locale.ROOT)) {
|
||||
case "aarch64" -> "arm64";
|
||||
case "x86_64" -> "amd64";
|
||||
default -> architecture.toLowerCase(Locale.ROOT);
|
||||
};
|
||||
resolvedPlatform = os.toLowerCase(Locale.ROOT) + "/" + normalizedArchitecture;
|
||||
resolvedPlatform = DockerPlatform.daemon(dockerClient);
|
||||
daemonPlatform = resolvedPlatform;
|
||||
return resolvedPlatform;
|
||||
}
|
||||
|
||||
private static boolean matchesPlatform(InspectImageResponse image, String platform) {
|
||||
if (image == null) {
|
||||
return false;
|
||||
}
|
||||
if (platform == null) {
|
||||
return true;
|
||||
}
|
||||
String[] parts = platform.split("/", 2);
|
||||
return parts.length == 2
|
||||
&& parts[0].equals(image.getOs())
|
||||
&& parts[1].equals(image.getArch());
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the reference the container is created from. Inspecting the image cannot confirm
|
||||
* the platform on Docker 29, whose containerd image store is the default: for an image whose
|
||||
|
||||
Reference in new issue
Block a user