Skip to content

Commit

Permalink
MGDSTRM-10301 switching routes to use the loadbalancer hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Jan 5, 2023
1 parent a16e647 commit be5a983
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.LabelSelector;
import io.fabric8.kubernetes.api.model.LoadBalancerIngress;
import io.fabric8.kubernetes.api.model.LoadBalancerStatus;
import io.fabric8.kubernetes.api.model.Node;
import io.fabric8.kubernetes.api.model.NodeList;
import io.fabric8.kubernetes.api.model.Pod;
Expand All @@ -14,6 +16,7 @@
import io.fabric8.kubernetes.api.model.ResourceRequirements;
import io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceStatus;
import io.fabric8.kubernetes.api.model.TolerationBuilder;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable;
Expand Down Expand Up @@ -109,12 +112,6 @@ public class IngressControllerManager {

protected static final String WORKER_NODE_LABEL = "node-role.kubernetes.io/worker";

/**
* Domain part prefixed to domain reported on IngressController status. The CNAME DNS records
* need to point to a sub-domain on the IngressController domain, so we just add this.
*/
private static final String ROUTER_SUBDOMAIN = "ingresscontroller.";

/**
* Predicate that will return true if the input string looks like a broker resource name.
*/
Expand Down Expand Up @@ -656,11 +653,13 @@ private int numReplicasForConnectionDemand(double connectionDemand) {
}

private String getIngressControllerDomain(String ingressControllerName) {
return ingressControllerInformer.getList().stream()
.filter(ic -> ic.getMetadata().getName().equals(ingressControllerName))
.map(ic -> ROUTER_SUBDOMAIN + (ic.getStatus() != null ? ic.getStatus().getDomain() : ic.getSpec().getDomain()))
.findFirst()
.orElse("");
return Optional.ofNullable(informerManager.getLocalService(INGRESS_ROUTER_NAMESPACE, "router-" + ingressControllerName))
.map(Service::getStatus)
.map(ServiceStatus::getLoadBalancer)
.map(LoadBalancerStatus::getIngress)
.flatMap(l -> l.stream().findFirst())
.map(LoadBalancerIngress::getHostname)
.orElse(null);
}

private Stream<Route> routesFor(ManagedKafka managedKafka) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bf2.operator.managers;

import io.fabric8.kubernetes.api.model.LoadBalancerIngressBuilder;
import io.fabric8.kubernetes.api.model.Node;
import io.fabric8.kubernetes.api.model.NodeBuilder;
import io.fabric8.kubernetes.api.model.Pod;
Expand Down Expand Up @@ -336,7 +337,28 @@ void testGetManagedKafkaRoutesFor() {
suffixes.stream().map(suffixToPod).forEach(pod -> openShiftClient.pods().inNamespace(mkName).createOrReplace(pod));
suffixes.stream().map(suffixToNode).forEach(node -> openShiftClient.nodes().createOrReplace(node));

final Function<? super String, ? extends Service> suffixToRouterService = suffix -> new ServiceBuilder()
.withNewMetadata()
.withName("router-" + suffix)
.endMetadata()
.withNewStatus()
.withNewLoadBalancer()
.withIngress(new LoadBalancerIngressBuilder()
.withHostname(suffix + "-loadbalancer.provider.com")
.build())
.endLoadBalancer()
.endStatus()
.build();

List.of("kas", "kas-zone-broker-0", "kas-zone-broker-1", "kas-zone-broker-2")
.stream()
.map(suffixToRouterService)
.forEach(svc -> openShiftClient.services()
.inNamespace(IngressControllerManager.INGRESS_ROUTER_NAMESPACE)
.createOrReplace(svc));

ingressControllerManager.reconcileIngressControllers();

List<ManagedKafkaRoute> managedKafkaRoutes = ingressControllerManager.getManagedKafkaRoutesFor(mk);

assertEquals(5, managedKafkaRoutes.size());
Expand All @@ -348,23 +370,23 @@ void testGetManagedKafkaRoutesFor() {

assertEquals("admin-server", managedKafkaRoutes.get(0).getName());
assertEquals("admin-server", managedKafkaRoutes.get(0).getPrefix());
assertEquals("ingresscontroller.kas.testing.domain.tld", managedKafkaRoutes.get(0).getRouter());
assertEquals("kas-loadbalancer.provider.com", managedKafkaRoutes.get(0).getRouter());

assertEquals("bootstrap", managedKafkaRoutes.get(1).getName());
assertEquals("", managedKafkaRoutes.get(1).getPrefix());
assertEquals("ingresscontroller.kas.testing.domain.tld", managedKafkaRoutes.get(1).getRouter());
assertEquals("kas-loadbalancer.provider.com", managedKafkaRoutes.get(1).getRouter());

assertEquals("broker-0", managedKafkaRoutes.get(2).getName());
assertEquals("broker-0", managedKafkaRoutes.get(2).getPrefix());
assertEquals("ingresscontroller.kas-zone-broker-0.testing.domain.tld", managedKafkaRoutes.get(2).getRouter());
assertEquals("kas-zone-broker-0-loadbalancer.provider.com", managedKafkaRoutes.get(2).getRouter());

assertEquals("broker-1", managedKafkaRoutes.get(3).getName());
assertEquals("broker-1", managedKafkaRoutes.get(3).getPrefix());
assertEquals("ingresscontroller.kas-zone-broker-1.testing.domain.tld", managedKafkaRoutes.get(3).getRouter());
assertEquals("kas-zone-broker-1-loadbalancer.provider.com", managedKafkaRoutes.get(3).getRouter());

assertEquals("broker-2", managedKafkaRoutes.get(4).getName());
assertEquals("broker-2", managedKafkaRoutes.get(4).getPrefix());
assertEquals("ingresscontroller.kas-zone-broker-2.testing.domain.tld", managedKafkaRoutes.get(4).getRouter());
assertEquals("kas-zone-broker-2-loadbalancer.provider.com", managedKafkaRoutes.get(4).getRouter());
}

@Test
Expand Down

0 comments on commit be5a983

Please sign in to comment.