Skip to content

Commit

Permalink
Extract route/ingress configuration into a specific object
Browse files Browse the repository at this point in the history
  • Loading branch information
glefloch committed Feb 26, 2021
1 parent 0176caa commit 8068cb2
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 14 deletions.
10 changes: 6 additions & 4 deletions docs/src/main/asciidoc/deploying-to-kubernetes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,6 @@ The table below describe all the available configuration options.
| quarkus.kubernetes.arguments | String[] | |
| quarkus.kubernetes.replicas | int | | 1
| quarkus.kubernetes.service-account | String | |
| quarkus.kubernetes.host | String | |
| quarkus.kubernetes.ports | Map<String, Port> | |
| quarkus.kubernetes.service-type | ServiceType | | ClusterIP
| quarkus.kubernetes.pvc-volumes | Map<String, PersistentVolumeClaimVolume> | |
Expand All @@ -658,7 +657,9 @@ The table below describe all the available configuration options.
| quarkus.kubernetes.liveness-probe | Probe | | ( see Probe )
| quarkus.kubernetes.readiness-probe | Probe | | ( see Probe )
| quarkus.kubernetes.sidecars | Map<String, Container> | |
| quarkus.kubernetes.expose | boolean | | false
| quarkus.kubernetes.ingress.expose | boolean | | false
| quarkus.kubernetes.ingress.host | String | |
| quarkus.kubernetes.ingress.annotations | Map<String, String> | |
| quarkus.kubernetes.headless | boolean | | false
| quarkus.kubernetes.hostaliases | Map<String, HostAlias> | |
| quarkus.kubernetes.resources.requests.cpu | String | |
Expand Down Expand Up @@ -882,7 +883,6 @@ The OpenShift resources can be customized in a similar approach with Kubernetes.
| quarkus.openshift.arguments | String[] | |
| quarkus.openshift.replicas | int | | 1
| quarkus.openshift.service-account | String | |
| quarkus.openshift.host | String | |
| quarkus.openshift.ports | Map<String, Port> | |
| quarkus.openshift.service-type | ServiceType | | ClusterIP
| quarkus.openshift.pvc-volumes | Map<String, PersistentVolumeClaimVolume> | |
Expand All @@ -898,7 +898,9 @@ The OpenShift resources can be customized in a similar approach with Kubernetes.
| quarkus.openshift.liveness-probe | Probe | | ( see Probe )
| quarkus.openshift.readiness-probe | Probe | | ( see Probe )
| quarkus.openshift.sidecars | Map<String, Container> | |
| quarkus.openshift.expose | boolean | | false
| quarkus.openshift.route.expose | boolean | | false
| quarkus.openshift.route.host | String | |
| quarkus.openshift.route.annotations | Map<String, String> | |
| quarkus.openshift.headless | boolean | | false
|====

Expand Down
9 changes: 6 additions & 3 deletions docs/src/main/asciidoc/deploying-to-openshift.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,20 @@ To expose a `Route` for the Quarkus application:

[source,properties]
----
quarkus.openshift.expose=true
quarkus.openshift.route.expose=true
----

Tip: You don't necessarily need to add this property in the `application.properties`. You can pass it as a command line argument:
[TIP]
====
You don't necessarily need to add this property in the `application.properties`. You can pass it as a command line argument:
[source,bash,subs=attributes+]
----
./mvnw clean package -Dquarkus.openshift.expose=true
./mvnw clean package -Dquarkus.openshift.route.expose=true
----
The same applies to all properties listed below.
====

==== Labels

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

import io.dekorate.kubernetes.config.BaseConfigFluent;
import io.dekorate.kubernetes.config.Configurator;

