use of io.strimzi.operator.common.PasswordGenerator in project strimzi by strimzi.
the class KafkaMirrorMakerAssemblyOperatorTest method testCreateCluster.
@Test
public void testCreateCluster(VertxTestContext context) {
ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(true);
CrdOperator mockMirrorOps = supplier.mirrorMakerOperator;
DeploymentOperator mockDcOps = supplier.deploymentOperations;
PodDisruptionBudgetOperator mockPdbOps = supplier.podDisruptionBudgetOperator;
ConfigMapOperator mockCmOps = supplier.configMapOperations;
String kmmName = "foo";
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 kmm = ResourceUtils.createKafkaMirrorMaker(kmmNamespace, kmmName, image, producer, consumer, include);
when(mockMirrorOps.get(kmmNamespace, kmmName)).thenReturn(kmm);
when(mockMirrorOps.getAsync(anyString(), anyString())).thenReturn(Future.succeededFuture(kmm));
ArgumentCaptor<Deployment> dcCaptor = ArgumentCaptor.forClass(Deployment.class);
when(mockDcOps.reconcile(any(), anyString(), anyString(), dcCaptor.capture())).thenReturn(Future.succeededFuture());
when(mockDcOps.scaleUp(any(), anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
when(mockDcOps.scaleDown(any(), anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
when(mockDcOps.readiness(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
when(mockDcOps.waitForObserved(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
ArgumentCaptor<PodDisruptionBudget> pdbCaptor = ArgumentCaptor.forClass(PodDisruptionBudget.class);
when(mockPdbOps.reconcile(any(), anyString(), any(), pdbCaptor.capture())).thenReturn(Future.succeededFuture());
ArgumentCaptor<KafkaMirrorMaker> statusCaptor = ArgumentCaptor.forClass(KafkaMirrorMaker.class);
when(mockMirrorOps.updateStatusAsync(any(), statusCaptor.capture())).thenReturn(Future.succeededFuture());
when(mockCmOps.reconcile(any(), anyString(), any(), any())).thenReturn(Future.succeededFuture(ReconcileResult.created(new ConfigMap())));
KafkaMirrorMakerAssemblyOperator ops = new KafkaMirrorMakerAssemblyOperator(vertx, new PlatformFeaturesAvailability(true, kubernetesVersion), new MockCertManager(), new PasswordGenerator(10, "a", "a"), supplier, ResourceUtils.dummyClusterOperatorConfig(VERSIONS));
KafkaMirrorMakerCluster mirror = KafkaMirrorMakerCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, kmm, VERSIONS);
Checkpoint async = context.checkpoint();
ops.reconcile(new Reconciliation("test-trigger", KafkaMirrorMaker.RESOURCE_KIND, kmmNamespace, kmmName)).onComplete(context.succeeding(v -> context.verify(() -> {
// No metrics config => no CMs created
Set<String> metricsNames = new HashSet<>();
if (mirror.isMetricsEnabled()) {
metricsNames.add(KafkaMirrorMakerResources.metricsAndLogConfigMapName(kmmName));
}
// Verify Deployment
List<Deployment> capturedDc = dcCaptor.getAllValues();
assertThat(capturedDc, hasSize(1));
Deployment dc = capturedDc.get(0);
assertThat(dc.getMetadata().getName(), is(mirror.getName()));
Map annotations = new HashMap();
annotations.put(Annotations.STRIMZI_LOGGING_ANNOTATION, LOGGING_CONFIG);
annotations.put(Annotations.ANNO_STRIMZI_AUTH_HASH, "0");
assertThat("Deployments are not equal", dc, is(mirror.generateDeployment(annotations, true, null, null)));
// Verify PodDisruptionBudget
List<PodDisruptionBudget> capturedPdb = pdbCaptor.getAllValues();
assertThat(capturedPdb, hasSize(1));
PodDisruptionBudget pdb = capturedPdb.get(0);
assertThat(pdb.getMetadata().getName(), is(mirror.getName()));
assertThat("PodDisruptionBudgets are not equal", pdb, is(mirror.generatePodDisruptionBudget()));
// Verify status
List<KafkaMirrorMaker> capturedMM = statusCaptor.getAllValues();
assertThat(capturedMM, hasSize(1));
KafkaMirrorMaker mm = capturedMM.get(0);
assertThat(mm.getStatus().getReplicas(), is(mirror.getReplicas()));
assertThat(mm.getStatus().getLabelSelector(), is(mirror.getSelectorLabels().toSelectorString()));
assertThat(mm.getStatus().getConditions().get(0).getType(), is("Ready"));
assertThat(mm.getStatus().getConditions().get(0).getStatus(), is("True"));
async.flag();
})));
}
use of io.strimzi.operator.common.PasswordGenerator in project strimzi by strimzi.
the class KafkaMirrorMakerAssemblyOperatorTest method testCreateClusterStatusNotReady.
@Test
public void testCreateClusterStatusNotReady(VertxTestContext context) throws InterruptedException {
ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(true);
CrdOperator mockMirrorOps = supplier.mirrorMakerOperator;
DeploymentOperator mockDcOps = supplier.deploymentOperations;
PodDisruptionBudgetOperator mockPdbOps = supplier.podDisruptionBudgetOperator;
ConfigMapOperator mockCmOps = supplier.configMapOperations;
String failureMsg = "failure";
String kmmName = "foo";
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 kmm = ResourceUtils.createKafkaMirrorMaker(kmmNamespace, kmmName, image, producer, consumer, include);
when(mockMirrorOps.get(kmmNamespace, kmmName)).thenReturn(kmm);
when(mockMirrorOps.getAsync(anyString(), anyString())).thenReturn(Future.succeededFuture(kmm));
when(mockDcOps.reconcile(any(), anyString(), anyString(), any())).thenReturn(Future.succeededFuture());
when(mockDcOps.scaleUp(any(), anyString(), anyString(), anyInt())).thenReturn(Future.failedFuture(failureMsg));
when(mockDcOps.scaleDown(any(), anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
when(mockDcOps.readiness(any(), eq(kmmNamespace), eq(kmmName), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
when(mockDcOps.waitForObserved(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
when(mockPdbOps.reconcile(any(), anyString(), any(), any())).thenReturn(Future.succeededFuture());
ArgumentCaptor<KafkaMirrorMaker> statusCaptor = ArgumentCaptor.forClass(KafkaMirrorMaker.class);
when(mockMirrorOps.updateStatusAsync(any(), statusCaptor.capture())).thenReturn(Future.succeededFuture());
when(mockCmOps.reconcile(any(), anyString(), any(), any())).thenReturn(Future.succeededFuture(ReconcileResult.created(new ConfigMap())));
KafkaMirrorMakerAssemblyOperator ops = new KafkaMirrorMakerAssemblyOperator(vertx, new PlatformFeaturesAvailability(true, kubernetesVersion), new MockCertManager(), new PasswordGenerator(10, "a", "a"), supplier, ResourceUtils.dummyClusterOperatorConfig(VERSIONS));
Checkpoint async = context.checkpoint();
ops.reconcile(new Reconciliation("test-trigger", KafkaMirrorMaker.RESOURCE_KIND, kmmNamespace, kmmName)).onComplete(context.failing(e -> context.verify(() -> {
// Verify status
List<KafkaMirrorMaker> capturedMM = statusCaptor.getAllValues();
assertThat(capturedMM, hasSize(1));
KafkaMirrorMaker mm = capturedMM.get(0);
assertThat(mm.getStatus().getConditions().get(0).getType(), is("NotReady"));
assertThat(mm.getStatus().getConditions().get(0).getStatus(), is("True"));
assertThat(mm.getStatus().getConditions().get(0).getMessage(), is(failureMsg));
async.flag();
})));
}
use of io.strimzi.operator.common.PasswordGenerator in project strimzi by strimzi.
the class KafkaMirrorMakerAssemblyOperatorTest method testCreateOrUpdateZeroReplica.
@Test
public void testCreateOrUpdateZeroReplica(VertxTestContext context) {
ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(true);
CrdOperator mockMirrorOps = supplier.mirrorMakerOperator;
DeploymentOperator mockDcOps = supplier.deploymentOperations;
PodDisruptionBudgetOperator mockPdbOps = supplier.podDisruptionBudgetOperator;
ConfigMapOperator mockCmOps = supplier.configMapOperations;
String kmmName = "foo";
String kmmNamespace = "test";
KafkaMirrorMakerConsumerSpec consumer = new KafkaMirrorMakerConsumerSpecBuilder().withBootstrapServers(consumerBootstrapServers).withGroupId(groupId).withNumStreams(numStreams).build();
KafkaMirrorMakerProducerSpec producer = new KafkaMirrorMakerProducerSpecBuilder().withBootstrapServers(producerBootstrapServers).build();
KafkaMirrorMaker kmm = ResourceUtils.createKafkaMirrorMaker(kmmNamespace, kmmName, image, 0, producer, consumer, include);
when(mockMirrorOps.get(kmmNamespace, kmmName)).thenReturn(kmm);
when(mockMirrorOps.getAsync(anyString(), anyString())).thenReturn(Future.succeededFuture(kmm));
when(mockDcOps.reconcile(any(), anyString(), anyString(), any())).thenReturn(Future.succeededFuture());
when(mockDcOps.scaleUp(any(), anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
when(mockDcOps.scaleDown(any(), anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
when(mockDcOps.waitForObserved(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
when(mockPdbOps.reconcile(any(), anyString(), any(), any())).thenReturn(Future.succeededFuture());
when(mockCmOps.reconcile(any(), anyString(), any(), any())).thenReturn(Future.succeededFuture(ReconcileResult.created(new ConfigMap())));
ArgumentCaptor<KafkaMirrorMaker> mirrorMakerCaptor = ArgumentCaptor.forClass(KafkaMirrorMaker.class);
when(mockMirrorOps.updateStatusAsync(any(), mirrorMakerCaptor.capture())).thenReturn(Future.succeededFuture());
KafkaMirrorMakerAssemblyOperator ops = new KafkaMirrorMakerAssemblyOperator(vertx, new PlatformFeaturesAvailability(true, kubernetesVersion), new MockCertManager(), new PasswordGenerator(10, "a", "a"), supplier, ResourceUtils.dummyClusterOperatorConfig(VERSIONS));
Checkpoint async = context.checkpoint();
ops.reconcile(new Reconciliation("test-trigger", KafkaMirrorMaker.RESOURCE_KIND, kmmNamespace, kmmName)).onComplete(context.succeeding(v -> context.verify(() -> {
// 0 Replicas - readiness should never get called.
verify(mockDcOps, never()).readiness(any(), anyString(), anyString(), anyLong(), anyLong());
assertThat(mirrorMakerCaptor.getValue().getStatus().getConditions().get(0).getType(), is("Ready"));
async.flag();
})));
}
use of io.strimzi.operator.common.PasswordGenerator in project strimzi by strimzi.
the class ZookeeperClusterTest method generateCertificatesSecret.
private Secret generateCertificatesSecret() {
ClusterCa clusterCa = new ClusterCa(Reconciliation.DUMMY_RECONCILIATION, new OpenSslCertManager(), new PasswordGenerator(10, "a", "a"), cluster, null, null);
clusterCa.createRenewOrReplace(namespace, cluster, emptyMap(), emptyMap(), emptyMap(), null, true);
return zc.generateCertificatesSecret(clusterCa, true);
}
use of io.strimzi.operator.common.PasswordGenerator 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