use of io.strimzi.operator.PlatformFeaturesAvailability in project strimzi-kafka-operator by strimzi.
the class KafkaMirrorMakerAssemblyOperatorTest method testReconcile.
@Test
public void testReconcile(VertxTestContext context) {
ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(true);
CrdOperator mockMirrorOps = supplier.mirrorMakerOperator;
DeploymentOperator mockDcOps = supplier.deploymentOperations;
SecretOperator mockSecretOps = supplier.secretOperations;
String kmmNamespace = "test";
KafkaMirrorMakerConsumerSpec consumer = new KafkaMirrorMakerConsumerSpecBuilder().withBootstrapServers(consumerBootstrapServers).withGroupId(groupId).withNumStreams(numStreams).build();
KafkaMirrorMakerProducerSpec producer = new KafkaMirrorMakerProducerSpecBuilder().withBootstrapServers(producerBootstrapServers).build();
Map<String, Object> metricsCm = new HashMap<>();
metricsCm.put("foo", "bar");
KafkaMirrorMaker foo = ResourceUtils.createKafkaMirrorMaker(kmmNamespace, "foo", image, producer, consumer, include);
KafkaMirrorMaker bar = ResourceUtils.createKafkaMirrorMaker(kmmNamespace, "bar", image, producer, consumer, include);
when(mockMirrorOps.listAsync(eq(kmmNamespace), any(Optional.class))).thenReturn(Future.succeededFuture(asList(foo, bar)));
// when requested ConfigMap for a specific Kafka Mirror Maker cluster
when(mockMirrorOps.get(eq(kmmNamespace), eq("foo"))).thenReturn(foo);
when(mockMirrorOps.get(eq(kmmNamespace), eq("bar"))).thenReturn(bar);
when(mockMirrorOps.getAsync(anyString(), anyString())).thenReturn(Future.succeededFuture());
// providing the list of ALL Deployments for all the Kafka Mirror Maker clusters
Labels newLabels = Labels.forStrimziKind(KafkaMirrorMaker.RESOURCE_KIND);
when(mockDcOps.list(eq(kmmNamespace), eq(newLabels))).thenReturn(asList(KafkaMirrorMakerCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, bar, VERSIONS).generateDeployment(new HashMap<String, String>(), true, null, null)));
// providing the list Deployments for already "existing" Kafka Mirror Maker clusters
Labels barLabels = Labels.forStrimziCluster("bar");
when(mockDcOps.list(eq(kmmNamespace), eq(barLabels))).thenReturn(asList(KafkaMirrorMakerCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, bar, VERSIONS).generateDeployment(new HashMap<String, String>(), true, null, null)));
when(mockDcOps.readiness(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
when(mockDcOps.waitForObserved(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
when(mockSecretOps.reconcile(any(), eq(kmmNamespace), any(), any())).thenReturn(Future.succeededFuture());
Set<String> createdOrUpdated = new CopyOnWriteArraySet<>();
Checkpoint createdOrUpdateAsync = context.checkpoint(2);
KafkaMirrorMakerAssemblyOperator ops = new KafkaMirrorMakerAssemblyOperator(vertx, new PlatformFeaturesAvailability(true, kubernetesVersion), new MockCertManager(), new PasswordGenerator(10, "a", "a"), supplier, ResourceUtils.dummyClusterOperatorConfig(VERSIONS)) {
@Override
public Future<KafkaMirrorMakerStatus> createOrUpdate(Reconciliation reconciliation, KafkaMirrorMaker kafkaMirrorMakerAssembly) {
createdOrUpdated.add(kafkaMirrorMakerAssembly.getMetadata().getName());
createdOrUpdateAsync.flag();
return Future.succeededFuture();
}
};
Checkpoint async = context.checkpoint();
// Now try to reconcile all the Kafka Mirror Maker clusters
ops.reconcileAll("test", kmmNamespace, context.succeeding(v -> context.verify(() -> {
assertThat(createdOrUpdated, is(new HashSet(asList("foo", "bar"))));
async.flag();
})));
}
use of io.strimzi.operator.PlatformFeaturesAvailability in project strimzi-kafka-operator by strimzi.
the class KafkaRebalanceStateMachineTest method checkTransition.
/**
* Checks the expected transition between two states of the Kafka Rebalance operator.
*
* @param vertx The vertx test instance.
* @param context The test context instance.
* @param currentState The current state of the resource before being passed to computeNextStatus.
* @param nextState The expected state of the resouce after computeNextStatus has been called.
* @param initialAnnotation The initial annotation attached to the Kafka Rebalance resource. For example none or refresh.
* @param kcRebalance The Kafka Rebalance instance that will be returned by the resourceSupplier.
* @return A future for the {@link KafkaRebalanceStatus} returned by the {@link KafkaRebalanceAssemblyOperator#computeNextStatus} method
*/
private Future<KafkaRebalanceStatus> checkTransition(Vertx vertx, VertxTestContext context, KafkaRebalanceState currentState, KafkaRebalanceState nextState, KafkaRebalanceAnnotation initialAnnotation, KafkaRebalance kcRebalance) {
CruiseControlApi client = new CruiseControlApiImpl(vertx, HTTP_DEFAULT_IDLE_TIMEOUT_SECONDS, MockCruiseControl.CC_SECRET, MockCruiseControl.CC_API_SECRET, true, true);
ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(true);
ConfigMapOperator mockCmOps = supplier.configMapOperations;
PlatformFeaturesAvailability pfa = new PlatformFeaturesAvailability(true, kubernetesVersion);
KafkaRebalanceAssemblyOperator kcrao = new KafkaRebalanceAssemblyOperator(vertx, pfa, supplier, ResourceUtils.dummyClusterOperatorConfig()) {
@Override
public String cruiseControlHost(String clusterName, String clusterNamespace) {
return HOST;
}
};
Reconciliation recon = new Reconciliation("test-trigger", KafkaRebalance.RESOURCE_KIND, CLUSTER_NAMESPACE, RESOURCE_NAME);
RebalanceOptions.RebalanceOptionsBuilder rbOptions = new RebalanceOptions.RebalanceOptionsBuilder();
if (kcRebalance.getSpec() != null) {
if (kcRebalance.getSpec().getGoals() != null) {
rbOptions.withGoals(kcRebalance.getSpec().getGoals());
}
if (kcRebalance.getSpec().isSkipHardGoalCheck()) {
rbOptions.withSkipHardGoalCheck();
}
}
CrdOperator<KubernetesClient, KafkaRebalance, KafkaRebalanceList> mockRebalanceOps = supplier.kafkaRebalanceOperator;
when(mockCmOps.getAsync(CLUSTER_NAMESPACE, RESOURCE_NAME)).thenReturn(Future.succeededFuture(new ConfigMap()));
when(mockRebalanceOps.get(CLUSTER_NAMESPACE, RESOURCE_NAME)).thenReturn(kcRebalance);
when(mockRebalanceOps.getAsync(CLUSTER_NAMESPACE, RESOURCE_NAME)).thenReturn(Future.succeededFuture(kcRebalance));
return kcrao.computeNextStatus(recon, HOST, client, kcRebalance, currentState, initialAnnotation, rbOptions).compose(result -> {
context.verify(() -> {
assertThat(result.getStatus().getConditions(), StateMatchers.hasStateInConditions(nextState));
});
return Future.succeededFuture(result.getStatus());
});
}
use of io.strimzi.operator.PlatformFeaturesAvailability in project strimzi-kafka-operator by strimzi.
the class KafkaStatusTest method testInitialStatusOnOldResource.
@Test
public void testInitialStatusOnOldResource() throws ParseException {
Kafka kafka = getKafkaCrd();
ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(false);
// Mock the Kafka Operator
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());
MockInitialStatusKafkaAssemblyOperator kao = new MockInitialStatusKafkaAssemblyOperator(vertx, new PlatformFeaturesAvailability(false, kubernetesVersion), certManager, passwordGenerator, supplier, config);
kao.reconcile(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, namespace, clusterName)).onComplete(res -> {
assertThat(res.succeeded(), is(true));
assertThat(kafkaCaptor.getAllValues().size(), is(1));
});
}
use of io.strimzi.operator.PlatformFeaturesAvailability 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();
});
}
use of io.strimzi.operator.PlatformFeaturesAvailability 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();
});
}
Aggregations