Search in sources :

Example 11 with KafkaStatus

use of io.strimzi.api.kafka.model.status.KafkaStatus in project strimzi-kafka-operator by strimzi.

the class KafkaAssemblyOperator method createOrUpdate.

@Override
public Future<KafkaStatus> createOrUpdate(Reconciliation reconciliation, Kafka kafkaAssembly) {
    Promise<KafkaStatus> createOrUpdatePromise = Promise.promise();
    ReconciliationState reconcileState = createReconciliationState(reconciliation, kafkaAssembly);
    reconcile(reconcileState).onComplete(reconcileResult -> {
        KafkaStatus status = reconcileState.kafkaStatus;
        Condition condition;
        if (kafkaAssembly.getMetadata().getGeneration() != null) {
            status.setObservedGeneration(kafkaAssembly.getMetadata().getGeneration());
        }
        if (reconcileResult.succeeded()) {
            condition = new ConditionBuilder().withLastTransitionTime(StatusUtils.iso8601(dateSupplier())).withType("Ready").withStatus("True").build();
            status.addCondition(condition);
            createOrUpdatePromise.complete(status);
        } else {
            condition = new ConditionBuilder().withLastTransitionTime(StatusUtils.iso8601(dateSupplier())).withType("NotReady").withStatus("True").withReason(reconcileResult.cause().getClass().getSimpleName()).withMessage(reconcileResult.cause().getMessage()).build();
            status.addCondition(condition);
            createOrUpdatePromise.fail(new ReconciliationException(status, reconcileResult.cause()));
        }
    });
    return createOrUpdatePromise.future();
}
Also used : Condition(io.strimzi.api.kafka.model.status.Condition) ReconciliationException(io.strimzi.operator.common.ReconciliationException) ConditionBuilder(io.strimzi.api.kafka.model.status.ConditionBuilder) KafkaStatus(io.strimzi.api.kafka.model.status.KafkaStatus)

Example 12 with KafkaStatus

use of io.strimzi.api.kafka.model.status.KafkaStatus in project strimzi-kafka-operator by strimzi.

the class KafkaAssemblyOperatorTest method testReconcile.

