Search in sources :

Example 31 with StatefulSetBuilder

use of io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder in project strimzi by strimzi.

the class StatefulSetOperatorTest method testInternalReplace.

@Test
public void testInternalReplace(VertxTestContext context) {
    StatefulSet sts1 = new StatefulSetBuilder().withNewMetadata().withNamespace(AbstractResourceOperatorTest.NAMESPACE).withName(AbstractResourceOperatorTest.RESOURCE_NAME).endMetadata().withNewSpec().withReplicas(3).withNewTemplate().withNewMetadata().endMetadata().endTemplate().endSpec().build();
    Map<String, Quantity> requests = new HashMap<>();
    requests.put("storage", new Quantity("100Gi"));
    PersistentVolumeClaim pvc = new PersistentVolumeClaimBuilder().withNewMetadata().withName("data").endMetadata().withNewSpec().withAccessModes("ReadWriteOnce").withNewResources().withRequests(requests).endResources().withStorageClassName("gp2").endSpec().build();
    StatefulSet sts2 = new StatefulSetBuilder().withNewMetadata().withNamespace(AbstractResourceOperatorTest.NAMESPACE).withName(AbstractResourceOperatorTest.RESOURCE_NAME).endMetadata().withNewSpec().withReplicas(3).withNewTemplate().withNewMetadata().endMetadata().endTemplate().withVolumeClaimTemplates(pvc).endSpec().build();
    Deletable mockDeletable = mock(Deletable.class);
    when(mockDeletable.delete()).thenReturn(Boolean.TRUE);
    Resource mockERPD = mock(resourceType());
    when(mockERPD.withPropagationPolicy(any(DeletionPropagation.class))).thenReturn(mockDeletable);
    when(mockERPD.withGracePeriod(anyLong())).thenReturn(mockDeletable);
    Resource mockResource = mock(resourceType());
    when(mockResource.get()).thenReturn(sts1);
    when(mockResource.withPropagationPolicy(eq(DeletionPropagation.ORPHAN))).thenReturn(mockERPD);
    when(mockResource.create(any(StatefulSet.class))).thenReturn(sts1);
    PodOperator podOperator = mock(PodOperator.class);
    when(podOperator.waitFor(any(), anyString(), anyString(), anyLong(), anyLong(), any(BiPredicate.class))).thenReturn(Future.succeededFuture());
    when(podOperator.readiness(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    when(podOperator.reconcile(any(), anyString(), anyString(), any())).thenReturn(Future.succeededFuture());
    when(podOperator.getAsync(anyString(), anyString())).thenReturn(Future.succeededFuture(new PodBuilder().withNewMetadata().withName("my-pod-0").endMetadata().build()));
    PvcOperator pvcOperator = mock(PvcOperator.class);
    when(pvcOperator.reconcile(any(), anyString(), anyString(), any())).thenReturn(Future.succeededFuture());
    NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class);
    when(mockNameable.withName(matches(RESOURCE_NAME))).thenReturn(mockResource);
    MixedOperation mockCms = mock(MixedOperation.class);
    when(mockCms.inNamespace(matches(NAMESPACE))).thenReturn(mockNameable);
    KubernetesClient mockClient = mock(KubernetesClient.class);
    mocker(mockClient, mockCms);
    StatefulSetOperator op = new StatefulSetOperator(AbstractResourceOperatorTest.vertx, mockClient, 5_000L, podOperator, pvcOperator) {

        @Override
        protected boolean shouldIncrementGeneration(Reconciliation reconciliation, StatefulSetDiff diff) {
            return true;
        }

        @Override
        public Future<Void> waitFor(Reconciliation reconciliation, String namespace, String name, String logState, long pollIntervalMs, final long timeoutMs, BiPredicate<String, String> predicate) {
            return Future.succeededFuture();
        }
    };
    Checkpoint async = context.checkpoint();
    op.reconcile(new Reconciliation("test", "kind", "namespace", "name"), sts1.getMetadata().getNamespace(), sts1.getMetadata().getName(), sts2).onComplete(context.succeeding(rrState -> {
        verify(mockDeletable).delete();
        async.flag();
    }));
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) AbstractScalableResourceOperator(io.strimzi.operator.common.operator.resource.AbstractScalableResourceOperator) VertxTestContext(io.vertx.junit5.VertxTestContext) CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DeletionPropagation(io.fabric8.kubernetes.api.model.DeletionPropagation) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ScalableResourceOperatorTest(io.strimzi.operator.common.operator.resource.ScalableResourceOperatorTest) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Watcher(io.fabric8.kubernetes.client.Watcher) Watch(io.fabric8.kubernetes.client.Watch) HashMap(java.util.HashMap) MockitoException(org.mockito.exceptions.base.MockitoException) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) BiPredicate(java.util.function.BiPredicate) Resource(io.fabric8.kubernetes.client.dsl.Resource) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) ArgumentCaptor(org.mockito.ArgumentCaptor) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) StatefulSetList(io.fabric8.kubernetes.api.model.apps.StatefulSetList) Map(java.util.Map) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Deletable(io.fabric8.kubernetes.client.dsl.Deletable) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) AbstractResourceOperatorTest(io.strimzi.operator.common.operator.resource.AbstractResourceOperatorTest) Vertx(io.vertx.core.Vertx) Mockito.when(org.mockito.Mockito.when) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) PvcOperator(io.strimzi.operator.common.operator.resource.PvcOperator) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) Reconciliation(io.strimzi.operator.common.Reconciliation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) ArgumentMatchers.matches(org.mockito.ArgumentMatchers.matches) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) Checkpoint(io.vertx.junit5.Checkpoint) AppsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.AppsAPIGroupDSL) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HashMap(java.util.HashMap) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Resource(io.fabric8.kubernetes.client.dsl.Resource) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) Quantity(io.fabric8.kubernetes.api.model.Quantity) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) DeletionPropagation(io.fabric8.kubernetes.api.model.DeletionPropagation) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PvcOperator(io.strimzi.operator.common.operator.resource.PvcOperator) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) Checkpoint(io.vertx.junit5.Checkpoint) Reconciliation(io.strimzi.operator.common.Reconciliation) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) Deletable(io.fabric8.kubernetes.client.dsl.Deletable) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) BiPredicate(java.util.function.BiPredicate) ScalableResourceOperatorTest(io.strimzi.operator.common.operator.resource.ScalableResourceOperatorTest) AbstractResourceOperatorTest(io.strimzi.operator.common.operator.resource.AbstractResourceOperatorTest) Test(org.junit.jupiter.api.Test)

