All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.streamthoughts.jikkou.kafka.reconciler.AdminClientConsumerGroupController Maven / Gradle / Ivy

The newest version!
/*
 * SPDX-License-Identifier: Apache-2.0
 * Copyright (c) The original authors
 *
 * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
 */
package io.streamthoughts.jikkou.kafka.reconciler;

import static io.streamthoughts.jikkou.core.ReconciliationMode.DELETE;

import io.streamthoughts.jikkou.core.ReconciliationContext;
import io.streamthoughts.jikkou.core.annotation.SupportedResource;
import io.streamthoughts.jikkou.core.extension.ContextualExtension;
import io.streamthoughts.jikkou.core.extension.ExtensionContext;
import io.streamthoughts.jikkou.core.models.change.ResourceChange;
import io.streamthoughts.jikkou.core.reconciler.ChangeExecutor;
import io.streamthoughts.jikkou.core.reconciler.ChangeHandler;
import io.streamthoughts.jikkou.core.reconciler.ChangeResult;
import io.streamthoughts.jikkou.core.reconciler.Controller;
import io.streamthoughts.jikkou.core.reconciler.annotations.ControllerConfiguration;
import io.streamthoughts.jikkou.kafka.ApiVersions;
import io.streamthoughts.jikkou.kafka.change.consumer.ConsumerGroupChangeComputer;
import io.streamthoughts.jikkou.kafka.change.consumer.ConsumerGroupChangeDescription;
import io.streamthoughts.jikkou.kafka.change.consumer.DeleteConsumerGroupHandler;
import io.streamthoughts.jikkou.kafka.internals.admin.AdminClientContext;
import io.streamthoughts.jikkou.kafka.internals.admin.AdminClientContextFactory;
import io.streamthoughts.jikkou.kafka.models.V1KafkaConsumerGroup;
import io.streamthoughts.jikkou.kafka.reconciler.service.KafkaConsumerGroupService;
import java.util.Collection;
import java.util.List;
import org.apache.kafka.clients.admin.AdminClient;
import org.jetbrains.annotations.NotNull;

@SupportedResource(type = V1KafkaConsumerGroup.class)
@SupportedResource(apiVersion = ApiVersions.KAFKA_V1BETA1, kind = "KafkaConsumerGroupChange")
@ControllerConfiguration(
        supportedModes = {DELETE}
)
public final class AdminClientConsumerGroupController
        extends ContextualExtension
        implements Controller {

    private AdminClientContextFactory adminClientContextFactory;

    /**
     * Creates a new {@link AdminClientConsumerGroupController} instance.
     * CLI requires any empty constructor.
     */
    public AdminClientConsumerGroupController() {
        super();
    }

    /**
     * Creates a new {@link AdminClientConsumerGroupController} instance with the specified {@link AdminClientContext}.
     *
     * @param adminClientContextFactory the {@link AdminClientContextFactory} to use for acquiring a new {@link AdminClientContext}.
     */
    public AdminClientConsumerGroupController(final @NotNull AdminClientContextFactory adminClientContextFactory) {
        this.adminClientContextFactory = adminClientContextFactory;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void init(@NotNull ExtensionContext context) {
        super.init(context);
        if (adminClientContextFactory == null) {
            this.adminClientContextFactory = new AdminClientContextFactory(context.appConfiguration());
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List execute(@NotNull ChangeExecutor executor,
                                      @NotNull ReconciliationContext context) {

        try (AdminClientContext clientContext = adminClientContextFactory.createAdminClientContext()) {
            AdminClient adminClient = clientContext.getAdminClient();
            List> handlers = List.of(
                    new DeleteConsumerGroupHandler(adminClient),
                    new ChangeHandler.None<>(ConsumerGroupChangeDescription::new)
            );
            return executor.applyChanges(handlers);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List plan(@NotNull Collection resources,
                                     @NotNull ReconciliationContext context) {

        // Get the expected Consumer Groups.
        List expectedState = resources
                .stream()
                .filter(context.selector()::apply)
                .map(resource -> resource.withStatus(null))
                .toList();

        // Get the Consumer Group ID.
        List consumerGroupsIds = expectedState.stream()
                .map(resource -> resource.getMetadata().getName())
                .distinct()
                .toList();

        try (AdminClientContext clientContext = adminClientContextFactory.createAdminClientContext()) {
            KafkaConsumerGroupService service = new KafkaConsumerGroupService(clientContext.getAdminClient());

            // Get the actual Consumer Groups.
            List actualStates = service.listConsumerGroups(consumerGroupsIds, false)
                    .getItems()
                    .stream()
                    .filter(context.selector()::apply)
                    .map(resource -> resource.withStatus(null))
                    .toList();

            ConsumerGroupChangeComputer changeComputer = new ConsumerGroupChangeComputer();
            return changeComputer.computeChanges(actualStates, expectedState);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy