gnodet-bot commented on code in PR #30672: URL: https://github.com/apache/camel/pull/30672#discussion_r4667310544
########## test-infra/camel-test-infra-common/src/main/java/org/apache/camel/test/infra/common/HostArchitectureImagePullPolicy.java: ########## @@ -0,0 +1,165 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.test.infra.common; + +import java.util.Locale; + +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.command.InspectImageResponse; +import com.github.dockerjava.api.exception.NotFoundException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.DockerClientFactory; +import org.testcontainers.containers.ContainerFetchException; +import org.testcontainers.images.ImagePullPolicy; +import org.testcontainers.images.TimeLimitedLoggedPullImageResultCallback; +import org.testcontainers.utility.DockerImageName; +import org.testcontainers.utility.DockerLoggerFactory; + +/** + * Testcontainers {@link ImagePullPolicy} making sure that the image used matches the architecture of the host. + * <p> + * When pulling an image fails (for instance because the registry mirror replies {@code toomanyrequests}), + * Testcontainers silently retries the pull for {@code linux/amd64}. On hosts with another architecture (ppc64le, s390x) + * this leaves an image with the wrong architecture in the local cache, that is then reused by all subsequent runs and + * fails with {@code exec format error}. This is reported to Testcontainers in + * <a href="https://github.com/testcontainers/testcontainers-java/issues/12180">testcontainers-java#12180</a>, this + * policy can be removed once it is fixed there. + * <p> + * When the check is enabled, this policy: + * <ul> + * <li>removes a local image whose architecture differs from the host one, and logs it</li> + * <li>pulls the image itself, explicitly for the host platform, without any fallback to another architecture. If this + * fails a {@link ContainerFetchException} is thrown, which is retried by + * {@link org.apache.camel.test.infra.common.services.TestServiceUtil#tryInitialize}</li> + * </ul> + * The check is enabled by default on ppc64le and s390x hosts, and can be forced on or off with the + * {@value #CHECK_PROPERTY} system property. When disabled, the behavior is the same as the Testcontainers default + * policy: an image is only pulled if it is not available locally. + * <p> + * This policy is registered through the {@code testcontainers.properties} file of this module. + */ +public class HostArchitectureImagePullPolicy implements ImagePullPolicy { + + public static final String CHECK_PROPERTY = "camel.test.infra.image.arch.check"; + + private static final Logger LOG = LoggerFactory.getLogger(HostArchitectureImagePullPolicy.class); + + private final String hostArch; + private final boolean enabled; + + public HostArchitectureImagePullPolicy() { + this.hostArch = normalizeArch(System.getProperty("os.arch")); + this.enabled = Boolean.parseBoolean( + System.getProperty(CHECK_PROPERTY, String.valueOf(isCheckedByDefault(hostArch)))); + } + + @Override + public boolean shouldPull(DockerImageName imageName) { + DockerClient client = DockerClientFactory.lazyClient(); + String name = imageName.asCanonicalNameString(); + + String localArch = inspectLocalArch(client, name); + if (!enabled) { + return localArch == null; + } + + if (localArch != null) { + if (hostArch.equals(normalizeArch(localArch))) { + return false; + } + LOG.warn("Local image {} has architecture {} but the host is {}, removing it and pulling it again", + name, localArch, hostArch); + removeImage(client, name); + } + + pullForHostPlatform(client, imageName); + return false; + } + + private void pullForHostPlatform(DockerClient client, DockerImageName imageName) { + String name = imageName.asCanonicalNameString(); + String platform = "linux/" + hostArch; + LOG.info("Pulling image {} for platform {}", name, platform); + try { + client.pullImageCmd(imageName.getUnversionedPart()) + .withTag(imageName.getVersionPart()) + .withPlatform(platform) + .exec(new TimeLimitedLoggedPullImageResultCallback(DockerLoggerFactory.getLogger(name))) + .awaitCompletion(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new ContainerFetchException("Interrupted while pulling image " + name + " for platform " + platform, e); + } catch (RuntimeException e) { + // Do not let Testcontainers fall back to linux/amd64, this would leave an image that cannot run on this host + throw new ContainerFetchException( + "Failed to pull image " + name + " for platform " + platform + " (set -D" + CHECK_PROPERTY + + "=false to disable the architecture check): " + e.getMessage(), + e); + } + + String pulledArch = inspectLocalArch(client, name); + if (pulledArch != null && !hostArch.equals(normalizeArch(pulledArch))) { Review Comment: ⚠️ **Post-pull null-arch gap:** if `inspectLocalArch` returns `null` here (inspect failed or no arch metadata), the condition short-circuits to `false` and the method silently accepts the state as "image is correct". This is asymmetric with the pre-pull path where `null` triggers a pull. Consider: ```suggestion if (pulledArch == null) { LOG.warn("Image {} has no architecture metadata after pulling for platform {}", name, platform); } else if (!hostArch.equals(normalizeArch(pulledArch))) { LOG.warn("Image {} has architecture {} after pulling it for platform {}, removing it", name, pulledArch, platform); removeImage(client, name); throw new ContainerFetchException( "Image " + name + " has architecture " + pulledArch + " but the host is " + hostArch); } ``` ########## test-infra/camel-test-infra-common/src/main/java/org/apache/camel/test/infra/common/HostArchitectureImagePullPolicy.java: ########## @@ -0,0 +1,165 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.test.infra.common; + +import java.util.Locale; + +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.command.InspectImageResponse; +import com.github.dockerjava.api.exception.NotFoundException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.DockerClientFactory; +import org.testcontainers.containers.ContainerFetchException; +import org.testcontainers.images.ImagePullPolicy; +import org.testcontainers.images.TimeLimitedLoggedPullImageResultCallback; +import org.testcontainers.utility.DockerImageName; +import org.testcontainers.utility.DockerLoggerFactory; + +/** + * Testcontainers {@link ImagePullPolicy} making sure that the image used matches the architecture of the host. + * <p> + * When pulling an image fails (for instance because the registry mirror replies {@code toomanyrequests}), + * Testcontainers silently retries the pull for {@code linux/amd64}. On hosts with another architecture (ppc64le, s390x) + * this leaves an image with the wrong architecture in the local cache, that is then reused by all subsequent runs and + * fails with {@code exec format error}. This is reported to Testcontainers in + * <a href="https://github.com/testcontainers/testcontainers-java/issues/12180">testcontainers-java#12180</a>, this + * policy can be removed once it is fixed there. + * <p> + * When the check is enabled, this policy: + * <ul> + * <li>removes a local image whose architecture differs from the host one, and logs it</li> + * <li>pulls the image itself, explicitly for the host platform, without any fallback to another architecture. If this + * fails a {@link ContainerFetchException} is thrown, which is retried by + * {@link org.apache.camel.test.infra.common.services.TestServiceUtil#tryInitialize}</li> + * </ul> + * The check is enabled by default on ppc64le and s390x hosts, and can be forced on or off with the + * {@value #CHECK_PROPERTY} system property. When disabled, the behavior is the same as the Testcontainers default + * policy: an image is only pulled if it is not available locally. + * <p> + * This policy is registered through the {@code testcontainers.properties} file of this module. + */ +public class HostArchitectureImagePullPolicy implements ImagePullPolicy { + + public static final String CHECK_PROPERTY = "camel.test.infra.image.arch.check"; + + private static final Logger LOG = LoggerFactory.getLogger(HostArchitectureImagePullPolicy.class); + + private final String hostArch; + private final boolean enabled; + + public HostArchitectureImagePullPolicy() { + this.hostArch = normalizeArch(System.getProperty("os.arch")); + this.enabled = Boolean.parseBoolean( + System.getProperty(CHECK_PROPERTY, String.valueOf(isCheckedByDefault(hostArch)))); + } + + @Override + public boolean shouldPull(DockerImageName imageName) { + DockerClient client = DockerClientFactory.lazyClient(); + String name = imageName.asCanonicalNameString(); + + String localArch = inspectLocalArch(client, name); + if (!enabled) { + return localArch == null; + } + + if (localArch != null) { + if (hostArch.equals(normalizeArch(localArch))) { + return false; + } + LOG.warn("Local image {} has architecture {} but the host is {}, removing it and pulling it again", + name, localArch, hostArch); + removeImage(client, name); + } + + pullForHostPlatform(client, imageName); + return false; + } + + private void pullForHostPlatform(DockerClient client, DockerImageName imageName) { + String name = imageName.asCanonicalNameString(); + String platform = "linux/" + hostArch; + LOG.info("Pulling image {} for platform {}", name, platform); + try { + client.pullImageCmd(imageName.getUnversionedPart()) + .withTag(imageName.getVersionPart()) + .withPlatform(platform) + .exec(new TimeLimitedLoggedPullImageResultCallback(DockerLoggerFactory.getLogger(name))) + .awaitCompletion(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new ContainerFetchException("Interrupted while pulling image " + name + " for platform " + platform, e); + } catch (RuntimeException e) { + // Do not let Testcontainers fall back to linux/amd64, this would leave an image that cannot run on this host + throw new ContainerFetchException( + "Failed to pull image " + name + " for platform " + platform + " (set -D" + CHECK_PROPERTY + + "=false to disable the architecture check): " + e.getMessage(), + e); + } + + String pulledArch = inspectLocalArch(client, name); + if (pulledArch != null && !hostArch.equals(normalizeArch(pulledArch))) { + LOG.warn("Image {} has architecture {} after pulling it for platform {}, removing it", name, pulledArch, + platform); + removeImage(client, name); + throw new ContainerFetchException( + "Image " + name + " has architecture " + pulledArch + " but the host is " + hostArch); + } + } + + private static String inspectLocalArch(DockerClient client, String name) { + try { + InspectImageResponse image = client.inspectImageCmd(name).exec(); + return image.getArch(); + } catch (NotFoundException e) { + return null; + } + } + + private static void removeImage(DockerClient client, String name) { + try { + client.removeImageCmd(name).withForce(true).exec(); Review Comment: 💡 **`withForce(true)` rationale:** Docker's `--force` removes the image tag even if running containers reference it. In a parallel CI environment two tests could try to start the same (wrong-arch) image simultaneously, and the second call to `removeImage` might fail without `force` (image in use). `withForce` avoids that failure, but a brief inline comment explaining the trade-off would help future readers. ########## test-infra/camel-test-infra-common/src/main/java/org/apache/camel/test/infra/common/HostArchitectureImagePullPolicy.java: ########## @@ -0,0 +1,165 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.test.infra.common; + +import java.util.Locale; + +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.command.InspectImageResponse; +import com.github.dockerjava.api.exception.NotFoundException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.DockerClientFactory; +import org.testcontainers.containers.ContainerFetchException; +import org.testcontainers.images.ImagePullPolicy; +import org.testcontainers.images.TimeLimitedLoggedPullImageResultCallback; +import org.testcontainers.utility.DockerImageName; +import org.testcontainers.utility.DockerLoggerFactory; + +/** + * Testcontainers {@link ImagePullPolicy} making sure that the image used matches the architecture of the host. + * <p> + * When pulling an image fails (for instance because the registry mirror replies {@code toomanyrequests}), + * Testcontainers silently retries the pull for {@code linux/amd64}. On hosts with another architecture (ppc64le, s390x) + * this leaves an image with the wrong architecture in the local cache, that is then reused by all subsequent runs and + * fails with {@code exec format error}. This is reported to Testcontainers in + * <a href="https://github.com/testcontainers/testcontainers-java/issues/12180">testcontainers-java#12180</a>, this + * policy can be removed once it is fixed there. + * <p> + * When the check is enabled, this policy: + * <ul> + * <li>removes a local image whose architecture differs from the host one, and logs it</li> + * <li>pulls the image itself, explicitly for the host platform, without any fallback to another architecture. If this + * fails a {@link ContainerFetchException} is thrown, which is retried by + * {@link org.apache.camel.test.infra.common.services.TestServiceUtil#tryInitialize}</li> + * </ul> + * The check is enabled by default on ppc64le and s390x hosts, and can be forced on or off with the + * {@value #CHECK_PROPERTY} system property. When disabled, the behavior is the same as the Testcontainers default + * policy: an image is only pulled if it is not available locally. + * <p> + * This policy is registered through the {@code testcontainers.properties} file of this module. + */ +public class HostArchitectureImagePullPolicy implements ImagePullPolicy { + + public static final String CHECK_PROPERTY = "camel.test.infra.image.arch.check"; + + private static final Logger LOG = LoggerFactory.getLogger(HostArchitectureImagePullPolicy.class); + + private final String hostArch; + private final boolean enabled; + + public HostArchitectureImagePullPolicy() { + this.hostArch = normalizeArch(System.getProperty("os.arch")); + this.enabled = Boolean.parseBoolean( + System.getProperty(CHECK_PROPERTY, String.valueOf(isCheckedByDefault(hostArch)))); + } + + @Override + public boolean shouldPull(DockerImageName imageName) { Review Comment: 💡 **Thread-safety note:** `shouldPull()` is stateless with respect to instance fields (only constructor-set `hostArch`/`enabled`), but the Docker operations (inspect → remove → pull) are not atomic. If two threads concurrently process the same image name, both could pass the `localArch != hostArch` check and attempt concurrent removes. In practice Testcontainers serializes `shouldPull` calls per image, but a Javadoc `@implNote` documenting the assumption would be defensive. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