Example 32 with StatefulSetBuilder

use of io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder in project strimzi by strimzi.

the class KafkaUpgradeDowngradeMockTest method testDowngradeRecoveryWithMessageAndProtocolVersions.

// Test partial downgrade => emulate previous downgrade failing in the middle and verify it is finished.
@Test
public void testDowngradeRecoveryWithMessageAndProtocolVersions(VertxTestContext context) {
    Kafka initialKafka = kafkaWithVersions(KafkaVersionTestUtils.LATEST_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_FORMAT_VERSION, KafkaVersionTestUtils.PREVIOUS_PROTOCOL_VERSION);
    Kafka updatedKafka = kafkaWithVersions(KafkaVersionTestUtils.PREVIOUS_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_FORMAT_VERSION, KafkaVersionTestUtils.PREVIOUS_PROTOCOL_VERSION);
    Checkpoint reconciliation = context.checkpoint();
    initialize(context, initialKafka).onComplete(context.succeeding(v -> {
        context.verify(() -> {
            assertVersionsInStatefulSet(KafkaVersionTestUtils.LATEST_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_FORMAT_VERSION, KafkaVersionTestUtils.PREVIOUS_PROTOCOL_VERSION, KafkaVersionTestUtils.LATEST_KAFKA_IMAGE);
        });
    })).compose(v -> {
        StatefulSet sts = client.apps().statefulSets().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka").get();
        StatefulSet modifiedSts = new StatefulSetBuilder(sts).editMetadata().addToAnnotations(KafkaCluster.ANNO_STRIMZI_IO_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_KAFKA_VERSION).endMetadata().editSpec().editTemplate().editMetadata().addToAnnotations(KafkaCluster.ANNO_STRIMZI_IO_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_KAFKA_VERSION).addToAnnotations(StatefulSetOperator.ANNO_STRIMZI_IO_GENERATION, "1").endMetadata().editSpec().editContainer(0).withImage(KafkaVersionTestUtils.PREVIOUS_KAFKA_IMAGE).endContainer().endSpec().endTemplate().endSpec().build();
        client.apps().statefulSets().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka").createOrReplace(modifiedSts);
        Pod pod = client.pods().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka-" + 1).get();
        Pod modifiedPod = new PodBuilder(pod).editMetadata().addToAnnotations(KafkaCluster.ANNO_STRIMZI_IO_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_KAFKA_VERSION).addToAnnotations(StatefulSetOperator.ANNO_STRIMZI_IO_GENERATION, "1").endMetadata().editSpec().editContainer(0).withImage(KafkaVersionTestUtils.PREVIOUS_KAFKA_IMAGE).endContainer().endSpec().build();
        client.pods().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka-" + 1).createOrReplace(modifiedPod);
        return Future.succeededFuture();
    }).compose(v -> operator.createOrUpdate(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, CLUSTER_NAME), updatedKafka)).onComplete(context.succeeding(v -> context.verify(() -> {
        assertVersionsInStatefulSet(KafkaVersionTestUtils.PREVIOUS_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_FORMAT_VERSION, KafkaVersionTestUtils.PREVIOUS_PROTOCOL_VERSION, KafkaVersionTestUtils.PREVIOUS_KAFKA_IMAGE);
        reconciliation.flag();
    })));
}
Also used : StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) VertxTestContext(io.vertx.junit5.VertxTestContext) CoreMatchers.is(org.hamcrest.CoreMatchers.is) KafkaList(io.strimzi.api.kafka.KafkaList) Crds(io.strimzi.api.kafka.Crds) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) FeatureGates(io.strimzi.operator.cluster.FeatureGates) MockKube(io.strimzi.test.mockkube.MockKube) AfterAll(org.junit.jupiter.api.AfterAll) KafkaCluster(io.strimzi.operator.cluster.model.KafkaCluster) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) KafkaVersionTestUtils(io.strimzi.operator.cluster.KafkaVersionTestUtils) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) ResourceUtils(io.strimzi.operator.cluster.ResourceUtils) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) StatefulSetOperator(io.strimzi.operator.cluster.operator.resource.StatefulSetOperator) StrimziPodSetList(io.strimzi.api.kafka.StrimziPodSetList) KubernetesVersion(io.strimzi.operator.KubernetesVersion) KafkaVersion(io.strimzi.operator.cluster.model.KafkaVersion) Vertx(io.vertx.core.Vertx) Pod(io.fabric8.kubernetes.api.model.Pod) GenericKafkaListenerBuilder(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder) VertxExtension(io.vertx.junit5.VertxExtension) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) Reconciliation(io.strimzi.operator.common.Reconciliation) KafkaConfiguration(io.strimzi.operator.cluster.model.KafkaConfiguration) Logger(org.apache.logging.log4j.Logger) StrimziPodSet(io.strimzi.api.kafka.model.StrimziPodSet) KafkaListenerType(io.strimzi.api.kafka.model.listener.arraylistener.KafkaListenerType) PasswordGenerator(io.strimzi.operator.common.PasswordGenerator) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Checkpoint(io.vertx.junit5.Checkpoint) Matchers.stringContainsInOrder(org.hamcrest.Matchers.stringContainsInOrder) Kafka(io.strimzi.api.kafka.model.Kafka) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ClusterOperatorConfig(io.strimzi.operator.cluster.ClusterOperatorConfig) MockCertManager(io.strimzi.operator.common.operator.MockCertManager) Checkpoint(io.vertx.junit5.Checkpoint) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Reconciliation(io.strimzi.operator.common.Reconciliation) Kafka(io.strimzi.api.kafka.model.Kafka) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) Test(org.junit.jupiter.api.Test)

