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 2, 2020
1 parent c63090f commit c5ac630
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 10 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.secure | boolean | | true
| 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.secure | boolean | | true
| 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,29 @@
package io.quarkus.kubernetes.deployment;

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;

/**
* If true, TLS will be enabled on the exposed service
*/
@ConfigItem(defaultValue = "true")
boolean secure;

}
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
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,18 @@ 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
*/
@ConfigItem
Optional<ExpositionConfig> ingress;

/**
* If true, the 'app.kubernetes.io/version' label will be part of the selectors of Service and Deployment
*/
Expand Down Expand Up @@ -405,6 +415,11 @@ public ResourcesConfig getResources() {
return resources;
}

@Override
public Optional<ExpositionConfig> getExposition() {
return ingress;
}

@Override
public boolean isExpose() {
return expose;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ public static Map<String, Object> toMap(PlatformConfiguration... platformConfigu

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

result.putAll(unPrefixed);
result.putAll(quarkusPrefixed);
result.putAll(toS2iProperties(quarkusPrefixed));
return result;
}

@Deprecated
private static void handleExpose(Config config, Map<String, Object> unPrefixed,
PlatformConfiguration... platformConfigurations) {
for (String generator : EXPOSABLE_GENERATORS) {
Expand All @@ -113,6 +115,25 @@ private static void handleExpose(Config config, Map<String, Object> unPrefixed,
}
}

private static void handleExpose(Map<String, Object> unPrefixed, PlatformConfiguration... platformConfigurations) {
for (PlatformConfiguration platformConfiguration : platformConfigurations) {
if (platformConfiguration.getExposition().isPresent()) {
final ExpositionConfig expositionConfig = platformConfiguration.getExposition().get();
if (expositionConfig.expose) {
unPrefixed.put(DEKORATE_PREFIX + platformConfiguration.getTargetPlatformName() + "." + EXPOSE_PROPERTY_NAME,
true);
expositionConfig.host.ifPresent(
host -> unPrefixed.put(DEKORATE_PREFIX + platformConfiguration.getTargetPlatformName() + ".host",
host));
if (expositionConfig.secure) {
unPrefixed.put(DEKORATE_PREFIX + platformConfiguration.getTargetPlatformName()
+ ".annotations.\"kubernetes.io/tls-acme\"", true);
}
}
}
}
}

/**
* 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 @@ -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,18 @@ 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
*/
@ConfigItem
Optional<ExpositionConfig> route;

/**
* If true, the 'app.kubernetes.io/version' label will be part of the selectors of Service and DeploymentConfig
*/
Expand Down Expand Up @@ -390,6 +400,11 @@ public boolean isExpose() {
return false;
}

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

@Override
public String getTargetPlatformName() {
return Constants.OPENSHIFT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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
@@ -0,0 +1,71 @@
package io.quarkus.it.kubernetes;

import static org.assertj.core.api.Assertions.*;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.openshift.api.model.Route;
import io.quarkus.test.ProdBuildResults;
import io.quarkus.test.ProdModeTestResults;
import io.quarkus.test.QuarkusProdModeTest;

public class OpenshiftWithRoutePropertiesTest {

@RegisterExtension
static final QuarkusProdModeTest config = new QuarkusProdModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClasses(GreetingResource.class))
.setApplicationName("openshift")
.setApplicationVersion("0.1-SNAPSHOT")
.withConfigurationResource("openshift-with-route.properties");

@ProdBuildResults
private ProdModeTestResults prodModeTestResults;

@Test
public void assertGeneratedResources() throws IOException {
Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
assertThat(kubernetesDir)
.isDirectoryContaining(p -> p.getFileName().endsWith("openshift.json"))
.isDirectoryContaining(p -> p.getFileName().endsWith("openshift.yml"));
List<HasMetadata> openshiftList = DeserializationUtil
.deserializeAsList(kubernetesDir.resolve("openshift.yml"));

assertThat(openshiftList).filteredOn(h -> "Service".equals(h.getKind())).singleElement().satisfies(h -> {
assertThat(h).isInstanceOfSatisfying(Service.class, s -> {
assertThat(s.getMetadata()).satisfies(m -> {
assertThat(m.getNamespace()).isEqualTo("applications");
});

assertThat(s.getSpec()).satisfies(spec -> {
assertThat(spec.getSelector()).containsOnly(entry("app.kubernetes.io/name", "test-it"));
assertThat(spec.getPorts()).hasSize(1).singleElement().satisfies(p -> {
assertThat(p.getPort()).isEqualTo(9090);
});
});
});
});

assertThat(openshiftList).filteredOn(i -> "Route".equals(i.getKind())).singleElement().satisfies(i -> {
assertThat(i).isInstanceOfSatisfying(Route.class, in -> {
//Check that labels and annotations are also applied to Routes (#10260)
assertThat(i.getMetadata()).satisfies(m -> {
assertThat(m.getName()).isEqualTo("test-it");
assertThat(m.getLabels()).contains(entry("foo", "bar"));
assertThat(m.getAnnotations()).contains(entry("bar", "baz"));
assertThat(m.getAnnotations()).contains(entry("kubernetes.io/tls-acme", "true"));
assertThat(m.getNamespace()).isEqualTo("applications");
});
assertThat(in.getSpec().getHost()).isEqualTo("foo.bar.io");
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ quarkus.kubernetes.env-vars.my-name.field=metadata.name
quarkus.kubernetes.add-version-to-label-selectors=false
quarkus.container-image.group=grp
quarkus.container-image.registry=quay.io
quarkus.kubernetes.expose=true
quarkus.kubernetes.host=example.com
quarkus.kubernetes.ingress.expose=true
quarkus.kubernetes.ingress.host=example.com
quarkus.kubernetes.service-type=NodePort
quarkus.kubernetes.image-pull-policy=IfNotPresent
quarkus.kubernetes.replicas=3
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ quarkus.openshift.labels.foo=bar
quarkus.openshift.annotations.bar=baz
quarkus.openshift.env-vars.my-env-var.value=SOMEVALUE
quarkus.openshift.group=grp
quarkus.openshift.expose=true
quarkus.openshift.route.expose=true
quarkus.s2i.registry=quay.io
quarkus.openshift.replicas=3
quarkus.openshift.add-version-to-label-selectors=false
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
quarkus.http.port=9090
quarkus.kubernetes.deployment-target=openshift
quarkus.openshift.name=test-it
quarkus.openshift.namespace=applications
quarkus.openshift.labels.foo=bar
quarkus.openshift.annotations.bar=baz
quarkus.openshift.group=grp

quarkus.openshift.route.expose=true
quarkus.openshift.route.host=foo.bar.io
quarkus.openshift.route.secure=true

quarkus.s2i.registry=quay.io

0 comments on commit c5ac630

Please sign in to comment.