Search in sources :

Example 1 with ManagedKafkaRoute

use of org.bf2.operator.resources.v1alpha1.ManagedKafkaRoute 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());
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) KubernetesServerTestResource(io.quarkus.test.kubernetes.client.KubernetesServerTestResource) QuarkusMock(io.quarkus.test.junit.QuarkusMock) KubernetesCrudDispatcher(io.fabric8.kubernetes.client.server.mock.KubernetesCrudDispatcher) ManagedKafkaRoute(org.bf2.operator.resources.v1alpha1.ManagedKafkaRoute) Function(java.util.function.Function) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Inject(javax.inject.Inject) Route(io.fabric8.openshift.api.model.Route) NodeBuilder(io.fabric8.kubernetes.api.model.NodeBuilder) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Map(java.util.Map) AbstractKafkaCluster(org.bf2.operator.operands.AbstractKafkaCluster) Service(io.fabric8.kubernetes.api.model.Service) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Node(io.fabric8.kubernetes.api.model.Node) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) LongSummaryStatistics(java.util.LongSummaryStatistics) QuarkusTestResource(io.quarkus.test.common.QuarkusTestResource) KubernetesServer(io.fabric8.kubernetes.client.server.mock.KubernetesServer) OperandUtils(org.bf2.common.OperandUtils) RouteBuilder(io.fabric8.openshift.api.model.RouteBuilder) ManagedKafkaSpecBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaSpecBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) KafkaCluster(org.bf2.operator.operands.KafkaCluster) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) KubernetesTestServer(io.quarkus.test.kubernetes.client.KubernetesTestServer) Kafka(io.strimzi.api.kafka.model.Kafka) Comparator(java.util.Comparator) RouteTargetReferenceBuilder(io.fabric8.openshift.api.model.RouteTargetReferenceBuilder) IngressController(io.fabric8.openshift.api.model.operator.v1.IngressController) ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) Collections(java.util.Collections) ManagedKafkaBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaBuilder) RouteBuilder(io.fabric8.openshift.api.model.RouteBuilder) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) ManagedKafkaBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaBuilder) NodeBuilder(io.fabric8.kubernetes.api.model.NodeBuilder) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) ManagedKafkaRoute(org.bf2.operator.resources.v1alpha1.ManagedKafkaRoute) ManagedKafkaSpecBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaSpecBuilder) RouteTargetReferenceBuilder(io.fabric8.openshift.api.model.RouteTargetReferenceBuilder) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 2 with ManagedKafkaRoute

use of org.bf2.operator.resources.v1alpha1.ManagedKafkaRoute in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class IngressControllerManager method getManagedKafkaRoutesFor.

public List<ManagedKafkaRoute> getManagedKafkaRoutesFor(ManagedKafka mk) {
    String multiZoneRoute = getIngressControllerDomain("kas");
    String bootstrapDomain = mk.getSpec().getEndpoint().getBootstrapServerHost();
    return Stream.concat(Stream.of(new ManagedKafkaRoute("bootstrap", "", multiZoneRoute), new ManagedKafkaRoute("admin-server", "admin-server", multiZoneRoute)), routesFor(mk).filter(IS_BROKER_ROUTE).map(r -> {
        String router = getIngressControllerDomain("kas-" + getZoneForBrokerRoute(r));
        String routePrefix = r.getSpec().getHost().replaceFirst("-" + bootstrapDomain, "");
        return new ManagedKafkaRoute(routePrefix, routePrefix, router);
    })).sorted(Comparator.comparing(ManagedKafkaRoute::getName)).collect(Collectors.toList());
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) KafkaClusterSpec(io.strimzi.api.kafka.model.KafkaClusterSpec) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) Cache(io.fabric8.kubernetes.client.informers.cache.Cache) ManagedKafkaRoute(org.bf2.operator.resources.v1alpha1.ManagedKafkaRoute) IngressControllerBuilder(io.fabric8.openshift.api.model.operator.v1.IngressControllerBuilder) TypedVisitor(io.fabric8.kubernetes.api.builder.TypedVisitor) BigDecimal(java.math.BigDecimal) Resource(io.fabric8.kubernetes.client.dsl.Resource) Route(io.fabric8.openshift.api.model.Route) IngressControllerSpec(io.fabric8.openshift.api.model.operator.v1.IngressControllerSpec) Map(java.util.Map) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) AbstractKafkaCluster(org.bf2.operator.operands.AbstractKafkaCluster) Scheduled(io.quarkus.scheduler.Scheduled) OperandUtils(org.bf2.common.OperandUtils) Predicate(java.util.function.Predicate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) FilterWatchListDeletable(io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable) Collectors(java.util.stream.Collectors) NodeList(io.fabric8.kubernetes.api.model.NodeList) Objects(java.util.Objects) Startup(io.quarkus.runtime.Startup) List(java.util.List) KafkaCluster(org.bf2.operator.operands.KafkaCluster) Stream(java.util.stream.Stream) IngressControllerList(io.fabric8.openshift.api.model.operator.v1.IngressControllerList) ResourceInformerFactory(org.bf2.common.ResourceInformerFactory) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) Pattern(java.util.regex.Pattern) IngressController(io.fabric8.openshift.api.model.operator.v1.IngressController) ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) GenericKafkaListener(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListener) ResourceEventHandler(io.fabric8.kubernetes.client.informers.ResourceEventHandler) Logger(org.jboss.logging.Logger) ResourceRequirementsBuilder(io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder) HashMap(java.util.HashMap) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Inject(javax.inject.Inject) UnlessBuildProperty(io.quarkus.arc.properties.UnlessBuildProperty) GenericKafkaListenerConfiguration(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerConfiguration) Service(io.fabric8.kubernetes.api.model.Service) Node(io.fabric8.kubernetes.api.model.Node) LongSummaryStatistics(java.util.LongSummaryStatistics) KafkaInstanceConfiguration(org.bf2.operator.operands.KafkaInstanceConfiguration) Pod(io.fabric8.kubernetes.api.model.Pod) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ResourceInformer(org.bf2.common.ResourceInformer) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) PodList(io.fabric8.kubernetes.api.model.PodList) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) Kafka(io.strimzi.api.kafka.model.Kafka) Comparator(java.util.Comparator) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ManagedKafkaRoute(org.bf2.operator.resources.v1alpha1.ManagedKafkaRoute)

