Search in sources :

Example 91 with PodOperator

use of io.strimzi.operator.common.operator.resource.PodOperator in project strimzi-kafka-operator by strimzi.

the class KafkaRollerTest method testControllerNeverRollable.

@Test
public void testControllerNeverRollable(VertxTestContext testContext) throws InterruptedException {
    PodOperator podOps = mockPodOps(podId -> succeededFuture());
    StatefulSet sts = buildStatefulSet();
    TestingKafkaRoller kafkaRoller = new TestingKafkaRoller(sts, null, null, addPodNames(sts.getSpec().getReplicas()), podOps, noException(), null, noException(), noException(), noException(), brokerId -> brokerId == 2 ? succeededFuture(false) : succeededFuture(true), 2);
    doFailingRollingRestart(testContext, kafkaRoller, asList(0, 1, 2, 3, 4), KafkaRoller.UnforceableProblem.class, "Pod c-kafka-2 is currently not rollable", // We expect all non-controller pods to be rolled
    asList(0, 1, 3, 4));
    clearRestarted();
    kafkaRoller = new TestingKafkaRoller(sts, null, null, addPodNames(sts.getSpec().getReplicas()), podOps, noException(), null, noException(), noException(), noException(), brokerId -> succeededFuture(brokerId != 2), 2);
    doFailingRollingRestart(testContext, kafkaRoller, singletonList(2), KafkaRoller.UnforceableProblem.class, "Pod c-kafka-2 is currently not rollable", // We expect all non-controller pods to be rolled
    emptyList());
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) BeforeEach(org.junit.jupiter.api.BeforeEach) Collections.singletonList(java.util.Collections.singletonList) AdminClient(org.apache.kafka.clients.admin.AdminClient) AfterAll(org.junit.jupiter.api.AfterAll) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) DefaultAdminClientProvider(io.strimzi.operator.common.DefaultAdminClientProvider) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) KafkaResources(io.strimzi.api.kafka.model.KafkaResources) BeforeAll(org.junit.jupiter.api.BeforeAll) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) IdentityHashMap(java.util.IdentityHashMap) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) Set(java.util.Set) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Labels(io.strimzi.operator.common.model.Labels) Logger(org.apache.logging.log4j.Logger) Secret(io.fabric8.kubernetes.api.model.Secret) Checkpoint(io.vertx.junit5.Checkpoint) TimeoutException(io.strimzi.operator.common.operator.resource.TimeoutException) Mockito.mock(org.mockito.Mockito.mock) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) VertxTestContext(io.vertx.junit5.VertxTestContext) Config(org.apache.kafka.clients.admin.Config) Future.succeededFuture(io.vertx.core.Future.succeededFuture) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BackOff(io.strimzi.operator.common.BackOff) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Function(java.util.function.Function) ArrayList(java.util.ArrayList) KafkaVersionTestUtils(io.strimzi.operator.cluster.KafkaVersionTestUtils) Admin(org.apache.kafka.clients.admin.Admin) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Vertx(io.vertx.core.Vertx) Pod(io.fabric8.kubernetes.api.model.Pod) Future.failedFuture(io.vertx.core.Future.failedFuture) Mockito.when(org.mockito.Mockito.when) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) TimeUnit(java.util.concurrent.TimeUnit) Reconciliation(io.strimzi.operator.common.Reconciliation) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Test(org.junit.jupiter.api.Test)

Example 92 with PodOperator

use of io.strimzi.operator.common.operator.resource.PodOperator in project strimzi-kafka-operator by strimzi.

the class KafkaRollerTest method pod1NotReadyAfterRolling.

