Skip to content

Commit

Permalink
Improve error message when multiple container image extensions are pr…
Browse files Browse the repository at this point in the history
…esent

Closes: quarkusio#26789
  • Loading branch information
geoand committed Jul 26, 2022
1 parent c712bf1 commit 3f0ec53
Showing 1 changed file with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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() {
}

Expand All @@ -29,16 +36,34 @@ public static Optional<String> 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=<extension name (without the `container-image-` prefix)>' 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<String> getContainerImageCapabilities(Capabilities capabilities) {
Set<String> 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);
}
Expand All @@ -47,4 +72,8 @@ private static Set<String> getContainerImageCapabilities(Capabilities capabiliti
}
return activeContainerImageCapabilities;
}

private static boolean isContainerImageCapability(String capability) {
return capability.toLowerCase().contains("container.image");
}
}

0 comments on commit 3f0ec53

Please sign in to comment.