@SuppressWarnings("unchecked")
@ParameterizedTest
@MethodSource("data")
@Timeout(value = 2, timeUnit = TimeUnit.MINUTES)
public void testReconcile(Params params, VertxTestContext context) {
    // Must create all checkpoints before flagging any, as not doing so can lead to premature test success
    Checkpoint fooAsync = context.checkpoint();
    Checkpoint barAsync = context.checkpoint();
    Checkpoint completeTest = context.checkpoint();
    setFields(params);
    // create CM, Service, headless service, statefulset
    ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(openShift);
    ClusterOperatorConfig config = ResourceUtils.dummyClusterOperatorConfig(VERSIONS);
    var mockKafkaOps = supplier.kafkaOperator;
    StatefulSetOperator mockStsOps = supplier.stsOperations;
    SecretOperator mockSecretOps = supplier.secretOperations;
    String kafkaNamespace = "test";
    Kafka foo = getKafkaAssembly("foo");
    Kafka bar = getKafkaAssembly("bar");
    when(mockKafkaOps.listAsync(eq(kafkaNamespace), any(Optional.class))).thenReturn(Future.succeededFuture(asList(foo, bar)));
    // when requested Custom Resource for a specific Kafka cluster
    when(mockKafkaOps.get(eq(kafkaNamespace), eq("foo"))).thenReturn(foo);
    when(mockKafkaOps.get(eq(kafkaNamespace), eq("bar"))).thenReturn(bar);
    when(mockKafkaOps.getAsync(eq(kafkaNamespace), eq("foo"))).thenReturn(Future.succeededFuture(foo));
    when(mockKafkaOps.getAsync(eq(kafkaNamespace), eq("bar"))).thenReturn(Future.succeededFuture(bar));
    when(mockKafkaOps.updateStatusAsync(any(), any(Kafka.class))).thenReturn(Future.succeededFuture());
    // providing certificates Secrets for existing clusters
    List<Secret> fooSecrets = ResourceUtils.createKafkaInitialSecrets(kafkaNamespace, "foo");
    // ClusterCa fooCerts = ResourceUtils.createInitialClusterCa("foo", ModelUtils.findSecretWithName(fooSecrets, AbstractModel.clusterCaCertSecretName("foo")));
    List<Secret> barSecrets = ResourceUtils.createKafkaSecretsWithReplicas(kafkaNamespace, "bar", bar.getSpec().getKafka().getReplicas(), bar.getSpec().getZookeeper().getReplicas());
    ClusterCa barClusterCa = ResourceUtils.createInitialClusterCa(Reconciliation.DUMMY_RECONCILIATION, "bar", findSecretWithName(barSecrets, AbstractModel.clusterCaCertSecretName("bar")), findSecretWithName(barSecrets, AbstractModel.clusterCaKeySecretName("bar")));
    ClientsCa barClientsCa = ResourceUtils.createInitialClientsCa(Reconciliation.DUMMY_RECONCILIATION, "bar", findSecretWithName(barSecrets, KafkaCluster.clientsCaCertSecretName("bar")), findSecretWithName(barSecrets, KafkaCluster.clientsCaKeySecretName("bar")));
    // providing the list of ALL StatefulSets for all the Kafka clusters
    Labels newLabels = Labels.forStrimziKind(Kafka.RESOURCE_KIND);
    when(mockStsOps.list(eq(kafkaNamespace), eq(newLabels))).thenReturn(List.of(KafkaCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, bar, VERSIONS).generateStatefulSet(openShift, null, null, null)));
    when(mockSecretOps.get(eq(kafkaNamespace), eq(AbstractModel.clusterCaCertSecretName(foo.getMetadata().getName())))).thenReturn(fooSecrets.get(0));
    when(mockSecretOps.reconcile(any(), eq(kafkaNamespace), eq(AbstractModel.clusterCaCertSecretName(foo.getMetadata().getName())), any(Secret.class))).thenReturn(Future.succeededFuture());
    // providing the list StatefulSets for already "existing" Kafka clusters
    Labels barLabels = Labels.forStrimziCluster("bar");
    KafkaCluster barCluster = KafkaCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, bar, VERSIONS);
    when(mockStsOps.list(eq(kafkaNamespace), eq(barLabels))).thenReturn(List.of(barCluster.generateStatefulSet(openShift, null, null, null)));
    when(mockSecretOps.list(eq(kafkaNamespace), eq(barLabels))).thenAnswer(invocation -> new ArrayList<>(asList(barClientsCa.caKeySecret(), barClientsCa.caCertSecret(), barCluster.generateBrokersSecret(barClusterCa, barClientsCa), barClusterCa.caCertSecret())));
    when(mockSecretOps.get(eq(kafkaNamespace), eq(AbstractModel.clusterCaCertSecretName(bar.getMetadata().getName())))).thenReturn(barSecrets.get(0));
    when(mockSecretOps.reconcile(any(), eq(kafkaNamespace), eq(AbstractModel.clusterCaCertSecretName(bar.getMetadata().getName())), any(Secret.class))).thenReturn(Future.succeededFuture());
    KafkaAssemblyOperator ops = new KafkaAssemblyOperator(vertx, new PlatformFeaturesAvailability(openShift, kubernetesVersion), certManager, passwordGenerator, supplier, config) {

        @Override
        public Future<KafkaStatus> createOrUpdate(Reconciliation reconciliation, Kafka kafkaAssembly) {
            String name = kafkaAssembly.getMetadata().getName();
            if ("foo".equals(name)) {
                fooAsync.flag();
            } else if ("bar".equals(name)) {
                barAsync.flag();
            } else {
                context.failNow(new AssertionError("Unexpected name " + name));
            }
            return Future.succeededFuture();
        }
    };
    // Now try to reconcile all the Kafka clusters
    ops.reconcileAll("test", kafkaNamespace, context.succeeding(v -> completeTest.flag()));
}
Also used : RouteIngressBuilder(io.fabric8.openshift.api.model.RouteIngressBuilder) JmxTransQueryTemplateBuilder(io.strimzi.api.kafka.model.template.JmxTransQueryTemplateBuilder) ArgumentMatchers(org.mockito.ArgumentMatchers) KafkaJmxOptions(io.strimzi.api.kafka.model.KafkaJmxOptions) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) PodDisruptionBudget(io.fabric8.kubernetes.api.model.policy.v1.PodDisruptionBudget) AfterAll(org.junit.jupiter.api.AfterAll) PersistentClaimStorage(io.strimzi.api.kafka.model.storage.PersistentClaimStorage) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) RouteStatusBuilder(io.fabric8.openshift.api.model.RouteStatusBuilder) KafkaResources(io.strimzi.api.kafka.model.KafkaResources) BeforeAll(org.junit.jupiter.api.BeforeAll) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) AbstractModel(io.strimzi.operator.cluster.model.AbstractModel) StatefulSetOperator(io.strimzi.operator.cluster.operator.resource.StatefulSetOperator) KafkaJmxOptionsBuilder(io.strimzi.api.kafka.model.KafkaJmxOptionsBuilder) KafkaVersion(io.strimzi.operator.cluster.model.KafkaVersion) Set(java.util.Set) GenericKafkaListenerBuilder(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder) EphemeralStorage(io.strimzi.api.kafka.model.storage.EphemeralStorage) PasswordGenerator(io.strimzi.operator.common.PasswordGenerator) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) RouteOperator(io.strimzi.operator.common.operator.resource.RouteOperator) EntityOperator(io.strimzi.operator.cluster.model.EntityOperator) ClusterCa(io.strimzi.operator.cluster.model.ClusterCa) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) EntityUserOperatorSpecBuilder(io.strimzi.api.kafka.model.EntityUserOperatorSpecBuilder) ClusterOperatorConfig(io.strimzi.operator.cluster.ClusterOperatorConfig) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) VertxTestContext(io.vertx.junit5.VertxTestContext) GenericKafkaListener(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListener) IngressOperator(io.strimzi.operator.common.operator.resource.IngressOperator) EntityUserOperator(io.strimzi.operator.cluster.model.EntityUserOperator) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) PersistentClaimStorageBuilder(io.strimzi.api.kafka.model.storage.PersistentClaimStorageBuilder) KafkaVersionTestUtils(io.strimzi.operator.cluster.KafkaVersionTestUtils) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) KafkaJmxAuthenticationPasswordBuilder(io.strimzi.api.kafka.model.KafkaJmxAuthenticationPasswordBuilder) KubernetesVersion(io.strimzi.operator.KubernetesVersion) Vertx(io.vertx.core.Vertx) JmxTransSpecBuilder(io.strimzi.api.kafka.model.JmxTransSpecBuilder) Mockito.times(org.mockito.Mockito.times) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) PvcOperator(io.strimzi.operator.common.operator.resource.PvcOperator) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) Reconciliation(io.strimzi.operator.common.Reconciliation) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Mockito.never(org.mockito.Mockito.never) KafkaListenerType(io.strimzi.api.kafka.model.listener.arraylistener.KafkaListenerType) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Storage(io.strimzi.api.kafka.model.storage.Storage) BiFunction(java.util.function.BiFunction) Timeout(io.vertx.junit5.Timeout) Matchers.hasKey(org.hamcrest.Matchers.hasKey) KafkaExporter(io.strimzi.operator.cluster.model.KafkaExporter) Route(io.fabric8.openshift.api.model.Route) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) ResourceUtils(io.strimzi.operator.cluster.ResourceUtils) KafkaExporterSpec(io.strimzi.api.kafka.model.KafkaExporterSpec) MethodSource(org.junit.jupiter.params.provider.MethodSource) ListenersUtils(io.strimzi.operator.cluster.model.ListenersUtils) Collections.emptyList(java.util.Collections.emptyList) DeploymentOperator(io.strimzi.operator.common.operator.resource.DeploymentOperator) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) ClientsCa(io.strimzi.operator.cluster.model.ClientsCa) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) EntityTopicOperator(io.strimzi.operator.cluster.model.EntityTopicOperator) Objects(java.util.Objects) List(java.util.List) Labels(io.strimzi.operator.common.model.Labels) StrimziPodSet(io.strimzi.api.kafka.model.StrimziPodSet) Secret(io.fabric8.kubernetes.api.model.Secret) Optional(java.util.Optional) Checkpoint(io.vertx.junit5.Checkpoint) PodDisruptionBudgetOperator(io.strimzi.operator.common.operator.resource.PodDisruptionBudgetOperator) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) MockCertManager(io.strimzi.operator.common.operator.MockCertManager) EntityOperatorSpecBuilder(io.strimzi.api.kafka.model.EntityOperatorSpecBuilder) JmxTrans(io.strimzi.operator.cluster.model.JmxTrans) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) EntityTopicOperatorSpecBuilder(io.strimzi.api.kafka.model.EntityTopicOperatorSpecBuilder) KafkaStatus(io.strimzi.api.kafka.model.status.KafkaStatus) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) NetworkPolicyOperator(io.strimzi.operator.common.operator.resource.NetworkPolicyOperator) SingleVolumeStorage(io.strimzi.api.kafka.model.storage.SingleVolumeStorage) HashMap(java.util.HashMap) ZookeeperCluster(io.strimzi.operator.cluster.model.ZookeeperCluster) VolumeUtils(io.strimzi.operator.cluster.model.VolumeUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) MetricsAndLogging(io.strimzi.operator.common.MetricsAndLogging) HashSet(java.util.HashSet) JmxPrometheusExporterMetrics(io.strimzi.api.kafka.model.JmxPrometheusExporterMetrics) ServiceOperator(io.strimzi.operator.common.operator.resource.ServiceOperator) ArgumentCaptor(org.mockito.ArgumentCaptor) KafkaCluster(io.strimzi.operator.cluster.model.KafkaCluster) ClusterOperator(io.strimzi.operator.cluster.ClusterOperator) ConfigMapOperator(io.strimzi.operator.common.operator.resource.ConfigMapOperator) InlineLogging(io.strimzi.api.kafka.model.InlineLogging) TestUtils(io.strimzi.test.TestUtils) ReconcileResult(io.strimzi.operator.common.operator.resource.ReconcileResult) Collections.singletonMap(java.util.Collections.singletonMap) Service(io.fabric8.kubernetes.api.model.Service) RouteStatus(io.fabric8.openshift.api.model.RouteStatus) StatefulSetDiff(io.strimzi.operator.cluster.operator.resource.StatefulSetDiff) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) EntityOperatorSpec(io.strimzi.api.kafka.model.EntityOperatorSpec) CruiseControl(io.strimzi.operator.cluster.model.CruiseControl) Collections.emptyMap(java.util.Collections.emptyMap) NodeOperator(io.strimzi.operator.common.operator.resource.NodeOperator) StrimziPodSetList(io.strimzi.api.kafka.StrimziPodSetList) Collections.emptySet(java.util.Collections.emptySet) JmxTransOutputDefinitionTemplateBuilder(io.strimzi.api.kafka.model.template.JmxTransOutputDefinitionTemplateBuilder) TestUtils.set(io.strimzi.test.TestUtils.set) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) NetworkPolicy(io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicy) Kafka(io.strimzi.api.kafka.model.Kafka) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) Collections(java.util.Collections) KafkaCluster(io.strimzi.operator.cluster.model.KafkaCluster) Optional(java.util.Optional) ClusterOperatorConfig(io.strimzi.operator.cluster.ClusterOperatorConfig) Kafka(io.strimzi.api.kafka.model.Kafka) ClusterCa(io.strimzi.operator.cluster.model.ClusterCa) Labels(io.strimzi.operator.common.model.Labels) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StatefulSetOperator(io.strimzi.operator.cluster.operator.resource.StatefulSetOperator) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) Secret(io.fabric8.kubernetes.api.model.Secret) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) Checkpoint(io.vertx.junit5.Checkpoint) ClientsCa(io.strimzi.operator.cluster.model.ClientsCa) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) Reconciliation(io.strimzi.operator.common.Reconciliation) KafkaStatus(io.strimzi.api.kafka.model.status.KafkaStatus) Timeout(io.vertx.junit5.Timeout) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 13 with KafkaStatus

