Skip to content

Commit

Permalink
Merge pull request #26926 from geoand/#26589
Browse files Browse the repository at this point in the history
Improve error message when multiple container image extensions are present
  • Loading branch information
geoand authored Jul 26, 2022
2 parents 0347930 + 3f0ec53 commit 0075ba0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class OpenshiftBuild implements BooleanSupplier {

private ContainerImageConfig containerImageConfig;
private final ContainerImageConfig containerImageConfig;

OpenshiftBuild(ContainerImageConfig containerImageConfig) {
this.containerImageConfig = containerImageConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public class S2iBuild implements BooleanSupplier {

private ContainerImageConfig containerImageConfig;
private final ContainerImageConfig containerImageConfig;

S2iBuild(ContainerImageConfig containerImageConfig) {
this.containerImageConfig = containerImageConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.quarkus.container.image.deployment;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
Expand All @@ -11,14 +10,19 @@

public final class ContainerImageCapabilitiesUtil {

public final static Map<String, String> CAPABILITY_TO_EXTENSION_NAME = new HashMap<>();
static {
CAPABILITY_TO_EXTENSION_NAME.put(Capability.CONTAINER_IMAGE_JIB, "quarkus-container-image-jib");
CAPABILITY_TO_EXTENSION_NAME.put(Capability.CONTAINER_IMAGE_DOCKER, "quarkus-container-image-docker");
CAPABILITY_TO_EXTENSION_NAME.put(Capability.CONTAINER_IMAGE_S2I, "quarkus-container-image-s2i");
CAPABILITY_TO_EXTENSION_NAME.put(Capability.CONTAINER_IMAGE_OPENSHIFT, "quarkus-container-image-openshift");
CAPABILITY_TO_EXTENSION_NAME.put(Capability.CONTAINER_IMAGE_BUILDPACK, "quarkus-container-image-buildpack");
}
public final static Map<String, String> CAPABILITY_TO_EXTENSION_NAME = Map.of(
Capability.CONTAINER_IMAGE_JIB, "quarkus-container-image-jib",
Capability.CONTAINER_IMAGE_DOCKER, "quarkus-container-image-docker",
Capability.CONTAINER_IMAGE_S2I, "quarkus-container-image-s2i",
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 @@ -32,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 @@ -50,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 0075ba0

Please sign in to comment.