Skip to content

Commit

Permalink
refactor: Kubernetes extension now uses quarkus.
Browse files Browse the repository at this point in the history
  • Loading branch information
iocanel committed Feb 23, 2020
1 parent 8aaae58 commit f8fdbac
Show file tree
Hide file tree
Showing 43 changed files with 3,040 additions and 283 deletions.
333 changes: 161 additions & 172 deletions docs/src/main/asciidoc/kubernetes.adoc

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
public class Constants {

static final String KUBERNETES = "kubernetes";
static final String OPENSHIFT = "openshift";
static final String KNATIVE = "knative";

static final String DEPLOYMENT_TARGET = "kubernetes.deployment.target";
static final String DEPLOY = "quarkus.kubernetes.deploy";
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package io.quarkus.kubernetes.deployment;

import static io.quarkus.kubernetes.deployment.Constants.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;

import io.dekorate.utils.Strings;

public class KubernetesConfigUtil {

private static final String DEKORATE_PREFIX = "dekorate.";
private static final String QUARKUS_PREFIX = "quarkus.";

//Most of quarkus prefixed properties are handling directly by the config items (KubenretesConfig, OpenshiftConfig, KnativeConfig)
//We just need group, name & version parsed here, as we don't have decorators for these (low level properties).
private static final Set<String> QUARKUS_PREFIX_WHITELIST = new HashSet(Arrays.asList("group", "name", "version"));

private static final Set<String> ALLOWED_GENERATORS = new HashSet(
Arrays.asList("kubernetes", "openshift", "knative", "docker", "s2i"));
private static final Set<String> IMAGE_GENERATORS = new HashSet(Arrays.asList("docker", "s2i"));

public static List<String> getDeploymentTargets() {
return getDeploymentTargets(toMap());
}

public static List<String> getDeploymentTargets(Map<String, Object> map) {
return Arrays.stream(map.getOrDefault(DEKORATE_PREFIX + DEPLOYMENT_TARGET, KUBERNETES).toString().split(","))
.map(String::trim)
.map(String::toLowerCase)
.collect(Collectors.toList());
}

public static Optional<String> getDockerRegistry(Map<String, Object> map) {
return IMAGE_GENERATORS.stream().map(g -> map.get(DEKORATE_PREFIX + g + ".registry")).filter(p -> p != null)
.map(String::valueOf).findFirst();
}

public static Optional<String> getGroup(Map<String, Object> map) {
return ALLOWED_GENERATORS.stream().map(g -> map.get(DEKORATE_PREFIX + g + ".group")).filter(p -> p != null)
.map(String::valueOf).findFirst();
}

public static Optional<String> getName(Map<String, Object> map) {
return ALLOWED_GENERATORS.stream().map(g -> map.get(DEKORATE_PREFIX + g + ".name")).filter(p -> p != null)
.map(String::valueOf).findFirst();
}

/*
* Collects configuration properties for Kubernetes. Reads all properties and
* matches properties that match known dekorate generators. These properties may
* or may not be prefixed with `quarkus.` though the prefixed ones take
* precedence.
*
* @return A map containing the properties.
*/
public static Map<String, Object> toMap() {
Config config = ConfigProvider.getConfig();
Map<String, Object> result = new HashMap<>();

Map<String, Object> quarkusPrefixed = StreamSupport.stream(config.getPropertyNames().spliterator(), false)
.filter(s -> s.startsWith(QUARKUS_PREFIX))
.map(s -> s.replaceFirst(QUARKUS_PREFIX, ""))
.filter(k -> ALLOWED_GENERATORS.contains(generatorName(k)))
.filter(k -> QUARKUS_PREFIX_WHITELIST.contains(propertyName(k)))
.filter(k -> config.getOptionalValue(QUARKUS_PREFIX + k, String.class).isPresent())
.collect(Collectors.toMap(k -> DEKORATE_PREFIX + k,
k -> config.getValue(QUARKUS_PREFIX + k, String.class)));

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

result.putAll(UnPrefixed);
result.putAll(quarkusPrefixed);
return result;
}

/**
* Returns the name of the generators that can handle the specified key.
*
* @param key The key.
* @return The generator name or null if the key format is unexpected.
*/
private static String generatorName(String key) {
if (Strings.isNullOrEmpty(key) || !key.contains(".")) {
return null;
}
return key.substring(0, key.indexOf("."));
}

/**
* Returns the name of the property stripped of all prefixes.
*
* @param key The key.
* @return The property name.
*/
private static String propertyName(String key) {
if (Strings.isNullOrEmpty(key) || !key.contains(".")) {
return key;
}
return key.substring(key.lastIndexOf(".") + 1);
}

private <T> T[] toArray(List<T> list) {
Class clazz = list.get(0).getClass();
T[] array = (T[]) java.lang.reflect.Array.newInstance(clazz, list.size());
return list.toArray(array);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import io.quarkus.container.image.deployment.ContainerImageConfig;
import io.quarkus.deployment.util.ExecUtil;
import io.quarkus.kubernetes.deployment.config.KubernetesConfig;

public class KubernetesDeploy implements BooleanSupplier {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@

package io.quarkus.kubernetes.deployment;

import static io.quarkus.kubernetes.deployment.Constants.DEPLOYMENT_TARGET;
import static io.quarkus.kubernetes.deployment.Constants.KUBERNETES;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import javax.net.ssl.SSLHandshakeException;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.logging.Logger;

import io.dekorate.deps.kubernetes.api.model.HasMetadata;
Expand All @@ -34,13 +30,15 @@
import io.quarkus.deployment.pkg.builditem.DeploymentResultBuildItem;
import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem;
import io.quarkus.kubernetes.client.spi.KubernetesClientBuildItem;
import io.quarkus.kubernetes.deployment.config.KubernetesConfig;

public class KubernetesDeployer {

private static final Logger LOG = Logger.getLogger(KubernetesDeployer.class);

@BuildStep(onlyIf = { IsNormal.class, KubernetesDeploy.class })
public void deploy(KubernetesClientBuildItem kubernetesClient,
public void deploy(KubernetesConfig kubernetesConfig,
KubernetesClientBuildItem kubernetesClient,
ApplicationInfoBuildItem applicationInfo,
Optional<ContainerImageResultBuildItem> containerImage,
OutputTargetBuildItem outputTarget,
Expand All @@ -51,10 +49,14 @@ public void deploy(KubernetesClientBuildItem kubernetesClient,
"A Kubernetes deployment was requested but no extension was found to build a container image. Consider adding one of following extensions: \"quarkus-container-image-jib\", \"quarkus-container-image-docker\" or \"quarkus-container-image-s2i\".");
}

Config config = ConfigProvider.getConfig();
List<DeploymentTarget> deploymentTargets = Arrays
.stream(config.getOptionalValue(DEPLOYMENT_TARGET, String.class).orElse(KUBERNETES).split(","))
.map(String::trim).map(String::toUpperCase).map(DeploymentTarget::valueOf).collect(Collectors.toList());
Map<String, Object> config = KubernetesConfigUtil.toMap();
Set<DeploymentTarget> deploymentTargets = new HashSet<>();
deploymentTargets.addAll(KubernetesConfigUtil.getDeploymentTargets(config).stream()
.map(String::toUpperCase)
.map(DeploymentTarget::valueOf)
.collect(Collectors.toList()));

deploymentTargets.addAll(kubernetesConfig.getDeploymentTarget());

final KubernetesClient client = Clients.fromConfig(kubernetesClient.getClient().getConfiguration());
DeploymentTarget target = deploymentTargets.stream().findFirst().orElse(DeploymentTarget.KUBERNETES);
Expand Down
Loading

0 comments on commit f8fdbac

Please sign in to comment.