use of io.strimzi.operator.PlatformFeaturesAvailability in project strimzi-kafka-operator by strimzi.
the class KafkaAssemblyOperatorPodSetTest method testReconciliationWithRoll.
/**
* Tests the regular reconciliation of the Kafka cluster which results in some rolling updates
*
* @param context Test context
*/
@Test
public void testReconciliationWithRoll(VertxTestContext context) {
Kafka oldKafka = new KafkaBuilder(KAFKA).editSpec().editZookeeper().withImage("old-image:latest").endZookeeper().editKafka().withImage("old-image:latest").endKafka().endSpec().build();
ZookeeperCluster oldZkCluster = ZookeeperCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, oldKafka, VERSIONS);
StrimziPodSet oldZkPodSet = oldZkCluster.generatePodSet(KAFKA.getSpec().getZookeeper().getReplicas(), false, null, null, null);
KafkaCluster oldKafkaCluster = KafkaCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, oldKafka, VERSIONS);
StrimziPodSet oldKafkaPodSet = oldKafkaCluster.generatePodSet(KAFKA.getSpec().getKafka().getReplicas(), false, null, null, null);
ZookeeperCluster newZkCluster = ZookeeperCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, KAFKA, VERSIONS);
KafkaCluster newKafkaCluster = 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());
CrdOperator<KubernetesClient, StrimziPodSet, StrimziPodSetList> mockPodSetOps = supplier.strimziPodSetOperator;
when(mockPodSetOps.getAsync(any(), eq(newZkCluster.getName()))).thenReturn(Future.succeededFuture(oldZkPodSet));
when(mockPodSetOps.reconcile(any(), any(), eq(newZkCluster.getName()), any())).thenAnswer(i -> Future.succeededFuture(ReconcileResult.noop(i.getArgument(3))));
when(mockPodSetOps.getAsync(any(), eq(newKafkaCluster.getName()))).thenReturn(Future.succeededFuture(oldKafkaPodSet));
when(mockPodSetOps.reconcile(any(), any(), eq(newKafkaCluster.getName()), any())).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(newZkCluster.getName()))).thenReturn(Future.succeededFuture(null));
// Kafka STS is queried and deleted if it still exists
when(mockStsOps.getAsync(any(), eq(newKafkaCluster.getName()))).thenReturn(Future.succeededFuture(null));
PodOperator mockPodOps = supplier.podOperations;
when(mockPodOps.listAsync(any(), eq(newZkCluster.getSelectorLabels()))).thenReturn(Future.succeededFuture(Collections.emptyList()));
when(mockPodOps.listAsync(any(), eq(newKafkaCluster.getSelectorLabels()))).thenReturn(Future.succeededFuture(Collections.emptyList()));
CrdOperator 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");
MockKafkaAssemblyOperator kao = new MockKafkaAssemblyOperator(vertx, new PlatformFeaturesAvailability(false, KUBERNETES_VERSION), CERT_MANAGER, PASSWORD_GENERATOR, supplier, config);
Checkpoint async = context.checkpoint();
kao.reconcile(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, CLUSTER_NAME)).onComplete(context.succeeding(v -> context.verify(() -> {
assertThat(kao.maybeRollZooKeeperInvocations, is(1));
assertThat(kao.zooPodNeedsRestart.apply(podFromPodSet(oldZkPodSet, "my-cluster-zookeeper-0")), is(List.of("Pod has old revision")));
assertThat(kao.zooPodNeedsRestart.apply(podFromPodSet(oldZkPodSet, "my-cluster-zookeeper-1")), is(List.of("Pod has old revision")));
assertThat(kao.zooPodNeedsRestart.apply(podFromPodSet(oldZkPodSet, "my-cluster-zookeeper-2")), is(List.of("Pod has old revision")));
assertThat(kao.maybeRollKafkaInvocations, is(1));
assertThat(kao.kafkaPodNeedsRestart.apply(podFromPodSet(oldKafkaPodSet, "my-cluster-kafka-0")), is(List.of("Pod has old revision")));
assertThat(kao.kafkaPodNeedsRestart.apply(podFromPodSet(oldKafkaPodSet, "my-cluster-kafka-1")), is(List.of("Pod has old revision")));
assertThat(kao.kafkaPodNeedsRestart.apply(podFromPodSet(oldKafkaPodSet, "my-cluster-kafka-2")), is(List.of("Pod has old revision")));
async.flag();
})));
}
use of io.strimzi.operator.PlatformFeaturesAvailability in project strimzi-kafka-operator by strimzi.
the class KafkaAssemblyOperatorPodSetTest method testScaleDown.
/**
* Tests reconciliation with scale-down from 5 to 3 ZooKeeper pods
*
* @param context Test context
*/
@Test
public void testScaleDown(VertxTestContext context) {
Kafka oldKafka = new KafkaBuilder(KAFKA).editSpec().editZookeeper().withReplicas(5).endZookeeper().editKafka().withReplicas(5).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, 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()));
CrdOperator<KubernetesClient, StrimziPodSet, StrimziPodSetList> mockPodSetOps = supplier.strimziPodSetOperator;
// Zoo
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))));
// Kafka
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.readiness(any(), any(), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
when(mockPodOps.waitFor(any(), any(), any(), any(), anyLong(), anyLong(), any())).thenReturn(Future.succeededFuture());
CrdOperator 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");
MockKafkaAssemblyOperator kao = new MockKafkaAssemblyOperator(vertx, new PlatformFeaturesAvailability(false, KUBERNETES_VERSION), CERT_MANAGER, PASSWORD_GENERATOR, supplier, config);
Checkpoint async = context.checkpoint();
kao.reconcile(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, CLUSTER_NAME)).onComplete(context.succeeding(v -> context.verify(() -> {
// Scale-down 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(5));
// => second capture is from zkScalingDown() with new replica count
assertThat(zkPodSetCaptor.getAllValues().get(1).getSpec().getPods().size(), is(4));
// => third capture is from zkScalingDown() with new replica count
assertThat(zkPodSetCaptor.getAllValues().get(2).getSpec().getPods().size(), is(3));
// Still one maybe-roll invocation
assertThat(kao.maybeRollZooKeeperInvocations, is(1));
// Scale-down 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 kafkaScaleDown() with old replica count
assertThat(kafkaPodSetCaptor.getAllValues().get(0).getSpec().getPods().size(), is(3));
// => second capture is from kafkaPodSet() with new replica count
assertThat(kafkaPodSetCaptor.getAllValues().get(1).getSpec().getPods().size(), is(3));
// Still one maybe-roll invocation
assertThat(kao.maybeRollKafkaInvocations, is(1));
async.flag();
})));
}
use of io.strimzi.operator.PlatformFeaturesAvailability in project strimzi-kafka-operator by strimzi.
the class KafkaAssemblyOperatorPodSetTest method testRegularReconciliation.
/**
* Tests the regular reconciliation of the Kafka cluster when the UseStrimziPodsSet is already enabled for some time
*
* @param context Test context
*/
@Test
public void testRegularReconciliation(VertxTestContext context) {
ZookeeperCluster zkCluster = ZookeeperCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, KAFKA, VERSIONS);
StrimziPodSet zkPodSet = zkCluster.generatePodSet(KAFKA.getSpec().getZookeeper().getReplicas(), false, null, null, null);
KafkaCluster kafkaCluster = KafkaCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, KAFKA, VERSIONS);
StrimziPodSet kafkaPodSet = kafkaCluster.generatePodSet(KAFKA.getSpec().getKafka().getReplicas(), false, null, null, null);
ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(false);
SecretOperator secretOps = supplier.secretOperations;
when(secretOps.reconcile(any(), any(), any(), any())).thenReturn(Future.succeededFuture());
CrdOperator<KubernetesClient, StrimziPodSet, StrimziPodSetList> mockPodSetOps = supplier.strimziPodSetOperator;
when(mockPodSetOps.getAsync(any(), eq(zkCluster.getName()))).thenReturn(Future.succeededFuture(zkPodSet));
when(mockPodSetOps.reconcile(any(), any(), eq(zkCluster.getName()), any())).thenReturn(Future.succeededFuture(ReconcileResult.noop(zkPodSet)));
when(mockPodSetOps.getAsync(any(), eq(kafkaCluster.getName()))).thenReturn(Future.succeededFuture(kafkaPodSet));
when(mockPodSetOps.reconcile(any(), any(), eq(kafkaCluster.getName()), any())).thenReturn(Future.succeededFuture(ReconcileResult.noop(kafkaPodSet)));
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()));
CrdOperator 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");
MockKafkaAssemblyOperator kao = new MockKafkaAssemblyOperator(vertx, new PlatformFeaturesAvailability(false, KUBERNETES_VERSION), CERT_MANAGER, PASSWORD_GENERATOR, supplier, config);
Checkpoint async = context.checkpoint();
kao.reconcile(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, CLUSTER_NAME)).onComplete(context.succeeding(v -> context.verify(() -> {
assertThat(kao.maybeRollZooKeeperInvocations, is(1));
assertThat(kao.zooPodNeedsRestart.apply(podFromPodSet(zkPodSet, "my-cluster-zookeeper-0")), is(List.of()));
assertThat(kao.zooPodNeedsRestart.apply(podFromPodSet(zkPodSet, "my-cluster-zookeeper-1")), is(List.of()));
assertThat(kao.zooPodNeedsRestart.apply(podFromPodSet(zkPodSet, "my-cluster-zookeeper-2")), is(List.of()));
assertThat(kao.maybeRollKafkaInvocations, is(1));
assertThat(kao.kafkaPodNeedsRestart.apply(podFromPodSet(kafkaPodSet, "my-cluster-kafka-0")), is(List.of()));
assertThat(kao.kafkaPodNeedsRestart.apply(podFromPodSet(kafkaPodSet, "my-cluster-kafka-1")), is(List.of()));
assertThat(kao.kafkaPodNeedsRestart.apply(podFromPodSet(kafkaPodSet, "my-cluster-kafka-2")), is(List.of()));
async.flag();
})));
}
use of io.strimzi.operator.PlatformFeaturesAvailability in project strimzi-kafka-operator by strimzi.
the class KafkaAssemblyOperatorLoadBalancerKafkaListenerTest method testLoadBalancerWithBootstrapService.
@Test
public void testLoadBalancerWithBootstrapService(VertxTestContext context) {
Kafka kafka = new KafkaBuilder().withNewMetadata().withName(NAME).withNamespace(NAMESPACE).endMetadata().withNewSpec().withNewKafka().withReplicas(3).withListeners(new GenericKafkaListenerBuilder().withName("external").withPort(LISTENER_PORT).withTls(true).withType(KafkaListenerType.LOADBALANCER).withNewConfiguration().withCreateBootstrapService(true).endConfiguration().build()).withNewEphemeralStorage().endEphemeralStorage().endKafka().withNewZookeeper().withReplicas(3).withNewEphemeralStorage().endEphemeralStorage().endZookeeper().withNewEntityOperator().withNewUserOperator().endUserOperator().withNewTopicOperator().endTopicOperator().endEntityOperator().endSpec().build();
ResourceOperatorSupplier supplier = prepareResourceOperatorSupplier(kafka);
KafkaAssemblyOperator op = new MockKafkaAssemblyOperatorForLoadBalancerTests(vertx, new PlatformFeaturesAvailability(false, KubernetesVersion.V1_16), certManager, passwordGenerator, supplier, ResourceUtils.dummyClusterOperatorConfig(KafkaVersionTestUtils.getKafkaVersionLookup()));
Reconciliation reconciliation = new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, NAME);
Checkpoint async = context.checkpoint();
op.reconcile(reconciliation).onComplete(context.succeeding(v -> context.verify(() -> {
assertThat(kafka.getStatus().getListeners().size(), is(1));
ListenerStatus listenerStatus = kafka.getStatus().getListeners().get(0);
assertThat(listenerStatus.getBootstrapServers(), is("bootstrap-broker.test.dns.name:9094"));
assertThat(listenerStatus.getAddresses().size(), is(1));
assertThat(listenerStatus.getAddresses().get(0).getHost(), is(DNS_NAME_FOR_BOOTSTRAP_SERVICE));
assertThat(listenerStatus.getAddresses().get(0).getPort(), is(LISTENER_PORT));
async.flag();
})));
}
use of io.strimzi.operator.PlatformFeaturesAvailability in project strimzi-kafka-operator by strimzi.
the class ClusterOperatorTest method startStop.
/**
* Asserts that Cluster Operator starts and then stops a verticle in each namespace
*
* @param context test context passed in for assertions
* @param namespaces namespaces the operator should be watching and operating on
*/
private void startStop(VertxTestContext context, String namespaces, boolean openShift, boolean strimziPodSets) throws InterruptedException {
AtomicInteger numWatchers = new AtomicInteger(0);
AtomicInteger numInformers = new AtomicInteger(0);
KubernetesClient client;
if (openShift) {
client = mock(OpenShiftClient.class);
when(client.isAdaptable(eq(OpenShiftClient.class))).thenReturn(true);
when(client.adapt(eq(OpenShiftClient.class))).thenReturn((OpenShiftClient) client);
} else {
client = mock(KubernetesClient.class);
when(client.isAdaptable(eq(OpenShiftClient.class))).thenReturn(false);
}
try {
when(client.getMasterUrl()).thenReturn(new URL("http://localhost"));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
MixedOperation mockCms = mock(MixedOperation.class);
when(client.resources(any(), any())).thenReturn(mockCms);
MixedOperation mockPods = mock(MixedOperation.class);
when(client.pods()).thenReturn(mockPods);
List<String> namespaceList = asList(namespaces.split(" *,+ *"));
for (String namespace : namespaceList) {
// Mock CRs
Indexer mockCmIndexer = mock(Indexer.class);
SharedIndexInformer mockCmInformer = mock(SharedIndexInformer.class);
when(mockCmInformer.getIndexer()).thenReturn(mockCmIndexer);
MixedOperation mockNamespacedCms = mock(MixedOperation.class);
when(mockNamespacedCms.watch(any())).thenAnswer(invo -> {
numWatchers.incrementAndGet();
Watch mockWatch = mock(Watch.class);
doAnswer(invo2 -> {
((Watcher) invo.getArgument(0)).onClose(null);
return null;
}).when(mockWatch).close();
return mockWatch;
});
when(mockNamespacedCms.inform()).thenAnswer(i -> {
numInformers.getAndIncrement();
return mockCmInformer;
});
when(mockNamespacedCms.withLabels(any())).thenReturn(mockNamespacedCms);
when(mockCms.inNamespace(namespace)).thenReturn(mockNamespacedCms);
// Mock Pods
Indexer mockPodIndexer = mock(Indexer.class);
SharedIndexInformer mockPodInformer = mock(SharedIndexInformer.class);
MixedOperation mockNamespacedPods = mock(MixedOperation.class);
when(mockPodInformer.getIndexer()).thenReturn(mockPodIndexer);
when(mockNamespacedPods.inform()).thenAnswer(i -> {
numInformers.getAndIncrement();
return mockPodInformer;
});
when(mockNamespacedPods.withLabels(any())).thenReturn(mockNamespacedPods);
when(mockPods.inNamespace(namespace)).thenReturn(mockNamespacedPods);
}
Map<String, String> env = buildEnv(namespaces, strimziPodSets);
CountDownLatch latch = new CountDownLatch(namespaceList.size() + 1);
Main.run(vertx, client, new PlatformFeaturesAvailability(openShift, KubernetesVersion.V1_16), ClusterOperatorConfig.fromMap(env, KafkaVersionTestUtils.getKafkaVersionLookup())).onComplete(context.succeeding(v -> context.verify(() -> {
assertThat("A verticle per namespace", vertx.deploymentIDs(), hasSize(namespaceList.size()));
for (String deploymentId : vertx.deploymentIDs()) {
vertx.undeploy(deploymentId, asyncResult -> {
if (asyncResult.failed()) {
LOGGER.error("Failed to undeploy {}", deploymentId);
context.failNow(asyncResult.cause());
}
latch.countDown();
});
}
int maximumExpectedNumberOfWatchers = 7 * namespaceList.size();
assertThat("Looks like there were more watchers than namespaces", numWatchers.get(), lessThanOrEqualTo(maximumExpectedNumberOfWatchers));
int expectedNumberOfInformers = strimziPodSets ? 3 * namespaceList.size() : 0;
assertThat("Looks like there were more informers than namespaces", numInformers.get(), is(expectedNumberOfInformers));
latch.countDown();
})));
latch.await(10, TimeUnit.SECONDS);
context.completeNow();
}
Aggregations