use of io.strimzi.api.kafka.model.StrimziPodSet in project strimzi by strimzi.
the class StrimziPodSetControllerMockTest method testCrSelector.
/**
* Tests that the controller will ignore pods or node sets when the Kafka cluster they belong to doesn't match the
* custom resource selector
*
* @param context Test context
*/
@Test
public void testCrSelector(VertxTestContext context) {
String podSetName = "matching-podset";
String otherPodSetName = "other-podset";
String podName = podSetName + "-0";
String preExistingPodName = podSetName + "-1";
String otherPodName = otherPodSetName + "-0";
String otherPreExistingPodName = otherPodSetName + "-1";
try {
// Create the pod set which should be reconciled
Pod pod = pod(podName, KAFKA_NAME, podSetName);
Pod preExistingPod = pod(preExistingPodName, KAFKA_NAME, podSetName);
client.pods().inNamespace(NAMESPACE).create(preExistingPod);
podSetOp().inNamespace(NAMESPACE).create(podSet(podSetName, KAFKA_NAME, pod));
// Create the pod set which should be ignored
Pod otherPod = pod(otherPodName, OTHER_KAFKA_NAME, otherPodSetName);
Pod otherPreExistingPod = pod(otherPreExistingPodName, OTHER_KAFKA_NAME, otherPodSetName);
client.pods().inNamespace(NAMESPACE).create(otherPreExistingPod);
podSetOp().inNamespace(NAMESPACE).create(podSet(otherPodSetName, OTHER_KAFKA_NAME, otherPod));
// Check that the pre-existing pod for matching pod set is deleted
TestUtils.waitFor("Wait for the pre-existing Pod to be deleted", 100, 10_000, () -> client.pods().inNamespace(NAMESPACE).withName(preExistingPodName).get() == null, () -> context.failNow("Test timed out waiting for pod deletion!"));
// Check that the pod for matching pod set is ready
TestUtils.waitFor("Wait for Pod to be ready", 100, 10_000, () -> client.pods().inNamespace(NAMESPACE).withName(podName).isReady(), () -> context.failNow("Test timed out waiting for pod readiness!"));
// Check status of the matching pod set which should be updated
TestUtils.waitFor("Wait for StrimziPodSetStatus", 100, 10_000, () -> {
StrimziPodSet podSet = podSetOp().inNamespace(NAMESPACE).withName(podSetName).get();
return podSet.getStatus().getCurrentPods() == 1 && podSet.getStatus().getReadyPods() == 1 && podSet.getStatus().getPods() == 1;
}, () -> context.failNow("Pod stats do not match"));
// Check that the non-matching pod set was ignored
assertThat(client.pods().inNamespace(NAMESPACE).withName(otherPodName).get(), is(nullValue()));
assertThat(podSetOp().inNamespace(NAMESPACE).withName(otherPodSetName).get().getStatus(), is(nullValue()));
assertThat(client.pods().inNamespace(NAMESPACE).withName(otherPreExistingPodName).get(), is(notNullValue()));
context.completeNow();
} finally {
podSetOp().inNamespace(NAMESPACE).withName(podSetName).delete();
podSetOp().inNamespace(NAMESPACE).withName(otherPodSetName).delete();
client.pods().inNamespace(NAMESPACE).withName(otherPreExistingPodName).delete();
client.pods().inNamespace(NAMESPACE).withName(preExistingPodName).delete();
}
}
use of io.strimzi.api.kafka.model.StrimziPodSet in project strimzi by strimzi.
the class StrimziPodSetControllerMockTest method testPodCreationDeletionAndRecreation.
/*
* Tests
*/
/**
* Tests the basic operations:
* - Creation of StrimziPodSet and the managed pod
* - Re-creation of the managed pod when it is deleted
* - Deletion of the StrimziPodSet and the managed pod
*
* @param context Test context
*/
@Test
public void testPodCreationDeletionAndRecreation(VertxTestContext context) {
String podSetName = "basic-test";
String podName = podSetName + "-0";
try {
Pod pod = pod(podName, KAFKA_NAME, podSetName);
podSetOp().inNamespace(NAMESPACE).create(podSet(podSetName, KAFKA_NAME, pod));
// Check that pod is created
TestUtils.waitFor("Wait for Pod to be created", 100, 10_000, () -> client.pods().inNamespace(NAMESPACE).withName(podName).get() != null, () -> context.failNow("Test timed out waiting for pod creation!"));
// Wait until the pod is ready
TestUtils.waitFor("Wait for Pod to be ready", 100, 10_000, () -> client.pods().inNamespace(NAMESPACE).withName(podName).isReady(), () -> context.failNow("Test timed out waiting for pod readiness!"));
Pod actualPod = client.pods().inNamespace(NAMESPACE).withName(podName).get();
// Check OwnerReference was added
checkOwnerReference(actualPod, podSetName);
// We keep the resource version for pod re-creation test
String resourceVersion = actualPod.getMetadata().getResourceVersion();
// Check status of the PodSet
TestUtils.waitFor("Wait for StrimziPodSetStatus", 100, 10_000, () -> {
StrimziPodSet podSet = podSetOp().inNamespace(NAMESPACE).withName(podSetName).get();
return podSet.getStatus().getCurrentPods() == 1 && podSet.getStatus().getReadyPods() == 1 && podSet.getStatus().getPods() == 1;
}, () -> context.failNow("Pod stats do not match"));
// Delete the pod and test that it is recreated
client.pods().inNamespace(NAMESPACE).withName(podName).delete();
// Check that pod is created
TestUtils.waitFor("Wait for Pod to be recreated", 100, 10_000, () -> {
Pod p = client.pods().inNamespace(NAMESPACE).withName(podName).get();
return p != null && !resourceVersion.equals(p.getMetadata().getResourceVersion());
}, () -> context.failNow("Test timed out waiting for pod recreation!"));
context.completeNow();
} finally {
podSetOp().inNamespace(NAMESPACE).withName(podSetName).delete();
}
}
use of io.strimzi.api.kafka.model.StrimziPodSet in project strimzi by strimzi.
the class StrimziPodSetControllerMockTest method testOwnerReferencePatching.
/**
* Tests patching of the owner reference in pre-existing pods
*
* @param context Test context
*/
@Test
public void testOwnerReferencePatching(VertxTestContext context) {
String podSetName = "owner-reference";
String podName = podSetName + "-0";
try {
Pod pod = pod(podName, KAFKA_NAME, podSetName);
client.pods().inNamespace(NAMESPACE).create(pod);
// Wait until the pod is ready
TestUtils.waitFor("Wait for Pod to be ready", 100, 10_000, () -> client.pods().inNamespace(NAMESPACE).withName(podName).isReady(), () -> context.failNow("Test timed out waiting for pod readiness!"));
podSetOp().inNamespace(NAMESPACE).create(podSet(podSetName, KAFKA_NAME, pod));
// Check status of the PodSet
TestUtils.waitFor("Wait for StrimziPodSetStatus", 100, 10_000, () -> {
StrimziPodSet podSet = podSetOp().inNamespace(NAMESPACE).withName(podSetName).get();
return podSet.getStatus().getCurrentPods() == 1 && podSet.getStatus().getReadyPods() == 1 && podSet.getStatus().getPods() == 1;
}, () -> context.failNow("Pod stats do not match"));
// Get the pod and check that the owner reference was set
Pod actualPod = client.pods().inNamespace(NAMESPACE).withName(podName).get();
checkOwnerReference(actualPod, podSetName);
context.completeNow();
} finally {
podSetOp().inNamespace(NAMESPACE).withName(podSetName).delete();
}
}
use of io.strimzi.api.kafka.model.StrimziPodSet in project strimzi by strimzi.
the class StrimziPodSetControllerMockTest method testScaleUpScaleDown.
/**
* Tests scaling up and down of the StrimziPodSet and updates of the StrimziPodSet status.
*
* @param context Test context
*/
@Test
public void testScaleUpScaleDown(VertxTestContext context) {
String podSetName = "scale-up-down";
String pod1Name = podSetName + "-0";
String pod2Name = podSetName + "-1";
try {
Pod pod1 = pod(pod1Name, KAFKA_NAME, podSetName);
podSetOp().inNamespace(NAMESPACE).create(podSet(podSetName, KAFKA_NAME, pod1));
// Wait until the pod is ready
TestUtils.waitFor("Wait for Pod to be ready", 100, 10_000, () -> client.pods().inNamespace(NAMESPACE).withName(pod1Name).isReady(), () -> context.failNow("Test timed out waiting for pod readiness!"));
// Check status of the PodSet
TestUtils.waitFor("Wait for StrimziPodSetStatus", 100, 10_000, () -> {
StrimziPodSet podSet = podSetOp().inNamespace(NAMESPACE).withName(podSetName).get();
return podSet.getStatus().getCurrentPods() == 1 && podSet.getStatus().getReadyPods() == 1 && podSet.getStatus().getPods() == 1;
}, () -> context.failNow("Pod stats do not match"));
// Scale-up the pod-set
Pod pod2 = pod(pod2Name, KAFKA_NAME, podSetName);
podSetOp().inNamespace(NAMESPACE).withName(podSetName).patch(podSet(podSetName, KAFKA_NAME, pod1, pod2));
// Wait until the new pod is ready
TestUtils.waitFor("Wait for second Pod to be ready", 100, 10_000, () -> client.pods().inNamespace(NAMESPACE).withName(pod2Name).isReady(), () -> context.failNow("Test timed out waiting for second pod readiness!"));
// Check status of the PodSet
TestUtils.waitFor("Wait for StrimziPodSetStatus", 100, 10_000, () -> {
StrimziPodSet podSet = podSetOp().inNamespace(NAMESPACE).withName(podSetName).get();
return podSet.getStatus().getCurrentPods() == 2 && podSet.getStatus().getReadyPods() == 2 && podSet.getStatus().getPods() == 2;
}, () -> context.failNow("Pod stats do not match"));
// Scale-down the pod-set
podSetOp().inNamespace(NAMESPACE).withName(podSetName).patch(podSet(podSetName, KAFKA_NAME, pod1));
// Wait until the pod is ready
TestUtils.waitFor("Wait for second Pod to be deleted", 100, 10_000, () -> client.pods().inNamespace(NAMESPACE).withName(pod2Name).get() == null, () -> context.failNow("Test timed out waiting for second pod to be deleted!"));
// Check status of the PodSet
TestUtils.waitFor("Wait for StrimziPodSetStatus", 100, 10_000, () -> {
StrimziPodSet podSet = podSetOp().inNamespace(NAMESPACE).withName(podSetName).get();
return podSet.getStatus().getCurrentPods() == 1 && podSet.getStatus().getReadyPods() == 1 && podSet.getStatus().getPods() == 1;
}, () -> context.failNow("Pod stats do not match"));
context.completeNow();
} finally {
podSetOp().inNamespace(NAMESPACE).withName(podSetName).delete();
}
}
use of io.strimzi.api.kafka.model.StrimziPodSet in project strimzi-kafka-operator by strimzi.
the class KafkaAssemblyOperatorTest method createCluster.
@SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:NPathComplexity", "checkstyle:JavaNCSS", "checkstyle:MethodLength" })
private void createCluster(VertxTestContext context, Kafka kafka, List<Secret> secrets) {
KafkaCluster kafkaCluster = KafkaCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, kafka, VERSIONS);
ZookeeperCluster zookeeperCluster = ZookeeperCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, kafka, VERSIONS);
EntityOperator entityOperator = EntityOperator.fromCrd(Reconciliation.DUMMY_RECONCILIATION, kafka, VERSIONS, true);
// create CM, Service, headless service, statefulset and so on
ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(openShift);
ClusterOperatorConfig config = ResourceUtils.dummyClusterOperatorConfig(VERSIONS);
var mockKafkaOps = supplier.kafkaOperator;
ConfigMapOperator mockCmOps = supplier.configMapOperations;
ServiceOperator mockServiceOps = supplier.serviceOperations;
StatefulSetOperator mockStsOps = supplier.stsOperations;
PvcOperator mockPvcOps = supplier.pvcOperations;
PodOperator mockPodOps = supplier.podOperations;
DeploymentOperator mockDepOps = supplier.deploymentOperations;
SecretOperator mockSecretOps = supplier.secretOperations;
NetworkPolicyOperator mockPolicyOps = supplier.networkPolicyOperator;
PodDisruptionBudgetOperator mockPdbOps = supplier.podDisruptionBudgetOperator;
RouteOperator mockRouteOps = supplier.routeOperations;
IngressOperator mockIngressOps = supplier.ingressOperations;
NodeOperator mockNodeOps = supplier.nodeOperator;
StrimziPodSetOperator mockPodSetOps = supplier.strimziPodSetOperator;
// Create a CM
String kafkaName = kafka.getMetadata().getName();
String kafkaNamespace = kafka.getMetadata().getNamespace();
when(mockKafkaOps.get(kafkaNamespace, kafkaName)).thenReturn(null);
when(mockKafkaOps.getAsync(eq(kafkaNamespace), eq(kafkaName))).thenReturn(Future.succeededFuture(kafka));
when(mockKafkaOps.updateStatusAsync(any(), any(Kafka.class))).thenReturn(Future.succeededFuture());
when(mockPodSetOps.reconcile(any(), any(), any(), any())).thenReturn(Future.succeededFuture(ReconcileResult.created(new StrimziPodSet())));
when(mockPodSetOps.getAsync(any(), any())).thenReturn(Future.succeededFuture(null));
ArgumentCaptor<Service> serviceCaptor = ArgumentCaptor.forClass(Service.class);
ArgumentCaptor<NetworkPolicy> policyCaptor = ArgumentCaptor.forClass(NetworkPolicy.class);
ArgumentCaptor<PodDisruptionBudget> pdbCaptor = ArgumentCaptor.forClass(PodDisruptionBudget.class);
ArgumentCaptor<io.fabric8.kubernetes.api.model.policy.v1beta1.PodDisruptionBudget> pdbV1Beta1Captor = ArgumentCaptor.forClass(io.fabric8.kubernetes.api.model.policy.v1beta1.PodDisruptionBudget.class);
ArgumentCaptor<StatefulSet> ssCaptor = ArgumentCaptor.forClass(StatefulSet.class);
when(mockStsOps.reconcile(any(), eq(kafkaNamespace), eq(KafkaResources.zookeeperStatefulSetName(kafkaName)), ssCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new StatefulSet())));
when(mockStsOps.scaleDown(any(), anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(null));
when(mockStsOps.scaleUp(any(), anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
AtomicReference<StatefulSet> ref = new AtomicReference<>();
when(mockStsOps.reconcile(any(), eq(kafkaNamespace), eq(KafkaResources.kafkaStatefulSetName(kafkaName)), ssCaptor.capture())).thenAnswer(i -> {
StatefulSet sts = new StatefulSetBuilder().withNewMetadata().withName(kafkaName + "kafka").withNamespace(kafkaNamespace).addToLabels(Labels.STRIMZI_CLUSTER_LABEL, kafkaName).endMetadata().withNewSpec().withReplicas(3).endSpec().build();
ref.set(sts);
return Future.succeededFuture(ReconcileResult.created(sts));
});
when(mockPolicyOps.reconcile(any(), anyString(), anyString(), policyCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new NetworkPolicy())));
when(mockStsOps.getAsync(eq(kafkaNamespace), eq(KafkaResources.zookeeperStatefulSetName(kafkaName)))).thenReturn(Future.succeededFuture());
when(mockStsOps.getAsync(eq(kafkaNamespace), eq(KafkaResources.kafkaStatefulSetName(kafkaName)))).thenAnswer(i -> Future.succeededFuture(ref.get()));
when(mockPdbOps.reconcile(any(), anyString(), anyString(), pdbCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new PodDisruptionBudget())));
// Service mocks
Set<Service> createdServices = new HashSet<>();
createdServices.add(kafkaCluster.generateService());
createdServices.add(kafkaCluster.generateHeadlessService());
createdServices.addAll(kafkaCluster.generateExternalBootstrapServices());
int replicas = kafkaCluster.getReplicas();
for (int i = 0; i < replicas; i++) {
createdServices.addAll(kafkaCluster.generateExternalServices(i));
}
Map<String, Service> expectedServicesMap = createdServices.stream().collect(Collectors.toMap(s -> s.getMetadata().getName(), s -> s));
when(mockServiceOps.get(eq(kafkaNamespace), anyString())).thenAnswer(i -> Future.succeededFuture(expectedServicesMap.get(i.<String>getArgument(1))));
when(mockServiceOps.getAsync(eq(kafkaNamespace), anyString())).thenAnswer(i -> {
Service svc = expectedServicesMap.get(i.<String>getArgument(1));
if (svc != null && "NodePort".equals(svc.getSpec().getType())) {
svc.getSpec().getPorts().get(0).setNodePort(32000);
}
return Future.succeededFuture(svc);
});
when(mockServiceOps.reconcile(any(), anyString(), anyString(), serviceCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new Service())));
when(mockServiceOps.endpointReadiness(any(), anyString(), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
when(mockServiceOps.listAsync(eq(kafkaNamespace), any(Labels.class))).thenReturn(Future.succeededFuture(emptyList()));
// Ingress mocks
when(mockIngressOps.listAsync(eq(kafkaNamespace), any(Labels.class))).thenReturn(Future.succeededFuture(emptyList()));
// Route Mocks
if (openShift) {
Set<Route> expectedRoutes = new HashSet<>(kafkaCluster.generateExternalBootstrapRoutes());
for (int i = 0; i < replicas; i++) {
expectedRoutes.addAll(kafkaCluster.generateExternalRoutes(i));
}
Map<String, Route> expectedRoutesMap = expectedRoutes.stream().collect(Collectors.toMap(s -> s.getMetadata().getName(), s -> s));
when(mockRouteOps.get(eq(kafkaNamespace), anyString())).thenAnswer(i -> Future.succeededFuture(expectedRoutesMap.get(i.<String>getArgument(1))));
when(mockRouteOps.getAsync(eq(kafkaNamespace), anyString())).thenAnswer(i -> {
Route rt = expectedRoutesMap.get(i.<String>getArgument(1));
if (rt != null) {
RouteStatus st = new RouteStatusBuilder().withIngress(new RouteIngressBuilder().withHost("host").build()).build();
rt.setStatus(st);
}
return Future.succeededFuture(rt);
});
when(mockRouteOps.listAsync(eq(kafkaNamespace), any(Labels.class))).thenReturn(Future.succeededFuture(emptyList()));
}
// Mock pod readiness
when(mockPodOps.readiness(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
when(mockPodOps.listAsync(anyString(), any(Labels.class))).thenReturn(Future.succeededFuture(emptyList()));
// Mock node ops
when(mockNodeOps.listAsync(any(Labels.class))).thenReturn(Future.succeededFuture(emptyList()));
Map<String, PersistentVolumeClaim> zkPvcs = createPvcs(kafkaNamespace, zookeeperCluster.getStorage(), zookeeperCluster.getReplicas(), (replica, storageId) -> AbstractModel.VOLUME_NAME + "-" + KafkaResources.zookeeperPodName(kafkaName, replica));
Map<String, PersistentVolumeClaim> kafkaPvcs = createPvcs(kafkaNamespace, kafkaCluster.getStorage(), kafkaCluster.getReplicas(), (replica, storageId) -> {
String name = VolumeUtils.createVolumePrefix(storageId, false);
return name + "-" + KafkaResources.kafkaPodName(kafkaName, replica);
});
when(mockPvcOps.get(eq(kafkaNamespace), ArgumentMatchers.startsWith("data-"))).thenAnswer(invocation -> {
String pvcName = invocation.getArgument(1);
if (pvcName.contains(zookeeperCluster.getName())) {
return zkPvcs.get(pvcName);
} else if (pvcName.contains(kafkaCluster.getName())) {
return kafkaPvcs.get(pvcName);
}
return null;
});
when(mockPvcOps.getAsync(eq(kafkaNamespace), ArgumentMatchers.startsWith("data-"))).thenAnswer(invocation -> {
String pvcName = invocation.getArgument(1);
if (pvcName.contains(zookeeperCluster.getName())) {
return Future.succeededFuture(zkPvcs.get(pvcName));
} else if (pvcName.contains(kafkaCluster.getName())) {
return Future.succeededFuture(kafkaPvcs.get(pvcName));
}
return Future.succeededFuture(null);
});
when(mockPvcOps.listAsync(eq(kafkaNamespace), ArgumentMatchers.any(Labels.class))).thenAnswer(invocation -> Future.succeededFuture(Collections.EMPTY_LIST));
Set<String> expectedPvcs = new HashSet<>(zkPvcs.keySet());
expectedPvcs.addAll(kafkaPvcs.keySet());
ArgumentCaptor<PersistentVolumeClaim> pvcCaptor = ArgumentCaptor.forClass(PersistentVolumeClaim.class);
when(mockPvcOps.reconcile(any(), anyString(), anyString(), pvcCaptor.capture())).thenReturn(Future.succeededFuture());
Set<String> expectedSecrets = set(KafkaResources.clientsCaKeySecretName(kafkaName), KafkaResources.clientsCaCertificateSecretName(kafkaName), KafkaResources.clusterCaCertificateSecretName(kafkaName), KafkaResources.clusterCaKeySecretName(kafkaName), KafkaResources.kafkaSecretName(kafkaName), KafkaResources.zookeeperSecretName(kafkaName), ClusterOperator.secretName(kafkaName));
if (metrics) {
expectedSecrets.add(KafkaExporterResources.secretName(kafkaName));
}
expectedSecrets.addAll(secrets.stream().map(s -> s.getMetadata().getName()).collect(Collectors.toSet()));
if (eoConfig != null) {
// it's expected only when the Entity Operator is deployed by the Cluster Operator
expectedSecrets.add(KafkaResources.entityTopicOperatorSecretName(kafkaName));
expectedSecrets.add(KafkaResources.entityUserOperatorSecretName(kafkaName));
}
when(mockDepOps.reconcile(any(), anyString(), anyString(), any())).thenAnswer(invocation -> {
String name = invocation.getArgument(2);
Deployment desired = invocation.getArgument(3);
if (desired != null) {
if (name.contains("operator")) {
if (entityOperator != null) {
context.verify(() -> assertThat(desired.getMetadata().getName(), is(KafkaResources.entityOperatorDeploymentName(kafkaName))));
}
} else if (name.contains("exporter")) {
context.verify(() -> assertThat(metrics, is(true)));
}
}
return Future.succeededFuture(desired != null ? ReconcileResult.created(desired) : ReconcileResult.deleted());
});
when(mockDepOps.getAsync(anyString(), anyString())).thenReturn(Future.succeededFuture());
when(mockDepOps.waitForObserved(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
when(mockDepOps.readiness(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
Map<String, Secret> secretsMap = secrets.stream().collect(Collectors.toMap(s -> s.getMetadata().getName(), s -> s));
when(mockSecretOps.list(anyString(), any())).thenAnswer(i -> new ArrayList<>(secretsMap.values()));
when(mockSecretOps.getAsync(anyString(), any())).thenAnswer(i -> Future.succeededFuture(secretsMap.get(i.<String>getArgument(1))));
when(mockSecretOps.getAsync(kafkaNamespace, KafkaResources.clusterCaCertificateSecretName(kafkaName))).thenAnswer(i -> Future.succeededFuture(secretsMap.get(i.<String>getArgument(1))));
when(mockSecretOps.getAsync(kafkaNamespace, ClusterOperator.secretName(kafkaName))).thenAnswer(i -> Future.succeededFuture(secretsMap.get(i.<String>getArgument(1))));
when(mockSecretOps.reconcile(any(), anyString(), anyString(), any())).thenAnswer(invocation -> {
Secret desired = invocation.getArgument(3);
if (desired != null) {
secretsMap.put(desired.getMetadata().getName(), desired);
}
return Future.succeededFuture(ReconcileResult.created(new Secret()));
});
ArgumentCaptor<ConfigMap> metricsCaptor = ArgumentCaptor.forClass(ConfigMap.class);
ArgumentCaptor<String> metricsNameCaptor = ArgumentCaptor.forClass(String.class);
when(mockCmOps.reconcile(any(), anyString(), metricsNameCaptor.capture(), metricsCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new ConfigMap())));
ArgumentCaptor<ConfigMap> logCaptor = ArgumentCaptor.forClass(ConfigMap.class);
ArgumentCaptor<String> logNameCaptor = ArgumentCaptor.forClass(String.class);
when(mockCmOps.reconcile(any(), anyString(), logNameCaptor.capture(), logCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new ConfigMap())));
ConfigMap metricsCm = kafkaCluster.generateSharedConfigurationConfigMap(new MetricsAndLogging(metricsCM, null), Map.of(), Map.of(), false);
when(mockCmOps.getAsync(kafkaNamespace, KafkaResources.kafkaMetricsAndLogConfigMapName(kafkaName))).thenReturn(Future.succeededFuture(metricsCm));
when(mockCmOps.getAsync(kafkaNamespace, metricsCMName)).thenReturn(Future.succeededFuture(metricsCM));
when(mockCmOps.getAsync(kafkaNamespace, differentMetricsCMName)).thenReturn(Future.succeededFuture(metricsCM));
when(mockCmOps.getAsync(anyString(), eq(JmxTransResources.configMapName(kafkaName)))).thenReturn(Future.succeededFuture(new ConfigMapBuilder().withNewMetadata().withResourceVersion("123").endMetadata().build()));
when(mockCmOps.listAsync(kafkaNamespace, kafkaCluster.getSelectorLabels())).thenReturn(Future.succeededFuture(List.of()));
ArgumentCaptor<Route> routeCaptor = ArgumentCaptor.forClass(Route.class);
ArgumentCaptor<String> routeNameCaptor = ArgumentCaptor.forClass(String.class);
if (openShift) {
when(mockRouteOps.reconcile(any(), eq(kafkaNamespace), routeNameCaptor.capture(), routeCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new Route())));
}
KafkaAssemblyOperator ops = new KafkaAssemblyOperator(vertx, new PlatformFeaturesAvailability(openShift, kubernetesVersion), certManager, passwordGenerator, supplier, config);
// Now try to create a KafkaCluster based on this CM
Checkpoint async = context.checkpoint();
ops.createOrUpdate(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, kafkaNamespace, kafkaName), kafka).onComplete(context.succeeding(v -> context.verify(() -> {
// We expect a headless and headful service
Set<String> expectedServices = set(KafkaResources.zookeeperHeadlessServiceName(kafkaName), KafkaResources.zookeeperServiceName(kafkaName), KafkaResources.bootstrapServiceName(kafkaName), KafkaResources.brokersServiceName(kafkaName));
if (kafkaListeners != null) {
List<GenericKafkaListener> externalListeners = ListenersUtils.externalListeners(kafkaListeners);
for (GenericKafkaListener listener : externalListeners) {
expectedServices.add(ListenersUtils.backwardsCompatibleBootstrapServiceName(kafkaName, listener));
for (int i = 0; i < kafkaCluster.getReplicas(); i++) {
expectedServices.add(ListenersUtils.backwardsCompatibleBrokerServiceName(kafkaName, i, listener));
}
}
}
List<Service> capturedServices = serviceCaptor.getAllValues();
assertThat(capturedServices.stream().filter(Objects::nonNull).map(svc -> svc.getMetadata().getName()).collect(Collectors.toSet()).size(), is(expectedServices.size()));
assertThat(capturedServices.stream().filter(Objects::nonNull).map(svc -> svc.getMetadata().getName()).collect(Collectors.toSet()), is(expectedServices));
// Assertions on the statefulset
List<StatefulSet> capturedSs = ssCaptor.getAllValues();
// We expect a statefulSet for kafka and zookeeper...
assertThat(capturedSs.stream().map(sts -> sts.getMetadata().getName()).collect(Collectors.toSet()), is(set(KafkaResources.kafkaStatefulSetName(kafkaName), KafkaResources.zookeeperStatefulSetName(kafkaName))));
// expected Secrets with certificates
assertThat(new TreeSet<>(secretsMap.keySet()), is(new TreeSet<>(expectedSecrets)));
// Check PDBs
assertThat(pdbCaptor.getAllValues(), hasSize(2));
assertThat(pdbCaptor.getAllValues().stream().map(sts -> sts.getMetadata().getName()).collect(Collectors.toSet()), is(set(KafkaResources.kafkaStatefulSetName(kafkaName), KafkaResources.zookeeperStatefulSetName(kafkaName))));
// Check PVCs
assertThat(pvcCaptor.getAllValues(), hasSize(expectedPvcs.size()));
assertThat(pvcCaptor.getAllValues().stream().map(pvc -> pvc.getMetadata().getName()).collect(Collectors.toSet()), is(expectedPvcs));
for (PersistentVolumeClaim pvc : pvcCaptor.getAllValues()) {
assertThat(pvc.getMetadata().getAnnotations(), hasKey(AbstractModel.ANNO_STRIMZI_IO_DELETE_CLAIM));
}
// Verify deleted routes
if (openShift) {
Set<String> expectedRoutes = set(KafkaResources.bootstrapServiceName(kafkaName));
for (int i = 0; i < kafkaCluster.getReplicas(); i++) {
expectedRoutes.add(KafkaResources.kafkaStatefulSetName(kafkaName) + "-" + i);
}
assertThat(captured(routeNameCaptor), is(expectedRoutes));
} else {
assertThat(routeNameCaptor.getAllValues(), hasSize(0));
}
async.flag();
})));
}
Aggregations