From 71ae07205748d3046bf6887a7a47b755dbab77b7 Mon Sep 17 00:00:00 2001 From: Alasdair Preston Date: Tue, 13 Sep 2022 11:23:38 +0100 Subject: [PATCH] Override target port used when creating openshift route --- .../ApplyRouteTargetPortDecorator.java | 27 +++++++ .../deployment/OpenshiftConfig.java | 4 ++ .../deployment/OpenshiftProcessor.java | 6 ++ .../kubernetes/deployment/RouteConfig.java | 6 ++ ...hRoutePropertiesCustomServicePortTest.java | 72 +++++++++++++++++++ .../OpenshiftWithRoutePropertiesTest.java | 2 +- ...-with-route-custom-service-port.properties | 14 ++++ 7 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/ApplyRouteTargetPortDecorator.java create mode 100644 integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/OpenshiftWithRoutePropertiesCustomServicePortTest.java create mode 100644 integration-tests/kubernetes/quarkus-standard-way/src/test/resources/openshift-with-route-custom-service-port.properties diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/ApplyRouteTargetPortDecorator.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/ApplyRouteTargetPortDecorator.java new file mode 100644 index 00000000000000..f7762aeca13941 --- /dev/null +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/ApplyRouteTargetPortDecorator.java @@ -0,0 +1,27 @@ +package io.quarkus.kubernetes.deployment; + +import io.dekorate.kubernetes.decorator.NamedResourceDecorator; +import io.fabric8.kubernetes.api.model.IntOrString; +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.openshift.api.model.RoutePort; +import io.fabric8.openshift.api.model.RouteSpecFluent; + +public class ApplyRouteTargetPortDecorator extends NamedResourceDecorator> { + + private IntOrString targetPort; + + public ApplyRouteTargetPortDecorator(Integer port) { + super(ANY); + targetPort = new IntOrString(port); + } + + public ApplyRouteTargetPortDecorator(String port) { + super(ANY); + targetPort = new IntOrString(port); + } + + @Override + public void andThenVisit(RouteSpecFluent routeSpecFluent, ObjectMeta objectMeta) { + routeSpecFluent.withPort(new RoutePort(targetPort)); + } +} diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftConfig.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftConfig.java index e12d0fa1aca9c4..efeddc075084b9 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftConfig.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftConfig.java @@ -552,4 +552,8 @@ public static boolean isOpenshiftBuildEnabled(ContainerImageConfig containerImag public DeploymentResourceKind getDeploymentResourceKind() { return deploymentKind.orElse(DeploymentResourceKind.DeploymentConfig); } + + public Optional getRoute() { + return Optional.ofNullable(route); + } } diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java index c97cee401e0265..d4e4cd005b07fe 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/OpenshiftProcessor.java @@ -305,6 +305,12 @@ public List createDecorators(ApplicationInfoBuildItem applic .findFirst().orElse(DEFAULT_HTTP_PORT); result.add(new DecoratorBuildItem(OPENSHIFT, new ApplyHttpGetActionPortDecorator(name, name, port))); + // Route target port override + config.getRoute().ifPresentOrElse( + r -> result.add(new DecoratorBuildItem(OPENSHIFT, + new ApplyRouteTargetPortDecorator(r.targetPort.orElse(HTTP_PORT)))), + () -> result.add(new DecoratorBuildItem(OPENSHIFT, new ApplyRouteTargetPortDecorator(HTTP_PORT)))); + // Handle non-openshift builds if (deploymentResourceKind == DeploymentResourceKind.DeploymentConfig && !OpenshiftConfig.isOpenshiftBuildEnabled(containerImageConfig, capabilities)) { diff --git a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/RouteConfig.java b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/RouteConfig.java index f92a015b118434..727029c147786b 100644 --- a/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/RouteConfig.java +++ b/extensions/kubernetes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/RouteConfig.java @@ -27,4 +27,10 @@ public class RouteConfig { @ConfigItem Map annotations; + /** + * Port of the Service to bind to + */ + @ConfigItem + Optional targetPort; + } diff --git a/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/OpenshiftWithRoutePropertiesCustomServicePortTest.java b/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/OpenshiftWithRoutePropertiesCustomServicePortTest.java new file mode 100644 index 00000000000000..5d25a917db8668 --- /dev/null +++ b/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/OpenshiftWithRoutePropertiesCustomServicePortTest.java @@ -0,0 +1,72 @@ +package io.quarkus.it.kubernetes; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.List; + +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 OpenshiftWithRoutePropertiesCustomServicePortTest { + + @RegisterExtension + static final QuarkusProdModeTest config = new QuarkusProdModeTest() + .withApplicationRoot((jar) -> jar.addClasses(GreetingResource.class)) + .setApplicationName("openshift") + .setApplicationVersion("0.1-SNAPSHOT") + .withConfigurationResource("openshift-with-route-custom-service-port.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 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()).contains(entry("app.kubernetes.io/name", "test-it")); + assertThat(spec.getPorts()).hasSize(1).singleElement().satisfies(p -> { + assertThat(p.getPort()).isEqualTo(80); + assertThat(p.getTargetPort().getIntVal()).isEqualTo(9090); + }); + }); + }); + }); + + assertThat(openshiftList).filteredOn(i -> "Route".equals(i.getKind())).singleElement().satisfies(i -> { + assertThat(i).isInstanceOfSatisfying(Route.class, r -> { + //Check that labels and annotations are also applied to Routes (#10260) + assertThat(r.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(r.getSpec().getPort().getTargetPort().getStrVal()).isEqualTo("my-port"); + assertThat(r.getSpec().getHost()).isEqualTo("foo.bar.io"); + }); + }); + } +} diff --git a/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/OpenshiftWithRoutePropertiesTest.java b/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/OpenshiftWithRoutePropertiesTest.java index fb5090d9e56227..187ac17189e296 100644 --- a/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/OpenshiftWithRoutePropertiesTest.java +++ b/integration-tests/kubernetes/quarkus-standard-way/src/test/java/io/quarkus/it/kubernetes/OpenshiftWithRoutePropertiesTest.java @@ -63,7 +63,7 @@ public void assertGeneratedResources() throws IOException { assertThat(m.getAnnotations()).contains(entry("kubernetes.io/tls-acme", "true")); assertThat(m.getNamespace()).isEqualTo("applications"); }); - assertThat(r.getSpec().getPort().getTargetPort().getIntVal()).isEqualTo(9090); + assertThat(r.getSpec().getPort().getTargetPort().getStrVal()).isEqualTo("http"); assertThat(r.getSpec().getHost()).isEqualTo("foo.bar.io"); }); }); diff --git a/integration-tests/kubernetes/quarkus-standard-way/src/test/resources/openshift-with-route-custom-service-port.properties b/integration-tests/kubernetes/quarkus-standard-way/src/test/resources/openshift-with-route-custom-service-port.properties new file mode 100644 index 00000000000000..c1f98980dceec5 --- /dev/null +++ b/integration-tests/kubernetes/quarkus-standard-way/src/test/resources/openshift-with-route-custom-service-port.properties @@ -0,0 +1,14 @@ +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.annotations."kubernetes.io/tls-acme"=true +quarkus.openshift.route-target-port=my-port + +quarkus.s2i.registry=quay.io \ No newline at end of file