use of io.strimzi.api.kafka.model.status.KafkaStatus in project strimzi-kafka-operator by strimzi.

the class KafkaStatusTest method testStatusAfterFailedReconciliationWithPreviousSuccess.

@Test
public void testStatusAfterFailedReconciliationWithPreviousSuccess(VertxTestContext context) throws ParseException {
    Kafka kafka = getKafkaCrd();
    ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(false);
    // Mock the Kafka Operator
    CrdOperator mockKafkaOps = supplier.kafkaOperator;
    Kafka readyKafka = new KafkaBuilder(kafka).editStatus().withObservedGeneration(1L).editCondition(0).withType("Ready").endCondition().withListeners(new ListenerStatusBuilder().withName("plain").withAddresses(new ListenerAddressBuilder().withHost("my-service.my-namespace.svc").withPort(9092).build()).build(), new ListenerStatusBuilder().withName("external").withAddresses(new ListenerAddressBuilder().withHost("my-route-address.domain.tld").withPort(443).build()).build()).endStatus().build();
    when(mockKafkaOps.getAsync(eq(namespace), eq(clusterName))).thenReturn(Future.succeededFuture(readyKafka));
    when(mockKafkaOps.get(eq(namespace), eq(clusterName))).thenReturn(readyKafka);
    ArgumentCaptor<Kafka> kafkaCaptor = ArgumentCaptor.forClass(Kafka.class);
    when(mockKafkaOps.updateStatusAsync(any(), kafkaCaptor.capture())).thenReturn(Future.succeededFuture());
    MockFailingKafkaAssemblyOperator kao = new MockFailingKafkaAssemblyOperator(new RuntimeException("Something went wrong"), vertx, new PlatformFeaturesAvailability(false, kubernetesVersion), certManager, passwordGenerator, supplier, config);
    Checkpoint async = context.checkpoint();
    kao.reconcile(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, namespace, clusterName)).onComplete(res -> {
        assertThat(res.succeeded(), is(false));
        assertThat(kafkaCaptor.getValue(), is(notNullValue()));
        assertThat(kafkaCaptor.getValue().getStatus(), is(notNullValue()));
        KafkaStatus status = kafkaCaptor.getValue().getStatus();
        assertThat(status.getListeners().size(), is(1));
        assertThat(status.getListeners().get(0).getType(), is("plain"));
        assertThat(status.getListeners().get(0).getName(), is("plain"));
        assertThat(status.getListeners().get(0).getAddresses().get(0).getHost(), is("my-service.my-namespace.svc"));
        assertThat(status.getListeners().get(0).getAddresses().get(0).getPort(), is(Integer.valueOf(9092)));
        assertThat(status.getListeners().get(0).getBootstrapServers(), is("my-service.my-namespace.svc:9092"));
        assertThat(status.getConditions().size(), is(1));
        assertThat(status.getConditions().get(0).getType(), is("NotReady"));
        assertThat(status.getConditions().get(0).getStatus(), is("True"));
        assertThat(status.getConditions().get(0).getReason(), is("RuntimeException"));
        assertThat(status.getConditions().get(0).getMessage(), is("Something went wrong"));
        assertThat(status.getObservedGeneration(), is(2L));
        async.flag();
    });
}
Also used : ListenerAddressBuilder(io.strimzi.api.kafka.model.status.ListenerAddressBuilder) Kafka(io.strimzi.api.kafka.model.Kafka) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) Checkpoint(io.vertx.junit5.Checkpoint) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) Reconciliation(io.strimzi.operator.common.Reconciliation) ListenerStatusBuilder(io.strimzi.api.kafka.model.status.ListenerStatusBuilder) KafkaStatus(io.strimzi.api.kafka.model.status.KafkaStatus) Test(org.junit.jupiter.api.Test)

