Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions operator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>openshift-client</artifactId>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-httpclient-okhttp</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import io.fabric8.kubernetes.api.model.OwnerReferenceBuilder;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.openshift.api.model.Route;
import io.fabric8.openshift.client.OpenShiftClient;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
Expand Down Expand Up @@ -64,6 +66,7 @@ public abstract class AbstractController<T extends CustomResource<? extends Full
implements Reconciler<T> {

protected final KubernetesClient client;
protected final OpenShiftClient openShiftClient;
private final Validator validator;
@Inject
OperatorRuntimeConfiguration operatorRuntimeConfiguration;
Expand All @@ -76,9 +79,14 @@ public AbstractController() {
public AbstractController(KubernetesClient client) {
this.client = client;
this.validator = createValidator();
openShiftClient = isOpenShiftCluster(client) ? client.adapt(OpenShiftClient.class) : null;

}

protected boolean isOpenShiftCluster(KubernetesClient client) {
return client != null && client.supports(Route.class);
}

private Validator createValidator() {
final HibernateValidatorConfiguration configuration = (HibernateValidatorConfiguration)
Validation.byDefaultProvider().configure();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ protected void patchResourceSet(SetInfo<ProxySetSpec, ProxyResourcesFactory> set
resourceFactory.patchConfigMap();
resourceFactory.patchConfigMapWsConfig();
resourceFactory.patchService();
resourceFactory.patchOpenShiftRoutes(openShiftClient);
resourceFactory.patchDeployment();
}

Expand All @@ -122,6 +123,7 @@ protected void deleteResourceSet(SetInfo<ProxySetSpec, ProxyResourcesFactory> se
@Override
protected void patchCommonResources(SetInfo<ProxySetSpec, ProxyResourcesFactory> set) {
set.getResourceFactory().patchService();
set.getResourceFactory().patchOpenShiftRoutes(openShiftClient);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.fabric8.kubernetes.api.model.ContainerPort;
import io.fabric8.kubernetes.api.model.ContainerPortBuilder;
import io.fabric8.kubernetes.api.model.EnvFromSourceBuilder;
import io.fabric8.kubernetes.api.model.IntOrString;
import io.fabric8.kubernetes.api.model.OwnerReference;
import io.fabric8.kubernetes.api.model.Probe;
import io.fabric8.kubernetes.api.model.Service;
Expand All @@ -41,6 +42,10 @@
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.openshift.api.model.Route;
import io.fabric8.openshift.api.model.RouteBuilder;
import io.fabric8.openshift.api.model.RouteFluent;
import io.fabric8.openshift.client.OpenShiftClient;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -628,4 +633,87 @@ private String getRack() {
}
return null;
}

public void patchOpenShiftRoutes(OpenShiftClient openShiftClient) {
if (openShiftClient == null) {
log.debug("OpenShift Routes not supported on this cluster, skipping Route reconciliation");
return;
}

Service svc = client.services()
.inNamespace(namespace)
.withName(resourceName)
.get();

if (svc == null || svc.getSpec() == null || svc.getSpec().getPorts() == null) {
log.warnf("Service {} not found or has no ports, skipping Route creation", resourceName);
return;
}

for (ServicePort port : svc.getSpec().getPorts()) {
patchSingleRoute(openShiftClient, svc, port);
}
}

private void patchSingleRoute(OpenShiftClient openShiftClient,
Service service,
ServicePort port) {

String routeName = resourceName + "-" + port.getName();

RouteFluent<RouteBuilder>.SpecNested<RouteBuilder> routeBuilderSpecNested = new RouteBuilder()
.withNewMetadata()
.withName(routeName)
.withNamespace(namespace)
.withLabels(getLabels(spec.getLabels()))
.endMetadata()
.withNewSpec()
.withNewTo()
.withKind("Service")
.withName(service.getMetadata().getName())
.endTo()
.withNewPort()
.withTargetPort(new IntOrString(port.getName()))
.endPort();

applyTlsConfig(routeBuilderSpecNested, port.getName());

Route desired = routeBuilderSpecNested.endSpec().build();

openShiftClient.routes()
.inNamespace(namespace)
.resource(desired).serverSideApply();

log.infof("Route '%s' reconciled for Service port '%s'",
routeName, port.getName());
}

private void applyTlsConfig(RouteFluent<RouteBuilder>.SpecNested<RouteBuilder> routeBuilderSpecNested,
String portName) {

switch (portName) {
case "http":
case "ws":
case "pulsar":
case "kafkaplaintext":
routeBuilderSpecNested.withNewTls()
.withTermination("edge")
.withInsecureEdgeTerminationPolicy("Redirect")
.endTls();
break;

case "https":
case "wss":
case "pulsarssl":
case "kafkassl":
routeBuilderSpecNested.withNewTls()
.withTermination("passthrough")
.endTls();
break;

default:
log.debugf("Port '%s' not exposed via Route (no TLS mapping)", portName);
}
}

}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>openshift-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
Expand Down
Loading