Skip to content

Commit

Permalink
Ignore unprefixed properties when the matching capability is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgitario committed Feb 23, 2023
1 parent 4cd3e4d commit 0446821
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@
import org.jboss.logging.Logger;

import io.dekorate.utils.Strings;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;

public class KubernetesConfigUtil {

private static final String DEKORATE_PREFIX = "dekorate.";
private static final String QUARKUS_PREFIX = "quarkus.";
private static final Pattern QUARKUS_DEPLOY_PATTERN = Pattern.compile("quarkus\\.([^\\.]+)\\.deploy");

private static final Set<String> ALLOWED_GENERATORS = new HashSet<>(
Arrays.asList(KUBERNETES, OPENSHIFT, KNATIVE, DOCKER, S2I));
private static final Set<String> ALLOWED_GENERATORS = Set.of(KUBERNETES, KNATIVE, OPENSHIFT);
private static final Map<String, String> ALLOWED_GENERATORS_BY_CAPABILITY = Map.of(
DOCKER, Capability.CONTAINER_IMAGE_DOCKER,
S2I, Capability.CONTAINER_IMAGE_S2I);

private static final String EXPOSE_PROPERTY_NAME = "expose";
private static final String[] EXPOSABLE_GENERATORS = { OPENSHIFT, KUBERNETES };
Expand Down Expand Up @@ -133,7 +137,7 @@ public static boolean isDeploymentEnabled() {
*
* @return A map containing the properties.
*/
public static Map<String, Object> toMap(PlatformConfiguration... platformConfigurations) {
public static Map<String, Object> toMap(Capabilities capabilities, PlatformConfiguration... platformConfigurations) {
Config config = ConfigProvider.getConfig();
Map<String, Object> result = new HashMap<>();

Expand All @@ -149,12 +153,13 @@ public static Map<String, Object> toMap(PlatformConfiguration... platformConfigu
.ifPresent(v -> quarkusPrefixed.put(DEKORATE_PREFIX + p.getConfigName() + ".version", v));
});

Set<String> allowedGenerators = allowedGenerators(capabilities);
Map<String, Object> unPrefixed = StreamSupport.stream(config.getPropertyNames().spliterator(), false)
.filter(k -> ALLOWED_GENERATORS.contains(generatorName(k)))
.filter(k -> allowedGenerators.contains(generatorName(k)))
.filter(k -> config.getOptionalValue(k, String.class).isPresent())
.collect(Collectors.toMap(k -> DEKORATE_PREFIX + k, k -> config.getValue(k, String.class)));

for (String generator : ALLOWED_GENERATORS) {
for (String generator : allowedGenerators) {
String oldKey = DEKORATE_PREFIX + generator + ".group";
String newKey = DEKORATE_PREFIX + generator + ".part-of";
if (unPrefixed.containsKey(oldKey)) {
Expand Down Expand Up @@ -206,6 +211,17 @@ private static void handleExpose(Config config, Map<String, Object> unPrefixed,
}
}

private static Set<String> allowedGenerators(Capabilities capabilities) {
Set<String> generators = new HashSet<>(ALLOWED_GENERATORS);
for (Map.Entry<String, String> generatorByCapability : ALLOWED_GENERATORS_BY_CAPABILITY.entrySet()) {
if (capabilities.isPresent(generatorByCapability.getValue())) {
generators.add(generatorByCapability.getKey());
}
}

return generators;
}

/**
* Returns the name of the generators that can handle the specified key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public void build(ApplicationInfoBuildItem applicationInfo,
throw new RuntimeException("Unable to setup environment for generating Kubernetes resources", e);
}

Map<String, Object> config = KubernetesConfigUtil.toMap(kubernetesConfig, openshiftConfig, knativeConfig);
Map<String, Object> config = KubernetesConfigUtil.toMap(capabilities, kubernetesConfig, openshiftConfig, knativeConfig);
Set<String> deploymentTargets = kubernetesDeploymentTargets.getEntriesSortedByPriority().stream()
.map(DeploymentTargetEntry::getName)
.collect(Collectors.toSet());
Expand Down

0 comments on commit 0446821

Please sign in to comment.