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 Nov 16, 2020
1 parent a0374b7 commit e1c09bd
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 32 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 @@ -595,7 +595,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 @@ -611,7 +610,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 @@ -833,7 +834,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 @@ -849,7 +849,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
Expand Up @@ -74,7 +74,7 @@ public void createAnnotations(KubernetesConfig config, BuildProducer<KubernetesA

@BuildStep
public void createLabels(KubernetesConfig config, BuildProducer<KubernetesLabelBuildItem> labels,
BuildProducer<ContainerImageLabelBuildItem> imageLabels) {
BuildProducer<ContainerImageLabelBuildItem> imageLabels) {
config.getLabels().forEach((k, v) -> {
labels.produce(new KubernetesLabelBuildItem(k, v, MINIKUBE));
imageLabels.produce(new ContainerImageLabelBuildItem(k, v));
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,31 @@
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,22 +1,20 @@

package io.quarkus.kubernetes.deployment;

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;

import java.nio.file.Path;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
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 io.dekorate.deps.openshift.api.model.Route;
import io.dekorate.kubernetes.config.Annotation;
import io.dekorate.kubernetes.config.PortBuilder;
import io.dekorate.kubernetes.configurator.AddPort;
Expand Down Expand Up @@ -45,6 +43,7 @@
import io.dekorate.kubernetes.decorator.ApplyServiceAccountNamedDecorator;
import io.dekorate.kubernetes.decorator.ApplyWorkingDirDecorator;
import io.dekorate.kubernetes.decorator.RemoveAnnotationDecorator;
import io.dekorate.openshift.decorator.AddRouteDecorator;
import io.dekorate.project.BuildInfo;
import io.dekorate.project.FileProjectFactory;
import io.dekorate.project.Project;
Expand All @@ -66,6 +65,8 @@
import io.quarkus.kubernetes.spi.KubernetesRoleBindingBuildItem;
import io.quarkus.kubernetes.spi.KubernetesRoleBuildItem;

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

public class KubernetesCommonHelper {

private static final String OUTPUT_ARTIFACT_FORMAT = "%s%s.jar";
Expand Down Expand Up @@ -145,6 +146,7 @@ public static List<DecoratorBuildItem> createDecorators(Optional<Project> projec
result.addAll(createContainerDecorators(project, target, name, config));
result.addAll(createMountAndVolumeDecorators(project, target, name, config));


//Handle Command and arguments
command.ifPresent(c -> {
result.add(new DecoratorBuildItem(new ApplyCommandDecorator(name, new String[] { c.getCommand() })));
Expand Down Expand Up @@ -325,6 +327,17 @@ private static List<DecoratorBuildItem> createAnnotationDecorators(Optional<Proj
}
});

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)));
}
}

//Add metrics annotations
return result;
}
Expand Down Expand Up @@ -383,4 +396,5 @@ private static Map<String, Integer> verifyPorts(List<KubernetesPortBuildItem> ku
return result;
}


}
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 @@ -227,10 +229,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 @@ -409,4 +418,9 @@ public ResourcesConfig getResources() {
public boolean isExpose() {
return expose;
}

@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 @@ -164,8 +164,8 @@ private Optional<KubernetesDeploymentTargetBuildItem> getOptionalDeploymentTarge
}

private DeploymentResultBuildItem deploy(DeploymentTargetEntry deploymentTarget,
KubernetesClient client, Path outputDir,
OpenshiftConfig openshiftConfig, ApplicationInfoBuildItem applicationInfo) {
KubernetesClient client, Path outputDir,
OpenshiftConfig openshiftConfig, ApplicationInfoBuildItem applicationInfo) {
String namespace = Optional.ofNullable(client.getNamespace()).orElse("default");
log.info("Deploying to " + deploymentTarget.getName().toLowerCase() + " server: " + client.getMasterUrl()
+ " in namespace: " + namespace + ".");
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 @@ -241,10 +243,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 @@ -428,4 +437,9 @@ public Map<String, EnvConfig> getEnvVars() {
public EnvVarsConfig getEnv() {
return env;
}

@Override
public Optional<ExpositionConfig> getExposition() {
return Optional.of(route);
}
}
Loading

0 comments on commit e1c09bd

Please sign in to comment.