@Test
public void pod1NotReadyAfterRolling(VertxTestContext testContext) throws InterruptedException {
    PodOperator podOps = mockPodOps(podId -> podId == 1 ? failedFuture(new TimeoutException("Timeout")) : succeededFuture());
    StatefulSet sts = buildStatefulSet();
    TestingKafkaRoller kafkaRoller = rollerWithControllers(sts, podOps, 2);
    // On the first reconciliation we expect it to abort when it gets to pod 1
    doFailingRollingRestart(testContext, kafkaRoller, asList(0, 1, 2, 3, 4), KafkaRoller.FatalProblem.class, "Error while waiting for restarted pod c-kafka-1 to become ready", asList(1));
    // On the next reconciliation only pod 2 (controller) would need rolling, and we expect it to fail in the same way
    kafkaRoller = rollerWithControllers(sts, podOps, 2);
    clearRestarted();
    doFailingRollingRestart(testContext, kafkaRoller, asList(2, 3, 4), KafkaRoller.FatalProblem.class, "Error while waiting for non-restarted pod c-kafka-1 to become ready", emptyList());
}
Also used : PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) TimeoutException(io.strimzi.operator.common.operator.resource.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 93 with PodOperator

use of io.strimzi.operator.common.operator.resource.PodOperator in project strimzi-kafka-operator by strimzi.

the class CertificateRenewalTest method reconcileCa.

private Future<ArgumentCaptor<Secret>> reconcileCa(Vertx vertx, Kafka kafka, Supplier<Date> dateSupplier) {
    ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(false);
    SecretOperator secretOps = supplier.secretOperations;
    DeploymentOperator deploymentOps = supplier.deploymentOperations;
    StatefulSetOperator stsOps = supplier.stsOperations;
    PodOperator podOps = supplier.podOperations;
    when(secretOps.list(eq(NAMESPACE), any())).thenAnswer(invocation -> {
        Map<String, String> requiredLabels = ((Labels) invocation.getArgument(1)).toMap();
        return secrets.stream().filter(s -> {
            Map<String, String> labels = s.getMetadata().getLabels();
            labels.keySet().retainAll(requiredLabels.keySet());
            return labels.equals(requiredLabels);
        }).collect(Collectors.toList());
    });
    ArgumentCaptor<Secret> c = ArgumentCaptor.forClass(Secret.class);
    when(secretOps.reconcile(any(), eq(NAMESPACE), eq(AbstractModel.clusterCaCertSecretName(NAME)), c.capture())).thenAnswer(i -> Future.succeededFuture(ReconcileResult.noop(i.getArgument(0))));
    when(secretOps.reconcile(any(), eq(NAMESPACE), eq(AbstractModel.clusterCaKeySecretName(NAME)), c.capture())).thenAnswer(i -> Future.succeededFuture(ReconcileResult.noop(i.getArgument(0))));
    when(secretOps.reconcile(any(), eq(NAMESPACE), eq(KafkaResources.clientsCaCertificateSecretName(NAME)), c.capture())).thenAnswer(i -> Future.succeededFuture(ReconcileResult.noop(i.getArgument(0))));
    when(secretOps.reconcile(any(), eq(NAMESPACE), eq(KafkaResources.clientsCaKeySecretName(NAME)), c.capture())).thenAnswer(i -> Future.succeededFuture(ReconcileResult.noop(i.getArgument(0))));
    when(secretOps.reconcile(any(), eq(NAMESPACE), eq(ClusterOperator.secretName(NAME)), any())).thenAnswer(i -> Future.succeededFuture(ReconcileResult.created(i.getArgument(0))));
    when(deploymentOps.getAsync(eq(NAMESPACE), any())).thenReturn(Future.succeededFuture());
    when(stsOps.getAsync(eq(NAMESPACE), any())).thenReturn(Future.succeededFuture());
    when(podOps.listAsync(eq(NAMESPACE), any(Labels.class))).thenReturn(Future.succeededFuture(List.of()));
    KafkaAssemblyOperator op = new KafkaAssemblyOperator(vertx, new PlatformFeaturesAvailability(false, KubernetesVersion.V1_16), certManager, passwordGenerator, supplier, ResourceUtils.dummyClusterOperatorConfig(1L));
    Reconciliation reconciliation = new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, NAME);
    Promise<ArgumentCaptor<Secret>> reconcileCasComplete = Promise.promise();
    op.new ReconciliationState(reconciliation, kafka).reconcileCas(dateSupplier).onComplete(ar -> {
        // If failed then return the throwable of the reconcileCas
        if (ar.succeeded()) {
            reconcileCasComplete.complete(c);
        } else {
            reconcileCasComplete.fail(ar.cause());
        }
    });
    return reconcileCasComplete.future();
}
Also used : X509Certificate(java.security.cert.X509Certificate) CoreMatchers.is(org.hamcrest.CoreMatchers.is) BeforeEach(org.junit.jupiter.api.BeforeEach) CertificateFactory(java.security.cert.CertificateFactory) CA_KEY(io.strimzi.operator.cluster.model.Ca.CA_KEY) Date(java.util.Date) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) KeyStoreException(java.security.KeyStoreException) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ByteArrayInputStream(java.io.ByteArrayInputStream) Collections.singleton(java.util.Collections.singleton) KafkaResources(io.strimzi.api.kafka.model.KafkaResources) Ca(io.strimzi.operator.cluster.model.Ca) Map(java.util.Map) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) ResourceUtils(io.strimzi.operator.cluster.ResourceUtils) Path(java.nio.file.Path) AbstractModel(io.strimzi.operator.cluster.model.AbstractModel) StatefulSetOperator(io.strimzi.operator.cluster.operator.resource.StatefulSetOperator) ModelUtils(io.strimzi.operator.cluster.model.ModelUtils) DeploymentOperator(io.strimzi.operator.common.operator.resource.DeploymentOperator) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) KeyStore(java.security.KeyStore) VertxExtension(io.vertx.junit5.VertxExtension) Instant(java.time.Instant) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Subject(io.strimzi.certs.Subject) Test(org.junit.jupiter.api.Test) Objects(java.util.Objects) Base64(java.util.Base64) List(java.util.List) Labels(io.strimzi.operator.common.model.Labels) PasswordGenerator(io.strimzi.operator.common.PasswordGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CA_STORE_PASSWORD(io.strimzi.operator.cluster.model.Ca.CA_STORE_PASSWORD) Secret(io.fabric8.kubernetes.api.model.Secret) CA_STORE(io.strimzi.operator.cluster.model.Ca.CA_STORE) Checkpoint(io.vertx.junit5.Checkpoint) ClusterCa(io.strimzi.operator.cluster.model.ClusterCa) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) Mockito.mock(org.mockito.Mockito.mock) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Matchers.aMapWithSize(org.hamcrest.Matchers.aMapWithSize) CoreMatchers.not(org.hamcrest.CoreMatchers.not) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) CertAndKey(io.strimzi.certs.CertAndKey) Supplier(java.util.function.Supplier) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) ArrayList(java.util.ArrayList) ArgumentCaptor(org.mockito.ArgumentCaptor) ClusterOperator(io.strimzi.operator.cluster.ClusterOperator) TestUtils(io.strimzi.test.TestUtils) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ReconcileResult(io.strimzi.operator.common.operator.resource.ReconcileResult) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) CertificateExpirationPolicy(io.strimzi.api.kafka.model.CertificateExpirationPolicy) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) CertificateAuthority(io.strimzi.api.kafka.model.CertificateAuthority) Files(java.nio.file.Files) InvalidResourceException(io.strimzi.operator.cluster.model.InvalidResourceException) Promise(io.vertx.core.Promise) KubernetesVersion(io.strimzi.operator.KubernetesVersion) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TestUtils.set(io.strimzi.test.TestUtils.set) Mockito.when(org.mockito.Mockito.when) Reconciliation(io.strimzi.operator.common.Reconciliation) CA_CRT(io.strimzi.operator.cluster.model.Ca.CA_CRT) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) Kafka(io.strimzi.api.kafka.model.Kafka) OpenSslCertManager(io.strimzi.certs.OpenSslCertManager) CertificateAuthorityBuilder(io.strimzi.api.kafka.model.CertificateAuthorityBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ArgumentCaptor(org.mockito.ArgumentCaptor) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) 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) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) Reconciliation(io.strimzi.operator.common.Reconciliation) DeploymentOperator(io.strimzi.operator.common.operator.resource.DeploymentOperator) Map(java.util.Map)

