use of org.bf2.cos.fleetshard.support.Service in project kas-fleetshard by bf2fc6cc711aee1a0c2a.
the class IngressControllerManagerTest method testGetManagedKafkaRoutesFor.
@Test
public void testGetManagedKafkaRoutesFor() {
final String mkName = "my-managedkafka";
ManagedKafka mk = new ManagedKafkaBuilder().withNewMetadata().withName(mkName).withNamespace(mkName).endMetadata().withSpec(new ManagedKafkaSpecBuilder().withNewEndpoint().withBootstrapServerHost("bs.bf2.example.tld").endEndpoint().build()).build();
final Function<? super String, ? extends Route> makeRoute = broker -> new RouteBuilder().editOrNewMetadata().withName(mkName + "-" + broker.replace("broker", "kafka")).withNamespace(mkName).addNewOwnerReference().withApiVersion(Kafka.V1BETA2).withKind(Kafka.RESOURCE_KIND).withName(AbstractKafkaCluster.kafkaClusterName(mk)).endOwnerReference().endMetadata().editOrNewSpec().withHost(broker + "-bs.bf2.example.tld").withTo(new RouteTargetReferenceBuilder().withKind("Service").withName(mkName + "-" + broker).withWeight(100).build()).endSpec().build();
final Function<? super String, ? extends Service> suffixToService = suffix -> new ServiceBuilder().editOrNewMetadata().withName(mkName + "-" + suffix).withNamespace(mkName).endMetadata().editOrNewSpec().withSelector(Map.of("dummy-label", mkName + "-" + suffix)).endSpec().build();
final Function<? super String, ? extends Pod> suffixToPod = suffix -> new PodBuilder().editOrNewMetadata().withName(mkName + "-" + suffix).withNamespace(mkName).addToLabels(Map.of("dummy-label", mkName + "-" + suffix, "app.kubernetes.io/name", "kafka", OperandUtils.MANAGED_BY_LABEL, OperandUtils.STRIMZI_OPERATOR_NAME)).endMetadata().editOrNewSpec().withNodeName("zone" + "-" + suffix).endSpec().build();
final Function<? super String, ? extends Node> suffixToNode = suffix -> new NodeBuilder().editOrNewMetadata().withName("zone" + "-" + suffix).withLabels(Map.of(IngressControllerManager.TOPOLOGY_KEY, "zone" + "-" + suffix, IngressControllerManager.WORKER_NODE_LABEL, "true")).endMetadata().build();
List<String> suffixes = List.of("broker-0", "broker-1", "broker-2");
suffixes.stream().map(makeRoute).forEach(route -> openShiftClient.routes().inNamespace(mkName).createOrReplace(route));
suffixes.stream().map(suffixToService).forEach(svc -> openShiftClient.services().inNamespace(mkName).createOrReplace(svc));
suffixes.stream().map(suffixToPod).forEach(pod -> openShiftClient.pods().inNamespace(mkName).createOrReplace(pod));
suffixes.stream().map(suffixToNode).forEach(node -> openShiftClient.nodes().createOrReplace(node));
ingressControllerManager.reconcileIngressControllers();
List<ManagedKafkaRoute> managedKafkaRoutes = ingressControllerManager.getManagedKafkaRoutesFor(mk);
assertEquals(5, managedKafkaRoutes.size());
assertEquals(managedKafkaRoutes.stream().sorted(Comparator.comparing(ManagedKafkaRoute::getName)).collect(Collectors.toList()), managedKafkaRoutes, "Expected list of ManagedKafkaRoutes to be sorted by name");
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("bootstrap", managedKafkaRoutes.get(1).getName());
assertEquals("", managedKafkaRoutes.get(1).getPrefix());
assertEquals("ingresscontroller.kas.testing.domain.tld", 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("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("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());
}
use of org.bf2.cos.fleetshard.support.Service in project kas-fleetshard by bf2fc6cc711aee1a0c2a.
the class CanaryTest method createCanaryService.
@Test
void createCanaryService() throws Exception {
ManagedKafka mk = new ManagedKafkaBuilder().withNewMetadata().withNamespace("test").withName("test-mk").endMetadata().withSpec(new ManagedKafkaSpecBuilder().withNewVersions().withKafka("2.6.0").endVersions().withNewEndpoint().withBootstrapServerHost("test-mk-kafka-bootstrap").endEndpoint().build()).build();
Service canaryService = canary.serviceFrom(mk, null);
server.getClient().services().create(canaryService);
assertNotNull(server.getClient().services().inNamespace(canaryService.getMetadata().getNamespace()).withName(canaryService.getMetadata().getName()).get());
KafkaClusterTest.diffToExpected(canaryService, "/expected/canary-service.yml");
}
use of org.bf2.cos.fleetshard.support.Service in project kas-fleetshard by bf2fc6cc711aee1a0c2a.
the class IngressControllerManager method getZoneForBrokerRoute.
private String getZoneForBrokerRoute(Route route) {
String serviceName = route.getSpec().getTo().getName();
String namespace = route.getMetadata().getNamespace();
Service svc = informerManager.getLocalService(namespace, serviceName);
if (svc == null) {
return "";
}
Map<String, String> labels = svc.getSpec().getSelector();
Stream<Pod> pods = brokerPodInformer.getList().stream().filter(p -> p.getMetadata().getNamespace().equals(namespace) && p.getMetadata().getLabels().entrySet().containsAll(labels.entrySet()));
return pods.findFirst().map(p -> p.getSpec().getNodeName()).map(nodeInformer::getByKey).map(n -> n.getMetadata().getLabels().get(IngressControllerManager.TOPOLOGY_KEY)).orElse("");
}
use of org.bf2.cos.fleetshard.support.Service in project kas-fleetshard by bf2fc6cc711aee1a0c2a.
the class LogCollector method saveClusterState.
private static void saveClusterState(Path logpath) throws IOException {
KubeClient kube = KubeClient.getInstance();
Files.writeString(logpath.resolve("describe-cluster-nodes.log"), kube.cmdClient().exec(false, false, "describe", "nodes").out());
Files.writeString(logpath.resolve("all-events.log"), kube.cmdClient().exec(false, false, "get", "events", "--all-namespaces").out());
Files.writeString(logpath.resolve("pvs.log"), kube.cmdClient().exec(false, false, "describe", "pv").out());
Files.writeString(logpath.resolve("operator-routes.yml"), kube.cmdClient().exec(false, false, "get", "routes", "-n", FleetShardOperatorManager.OPERATOR_NS, "-o", "yaml").out());
Files.writeString(logpath.resolve("operator-services.yml"), kube.cmdClient().exec(false, false, "get", "service", "-n", FleetShardOperatorManager.OPERATOR_NS, "-o", "yaml").out());
Files.writeString(logpath.resolve("kas-fleetshard-operator-pods.yml"), kube.cmdClient().exec(false, false, "get", "pod", "-l", "app=" + FleetShardOperatorManager.OPERATOR_NAME, "--all-namespaces", "-o", "yaml").out());
Files.writeString(logpath.resolve("strimzi-kafka-pods.yml"), kube.cmdClient().exec(false, false, "get", "pod", "-l", "app.kubernetes.io/managed-by=strimzi-cluster-operator", "--all-namespaces", "-o", "yaml").out());
Files.writeString(logpath.resolve("managedkafkas.yml"), kube.cmdClient().exec(false, false, "get", "managedkafka", "--all-namespaces", "-o", "yaml").out());
Files.writeString(logpath.resolve("kafkas.yml"), kube.cmdClient().exec(false, false, "get", "kafka", "-l", "app.kubernetes.io/managed-by=" + FleetShardOperatorManager.OPERATOR_NAME, "--all-namespaces", "-o", "yaml").out());
Files.writeString(logpath.resolve("pods-managed-by-operator.yml"), kube.cmdClient().exec(false, false, "get", "pods", "-l", "app.kubernetes.io/managed-by=" + FleetShardOperatorManager.OPERATOR_NAME, "--all-namespaces", "-o", "yaml").out());
Files.writeString(logpath.resolve("operator-namespace-events.yml"), kube.cmdClient().exec(false, false, "get", "events", "-n", FleetShardOperatorManager.OPERATOR_NS).out());
Files.writeString(logpath.resolve("operator.log"), kube.cmdClient().exec(false, false, "logs", "deployment/" + FleetShardOperatorManager.OPERATOR_NAME, "-n", FleetShardOperatorManager.OPERATOR_NS).out());
Files.writeString(logpath.resolve("sync.log"), kube.cmdClient().exec(false, false, "logs", "deployment/" + FleetShardOperatorManager.SYNC_NAME, "-n", FleetShardOperatorManager.OPERATOR_NS).out());
StrimziOperatorManager.getStrimziOperatorPods().forEach(pod -> {
try {
Files.writeString(logpath.resolve(pod.getMetadata().getName() + ".log"), kube.cmdClient().exec(false, false, "logs", pod.getMetadata().getName(), "--tail", "-1", "-n", pod.getMetadata().getNamespace()).out());
} catch (Exception e) {
LOGGER.warn("Cannot get logs from pod {} in namespace {}", pod.getMetadata().getName(), pod.getMetadata().getNamespace());
}
});
}
use of org.bf2.cos.fleetshard.support.Service in project kas-fleetshard by bf2fc6cc711aee1a0c2a.
the class FleetShardOperatorManager method createEndpoint.
public static String createEndpoint(KubeClient kubeClient) {
String externalEndpointName = SYNC_NAME + "-external";
if (kubeClient.isGenericKubernetes()) {
if (kubeClient.client().services().inNamespace(OPERATOR_NS).list().getItems().stream().anyMatch(service -> service.getMetadata().getName().equals(externalEndpointName))) {
kubeClient.client().services().inNamespace(OPERATOR_NS).withName(externalEndpointName).delete();
}
kubeClient.cmdClient().namespace(OPERATOR_NS).execInCurrentNamespace("expose", "service", SYNC_NAME, "--type=LoadBalancer", "--name", externalEndpointName);
return new ExecBuilder().withCommand("minikube", "service", "--url", externalEndpointName, "-n", OPERATOR_NS).logToOutput(false).exec().out().trim();
} else {
OpenShiftClient openShiftClient = kubeClient.client().adapt(OpenShiftClient.class);
if (openShiftClient.routes().inNamespace(OPERATOR_NS).list().getItems().stream().anyMatch(service -> service.getMetadata().getName().equals(externalEndpointName))) {
openShiftClient.routes().inNamespace(OPERATOR_NS).withName(externalEndpointName).delete();
}
kubeClient.cmdClient().namespace(OPERATOR_NS).execInCurrentNamespace("expose", "service", SYNC_NAME, "--name", externalEndpointName);
Route r = openShiftClient.routes().inNamespace(OPERATOR_NS).withName(externalEndpointName).get();
return String.format("%s://%s:%d", r.getSpec().getPort().getTargetPort().getStrVal(), r.getSpec().getHost(), r.getSpec().getPort().getTargetPort().getStrVal().equals("http") ? 80 : 443);
}
}
Aggregations