public class ApplyExpositionConfigurator extends Configurator<BaseConfigFluent> {

private final ExpositionConfig expositionConfig;

public ApplyExpositionConfigurator(ExpositionConfig expositionConfig) {
this.expositionConfig = expositionConfig;
}

@Override
public void visit(BaseConfigFluent config) {
if (expositionConfig.expose) {
config.withExpose(true);
if (expositionConfig.host.isPresent()) {
config.withHost(expositionConfig.host.get());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.quarkus.kubernetes.deployment;

import java.util.Map;
import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;

@ConfigGroup
public class ExpositionConfig {

/**
* If true, the service will be exposed
*/
@ConfigItem
boolean expose;

/**
* The host under which the application is going to be exposed
*/
@ConfigItem
Optional<String> host;

/**
* Custom annotations to add to exposition (route or ingress) resources
*/
@ConfigItem
Map<String, String> annotations;

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class KnativeConfig implements PlatformConfiguration {

/**
* The host under which the application is going to be exposed
*
*/
@ConfigItem
Optional<String> host;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

package io.quarkus.kubernetes.deployment;

import static io.quarkus.kubernetes.deployment.Constants.OPENSHIFT;
import static io.quarkus.kubernetes.deployment.Constants.QUARKUS_ANNOTATIONS_BUILD_TIMESTAMP;
import static io.quarkus.kubernetes.deployment.Constants.QUARKUS_ANNOTATIONS_COMMIT_ID;
import static io.quarkus.kubernetes.deployment.Constants.QUARKUS_ANNOTATIONS_VCS_URL;
Expand Down Expand Up @@ -364,6 +365,18 @@ private static List<DecoratorBuildItem> createAnnotationDecorators(Optional<Proj
now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd - HH:mm:ss Z")), new String[0]))));
}

if (config.getExposition().isPresent() && config.getExposition().get().expose) {
Map<String, String> expostionAnnotations = config.getExposition().get().annotations;
String kind = "Ingress";
if (config.getTargetPlatformName().equals(OPENSHIFT)) {
kind = "Route";
}
for (Map.Entry<String, String> annotation : expostionAnnotations.entrySet()) {
result.add(new DecoratorBuildItem(target,
new AddAnnotationDecorator(name, annotation.getKey(), annotation.getValue(), kind)));
}
}

if (config.getPrometheusConfig().annotations) {
// Add metrics annotations
metricsConfiguration.ifPresent(m -> {
Expand All @@ -382,6 +395,7 @@ private static List<DecoratorBuildItem> createAnnotationDecorators(Optional<Proj
});
}

//Add metrics annotations
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public class KubernetesConfig implements PlatformConfiguration {

/**
* The host under which the application is going to be exposed
*
* @deprecated Use the {@code quarkus.kubernetes.ingress.host} instead
*/
@ConfigItem
Optional<String> host;
Expand Down Expand Up @@ -233,10 +235,17 @@ public class KubernetesConfig implements PlatformConfiguration {

/**
* If true, a Kubernetes Ingress will be created
*
* @deprecated Use the {@code quarkus.kubernetes.ingress.expose} instead
*/
@ConfigItem
boolean expose;

/**
* Ingress configuration
*/
ExpositionConfig ingress;

/**
* If true, the 'app.kubernetes.io/version' label will be part of the selectors of Service and Deployment
*/
Expand Down Expand Up @@ -447,4 +456,8 @@ public Optional<String> getAppConfigMap() {
return appConfigMap;
}

@Override
public Optional<ExpositionConfig> getExposition() {
return Optional.of(ingress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

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

import io.dekorate.utils.Strings;

Expand All @@ -34,6 +35,8 @@ public class KubernetesConfigUtil {
private static final String EXPOSE_PROPERTY_NAME = "expose";
private static final String[] EXPOSABLE_GENERATORS = { OPENSHIFT, KUBERNETES };

private static final Logger log = Logger.getLogger(KubernetesConfigUtil.class);

public static List<String> getUserSpecifiedDeploymentTargets() {
Config config = ConfigProvider.getConfig();
String configValue = config.getOptionalValue(DEPLOYMENT_TARGET, String.class)
Expand Down Expand Up @@ -83,7 +86,6 @@ public static Map<String, Object> toMap(PlatformConfiguration... platformConfigu
}
}

// hard-coded support for exposed
handleExpose(config, unPrefixed, platformConfigurations);

result.putAll(unPrefixed);
Expand All @@ -92,6 +94,7 @@ public static Map<String, Object> toMap(PlatformConfiguration... platformConfigu
return result;
}

@Deprecated
private static void handleExpose(Config config, Map<String, Object> unPrefixed,
PlatformConfiguration... platformConfigurations) {
for (String generator : EXPOSABLE_GENERATORS) {
Expand All @@ -101,11 +104,25 @@ private static void handleExpose(Config config, Map<String, Object> unPrefixed,
.getOptionalValue(QUARKUS_PREFIX + generator + "." + EXPOSE_PROPERTY_NAME, Boolean.class)
.orElse(false);
if (unprefixedExpose || prefixedExpose) {
if (generator == KUBERNETES) {
log.warn("Usage of quarkus.kubernetes.expose is deprecated in favor of quarkus.kubernetes.ingress.expose");
} else {
log.warn("Usage of quarkus.openshift.expose is deprecated in favor of quarkus.openshift.route.expose");
}
unPrefixed.put(DEKORATE_PREFIX + generator + "." + EXPOSE_PROPERTY_NAME, true);
for (PlatformConfiguration platformConfiguration : platformConfigurations) {
if (platformConfiguration.getConfigName().equals(generator)) {
platformConfiguration.getHost()
.ifPresent(h -> unPrefixed.put(DEKORATE_PREFIX + generator + ".host", h));
.ifPresent(h -> {
unPrefixed.put(DEKORATE_PREFIX + generator + ".host", h);
if (generator == KUBERNETES) {
log.warn(
"Usage of quarkus.kubernetes.host is deprecated in favor of quarkus.kubernetes.ingress.host");
} else {
log.warn(
"Usage of quarkus.openshift.host is deprecated in favor of quarkus.openshift.route.host");
}
});
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public void build(ApplicationInfoBuildItem applicationInfo,
throw new RuntimeException("Unable to setup environment for generating Kubernetes resources", e);
}

log.warn("exposition openshift=" + openshiftConfig.route.host);

Map<String, Object> config = KubernetesConfigUtil.toMap(kubernetesConfig, openshiftConfig, knativeConfig);
Set<String> deploymentTargets = kubernetesDeploymentTargets.getEntriesSortedByPriority().stream()
.map(DeploymentTargetEntry::getName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public static enum OpenshiftFlavor {

/**
* The host under which the application is going to be exposed
*
* @deprecated Use the {@code quarkus.openshift.route.host} instead
*/
@ConfigItem
Optional<String> host;
Expand Down Expand Up @@ -247,10 +249,17 @@ public static enum OpenshiftFlavor {

/**
* If true, an Openshift Route will be created
*
* @deprecated Use the {@code quarkus.openshift.route.exposition} instead
*/
@ConfigItem
boolean expose;

/**
* Openshift route configuration
*/
ExpositionConfig route;

/**
* If true, the 'app.kubernetes.io/version' label will be part of the selectors of Service and DeploymentConfig
*/
Expand Down Expand Up @@ -460,4 +469,8 @@ public Optional<String> getAppConfigMap() {
return this.appConfigMap;
}

@Override
public Optional<ExpositionConfig> getExposition() {
return Optional.of(route);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public List<ConfiguratorBuildItem> createConfigurators(ApplicationInfoBuildItem
List<ConfiguratorBuildItem> result = new ArrayList<>();
result.addAll(KubernetesCommonHelper.createPlatformConfigurators(config));
result.addAll(KubernetesCommonHelper.createGlobalConfigurators(ports));
result.add(new ConfiguratorBuildItem(new ApplyExpositionConfigurator(config.route)));

if (!capabilities.isPresent(Capability.CONTAINER_IMAGE_S2I)
&& !capabilities.isPresent(Capability.CONTAINER_IMAGE_OPENSHIFT)) {
Expand Down Expand Up @@ -199,5 +200,4 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic

return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public interface PlatformConfiguration extends EnvVarHolder {

ResourcesConfig getResources();

default Optional<ExpositionConfig> getExposition() {
return Optional.empty();
}

default boolean isExpose() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public List<ConfiguratorBuildItem> createConfigurators(KubernetesConfig config,
List<ConfiguratorBuildItem> result = new ArrayList<>();
result.addAll(KubernetesCommonHelper.createPlatformConfigurators(config));
result.addAll(KubernetesCommonHelper.createGlobalConfigurators(ports));
result.add(new ConfiguratorBuildItem(new ApplyExpositionConfigurator((config.ingress))));
return result;

}
Expand All @@ -103,7 +104,6 @@ public List<DecoratorBuildItem> createDecorators(ApplicationInfoBuildItem applic
result.addAll(KubernetesCommonHelper.createDecorators(project, KUBERNETES, name, config,
metricsConfiguration,
annotations, labels, command, ports, livenessPath, readinessPath, roles, roleBindings));

if (config.getReplicas() != 1) {
result.add(new DecoratorBuildItem(KUBERNETES, new ApplyReplicasDecorator(name, config.getReplicas())));
}
Expand Down
Loading

0 comments on commit 8068cb2

Please sign in to comment.