Example 14 with KafkaStatus

use of io.strimzi.api.kafka.model.status.KafkaStatus in project strimzi-kafka-operator by strimzi.

the class KafkaStatusTest method testKafkaListenerNodePortAddressInStatus.

@Test
public void testKafkaListenerNodePortAddressInStatus(VertxTestContext context) throws ParseException {
    Kafka kafka = new KafkaBuilder(getKafkaCrd()).editOrNewSpec().editOrNewKafka().withListeners(new GenericKafkaListenerBuilder().withName("external").withPort(9094).withType(KafkaListenerType.NODEPORT).withTls(true).build()).endKafka().endSpec().build();
    KafkaCluster kafkaCluster = KafkaCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, kafka, VERSIONS);
    ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(false);
    // Mock the CRD Operator for Kafka resources
    CrdOperator mockKafkaOps = supplier.kafkaOperator;
    when(mockKafkaOps.getAsync(eq(namespace), eq(clusterName))).thenReturn(Future.succeededFuture(kafka));
    when(mockKafkaOps.get(eq(namespace), eq(clusterName))).thenReturn(kafka);
    ArgumentCaptor<Kafka> kafkaCaptor = ArgumentCaptor.forClass(Kafka.class);
    when(mockKafkaOps.updateStatusAsync(any(), kafkaCaptor.capture())).thenReturn(Future.succeededFuture());
    // Mock the KafkaSetOperator
    StatefulSetOperator mockStsOps = supplier.stsOperations;
    when(mockStsOps.getAsync(eq(namespace), eq(KafkaCluster.kafkaClusterName(clusterName)))).thenReturn(Future.succeededFuture(kafkaCluster.generateStatefulSet(false, null, null, null)));
    // Mock the StrimziPodSet operator
    CrdOperator<KubernetesClient, StrimziPodSet, StrimziPodSetList> mockPodSetOps = supplier.strimziPodSetOperator;
    when(mockPodSetOps.getAsync(any(), any())).thenReturn(Future.succeededFuture(null));
    // Mock the ConfigMapOperator
    ConfigMapOperator mockCmOps = supplier.configMapOperations;
    when(mockCmOps.getAsync(eq(namespace), eq(clusterName))).thenReturn(Future.succeededFuture(kafkaCluster.generateMetricsAndLogConfigMap(new MetricsAndLogging(null, null))));
    // Mock Pods Operator
    Pod pod0 = new PodBuilder().withNewMetadata().withName(clusterName + "-kafka-" + 0).endMetadata().withNewStatus().withHostIP("10.0.0.1").endStatus().build();
    Pod pod1 = new PodBuilder().withNewMetadata().withName(clusterName + "-kafka-" + 1).endMetadata().withNewStatus().withHostIP("10.0.0.25").endStatus().build();
    Pod pod2 = new PodBuilder().withNewMetadata().withName(clusterName + "-kafka-" + 2).endMetadata().withNewStatus().withHostIP("10.0.0.13").endStatus().build();
    List<Pod> pods = new ArrayList<>();
    pods.add(pod0);
    pods.add(pod1);
    pods.add(pod2);
    PodOperator mockPodOps = supplier.podOperations;
    when(mockPodOps.listAsync(eq(namespace), any(Labels.class))).thenReturn(Future.succeededFuture(pods));
    // Mock Node operator
    NodeOperator mockNodeOps = supplier.nodeOperator;
    when(mockNodeOps.listAsync(any(Labels.class))).thenReturn(Future.succeededFuture(getClusterNodes()));
    MockNodePortStatusKafkaAssemblyOperator kao = new MockNodePortStatusKafkaAssemblyOperator(vertx, new PlatformFeaturesAvailability(false, kubernetesVersion), certManager, passwordGenerator, supplier, config);
    Checkpoint async = context.checkpoint();
    kao.reconcile(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, namespace, clusterName)).onComplete(res -> {
        assertThat(res.succeeded(), is(true));
        assertThat(kafkaCaptor.getValue(), is(notNullValue()));
        assertThat(kafkaCaptor.getValue().getStatus(), is(notNullValue()));
        KafkaStatus status = kafkaCaptor.getValue().getStatus();
        assertThat(status.getListeners().size(), is(1));
        assertThat(status.getListeners().get(0).getType(), is("external"));
        assertThat(status.getListeners().get(0).getName(), is("external"));
        List<ListenerAddress> addresses = status.getListeners().get(0).getAddresses();
        assertThat(addresses.size(), is(3));
        List<ListenerAddress> expected = new ArrayList<>();
        expected.add(new ListenerAddressBuilder().withHost("50.35.18.119").withPort(31234).build());
        expected.add(new ListenerAddressBuilder().withHost("55.36.78.115").withPort(31234).build());
        expected.add(new ListenerAddressBuilder().withHost("5.124.16.8").withPort(31234).build());
        async.flag();
    });
}
Also used : ListenerAddressBuilder(io.strimzi.api.kafka.model.status.ListenerAddressBuilder) StrimziPodSet(io.strimzi.api.kafka.model.StrimziPodSet) KafkaCluster(io.strimzi.operator.cluster.model.KafkaCluster) Kafka(io.strimzi.api.kafka.model.Kafka) ArrayList(java.util.ArrayList) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) GenericKafkaListenerBuilder(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder) ListenerAddress(io.strimzi.api.kafka.model.status.ListenerAddress) Reconciliation(io.strimzi.operator.common.Reconciliation) NodeOperator(io.strimzi.operator.common.operator.resource.NodeOperator) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Pod(io.fabric8.kubernetes.api.model.Pod) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) MetricsAndLogging(io.strimzi.operator.common.MetricsAndLogging) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) Labels(io.strimzi.operator.common.model.Labels) StatefulSetOperator(io.strimzi.operator.cluster.operator.resource.StatefulSetOperator) StrimziPodSetList(io.strimzi.api.kafka.StrimziPodSetList) Checkpoint(io.vertx.junit5.Checkpoint) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) ConfigMapOperator(io.strimzi.operator.common.operator.resource.ConfigMapOperator) KafkaStatus(io.strimzi.api.kafka.model.status.KafkaStatus) Test(org.junit.jupiter.api.Test)

