From 3f0ec53efd77ae9dffb90c5ac075c3714cb0518a Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 26 Jul 2022 12:37:31 +0300 Subject: [PATCH] Improve error message when multiple container image extensions are present Closes: #26789 --- .../ContainerImageCapabilitiesUtil.java | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/extensions/container-image/deployment/src/main/java/io/quarkus/container/image/deployment/ContainerImageCapabilitiesUtil.java b/extensions/container-image/deployment/src/main/java/io/quarkus/container/image/deployment/ContainerImageCapabilitiesUtil.java index 6a42d6c13cc76..c07c9b483f242 100644 --- a/extensions/container-image/deployment/src/main/java/io/quarkus/container/image/deployment/ContainerImageCapabilitiesUtil.java +++ b/extensions/container-image/deployment/src/main/java/io/quarkus/container/image/deployment/ContainerImageCapabilitiesUtil.java @@ -17,6 +17,13 @@ public final class ContainerImageCapabilitiesUtil { Capability.CONTAINER_IMAGE_OPENSHIFT, "quarkus-container-image-openshift", Capability.CONTAINER_IMAGE_BUILDPACK, "quarkus-container-image-buildpack"); + private final static Map CAPABILITY_TO_BUILDER_NAME = Map.of( + Capability.CONTAINER_IMAGE_JIB, "jib", + Capability.CONTAINER_IMAGE_DOCKER, "docker", + Capability.CONTAINER_IMAGE_S2I, "s2i", + Capability.CONTAINER_IMAGE_OPENSHIFT, "openshift", + Capability.CONTAINER_IMAGE_BUILDPACK, "buildpack"); + private ContainerImageCapabilitiesUtil() { } @@ -29,16 +36,34 @@ public static Optional getActiveContainerImageCapability(Capabilities ca if (activeContainerImageCapabilities.size() > 1) { throw new IllegalStateException(String.join(" and ", activeContainerImageCapabilities) + " were detected, at most one container-image extension can be present.\n" - + "Either remove the unneeded ones, or select one by adding the property 'quarkus.container-image.builder=' in application.properties or as a system property."); + + "Either remove the unneeded ones, or select one by setting any of the following configuration properties: " + + createBuilderSelectionPropertySuggestion(capabilities)); } return activeContainerImageCapabilities.isEmpty() ? Optional.empty() : Optional.of(activeContainerImageCapabilities.iterator().next()); } + private static StringBuilder createBuilderSelectionPropertySuggestion(Capabilities capabilities) { + StringBuilder suggestion = new StringBuilder(); + boolean isFirst = true; + for (String capability : capabilities.getCapabilities()) { + if (!isContainerImageCapability(capability)) { + continue; + } + if (!isFirst) { + suggestion.append(", "); + } + isFirst = false; + suggestion.append('\'').append("quarkus.container-image.builder=") + .append(CAPABILITY_TO_BUILDER_NAME.get(capability)).append('\''); + } + return suggestion; + } + private static Set getContainerImageCapabilities(Capabilities capabilities) { Set activeContainerImageCapabilities = new HashSet<>(); for (String capability : capabilities.getCapabilities()) { - if (capability.toLowerCase().contains("container.image")) { + if (isContainerImageCapability(capability)) { if (!CAPABILITY_TO_EXTENSION_NAME.containsKey(capability)) { throw new IllegalArgumentException("Unknown container image capability: " + capability); } @@ -47,4 +72,8 @@ private static Set getContainerImageCapabilities(Capabilities capabiliti } return activeContainerImageCapabilities; } + + private static boolean isContainerImageCapability(String capability) { + return capability.toLowerCase().contains("container.image"); + } }