Skip to content

Commit

Permalink
Override target port used when creating openshift route
Browse files Browse the repository at this point in the history
  • Loading branch information
Alasdair Preston committed Sep 14, 2022
1 parent b60ad38 commit 71ae072
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<RouteSpecFluent<?>> {

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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -552,4 +552,8 @@ public static boolean isOpenshiftBuildEnabled(ContainerImageConfig containerImag
public DeploymentResourceKind getDeploymentResourceKind() {
return deploymentKind.orElse(DeploymentResourceKind.DeploymentConfig);
}

public Optional<RouteConfig> getRoute() {
return Optional.ofNullable(route);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ public List<DecoratorBuildItem> 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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ public class RouteConfig {
@ConfigItem
Map<String, String> annotations;

/**
* Port of the Service to bind to
*/
@ConfigItem
Optional<String> targetPort;

}
Original file line number Diff line number Diff line change
@@ -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<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()).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");
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 71ae072

Please sign in to comment.