Example 94 with PodOperator

use of io.strimzi.operator.common.operator.resource.PodOperator in project strimzi by strimzi.

the class KafkaAssemblyOperatorNonParametrizedTest method testCustomLabelsAndAnnotations.

@Test
public void testCustomLabelsAndAnnotations(VertxTestContext context) {
    Map<String, String> labels = new HashMap<>(2);
    labels.put("label1", "value1");
    labels.put("label2", "value2");
    Map<String, String> annos = new HashMap<>(2);
    annos.put("anno1", "value3");
    annos.put("anno2", "value4");
    Kafka kafka = new KafkaBuilder().withNewMetadata().withName(NAME).withNamespace(NAMESPACE).endMetadata().withNewSpec().withNewKafka().withReplicas(3).withNewEphemeralStorage().endEphemeralStorage().withNewTemplate().withNewClusterCaCert().withNewMetadata().withAnnotations(annos).withLabels(labels).endMetadata().endClusterCaCert().endTemplate().endKafka().withNewZookeeper().withReplicas(3).withNewEphemeralStorage().endEphemeralStorage().endZookeeper().endSpec().build();
    ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(false);
    SecretOperator secretOps = supplier.secretOperations;
    PodOperator podOps = supplier.podOperations;
    ArgumentCaptor<Secret> clusterCaCert = ArgumentCaptor.forClass(Secret.class);
    ArgumentCaptor<Secret> clusterCaKey = ArgumentCaptor.forClass(Secret.class);
    ArgumentCaptor<Secret> clientsCaCert = ArgumentCaptor.forClass(Secret.class);
    ArgumentCaptor<Secret> clientsCaKey = ArgumentCaptor.forClass(Secret.class);
    when(secretOps.reconcile(any(), eq(NAMESPACE), eq(AbstractModel.clusterCaCertSecretName(NAME)), clusterCaCert.capture())).thenAnswer(i -> Future.succeededFuture(ReconcileResult.created(i.getArgument(0))));
    when(secretOps.reconcile(any(), eq(NAMESPACE), eq(AbstractModel.clusterCaKeySecretName(NAME)), clusterCaKey.capture())).thenAnswer(i -> Future.succeededFuture(ReconcileResult.created(i.getArgument(0))));
    when(secretOps.reconcile(any(), eq(NAMESPACE), eq(KafkaResources.clientsCaCertificateSecretName(NAME)), clientsCaCert.capture())).thenAnswer(i -> Future.succeededFuture(ReconcileResult.created(i.getArgument(0))));
    when(secretOps.reconcile(any(), eq(NAMESPACE), eq(KafkaResources.clientsCaKeySecretName(NAME)), clientsCaKey.capture())).thenAnswer(i -> Future.succeededFuture(ReconcileResult.created(i.getArgument(0))));
    when(secretOps.reconcile(any(), eq(NAMESPACE), eq(ClusterOperator.secretName(NAME)), any())).thenAnswer(i -> Future.succeededFuture(ReconcileResult.created(i.getArgument(0))));
    when(podOps.listAsync(eq(NAMESPACE), any(Labels.class))).thenReturn(Future.succeededFuture(List.of()));
    KafkaAssemblyOperator op = new KafkaAssemblyOperator(vertx, new PlatformFeaturesAvailability(false, KubernetesVersion.V1_16), certManager, passwordGenerator, supplier, ResourceUtils.dummyClusterOperatorConfig(1L));
    Reconciliation reconciliation = new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, NAME);
    Checkpoint async = context.checkpoint();
    op.new ReconciliationState(reconciliation, kafka).reconcileCas(() -> new Date()).onComplete(context.succeeding(c -> context.verify(() -> {
        assertThat(clusterCaCert.getAllValues(), hasSize(1));
        assertThat(clusterCaKey.getAllValues(), hasSize(1));
        assertThat(clientsCaCert.getAllValues(), hasSize(1));
        assertThat(clientsCaKey.getAllValues(), hasSize(1));
        Secret clusterCaCertSecret = clusterCaCert.getValue();
        Secret clusterCaKeySecret = clusterCaKey.getValue();
        Secret clientsCaCertSecret = clientsCaCert.getValue();
        Secret clientsCaKeySecret = clientsCaKey.getValue();
        for (Map.Entry<String, String> entry : annos.entrySet()) {
            assertThat(clusterCaCertSecret.getMetadata().getAnnotations(), hasEntry(entry.getKey(), entry.getValue()));
            assertThat(clusterCaKeySecret.getMetadata().getAnnotations(), not(hasEntry(entry.getKey(), entry.getValue())));
            assertThat(clientsCaCertSecret.getMetadata().getAnnotations(), not(hasEntry(entry.getKey(), entry.getValue())));
            assertThat(clientsCaKeySecret.getMetadata().getAnnotations(), not(hasEntry(entry.getKey(), entry.getValue())));
        }
        for (Map.Entry<String, String> entry : labels.entrySet()) {
            assertThat(clusterCaCertSecret.getMetadata().getLabels(), hasEntry(entry.getKey(), entry.getValue()));
            assertThat(clusterCaKeySecret.getMetadata().getLabels(), not(hasEntry(entry.getKey(), entry.getValue())));
            assertThat(clientsCaCertSecret.getMetadata().getLabels(), not(hasEntry(entry.getKey(), entry.getValue())));
            assertThat(clientsCaKeySecret.getMetadata().getLabels(), not(hasEntry(entry.getKey(), entry.getValue())));
        }
        async.flag();
    })));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Date(java.util.Date) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ClusterRoleBindingOperator(io.strimzi.operator.common.operator.resource.ClusterRoleBindingOperator) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) AfterAll(org.junit.jupiter.api.AfterAll) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Collections.singleton(java.util.Collections.singleton) KafkaResources(io.strimzi.api.kafka.model.KafkaResources) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) ResourceUtils(io.strimzi.operator.cluster.ResourceUtils) AbstractModel(io.strimzi.operator.cluster.model.AbstractModel) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) List(java.util.List) Labels(io.strimzi.operator.common.model.Labels) PasswordGenerator(io.strimzi.operator.common.PasswordGenerator) Secret(io.fabric8.kubernetes.api.model.Secret) Checkpoint(io.vertx.junit5.Checkpoint) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) ClusterOperatorConfig(io.strimzi.operator.cluster.ClusterOperatorConfig) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ClusterRoleBinding(io.fabric8.kubernetes.api.model.rbac.ClusterRoleBinding) CoreMatchers.not(org.hamcrest.CoreMatchers.not) HashMap(java.util.HashMap) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) ArgumentCaptor(org.mockito.ArgumentCaptor) ClusterOperator(io.strimzi.operator.cluster.ClusterOperator) KafkaVersionTestUtils(io.strimzi.operator.cluster.KafkaVersionTestUtils) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ReconcileResult(io.strimzi.operator.common.operator.resource.ReconcileResult) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) CertificateAuthority(io.strimzi.api.kafka.model.CertificateAuthority) OwnerReferenceBuilder(io.fabric8.kubernetes.api.model.OwnerReferenceBuilder) KubernetesVersion(io.strimzi.operator.KubernetesVersion) Vertx(io.vertx.core.Vertx) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Reconciliation(io.strimzi.operator.common.Reconciliation) Mockito(org.mockito.Mockito) Kafka(io.strimzi.api.kafka.model.Kafka) OpenSslCertManager(io.strimzi.certs.OpenSslCertManager) HashMap(java.util.HashMap) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) Kafka(io.strimzi.api.kafka.model.Kafka) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) Labels(io.strimzi.operator.common.model.Labels) Date(java.util.Date) 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) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) Reconciliation(io.strimzi.operator.common.Reconciliation) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 95 with PodOperator