Example 3 with ManagedKafkaRoute

use of org.bf2.operator.resources.v1alpha1.ManagedKafkaRoute in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class ManagedKafkaController method updateManagedKafkaStatus.

/**
 * Extract from the current KafkaInstance overall status (Kafka, Canary and AdminServer)
 * a corresponding list of ManagedKafkaCondition(s) to set on the ManagedKafka status
 *
 * @param managedKafka ManagedKafka instance
 */
private void updateManagedKafkaStatus(ManagedKafka managedKafka) {
    // add status if not already available on the ManagedKafka resource
    ManagedKafkaStatus status = Objects.requireNonNullElse(managedKafka.getStatus(), new ManagedKafkaStatusBuilder().build());
    status.setUpdatedTimestamp(ConditionUtils.iso8601Now());
    managedKafka.setStatus(status);
    // add conditions if not already available
    List<ManagedKafkaCondition> managedKafkaConditions = managedKafka.getStatus().getConditions();
    if (managedKafkaConditions == null) {
        managedKafkaConditions = new ArrayList<>();
        status.setConditions(managedKafkaConditions);
    }
    Optional<ManagedKafkaCondition> optReady = ConditionUtils.findManagedKafkaCondition(managedKafkaConditions, ManagedKafkaCondition.Type.Ready);
    ManagedKafkaCondition ready = null;
    if (optReady.isPresent()) {
        ready = optReady.get();
    } else {
        ready = ConditionUtils.buildCondition(ManagedKafkaCondition.Type.Ready, Status.Unknown);
        managedKafkaConditions.add(ready);
    }
    // a not valid ManagedKafka skips the handling of it, so the status will report an error condition
    OperandReadiness readiness = this.validity(managedKafka).orElse(kafkaInstance.getReadiness(managedKafka));
    ConditionUtils.updateConditionStatus(ready, readiness.getStatus(), readiness.getReason(), readiness.getMessage());
    // routes should always be set on the CR status, even if it's just an empty list
    status.setRoutes(List.of());
    int replicas = kafkaCluster.getReplicas(managedKafka);
    if (ingressControllerManagerInstance.isResolvable()) {
        IngressControllerManager ingressControllerManager = ingressControllerManagerInstance.get();
        List<ManagedKafkaRoute> routes = ingressControllerManager.getManagedKafkaRoutesFor(managedKafka);
        // expect route for each broker + 1 for bootstrap URL + 1 for Admin API server
        int expectedNumRoutes = replicas + NUM_NON_BROKER_ROUTES;
        if (routes.size() >= expectedNumRoutes && routes.stream().noneMatch(r -> "".equals(r.getRouter()))) {
            status.setRoutes(routes);
        }
    }
    if (Status.True.equals(readiness.getStatus())) {
        status.setCapacity(new ManagedKafkaCapacityBuilder(managedKafka.getSpec().getCapacity()).withMaxDataRetentionSize(kafkaInstance.getKafkaCluster().calculateRetentionSize(managedKafka)).build());
        // the versions in the status are updated incrementally copying the spec only when each stage ends
        VersionsBuilder versionsBuilder = status.getVersions() != null ? new VersionsBuilder(status.getVersions()) : new VersionsBuilder(managedKafka.getSpec().getVersions());
        if (!Reason.StrimziUpdating.equals(readiness.getReason()) && !this.strimziManager.hasStrimziChanged(managedKafka)) {
            versionsBuilder.withStrimzi(managedKafka.getSpec().getVersions().getStrimzi());
        }
        if (!Reason.KafkaUpdating.equals(readiness.getReason()) && !this.kafkaManager.hasKafkaVersionChanged(managedKafka)) {
            versionsBuilder.withKafka(managedKafka.getSpec().getVersions().getKafka());
        }
        if (!Reason.KafkaIbpUpdating.equals(readiness.getReason()) && !this.kafkaManager.hasKafkaIbpVersionChanged(managedKafka)) {
            String kafkaIbp = managedKafka.getSpec().getVersions().getKafkaIbp() != null ? managedKafka.getSpec().getVersions().getKafkaIbp() : AbstractKafkaCluster.getKafkaIbpVersion(managedKafka.getSpec().getVersions().getKafka());
            versionsBuilder.withKafkaIbp(kafkaIbp);
        }
        status.setVersions(versionsBuilder.build());
        status.setAdminServerURI(kafkaInstance.getAdminServer().uri(managedKafka));
        status.setServiceAccounts(managedKafka.getSpec().getServiceAccounts());
    }
}
Also used : DeleteControl(io.javaoperatorsdk.operator.api.DeleteControl) ManagedKafkaResourceClient(org.bf2.common.ManagedKafkaResourceClient) Context(io.javaoperatorsdk.operator.api.Context) Status(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition.Status) Timed(io.micrometer.core.annotation.Timed) StrimziVersionStatus(org.bf2.operator.resources.v1alpha1.StrimziVersionStatus) Logger(org.jboss.logging.Logger) StrimziManager(org.bf2.operator.managers.StrimziManager) ManagedKafkaRoute(org.bf2.operator.resources.v1alpha1.ManagedKafkaRoute) ResourceEventSource(org.bf2.operator.events.ResourceEventSource) ArrayList(java.util.ArrayList) Controller(io.javaoperatorsdk.operator.api.Controller) VersionsBuilder(org.bf2.operator.resources.v1alpha1.VersionsBuilder) Inject(javax.inject.Inject) KafkaInstance(org.bf2.operator.operands.KafkaInstance) ManagedKafkaStatus(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatus) UpdateControl(io.javaoperatorsdk.operator.api.UpdateControl) AbstractKafkaCluster(org.bf2.operator.operands.AbstractKafkaCluster) KafkaManager(org.bf2.operator.managers.KafkaManager) Instance(javax.enterprise.inject.Instance) NDC(org.jboss.logging.NDC) KafkaInstanceConfiguration(org.bf2.operator.operands.KafkaInstanceConfiguration) ManagedKafkaStatusBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder) IngressControllerManager(org.bf2.operator.managers.IngressControllerManager) ConditionUtils(org.bf2.common.ConditionUtils) Reason(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition.Reason) Objects(java.util.Objects) List(java.util.List) Counted(io.micrometer.core.annotation.Counted) ManagedKafkaCondition(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition) OperandReadiness(org.bf2.operator.operands.OperandReadiness) ManagedKafkaCapacityBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaCapacityBuilder) Optional(java.util.Optional) ResourceController(io.javaoperatorsdk.operator.api.ResourceController) EventSourceManager(io.javaoperatorsdk.operator.processing.event.EventSourceManager) ManagedKafka(org.bf2.operator.resources.v1alpha1.ManagedKafka) ManagedKafkaCapacityBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaCapacityBuilder) ManagedKafkaCondition(org.bf2.operator.resources.v1alpha1.ManagedKafkaCondition) VersionsBuilder(org.bf2.operator.resources.v1alpha1.VersionsBuilder) IngressControllerManager(org.bf2.operator.managers.IngressControllerManager) ManagedKafkaStatusBuilder(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatusBuilder) ManagedKafkaRoute(org.bf2.operator.resources.v1alpha1.ManagedKafkaRoute) ManagedKafkaStatus(org.bf2.operator.resources.v1alpha1.ManagedKafkaStatus) OperandReadiness(org.bf2.operator.operands.OperandReadiness)

Aggregations

List (java.util.List)3 Inject (javax.inject.Inject)3 AbstractKafkaCluster (org.bf2.operator.operands.AbstractKafkaCluster)3 ManagedKafka (org.bf2.operator.resources.v1alpha1.ManagedKafka)3 ManagedKafkaRoute (org.bf2.operator.resources.v1alpha1.ManagedKafkaRoute)3 Node (io.fabric8.kubernetes.api.model.Node)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 Quantity (io.fabric8.kubernetes.api.model.Quantity)2 Service (io.fabric8.kubernetes.api.model.Service)2 Route (io.fabric8.openshift.api.model.Route)2 IngressController (io.fabric8.openshift.api.model.operator.v1.IngressController)2 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 KafkaInstanceConfiguration (org.bf2.operator.operands.KafkaInstanceConfiguration)2 Logger (org.jboss.logging.Logger)2 TypedVisitor (io.fabric8.kubernetes.api.builder.TypedVisitor)1 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)1