Example 33 with StatefulSetBuilder

use of io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder in project strimzi by strimzi.

the class KafkaUpgradeDowngradeMockTest method testUpgradeFromUnsupportedKafkaVersionWithMessageAndProtocol.

// Tests upgrade from Kafka version not supported by the current version of the operator with message format and
// protocol versions specified.
@Test
public void testUpgradeFromUnsupportedKafkaVersionWithMessageAndProtocol(VertxTestContext context) {
    KafkaVersion unsupported = VERSIONS.version("2.1.0");
    Kafka initialKafka = kafkaWithVersions(KafkaVersionTestUtils.LATEST_KAFKA_VERSION, unsupported.messageVersion(), unsupported.protocolVersion());
    Kafka updatedKafka = kafkaWithVersions(KafkaVersionTestUtils.LATEST_KAFKA_VERSION, unsupported.messageVersion(), unsupported.protocolVersion());
    Checkpoint reconciliation = context.checkpoint();
    initialize(context, initialKafka).onComplete(context.succeeding(v -> {
        context.verify(() -> {
            assertVersionsInStatefulSet(KafkaVersionTestUtils.LATEST_KAFKA_VERSION, unsupported.messageVersion(), unsupported.protocolVersion(), KafkaVersionTestUtils.LATEST_KAFKA_IMAGE);
        });
    })).compose(v -> {
        StatefulSet sts = client.apps().statefulSets().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka").get();
        StatefulSet modifiedSts = new StatefulSetBuilder(sts).editMetadata().addToAnnotations(KafkaCluster.ANNO_STRIMZI_IO_KAFKA_VERSION, unsupported.version()).endMetadata().editSpec().editTemplate().editMetadata().removeFromAnnotations(KafkaCluster.ANNO_STRIMZI_IO_KAFKA_VERSION).removeFromAnnotations(KafkaCluster.ANNO_STRIMZI_IO_LOG_MESSAGE_FORMAT_VERSION).removeFromAnnotations(KafkaCluster.ANNO_STRIMZI_IO_INTER_BROKER_PROTOCOL_VERSION).endMetadata().editSpec().editContainer(0).withImage("strimzi/kafka:old-kafka-2.1.0").endContainer().endSpec().endTemplate().endSpec().build();
        client.apps().statefulSets().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka").createOrReplace(modifiedSts);
        for (int i = 0; i < 3; i++) {
            Pod pod = client.pods().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka-" + i).get();
            Pod modifiedPod = new PodBuilder(pod).editMetadata().removeFromAnnotations(KafkaCluster.ANNO_STRIMZI_IO_KAFKA_VERSION).removeFromAnnotations(KafkaCluster.ANNO_STRIMZI_IO_LOG_MESSAGE_FORMAT_VERSION).removeFromAnnotations(KafkaCluster.ANNO_STRIMZI_IO_INTER_BROKER_PROTOCOL_VERSION).endMetadata().editSpec().editContainer(0).withImage("strimzi/kafka:old-kafka-2.1.0").endContainer().endSpec().build();
            client.pods().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka-" + i).createOrReplace(modifiedPod);
        }
        return Future.succeededFuture();
    }).compose(v -> operator.createOrUpdate(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, CLUSTER_NAME), updatedKafka)).onComplete(context.succeeding(v -> context.verify(() -> {
        assertVersionsInStatefulSet(KafkaVersionTestUtils.LATEST_KAFKA_VERSION, unsupported.messageVersion(), unsupported.protocolVersion(), KafkaVersionTestUtils.LATEST_KAFKA_IMAGE);
        reconciliation.flag();
    })));
}
Also used : StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) VertxTestContext(io.vertx.junit5.VertxTestContext) CoreMatchers.is(org.hamcrest.CoreMatchers.is) KafkaList(io.strimzi.api.kafka.KafkaList) Crds(io.strimzi.api.kafka.Crds) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) FeatureGates(io.strimzi.operator.cluster.FeatureGates) MockKube(io.strimzi.test.mockkube.MockKube) AfterAll(org.junit.jupiter.api.AfterAll) KafkaCluster(io.strimzi.operator.cluster.model.KafkaCluster) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) KafkaVersionTestUtils(io.strimzi.operator.cluster.KafkaVersionTestUtils) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) ResourceUtils(io.strimzi.operator.cluster.ResourceUtils) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) StatefulSetOperator(io.strimzi.operator.cluster.operator.resource.StatefulSetOperator) StrimziPodSetList(io.strimzi.api.kafka.StrimziPodSetList) KubernetesVersion(io.strimzi.operator.KubernetesVersion) KafkaVersion(io.strimzi.operator.cluster.model.KafkaVersion) Vertx(io.vertx.core.Vertx) Pod(io.fabric8.kubernetes.api.model.Pod) GenericKafkaListenerBuilder(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder) VertxExtension(io.vertx.junit5.VertxExtension) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) Reconciliation(io.strimzi.operator.common.Reconciliation) KafkaConfiguration(io.strimzi.operator.cluster.model.KafkaConfiguration) Logger(org.apache.logging.log4j.Logger) StrimziPodSet(io.strimzi.api.kafka.model.StrimziPodSet) KafkaListenerType(io.strimzi.api.kafka.model.listener.arraylistener.KafkaListenerType) PasswordGenerator(io.strimzi.operator.common.PasswordGenerator) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Checkpoint(io.vertx.junit5.Checkpoint) Matchers.stringContainsInOrder(org.hamcrest.Matchers.stringContainsInOrder) Kafka(io.strimzi.api.kafka.model.Kafka) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ClusterOperatorConfig(io.strimzi.operator.cluster.ClusterOperatorConfig) MockCertManager(io.strimzi.operator.common.operator.MockCertManager) KafkaVersion(io.strimzi.operator.cluster.model.KafkaVersion) Checkpoint(io.vertx.junit5.Checkpoint) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Reconciliation(io.strimzi.operator.common.Reconciliation) Kafka(io.strimzi.api.kafka.model.Kafka) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) Checkpoint(io.vertx.junit5.Checkpoint) Test(org.junit.jupiter.api.Test)

