Search in sources :

Example 1 with KafkaUserStatus

use of io.strimzi.api.kafka.model.status.KafkaUserStatus in project strimzi by strimzi.

the class KafkaUserOperatorTest method testReconcileAllWithoutAcls.

@Test
public void testReconcileAllWithoutAcls(VertxTestContext context) {
    CrdOperator mockCrdOps = mock(CrdOperator.class);
    SecretOperator mockSecretOps = mock(SecretOperator.class);
    SimpleAclOperator aclOps = mock(SimpleAclOperator.class);
    ScramCredentialsOperator scramOps = mock(ScramCredentialsOperator.class);
    QuotasOperator quotasOps = mock(QuotasOperator.class);
    KafkaUser newTlsUser = ResourceUtils.createKafkaUserTls();
    newTlsUser.getMetadata().setName("new-tls-user");
    KafkaUser newScramShaUser = ResourceUtils.createKafkaUserScramSha();
    newScramShaUser.getMetadata().setName("new-scram-sha-user");
    KafkaUser existingTlsUser = ResourceUtils.createKafkaUserTls();
    existingTlsUser.getMetadata().setName("existing-tls-user");
    Secret clientsCa = ResourceUtils.createClientsCaCertSecret();
    Secret existingTlsUserSecret = ResourceUtils.createUserSecretTls();
    existingTlsUserSecret.getMetadata().setName("existing-tls-user");
    Secret existingScramShaUserSecret = ResourceUtils.createUserSecretScramSha();
    existingScramShaUserSecret.getMetadata().setName("existing-scram-sha-user");
    KafkaUser existingScramShaUser = ResourceUtils.createKafkaUserTls();
    existingScramShaUser.getMetadata().setName("existing-scram-sha-user");
    when(mockCrdOps.listAsync(eq(ResourceUtils.NAMESPACE), eq(Optional.of(new LabelSelector(null, Labels.fromMap(ResourceUtils.LABELS).toMap()))))).thenReturn(Future.succeededFuture(Arrays.asList(newTlsUser, newScramShaUser, existingTlsUser, existingScramShaUser)));
    when(mockSecretOps.list(eq(ResourceUtils.NAMESPACE), eq(Labels.fromMap(ResourceUtils.LABELS).withStrimziKind(KafkaUser.RESOURCE_KIND)))).thenReturn(Arrays.asList(existingTlsUserSecret, existingScramShaUserSecret));
    when(scramOps.getAllUsers()).thenReturn(Future.succeededFuture(List.of("existing-tls-user", "deleted-scram-sha-user")));
    when(quotasOps.getAllUsers()).thenReturn(Future.succeededFuture(Set.of("existing-tls-user", "quota-user")));
    when(mockCrdOps.get(eq(newTlsUser.getMetadata().getNamespace()), eq(newTlsUser.getMetadata().getName()))).thenReturn(newTlsUser);
    when(mockCrdOps.get(eq(newScramShaUser.getMetadata().getNamespace()), eq(newScramShaUser.getMetadata().getName()))).thenReturn(newScramShaUser);
    when(mockCrdOps.get(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingTlsUser.getMetadata().getName()))).thenReturn(existingTlsUser);
    when(mockCrdOps.get(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingScramShaUser.getMetadata().getName()))).thenReturn(existingScramShaUser);
    when(mockCrdOps.getAsync(eq(newTlsUser.getMetadata().getNamespace()), eq(newTlsUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(newTlsUser));
    when(mockCrdOps.getAsync(eq(newScramShaUser.getMetadata().getNamespace()), eq(newScramShaUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(newScramShaUser));
    when(mockCrdOps.getAsync(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingTlsUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(existingTlsUser));
    when(mockCrdOps.getAsync(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingScramShaUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(existingScramShaUser));
    when(mockCrdOps.updateStatusAsync(any(), any())).thenReturn(Future.succeededFuture());
    when(mockSecretOps.get(eq(clientsCa.getMetadata().getNamespace()), eq(clientsCa.getMetadata().getName()))).thenReturn(clientsCa);
    when(mockSecretOps.get(eq(newTlsUser.getMetadata().getNamespace()), eq(newTlsUser.getMetadata().getName()))).thenReturn(null);
    when(mockSecretOps.get(eq(newScramShaUser.getMetadata().getNamespace()), eq(newScramShaUser.getMetadata().getName()))).thenReturn(null);
    when(mockSecretOps.get(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingTlsUser.getMetadata().getName()))).thenReturn(existingTlsUserSecret);
    when(mockSecretOps.get(eq(existingScramShaUser.getMetadata().getNamespace()), eq(existingScramShaUser.getMetadata().getName()))).thenReturn(existingScramShaUserSecret);
    Set<String> createdOrUpdated = new CopyOnWriteArraySet<>();
    Set<String> deleted = new CopyOnWriteArraySet<>();
    Checkpoint async = context.checkpoint();
    Promise reconcileAllCompleted = Promise.promise();
    KafkaUserOperator op = new KafkaUserOperator(vertx, mockCertManager, mockCrdOps, mockSecretOps, scramOps, quotasOps, aclOps, ResourceUtils.createUserOperatorConfig(ResourceUtils.LABELS, false, "12")) {

        @Override
        public Future<KafkaUserStatus> createOrUpdate(Reconciliation reconciliation, KafkaUser resource) {
            createdOrUpdated.add(resource.getMetadata().getName());
            return Future.succeededFuture(new KafkaUserStatus());
        }

        @Override
        public Future<Boolean> delete(Reconciliation reconciliation) {
            deleted.add(reconciliation.name());
            return Future.succeededFuture(Boolean.TRUE);
        }
    };
    // call reconcileAll and pass in promise to the handler to run assertions on completion
    op.reconcileAll("test", ResourceUtils.NAMESPACE, ar -> reconcileAllCompleted.complete());
    reconcileAllCompleted.future().compose(v -> context.verify(() -> {
        assertThat(createdOrUpdated, is(new HashSet(asList("new-tls-user", "existing-tls-user", "new-scram-sha-user", "existing-scram-sha-user"))));
        assertThat(deleted, is(new HashSet(asList("quota-user", "deleted-scram-sha-user"))));
        verify(aclOps, never()).getAllUsers();
        async.flag();
    }));
}
Also used : LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) Secret(io.fabric8.kubernetes.api.model.Secret) Promise(io.vertx.core.Promise) Checkpoint(io.vertx.junit5.Checkpoint) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) Reconciliation(io.strimzi.operator.common.Reconciliation) KafkaUserStatus(io.strimzi.api.kafka.model.status.KafkaUserStatus) KafkaUser(io.strimzi.api.kafka.model.KafkaUser) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 2 with KafkaUserStatus

use of io.strimzi.api.kafka.model.status.KafkaUserStatus in project strimzi-kafka-operator by strimzi.

the class KafkaUserOperatorTest method testReconcileAllWithoutAcls.

@Test
public void testReconcileAllWithoutAcls(VertxTestContext context) {
    CrdOperator mockCrdOps = mock(CrdOperator.class);
    SecretOperator mockSecretOps = mock(SecretOperator.class);
    SimpleAclOperator aclOps = mock(SimpleAclOperator.class);
    ScramCredentialsOperator scramOps = mock(ScramCredentialsOperator.class);
    QuotasOperator quotasOps = mock(QuotasOperator.class);
    KafkaUser newTlsUser = ResourceUtils.createKafkaUserTls();
    newTlsUser.getMetadata().setName("new-tls-user");
    KafkaUser newScramShaUser = ResourceUtils.createKafkaUserScramSha();
    newScramShaUser.getMetadata().setName("new-scram-sha-user");
    KafkaUser existingTlsUser = ResourceUtils.createKafkaUserTls();
    existingTlsUser.getMetadata().setName("existing-tls-user");
    Secret clientsCa = ResourceUtils.createClientsCaCertSecret();
    Secret existingTlsUserSecret = ResourceUtils.createUserSecretTls();
    existingTlsUserSecret.getMetadata().setName("existing-tls-user");
    Secret existingScramShaUserSecret = ResourceUtils.createUserSecretScramSha();
    existingScramShaUserSecret.getMetadata().setName("existing-scram-sha-user");
    KafkaUser existingScramShaUser = ResourceUtils.createKafkaUserTls();
    existingScramShaUser.getMetadata().setName("existing-scram-sha-user");
    when(mockCrdOps.listAsync(eq(ResourceUtils.NAMESPACE), eq(Optional.of(new LabelSelector(null, Labels.fromMap(ResourceUtils.LABELS).toMap()))))).thenReturn(Future.succeededFuture(Arrays.asList(newTlsUser, newScramShaUser, existingTlsUser, existingScramShaUser)));
    when(mockSecretOps.list(eq(ResourceUtils.NAMESPACE), eq(Labels.fromMap(ResourceUtils.LABELS).withStrimziKind(KafkaUser.RESOURCE_KIND)))).thenReturn(Arrays.asList(existingTlsUserSecret, existingScramShaUserSecret));
    when(scramOps.getAllUsers()).thenReturn(Future.succeededFuture(List.of("existing-tls-user", "deleted-scram-sha-user")));
    when(quotasOps.getAllUsers()).thenReturn(Future.succeededFuture(Set.of("existing-tls-user", "quota-user")));
    when(mockCrdOps.get(eq(newTlsUser.getMetadata().getNamespace()), eq(newTlsUser.getMetadata().getName()))).thenReturn(newTlsUser);
    when(mockCrdOps.get(eq(newScramShaUser.getMetadata().getNamespace()), eq(newScramShaUser.getMetadata().getName()))).thenReturn(newScramShaUser);
    when(mockCrdOps.get(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingTlsUser.getMetadata().getName()))).thenReturn(existingTlsUser);
    when(mockCrdOps.get(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingScramShaUser.getMetadata().getName()))).thenReturn(existingScramShaUser);
    when(mockCrdOps.getAsync(eq(newTlsUser.getMetadata().getNamespace()), eq(newTlsUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(newTlsUser));
    when(mockCrdOps.getAsync(eq(newScramShaUser.getMetadata().getNamespace()), eq(newScramShaUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(newScramShaUser));
    when(mockCrdOps.getAsync(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingTlsUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(existingTlsUser));
    when(mockCrdOps.getAsync(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingScramShaUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(existingScramShaUser));
    when(mockCrdOps.updateStatusAsync(any(), any())).thenReturn(Future.succeededFuture());
    when(mockSecretOps.get(eq(clientsCa.getMetadata().getNamespace()), eq(clientsCa.getMetadata().getName()))).thenReturn(clientsCa);
    when(mockSecretOps.get(eq(newTlsUser.getMetadata().getNamespace()), eq(newTlsUser.getMetadata().getName()))).thenReturn(null);
    when(mockSecretOps.get(eq(newScramShaUser.getMetadata().getNamespace()), eq(newScramShaUser.getMetadata().getName()))).thenReturn(null);
    when(mockSecretOps.get(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingTlsUser.getMetadata().getName()))).thenReturn(existingTlsUserSecret);
    when(mockSecretOps.get(eq(existingScramShaUser.getMetadata().getNamespace()), eq(existingScramShaUser.getMetadata().getName()))).thenReturn(existingScramShaUserSecret);
    Set<String> createdOrUpdated = new CopyOnWriteArraySet<>();
    Set<String> deleted = new CopyOnWriteArraySet<>();
    Checkpoint async = context.checkpoint();
    Promise reconcileAllCompleted = Promise.promise();
    KafkaUserOperator op = new KafkaUserOperator(vertx, mockCertManager, mockCrdOps, mockSecretOps, scramOps, quotasOps, aclOps, ResourceUtils.createUserOperatorConfig(ResourceUtils.LABELS, false, "12")) {

        @Override
        public Future<KafkaUserStatus> createOrUpdate(Reconciliation reconciliation, KafkaUser resource) {
            createdOrUpdated.add(resource.getMetadata().getName());
            return Future.succeededFuture(new KafkaUserStatus());
        }

        @Override
        public Future<Boolean> delete(Reconciliation reconciliation) {
            deleted.add(reconciliation.name());
            return Future.succeededFuture(Boolean.TRUE);
        }
    };
    // call reconcileAll and pass in promise to the handler to run assertions on completion
    op.reconcileAll("test", ResourceUtils.NAMESPACE, ar -> reconcileAllCompleted.complete());
    reconcileAllCompleted.future().compose(v -> context.verify(() -> {
        assertThat(createdOrUpdated, is(new HashSet(asList("new-tls-user", "existing-tls-user", "new-scram-sha-user", "existing-scram-sha-user"))));
        assertThat(deleted, is(new HashSet(asList("quota-user", "deleted-scram-sha-user"))));
        verify(aclOps, never()).getAllUsers();
        async.flag();
    }));
}
Also used : LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) Secret(io.fabric8.kubernetes.api.model.Secret) Promise(io.vertx.core.Promise) Checkpoint(io.vertx.junit5.Checkpoint) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) Reconciliation(io.strimzi.operator.common.Reconciliation) KafkaUserStatus(io.strimzi.api.kafka.model.status.KafkaUserStatus) KafkaUser(io.strimzi.api.kafka.model.KafkaUser) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with KafkaUserStatus

use of io.strimzi.api.kafka.model.status.KafkaUserStatus in project strimzi-kafka-operator by strimzi.

the class KafkaUserOperatorTest method testReconcileAll.

@Test
public void testReconcileAll(VertxTestContext context) {
    CrdOperator mockCrdOps = mock(CrdOperator.class);
    SecretOperator mockSecretOps = mock(SecretOperator.class);
    SimpleAclOperator aclOps = mock(SimpleAclOperator.class);
    ScramCredentialsOperator scramOps = mock(ScramCredentialsOperator.class);
    QuotasOperator quotasOps = mock(QuotasOperator.class);
    KafkaUser newTlsUser = ResourceUtils.createKafkaUserTls();
    newTlsUser.getMetadata().setName("new-tls-user");
    KafkaUser newScramShaUser = ResourceUtils.createKafkaUserScramSha();
    newScramShaUser.getMetadata().setName("new-scram-sha-user");
    KafkaUser existingTlsUser = ResourceUtils.createKafkaUserTls();
    existingTlsUser.getMetadata().setName("existing-tls-user");
    Secret clientsCa = ResourceUtils.createClientsCaCertSecret();
    Secret existingTlsUserSecret = ResourceUtils.createUserSecretTls();
    existingTlsUserSecret.getMetadata().setName("existing-tls-user");
    Secret existingScramShaUserSecret = ResourceUtils.createUserSecretScramSha();
    existingScramShaUserSecret.getMetadata().setName("existing-scram-sha-user");
    KafkaUser existingScramShaUser = ResourceUtils.createKafkaUserTls();
    existingScramShaUser.getMetadata().setName("existing-scram-sha-user");
    when(mockCrdOps.listAsync(eq(ResourceUtils.NAMESPACE), eq(Optional.of(new LabelSelector(null, Labels.fromMap(ResourceUtils.LABELS).toMap()))))).thenReturn(Future.succeededFuture(Arrays.asList(newTlsUser, newScramShaUser, existingTlsUser, existingScramShaUser)));
    when(mockSecretOps.list(eq(ResourceUtils.NAMESPACE), eq(Labels.fromMap(ResourceUtils.LABELS).withStrimziKind(KafkaUser.RESOURCE_KIND)))).thenReturn(Arrays.asList(existingTlsUserSecret, existingScramShaUserSecret));
    when(aclOps.getAllUsers()).thenReturn(Future.succeededFuture(new HashSet<String>(Arrays.asList("existing-tls-user", "second-deleted-user"))));
    when(scramOps.getAllUsers()).thenReturn(Future.succeededFuture(List.of("existing-tls-user", "deleted-scram-sha-user")));
    when(quotasOps.getAllUsers()).thenReturn(Future.succeededFuture(Set.of("existing-tls-user", "quota-user")));
    when(mockCrdOps.get(eq(newTlsUser.getMetadata().getNamespace()), eq(newTlsUser.getMetadata().getName()))).thenReturn(newTlsUser);
    when(mockCrdOps.get(eq(newScramShaUser.getMetadata().getNamespace()), eq(newScramShaUser.getMetadata().getName()))).thenReturn(newScramShaUser);
    when(mockCrdOps.get(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingTlsUser.getMetadata().getName()))).thenReturn(existingTlsUser);
    when(mockCrdOps.get(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingScramShaUser.getMetadata().getName()))).thenReturn(existingScramShaUser);
    when(mockCrdOps.getAsync(eq(newTlsUser.getMetadata().getNamespace()), eq(newTlsUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(newTlsUser));
    when(mockCrdOps.getAsync(eq(newScramShaUser.getMetadata().getNamespace()), eq(newScramShaUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(newScramShaUser));
    when(mockCrdOps.getAsync(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingTlsUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(existingTlsUser));
    when(mockCrdOps.getAsync(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingScramShaUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(existingScramShaUser));
    when(mockCrdOps.updateStatusAsync(any(), any())).thenReturn(Future.succeededFuture());
    when(mockSecretOps.get(eq(clientsCa.getMetadata().getNamespace()), eq(clientsCa.getMetadata().getName()))).thenReturn(clientsCa);
    when(mockSecretOps.get(eq(newTlsUser.getMetadata().getNamespace()), eq(newTlsUser.getMetadata().getName()))).thenReturn(null);
    when(mockSecretOps.get(eq(newScramShaUser.getMetadata().getNamespace()), eq(newScramShaUser.getMetadata().getName()))).thenReturn(null);
    when(mockSecretOps.get(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingTlsUser.getMetadata().getName()))).thenReturn(existingTlsUserSecret);
    when(mockSecretOps.get(eq(existingScramShaUser.getMetadata().getNamespace()), eq(existingScramShaUser.getMetadata().getName()))).thenReturn(existingScramShaUserSecret);
    Set<String> createdOrUpdated = new CopyOnWriteArraySet<>();
    Set<String> deleted = new CopyOnWriteArraySet<>();
    Checkpoint async = context.checkpoint();
    Promise reconcileAllCompleted = Promise.promise();
    KafkaUserOperator op = new KafkaUserOperator(vertx, mockCertManager, mockCrdOps, mockSecretOps, scramOps, quotasOps, aclOps, ResourceUtils.createUserOperatorConfig(ResourceUtils.LABELS)) {

        @Override
        public Future<KafkaUserStatus> createOrUpdate(Reconciliation reconciliation, KafkaUser resource) {
            createdOrUpdated.add(resource.getMetadata().getName());
            return Future.succeededFuture(new KafkaUserStatus());
        }

        @Override
        public Future<Boolean> delete(Reconciliation reconciliation) {
            deleted.add(reconciliation.name());
            return Future.succeededFuture(Boolean.TRUE);
        }
    };
    // call reconcileAll and pass in promise to the handler to run assertions on completion
    op.reconcileAll("test", ResourceUtils.NAMESPACE, ar -> reconcileAllCompleted.complete());
    reconcileAllCompleted.future().compose(v -> context.verify(() -> {
        assertThat(createdOrUpdated, is(new HashSet(asList("new-tls-user", "existing-tls-user", "new-scram-sha-user", "existing-scram-sha-user"))));
        assertThat(deleted, is(new HashSet(asList("quota-user", "second-deleted-user", "deleted-scram-sha-user"))));
        async.flag();
    }));
}
Also used : LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) Secret(io.fabric8.kubernetes.api.model.Secret) Promise(io.vertx.core.Promise) Checkpoint(io.vertx.junit5.Checkpoint) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) Reconciliation(io.strimzi.operator.common.Reconciliation) KafkaUserStatus(io.strimzi.api.kafka.model.status.KafkaUserStatus) KafkaUser(io.strimzi.api.kafka.model.KafkaUser) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with KafkaUserStatus

use of io.strimzi.api.kafka.model.status.KafkaUserStatus in project strimzi by strimzi.

the class KafkaUserOperator method createOrUpdate.

/**
 * Creates or updates the user. The implementation
 * should not assume that any resources are in any particular state (e.g. that the absence on
 * one resource means that all resources need to be created).
 *
 * @param reconciliation Unique identification for the reconciliation
 * @param resource KafkaUser resources with the desired user configuration.
 * @return a Future
 */
@Override
protected Future<KafkaUserStatus> createOrUpdate(Reconciliation reconciliation, KafkaUser resource) {
    KafkaUserModel user;
    KafkaUserStatus userStatus = new KafkaUserStatus();
    try {
        user = KafkaUserModel.fromCrd(resource, config.getSecretPrefix(), config.isAclsAdminApiSupported());
        LOGGER.debugCr(reconciliation, "Updating User {} in namespace {}", reconciliation.name(), reconciliation.namespace());
    } catch (Exception e) {
        LOGGER.warnCr(reconciliation, e);
        StatusUtils.setStatusConditionAndObservedGeneration(resource, userStatus, Future.failedFuture(e));
        return Future.failedFuture(new ReconciliationException(userStatus, e));
    }
    Promise<KafkaUserStatus> handler = Promise.promise();
    secretOperations.getAsync(reconciliation.namespace(), user.getSecretName()).compose(userSecret -> maybeGenerateCredentials(reconciliation, user, userSecret)).compose(ignore -> reconcileCredentialsQuotasAndAcls(reconciliation, user, userStatus)).onComplete(reconciliationResult -> {
        StatusUtils.setStatusConditionAndObservedGeneration(resource, userStatus, reconciliationResult.mapEmpty());
        userStatus.setUsername(user.getUserName());
        if (reconciliationResult.succeeded()) {
            handler.complete(userStatus);
        } else {
            handler.fail(new ReconciliationException(userStatus, reconciliationResult.cause()));
        }
    });
    return handler.future();
}
Also used : ReconciliationException(io.strimzi.operator.common.ReconciliationException) CertManager(io.strimzi.certs.CertManager) KafkaUser(io.strimzi.api.kafka.model.KafkaUser) KafkaUserList(io.strimzi.api.kafka.KafkaUserList) CompositeFuture(io.vertx.core.CompositeFuture) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) ReconciliationException(io.strimzi.operator.common.ReconciliationException) ReconcileResult(io.strimzi.operator.common.operator.resource.ReconcileResult) AbstractOperator(io.strimzi.operator.common.AbstractOperator) StatusUtils(io.strimzi.operator.common.operator.resource.StatusUtils) ReconciliationLogger(io.strimzi.operator.common.ReconciliationLogger) KafkaUserStatus(io.strimzi.api.kafka.model.status.KafkaUserStatus) SimpleAclRule(io.strimzi.operator.user.model.acl.SimpleAclRule) Promise(io.vertx.core.Promise) Collection(java.util.Collection) MicrometerMetricsProvider(io.strimzi.operator.common.MicrometerMetricsProvider) Vertx(io.vertx.core.Vertx) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) Set(java.util.Set) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) KafkaUserModel(io.strimzi.operator.user.model.KafkaUserModel) Reconciliation(io.strimzi.operator.common.Reconciliation) NamespaceAndName(io.strimzi.operator.common.model.NamespaceAndName) List(java.util.List) KafkaUserQuotas(io.strimzi.api.kafka.model.KafkaUserQuotas) PasswordGenerator(io.strimzi.operator.common.PasswordGenerator) UserOperatorConfig(io.strimzi.operator.user.UserOperatorConfig) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Secret(io.fabric8.kubernetes.api.model.Secret) KafkaUserSpec(io.strimzi.api.kafka.model.KafkaUserSpec) KafkaUserModel(io.strimzi.operator.user.model.KafkaUserModel) KafkaUserStatus(io.strimzi.api.kafka.model.status.KafkaUserStatus) ReconciliationException(io.strimzi.operator.common.ReconciliationException)

Example 5 with KafkaUserStatus

use of io.strimzi.api.kafka.model.status.KafkaUserStatus in project strimzi by strimzi.

the class KafkaUserOperatorTest method testReconcileAll.

@Test
public void testReconcileAll(VertxTestContext context) {
    CrdOperator mockCrdOps = mock(CrdOperator.class);
    SecretOperator mockSecretOps = mock(SecretOperator.class);
    SimpleAclOperator aclOps = mock(SimpleAclOperator.class);
    ScramCredentialsOperator scramOps = mock(ScramCredentialsOperator.class);
    QuotasOperator quotasOps = mock(QuotasOperator.class);
    KafkaUser newTlsUser = ResourceUtils.createKafkaUserTls();
    newTlsUser.getMetadata().setName("new-tls-user");
    KafkaUser newScramShaUser = ResourceUtils.createKafkaUserScramSha();
    newScramShaUser.getMetadata().setName("new-scram-sha-user");
    KafkaUser existingTlsUser = ResourceUtils.createKafkaUserTls();
    existingTlsUser.getMetadata().setName("existing-tls-user");
    Secret clientsCa = ResourceUtils.createClientsCaCertSecret();
    Secret existingTlsUserSecret = ResourceUtils.createUserSecretTls();
    existingTlsUserSecret.getMetadata().setName("existing-tls-user");
    Secret existingScramShaUserSecret = ResourceUtils.createUserSecretScramSha();
    existingScramShaUserSecret.getMetadata().setName("existing-scram-sha-user");
    KafkaUser existingScramShaUser = ResourceUtils.createKafkaUserTls();
    existingScramShaUser.getMetadata().setName("existing-scram-sha-user");
    when(mockCrdOps.listAsync(eq(ResourceUtils.NAMESPACE), eq(Optional.of(new LabelSelector(null, Labels.fromMap(ResourceUtils.LABELS).toMap()))))).thenReturn(Future.succeededFuture(Arrays.asList(newTlsUser, newScramShaUser, existingTlsUser, existingScramShaUser)));
    when(mockSecretOps.list(eq(ResourceUtils.NAMESPACE), eq(Labels.fromMap(ResourceUtils.LABELS).withStrimziKind(KafkaUser.RESOURCE_KIND)))).thenReturn(Arrays.asList(existingTlsUserSecret, existingScramShaUserSecret));
    when(aclOps.getAllUsers()).thenReturn(Future.succeededFuture(new HashSet<String>(Arrays.asList("existing-tls-user", "second-deleted-user"))));
    when(scramOps.getAllUsers()).thenReturn(Future.succeededFuture(List.of("existing-tls-user", "deleted-scram-sha-user")));
    when(quotasOps.getAllUsers()).thenReturn(Future.succeededFuture(Set.of("existing-tls-user", "quota-user")));
    when(mockCrdOps.get(eq(newTlsUser.getMetadata().getNamespace()), eq(newTlsUser.getMetadata().getName()))).thenReturn(newTlsUser);
    when(mockCrdOps.get(eq(newScramShaUser.getMetadata().getNamespace()), eq(newScramShaUser.getMetadata().getName()))).thenReturn(newScramShaUser);
    when(mockCrdOps.get(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingTlsUser.getMetadata().getName()))).thenReturn(existingTlsUser);
    when(mockCrdOps.get(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingScramShaUser.getMetadata().getName()))).thenReturn(existingScramShaUser);
    when(mockCrdOps.getAsync(eq(newTlsUser.getMetadata().getNamespace()), eq(newTlsUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(newTlsUser));
    when(mockCrdOps.getAsync(eq(newScramShaUser.getMetadata().getNamespace()), eq(newScramShaUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(newScramShaUser));
    when(mockCrdOps.getAsync(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingTlsUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(existingTlsUser));
    when(mockCrdOps.getAsync(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingScramShaUser.getMetadata().getName()))).thenReturn(Future.succeededFuture(existingScramShaUser));
    when(mockCrdOps.updateStatusAsync(any(), any())).thenReturn(Future.succeededFuture());
    when(mockSecretOps.get(eq(clientsCa.getMetadata().getNamespace()), eq(clientsCa.getMetadata().getName()))).thenReturn(clientsCa);
    when(mockSecretOps.get(eq(newTlsUser.getMetadata().getNamespace()), eq(newTlsUser.getMetadata().getName()))).thenReturn(null);
    when(mockSecretOps.get(eq(newScramShaUser.getMetadata().getNamespace()), eq(newScramShaUser.getMetadata().getName()))).thenReturn(null);
    when(mockSecretOps.get(eq(existingTlsUser.getMetadata().getNamespace()), eq(existingTlsUser.getMetadata().getName()))).thenReturn(existingTlsUserSecret);
    when(mockSecretOps.get(eq(existingScramShaUser.getMetadata().getNamespace()), eq(existingScramShaUser.getMetadata().getName()))).thenReturn(existingScramShaUserSecret);
    Set<String> createdOrUpdated = new CopyOnWriteArraySet<>();
    Set<String> deleted = new CopyOnWriteArraySet<>();
    Checkpoint async = context.checkpoint();
    Promise reconcileAllCompleted = Promise.promise();
    KafkaUserOperator op = new KafkaUserOperator(vertx, mockCertManager, mockCrdOps, mockSecretOps, scramOps, quotasOps, aclOps, ResourceUtils.createUserOperatorConfig(ResourceUtils.LABELS)) {

        @Override
        public Future<KafkaUserStatus> createOrUpdate(Reconciliation reconciliation, KafkaUser resource) {
            createdOrUpdated.add(resource.getMetadata().getName());
            return Future.succeededFuture(new KafkaUserStatus());
        }

        @Override
        public Future<Boolean> delete(Reconciliation reconciliation) {
            deleted.add(reconciliation.name());
            return Future.succeededFuture(Boolean.TRUE);
        }
    };
    // call reconcileAll and pass in promise to the handler to run assertions on completion
    op.reconcileAll("test", ResourceUtils.NAMESPACE, ar -> reconcileAllCompleted.complete());
    reconcileAllCompleted.future().compose(v -> context.verify(() -> {
        assertThat(createdOrUpdated, is(new HashSet(asList("new-tls-user", "existing-tls-user", "new-scram-sha-user", "existing-scram-sha-user"))));
        assertThat(deleted, is(new HashSet(asList("quota-user", "second-deleted-user", "deleted-scram-sha-user"))));
        async.flag();
    }));
}
Also used : LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) Secret(io.fabric8.kubernetes.api.model.Secret) Promise(io.vertx.core.Promise) Checkpoint(io.vertx.junit5.Checkpoint) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) Reconciliation(io.strimzi.operator.common.Reconciliation) KafkaUserStatus(io.strimzi.api.kafka.model.status.KafkaUserStatus) KafkaUser(io.strimzi.api.kafka.model.KafkaUser) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

Secret (io.fabric8.kubernetes.api.model.Secret)6 KafkaUser (io.strimzi.api.kafka.model.KafkaUser)6 KafkaUserStatus (io.strimzi.api.kafka.model.status.KafkaUserStatus)6 Reconciliation (io.strimzi.operator.common.Reconciliation)6 CrdOperator (io.strimzi.operator.common.operator.resource.CrdOperator)6 SecretOperator (io.strimzi.operator.common.operator.resource.SecretOperator)6 Promise (io.vertx.core.Promise)6 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)4 Checkpoint (io.vertx.junit5.Checkpoint)4 HashSet (java.util.HashSet)4 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)4 Test (org.junit.jupiter.api.Test)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)2 KafkaUserList (io.strimzi.api.kafka.KafkaUserList)2 KafkaUserQuotas (io.strimzi.api.kafka.model.KafkaUserQuotas)2 KafkaUserSpec (io.strimzi.api.kafka.model.KafkaUserSpec)2 CertManager (io.strimzi.certs.CertManager)2 AbstractOperator (io.strimzi.operator.common.AbstractOperator)2 MicrometerMetricsProvider (io.strimzi.operator.common.MicrometerMetricsProvider)2