use of io.strimzi.operator.common.operator.resource.PodOperator in project strimzi by strimzi.

the class KafkaAssemblyOperatorPodSetTest method testScaleUp.

/**
 * Tests reconciliation with scale-up from 1 to 3 ZooKeeper pods
 *
 * @param context   Test context
 */
@Test
public void testScaleUp(VertxTestContext context) {
    Kafka oldKafka = new KafkaBuilder(KAFKA).editSpec().editZookeeper().withReplicas(1).endZookeeper().editKafka().withReplicas(1).endKafka().endSpec().build();
    ZookeeperCluster oldZkCluster = ZookeeperCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, oldKafka, VERSIONS);
    StrimziPodSet oldZkPodSet = oldZkCluster.generatePodSet(oldKafka.getSpec().getZookeeper().getReplicas(), false, null, null, null);
    KafkaCluster oldKafkaCluster = KafkaCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, oldKafka, VERSIONS);
    StrimziPodSet oldKafkaPodSet = oldKafkaCluster.generatePodSet(oldKafka.getSpec().getKafka().getReplicas(), false, null, null, brokerId -> null);
    ZookeeperCluster zkCluster = ZookeeperCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, KAFKA, VERSIONS);
    KafkaCluster kafkaCluster = KafkaCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, KAFKA, VERSIONS);
    ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(false);
    SecretOperator secretOps = supplier.secretOperations;
    when(secretOps.reconcile(any(), any(), any(), any())).thenReturn(Future.succeededFuture());
    when(secretOps.getAsync(any(), any())).thenReturn(Future.succeededFuture(new Secret()));
    ConfigMapOperator mockCmOps = supplier.configMapOperations;
    when(mockCmOps.listAsync(any(), eq(oldKafkaCluster.getSelectorLabels()))).thenReturn(Future.succeededFuture(oldKafkaCluster.generatePerBrokerConfigurationConfigMaps(new MetricsAndLogging(null, null), ADVERTISED_HOSTNAMES, ADVERTISED_PORTS, true)));
    ArgumentCaptor<String> cmReconciliationCaptor = ArgumentCaptor.forClass(String.class);
    when(mockCmOps.reconcile(any(), any(), cmReconciliationCaptor.capture(), any())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> cmDeletionCaptor = ArgumentCaptor.forClass(String.class);
    when(mockCmOps.deleteAsync(any(), any(), cmDeletionCaptor.capture(), anyBoolean())).thenReturn(Future.succeededFuture());
    StrimziPodSetOperator mockPodSetOps = supplier.strimziPodSetOperator;
    // Kafka
    when(mockPodSetOps.getAsync(any(), eq(zkCluster.getName()))).thenReturn(Future.succeededFuture(oldZkPodSet));
    ArgumentCaptor<StrimziPodSet> zkPodSetCaptor = ArgumentCaptor.forClass(StrimziPodSet.class);
    when(mockPodSetOps.reconcile(any(), any(), eq(zkCluster.getName()), zkPodSetCaptor.capture())).thenAnswer(i -> Future.succeededFuture(ReconcileResult.noop(i.getArgument(3))));
    // Zoo
    when(mockPodSetOps.getAsync(any(), eq(kafkaCluster.getName()))).thenReturn(Future.succeededFuture(oldKafkaPodSet));
    ArgumentCaptor<StrimziPodSet> kafkaPodSetCaptor = ArgumentCaptor.forClass(StrimziPodSet.class);
    when(mockPodSetOps.reconcile(any(), any(), eq(kafkaCluster.getName()), kafkaPodSetCaptor.capture())).thenAnswer(i -> Future.succeededFuture(ReconcileResult.noop(i.getArgument(3))));
    StatefulSetOperator mockStsOps = supplier.stsOperations;
    // Zoo STS is queried and deleted if it still exists
    when(mockStsOps.getAsync(any(), eq(zkCluster.getName()))).thenReturn(Future.succeededFuture(null));
    // Kafka STS is queried and deleted if it still exists
    when(mockStsOps.getAsync(any(), eq(kafkaCluster.getName()))).thenReturn(Future.succeededFuture(null));
    PodOperator mockPodOps = supplier.podOperations;
    when(mockPodOps.listAsync(any(), eq(zkCluster.getSelectorLabels()))).thenReturn(Future.succeededFuture(Collections.emptyList()));
    when(mockPodOps.listAsync(any(), eq(kafkaCluster.getSelectorLabels()))).thenReturn(Future.succeededFuture(Collections.emptyList()));
    when(mockPodOps.listAsync(any(), any(Labels.class))).thenReturn(Future.succeededFuture(Collections.emptyList()));
    when(mockPodOps.readiness(any(), any(), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    CrdOperator<KubernetesClient, Kafka, KafkaList> mockKafkaOps = supplier.kafkaOperator;
    when(mockKafkaOps.getAsync(eq(NAMESPACE), eq(CLUSTER_NAME))).thenReturn(Future.succeededFuture(KAFKA));
    when(mockKafkaOps.get(eq(NAMESPACE), eq(CLUSTER_NAME))).thenReturn(KAFKA);
    when(mockKafkaOps.updateStatusAsync(any(), any())).thenReturn(Future.succeededFuture());
    ClusterOperatorConfig config = ResourceUtils.dummyClusterOperatorConfig(VERSIONS, ClusterOperatorConfig.DEFAULT_OPERATION_TIMEOUT_MS, "+UseStrimziPodSets");
    MockZooKeeperReconciler zr = new MockZooKeeperReconciler(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, CLUSTER_NAME), vertx, config, supplier, new PlatformFeaturesAvailability(false, KUBERNETES_VERSION), KAFKA, VERSION_CHANGE, null, 1, CLUSTER_CA);
    MockKafkaReconciler kr = new MockKafkaReconciler(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, CLUSTER_NAME), vertx, config, supplier, new PlatformFeaturesAvailability(false, KUBERNETES_VERSION), KAFKA, VERSION_CHANGE, null, 1, CLUSTER_CA, CLIENTS_CA);
    MockKafkaAssemblyOperator kao = new MockKafkaAssemblyOperator(vertx, new PlatformFeaturesAvailability(false, KUBERNETES_VERSION), CERT_MANAGER, PASSWORD_GENERATOR, supplier, config, zr, kr);
    Checkpoint async = context.checkpoint();
    kao.reconcile(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, CLUSTER_NAME)).onComplete(context.succeeding(v -> context.verify(() -> {
        // Scale-up of Zoo is done pod by pod => the reconcile method is called 3 times with 1, 2 and 3 pods.
        assertThat(zkPodSetCaptor.getAllValues().size(), is(3));
        // => first capture is from zkPodSet() with old replica count
        assertThat(zkPodSetCaptor.getAllValues().get(0).getSpec().getPods().size(), is(1));
        // => second capture is from zkScalingUp() with new replica count
        assertThat(zkPodSetCaptor.getAllValues().get(1).getSpec().getPods().size(), is(2));
        // => third capture is from zkScalingUp() with new replica count
        assertThat(zkPodSetCaptor.getAllValues().get(2).getSpec().getPods().size(), is(3));
        // Still one maybe-roll invocation
        assertThat(zr.maybeRollZooKeeperInvocations, is(1));
        // Scale-up of Kafka is done in one go => we should see two invocations (first from regular patching and second from scale-up)
        assertThat(kafkaPodSetCaptor.getAllValues().size(), is(2));
        // => first capture is from kafkaPodSet() with old replica count
        assertThat(kafkaPodSetCaptor.getAllValues().get(0).getSpec().getPods().size(), is(1));
        // => second capture is from kafkaScaleUp() with new replica count
        assertThat(kafkaPodSetCaptor.getAllValues().get(1).getSpec().getPods().size(), is(3));
        // Still one maybe-roll invocation
        assertThat(kr.maybeRollKafkaInvocations, is(1));
        // CMs for all pods are reconciled
        assertThat(cmReconciliationCaptor.getAllValues().size(), is(3));
        assertThat(cmReconciliationCaptor.getAllValues(), is(List.of("my-cluster-kafka-0", "my-cluster-kafka-1", "my-cluster-kafka-2")));
        // Only the shared CM is deleted
        assertThat(cmDeletionCaptor.getAllValues().size(), is(1));
        assertThat(cmDeletionCaptor.getAllValues().get(0), is("my-cluster-kafka-config"));
        async.flag();
    })));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Storage(io.strimzi.api.kafka.model.storage.Storage) StrimziPodSetOperator(io.strimzi.operator.common.operator.resource.StrimziPodSetOperator) Date(java.util.Date) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) AfterAll(org.junit.jupiter.api.AfterAll) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) KafkaResources(io.strimzi.api.kafka.model.KafkaResources) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) ResourceUtils(io.strimzi.operator.cluster.ResourceUtils) AbstractModel(io.strimzi.operator.cluster.model.AbstractModel) StatefulSetOperator(io.strimzi.operator.cluster.operator.resource.StatefulSetOperator) KafkaVersionChange(io.strimzi.operator.cluster.model.KafkaVersionChange) ArgumentMatchers.startsWith(org.mockito.ArgumentMatchers.startsWith) KafkaVersion(io.strimzi.operator.cluster.model.KafkaVersion) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) GenericKafkaListenerBuilder(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder) ClientsCa(io.strimzi.operator.cluster.model.ClientsCa) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) List(java.util.List) Labels(io.strimzi.operator.common.model.Labels) StrimziPodSet(io.strimzi.api.kafka.model.StrimziPodSet) PasswordGenerator(io.strimzi.operator.common.PasswordGenerator) Secret(io.fabric8.kubernetes.api.model.Secret) Checkpoint(io.vertx.junit5.Checkpoint) ClusterCa(io.strimzi.operator.cluster.model.ClusterCa) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) ClusterOperatorConfig(io.strimzi.operator.cluster.ClusterOperatorConfig) MockCertManager(io.strimzi.operator.common.operator.MockCertManager) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) KafkaList(io.strimzi.api.kafka.KafkaList) KafkaStatus(io.strimzi.api.kafka.model.status.KafkaStatus) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) CertManager(io.strimzi.certs.CertManager) ZookeeperCluster(io.strimzi.operator.cluster.model.ZookeeperCluster) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) Function(java.util.function.Function) Supplier(java.util.function.Supplier) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) MetricsAndLogging(io.strimzi.operator.common.MetricsAndLogging) ArgumentCaptor(org.mockito.ArgumentCaptor) KafkaCluster(io.strimzi.operator.cluster.model.KafkaCluster) KafkaVersionTestUtils(io.strimzi.operator.cluster.KafkaVersionTestUtils) ConfigMapOperator(io.strimzi.operator.common.operator.resource.ConfigMapOperator) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) ReconcileResult(io.strimzi.operator.common.operator.resource.ReconcileResult) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) KubernetesVersion(io.strimzi.operator.KubernetesVersion) Vertx(io.vertx.core.Vertx) Pod(io.fabric8.kubernetes.api.model.Pod) PodSetUtils(io.strimzi.operator.cluster.model.PodSetUtils) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Reconciliation(io.strimzi.operator.common.Reconciliation) KafkaListenerType(io.strimzi.api.kafka.model.listener.arraylistener.KafkaListenerType) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Kafka(io.strimzi.api.kafka.model.Kafka) OpenSslCertManager(io.strimzi.certs.OpenSslCertManager) Collections(java.util.Collections) StrimziPodSet(io.strimzi.api.kafka.model.StrimziPodSet) KafkaCluster(io.strimzi.operator.cluster.model.KafkaCluster) StrimziPodSetOperator(io.strimzi.operator.common.operator.resource.StrimziPodSetOperator) ClusterOperatorConfig(io.strimzi.operator.cluster.ClusterOperatorConfig) Kafka(io.strimzi.api.kafka.model.Kafka) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) Reconciliation(io.strimzi.operator.common.Reconciliation) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) MetricsAndLogging(io.strimzi.operator.common.MetricsAndLogging) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) ZookeeperCluster(io.strimzi.operator.cluster.model.ZookeeperCluster) Labels(io.strimzi.operator.common.model.Labels) StatefulSetOperator(io.strimzi.operator.cluster.operator.resource.StatefulSetOperator) KafkaList(io.strimzi.api.kafka.KafkaList) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) Secret(io.fabric8.kubernetes.api.model.Secret) Checkpoint(io.vertx.junit5.Checkpoint) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) ConfigMapOperator(io.strimzi.operator.common.operator.resource.ConfigMapOperator) Test(org.junit.jupiter.api.Test)

Aggregations

PodOperator (io.strimzi.operator.common.operator.resource.PodOperator)180 Test (org.junit.jupiter.api.Test)166 Checkpoint (io.vertx.junit5.Checkpoint)138 Reconciliation (io.strimzi.operator.common.Reconciliation)134 ResourceOperatorSupplier (io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier)114 Future (io.vertx.core.Future)114 VertxTestContext (io.vertx.junit5.VertxTestContext)112 CoreMatchers.is (org.hamcrest.CoreMatchers.is)112 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)112 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)112 Mockito.when (org.mockito.Mockito.when)112 Vertx (io.vertx.core.Vertx)104 VertxExtension (io.vertx.junit5.VertxExtension)102 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)102 List (java.util.List)100 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)100 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)98 PlatformFeaturesAvailability (io.strimzi.operator.PlatformFeaturesAvailability)98 CrdOperator (io.strimzi.operator.common.operator.resource.CrdOperator)94 KafkaVersionTestUtils (io.strimzi.operator.cluster.KafkaVersionTestUtils)92