Example 34 with StatefulSetBuilder

use of io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder in project strimzi by strimzi.

the class KafkaUpgradeDowngradeMockTest method testDowngradeWithNoMessageAndProtocolVersionsOnPodsFails.

// Test downgrade with message and protocol versions defined to correct version in the CR, but not on the broker pods.
@Test
public void testDowngradeWithNoMessageAndProtocolVersionsOnPodsFails(VertxTestContext context) {
    Kafka initialKafka = kafkaWithVersions(KafkaVersionTestUtils.LATEST_KAFKA_VERSION);
    Kafka updatedKafka = kafkaWithVersions(KafkaVersionTestUtils.PREVIOUS_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_FORMAT_VERSION, KafkaVersionTestUtils.PREVIOUS_PROTOCOL_VERSION);
    Checkpoint reconciliation = context.checkpoint();
    initialize(context, initialKafka).onComplete(context.succeeding(v -> {
        context.verify(() -> {
            assertVersionsInStatefulSet(KafkaVersionTestUtils.LATEST_KAFKA_VERSION, KafkaVersionTestUtils.LATEST_FORMAT_VERSION, KafkaVersionTestUtils.LATEST_PROTOCOL_VERSION, KafkaVersionTestUtils.LATEST_KAFKA_IMAGE);
        });
    })).compose(v -> {
        StatefulSet sts = client.apps().statefulSets().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka").get();
        StatefulSet modifiedSts = new StatefulSetBuilder(sts).editSpec().editTemplate().editMetadata().removeFromAnnotations(KafkaCluster.ANNO_STRIMZI_IO_LOG_MESSAGE_FORMAT_VERSION).removeFromAnnotations(KafkaCluster.ANNO_STRIMZI_IO_INTER_BROKER_PROTOCOL_VERSION).endMetadata().endTemplate().endSpec().build();
        client.apps().statefulSets().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka").createOrReplace(modifiedSts);
        for (int i = 0; i < 3; i++) {
            Pod pod = client.pods().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka-" + i).get();
            Pod modifiedPod = new PodBuilder(pod).editMetadata().removeFromAnnotations(KafkaCluster.ANNO_STRIMZI_IO_LOG_MESSAGE_FORMAT_VERSION).removeFromAnnotations(KafkaCluster.ANNO_STRIMZI_IO_INTER_BROKER_PROTOCOL_VERSION).endMetadata().build();
            client.pods().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka-" + i).createOrReplace(modifiedPod);
        }
        return Future.succeededFuture();
    }).compose(v -> operator.createOrUpdate(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, CLUSTER_NAME), updatedKafka)).onComplete(context.failing(v -> context.verify(() -> {
        assertThat(v.getMessage(), stringContainsInOrder("log.message.format.version (null) and inter.broker.protocol.version (null) used by the brokers have to be set and be lower or equal to the Kafka broker version we downgrade to"));
        reconciliation.flag();
    })));
}
Also used : StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) VertxTestContext(io.vertx.junit5.VertxTestContext) CoreMatchers.is(org.hamcrest.CoreMatchers.is) KafkaList(io.strimzi.api.kafka.KafkaList) Crds(io.strimzi.api.kafka.Crds) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) FeatureGates(io.strimzi.operator.cluster.FeatureGates) MockKube(io.strimzi.test.mockkube.MockKube) AfterAll(org.junit.jupiter.api.AfterAll) KafkaCluster(io.strimzi.operator.cluster.model.KafkaCluster) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) KafkaVersionTestUtils(io.strimzi.operator.cluster.KafkaVersionTestUtils) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) ResourceUtils(io.strimzi.operator.cluster.ResourceUtils) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) StatefulSetOperator(io.strimzi.operator.cluster.operator.resource.StatefulSetOperator) StrimziPodSetList(io.strimzi.api.kafka.StrimziPodSetList) KubernetesVersion(io.strimzi.operator.KubernetesVersion) KafkaVersion(io.strimzi.operator.cluster.model.KafkaVersion) Vertx(io.vertx.core.Vertx) Pod(io.fabric8.kubernetes.api.model.Pod) GenericKafkaListenerBuilder(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder) VertxExtension(io.vertx.junit5.VertxExtension) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) Reconciliation(io.strimzi.operator.common.Reconciliation) KafkaConfiguration(io.strimzi.operator.cluster.model.KafkaConfiguration) Logger(org.apache.logging.log4j.Logger) StrimziPodSet(io.strimzi.api.kafka.model.StrimziPodSet) KafkaListenerType(io.strimzi.api.kafka.model.listener.arraylistener.KafkaListenerType) PasswordGenerator(io.strimzi.operator.common.PasswordGenerator) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Checkpoint(io.vertx.junit5.Checkpoint) Matchers.stringContainsInOrder(org.hamcrest.Matchers.stringContainsInOrder) Kafka(io.strimzi.api.kafka.model.Kafka) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ClusterOperatorConfig(io.strimzi.operator.cluster.ClusterOperatorConfig) MockCertManager(io.strimzi.operator.common.operator.MockCertManager) Checkpoint(io.vertx.junit5.Checkpoint) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Reconciliation(io.strimzi.operator.common.Reconciliation) Kafka(io.strimzi.api.kafka.model.Kafka) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) Checkpoint(io.vertx.junit5.Checkpoint) Test(org.junit.jupiter.api.Test)