Example 15 with KafkaStatus

use of io.strimzi.api.kafka.model.status.KafkaStatus in project strimzi-kafka-operator by strimzi.

the class KafkaStatusTest method testKafkaListenerNodePortAddressInStatusWithOverrides.

@Test
public void testKafkaListenerNodePortAddressInStatusWithOverrides(VertxTestContext context) throws ParseException {
    GenericKafkaListenerConfigurationBroker broker0 = new GenericKafkaListenerConfigurationBrokerBuilder().withBroker(0).withAdvertisedHost("my-address-0").build();
    GenericKafkaListenerConfigurationBroker broker1 = new GenericKafkaListenerConfigurationBrokerBuilder().withBroker(1).withAdvertisedHost("my-address-1").build();
    Kafka kafka = new KafkaBuilder(getKafkaCrd()).editOrNewSpec().editOrNewKafka().withListeners(new GenericKafkaListenerBuilder().withName("external").withPort(9094).withType(KafkaListenerType.NODEPORT).withTls(true).withNewConfiguration().withBrokers(broker0, broker1).endConfiguration().build()).endKafka().endSpec().build();
    KafkaCluster kafkaCluster = KafkaCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, kafka, VERSIONS);
    ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(false);
    // Mock the CRD Operator for Kafka resources
    CrdOperator mockKafkaOps = supplier.kafkaOperator;
    when(mockKafkaOps.getAsync(eq(namespace), eq(clusterName))).thenReturn(Future.succeededFuture(kafka));
    when(mockKafkaOps.get(eq(namespace), eq(clusterName))).thenReturn(kafka);
    ArgumentCaptor<Kafka> kafkaCaptor = ArgumentCaptor.forClass(Kafka.class);
    when(mockKafkaOps.updateStatusAsync(any(), kafkaCaptor.capture())).thenReturn(Future.succeededFuture());
    // Mock the KafkaSetOperator
    StatefulSetOperator mockStsOps = supplier.stsOperations;
    when(mockStsOps.getAsync(eq(namespace), eq(KafkaCluster.kafkaClusterName(clusterName)))).thenReturn(Future.succeededFuture(kafkaCluster.generateStatefulSet(false, null, null, null)));
    // Mock the StrimziPodSet operator
    CrdOperator<KubernetesClient, StrimziPodSet, StrimziPodSetList> mockPodSetOps = supplier.strimziPodSetOperator;
    when(mockPodSetOps.getAsync(any(), any())).thenReturn(Future.succeededFuture(null));
    // Mock the ConfigMapOperator
    ConfigMapOperator mockCmOps = supplier.configMapOperations;
    when(mockCmOps.getAsync(eq(namespace), eq(clusterName))).thenReturn(Future.succeededFuture(kafkaCluster.generateMetricsAndLogConfigMap(new MetricsAndLogging(null, null))));
    // Mock Pods Operator
    Pod pod0 = new PodBuilder().withNewMetadata().withName(clusterName + "-kafka-" + 0).endMetadata().withNewStatus().withHostIP("10.0.0.1").endStatus().build();
    Pod pod1 = new PodBuilder().withNewMetadata().withName(clusterName + "-kafka-" + 1).endMetadata().withNewStatus().withHostIP("10.0.0.25").endStatus().build();
    Pod pod2 = new PodBuilder().withNewMetadata().withName(clusterName + "-kafka-" + 2).endMetadata().withNewStatus().withHostIP("10.0.0.13").endStatus().build();
    List<Pod> pods = new ArrayList<>();
    pods.add(pod0);
    pods.add(pod1);
    pods.add(pod2);
    PodOperator mockPodOps = supplier.podOperations;
    when(mockPodOps.listAsync(eq(namespace), any(Labels.class))).thenReturn(Future.succeededFuture(pods));
    // Mock Node operator
    NodeOperator mockNodeOps = supplier.nodeOperator;
    when(mockNodeOps.listAsync(any(Labels.class))).thenReturn(Future.succeededFuture(getClusterNodes()));
    MockNodePortStatusKafkaAssemblyOperator kao = new MockNodePortStatusKafkaAssemblyOperator(vertx, new PlatformFeaturesAvailability(false, kubernetesVersion), certManager, passwordGenerator, supplier, config);
    Checkpoint async = context.checkpoint();
    kao.reconcile(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, namespace, clusterName)).onComplete(res -> {
        assertThat(res.succeeded(), is(true));
        assertThat(kafkaCaptor.getValue(), is(notNullValue()));
        assertThat(kafkaCaptor.getValue().getStatus(), is(notNullValue()));
        KafkaStatus status = kafkaCaptor.getValue().getStatus();
        assertThat(status.getListeners().size(), is(1));
        assertThat(status.getListeners().get(0).getType(), is("external"));
        assertThat(status.getListeners().get(0).getName(), is("external"));
        List<ListenerAddress> addresses = status.getListeners().get(0).getAddresses();
        assertThat(addresses.size(), is(3));
        List<ListenerAddress> expected = new ArrayList<>();
        expected.add(new ListenerAddressBuilder().withHost("my-address-0").withPort(31234).build());
        expected.add(new ListenerAddressBuilder().withHost("my-address-1").withPort(31234).build());
        expected.add(new ListenerAddressBuilder().withHost("5.124.16.8").withPort(31234).build());
        async.flag();
    });
}
Also used : ListenerAddressBuilder(io.strimzi.api.kafka.model.status.ListenerAddressBuilder) StrimziPodSet(io.strimzi.api.kafka.model.StrimziPodSet) KafkaCluster(io.strimzi.operator.cluster.model.KafkaCluster) Kafka(io.strimzi.api.kafka.model.Kafka) ArrayList(java.util.ArrayList) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) GenericKafkaListenerBuilder(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder) ListenerAddress(io.strimzi.api.kafka.model.status.ListenerAddress) Reconciliation(io.strimzi.operator.common.Reconciliation) NodeOperator(io.strimzi.operator.common.operator.resource.NodeOperator) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Pod(io.fabric8.kubernetes.api.model.Pod) GenericKafkaListenerConfigurationBrokerBuilder(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerConfigurationBrokerBuilder) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) MetricsAndLogging(io.strimzi.operator.common.MetricsAndLogging) GenericKafkaListenerConfigurationBroker(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerConfigurationBroker) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) Labels(io.strimzi.operator.common.model.Labels) StatefulSetOperator(io.strimzi.operator.cluster.operator.resource.StatefulSetOperator) StrimziPodSetList(io.strimzi.api.kafka.StrimziPodSetList) Checkpoint(io.vertx.junit5.Checkpoint) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) ConfigMapOperator(io.strimzi.operator.common.operator.resource.ConfigMapOperator) KafkaStatus(io.strimzi.api.kafka.model.status.KafkaStatus) Test(org.junit.jupiter.api.Test)

Aggregations

KafkaStatus (io.strimzi.api.kafka.model.status.KafkaStatus)38 Kafka (io.strimzi.api.kafka.model.Kafka)28 PlatformFeaturesAvailability (io.strimzi.operator.PlatformFeaturesAvailability)28 ResourceOperatorSupplier (io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier)28 Reconciliation (io.strimzi.operator.common.Reconciliation)28 CrdOperator (io.strimzi.operator.common.operator.resource.CrdOperator)28 Checkpoint (io.vertx.junit5.Checkpoint)26 Test (org.junit.jupiter.api.Test)22 KafkaBuilder (io.strimzi.api.kafka.model.KafkaBuilder)20 KafkaCluster (io.strimzi.operator.cluster.model.KafkaCluster)18 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)16 StrimziPodSetList (io.strimzi.api.kafka.StrimziPodSetList)16 StrimziPodSet (io.strimzi.api.kafka.model.StrimziPodSet)16 GenericKafkaListenerBuilder (io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder)16 ListenerAddressBuilder (io.strimzi.api.kafka.model.status.ListenerAddressBuilder)16 StatefulSetOperator (io.strimzi.operator.cluster.operator.resource.StatefulSetOperator)16 MetricsAndLogging (io.strimzi.operator.common.MetricsAndLogging)16 Labels (io.strimzi.operator.common.model.Labels)16 ConfigMapOperator (io.strimzi.operator.common.operator.resource.ConfigMapOperator)16 NodeOperator (io.strimzi.operator.common.operator.resource.NodeOperator)16