Example 35 with StatefulSetBuilder

use of io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder in project strimzi by strimzi.

the class KafkaUpgradeDowngradeMockTest method testUpgradeRecoveryWithMessageAndProtocolVersions.

// Tests recovery from failed upgrade with the message format and protocol versions configured to the same Kafka
// version as we are upgrading from.
@Test
public void testUpgradeRecoveryWithMessageAndProtocolVersions(VertxTestContext context) {
    Kafka initialKafka = kafkaWithVersions(KafkaVersionTestUtils.PREVIOUS_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_FORMAT_VERSION, KafkaVersionTestUtils.PREVIOUS_PROTOCOL_VERSION);
    Kafka updatedKafka = kafkaWithVersions(KafkaVersionTestUtils.LATEST_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_FORMAT_VERSION, KafkaVersionTestUtils.PREVIOUS_PROTOCOL_VERSION);
    Checkpoint reconciliation = context.checkpoint();
    initialize(context, initialKafka).onComplete(context.succeeding(v -> {
        context.verify(() -> {
            assertVersionsInStatefulSet(KafkaVersionTestUtils.PREVIOUS_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_FORMAT_VERSION, KafkaVersionTestUtils.PREVIOUS_PROTOCOL_VERSION, KafkaVersionTestUtils.PREVIOUS_KAFKA_IMAGE);
        });
    })).compose(v -> {
        StatefulSet sts = client.apps().statefulSets().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka").get();
        StatefulSet modifiedSts = new StatefulSetBuilder(sts).editMetadata().addToAnnotations(KafkaCluster.ANNO_STRIMZI_IO_KAFKA_VERSION, KafkaVersionTestUtils.LATEST_KAFKA_VERSION).endMetadata().editSpec().editTemplate().editMetadata().addToAnnotations(KafkaCluster.ANNO_STRIMZI_IO_KAFKA_VERSION, KafkaVersionTestUtils.LATEST_KAFKA_VERSION).addToAnnotations(StatefulSetOperator.ANNO_STRIMZI_IO_GENERATION, "1").endMetadata().editSpec().editContainer(0).withImage(KafkaVersionTestUtils.LATEST_KAFKA_IMAGE).endContainer().endSpec().endTemplate().endSpec().build();
        client.apps().statefulSets().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka").createOrReplace(modifiedSts);
        Pod pod = client.pods().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka-" + 1).get();
        Pod modifiedPod = new PodBuilder(pod).editMetadata().addToAnnotations(KafkaCluster.ANNO_STRIMZI_IO_KAFKA_VERSION, KafkaVersionTestUtils.LATEST_KAFKA_VERSION).addToAnnotations(StatefulSetOperator.ANNO_STRIMZI_IO_GENERATION, "1").endMetadata().editSpec().editContainer(0).withImage(KafkaVersionTestUtils.LATEST_KAFKA_IMAGE).endContainer().endSpec().build();
        client.pods().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka-" + 1).createOrReplace(modifiedPod);
        return Future.succeededFuture();
    }).compose(v -> operator.createOrUpdate(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, CLUSTER_NAME), updatedKafka)).onComplete(context.succeeding(v -> context.verify(() -> {
        assertVersionsInStatefulSet(KafkaVersionTestUtils.LATEST_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_FORMAT_VERSION, KafkaVersionTestUtils.PREVIOUS_PROTOCOL_VERSION, KafkaVersionTestUtils.LATEST_KAFKA_IMAGE);
        reconciliation.flag();
    })));
}
Also used : StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) VertxTestContext(io.vertx.junit5.VertxTestContext) CoreMatchers.is(org.hamcrest.CoreMatchers.is) KafkaList(io.strimzi.api.kafka.KafkaList) Crds(io.strimzi.api.kafka.Crds) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) FeatureGates(io.strimzi.operator.cluster.FeatureGates) MockKube(io.strimzi.test.mockkube.MockKube) AfterAll(org.junit.jupiter.api.AfterAll) KafkaCluster(io.strimzi.operator.cluster.model.KafkaCluster) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) KafkaVersionTestUtils(io.strimzi.operator.cluster.KafkaVersionTestUtils) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) ResourceUtils(io.strimzi.operator.cluster.ResourceUtils) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) StatefulSetOperator(io.strimzi.operator.cluster.operator.resource.StatefulSetOperator) StrimziPodSetList(io.strimzi.api.kafka.StrimziPodSetList) KubernetesVersion(io.strimzi.operator.KubernetesVersion) KafkaVersion(io.strimzi.operator.cluster.model.KafkaVersion) Vertx(io.vertx.core.Vertx) Pod(io.fabric8.kubernetes.api.model.Pod) GenericKafkaListenerBuilder(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder) VertxExtension(io.vertx.junit5.VertxExtension) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) Reconciliation(io.strimzi.operator.common.Reconciliation) KafkaConfiguration(io.strimzi.operator.cluster.model.KafkaConfiguration) Logger(org.apache.logging.log4j.Logger) StrimziPodSet(io.strimzi.api.kafka.model.StrimziPodSet) KafkaListenerType(io.strimzi.api.kafka.model.listener.arraylistener.KafkaListenerType) PasswordGenerator(io.strimzi.operator.common.PasswordGenerator) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Checkpoint(io.vertx.junit5.Checkpoint) Matchers.stringContainsInOrder(org.hamcrest.Matchers.stringContainsInOrder) Kafka(io.strimzi.api.kafka.model.Kafka) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ClusterOperatorConfig(io.strimzi.operator.cluster.ClusterOperatorConfig) MockCertManager(io.strimzi.operator.common.operator.MockCertManager) Checkpoint(io.vertx.junit5.Checkpoint) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Reconciliation(io.strimzi.operator.common.Reconciliation) Kafka(io.strimzi.api.kafka.model.Kafka) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

StatefulSetBuilder (io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder)48 StatefulSet (io.fabric8.kubernetes.api.model.apps.StatefulSet)43 Test (org.junit.jupiter.api.Test)34 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)18 Checkpoint (io.vertx.junit5.Checkpoint)18 Pod (io.fabric8.kubernetes.api.model.Pod)16 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)16 Reconciliation (io.strimzi.operator.common.Reconciliation)16 Future (io.vertx.core.Future)16 Vertx (io.vertx.core.Vertx)16 VertxTestContext (io.vertx.junit5.VertxTestContext)16 Map (java.util.Map)16 CoreMatchers.is (org.hamcrest.CoreMatchers.is)16 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)16 StrimziPodSetList (io.strimzi.api.kafka.StrimziPodSetList)14 Kafka (io.strimzi.api.kafka.model.Kafka)14 KafkaBuilder (io.strimzi.api.kafka.model.KafkaBuilder)14 StrimziPodSet (io.strimzi.api.kafka.model.StrimziPodSet)14 GenericKafkaListenerBuilder (io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder)14 KafkaListenerType (io.strimzi.api.kafka.model.listener.arraylistener.KafkaListenerType)14