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

io.streamthoughts.jikkou.kafka.change.quota.KafkaClientQuotaChangeComputer 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.change.quota;

import static io.streamthoughts.jikkou.kafka.adapters.V1KafkaClientQuotaConfigsAdapter.toClientQuotaConfigs;

import io.streamthoughts.jikkou.core.models.change.GenericResourceChange;
import io.streamthoughts.jikkou.core.models.change.ResourceChange;
import io.streamthoughts.jikkou.core.models.change.ResourceChangeSpec;
import io.streamthoughts.jikkou.core.models.change.StateChange;
import io.streamthoughts.jikkou.core.reconciler.Change;
import io.streamthoughts.jikkou.core.reconciler.Operation;
import io.streamthoughts.jikkou.core.reconciler.change.ChangeComputer;
import io.streamthoughts.jikkou.core.reconciler.change.ResourceChangeComputer;
import io.streamthoughts.jikkou.core.reconciler.change.ResourceChangeFactory;
import io.streamthoughts.jikkou.kafka.change.topics.TopicChangeComputer;
import io.streamthoughts.jikkou.kafka.model.KafkaClientQuotaEntity;
import io.streamthoughts.jikkou.kafka.models.V1KafkaClientQuota;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.kafka.common.quota.ClientQuotaEntity;

public final class KafkaClientQuotaChangeComputer extends ResourceChangeComputer {

    /**
     * Creates a new {@link TopicChangeComputer} instance.
     *
     * @param isLimitDeletionEnabled {@code true} to delete orphaned limits.
     */
    public KafkaClientQuotaChangeComputer(boolean isLimitDeletionEnabled) {
        super(
                object -> new ClientQuotaEntity(object.getSpec().getType().toEntities(object.getSpec().getEntity())),
                new KafkaClientQuotaChangeFactory(isLimitDeletionEnabled)
        );
    }

    public static final class KafkaClientQuotaChangeFactory extends ResourceChangeFactory {

        private final boolean isLimitDeletionEnabled;

        public KafkaClientQuotaChangeFactory(boolean isLimitDeletionEnabled) {
            this.isLimitDeletionEnabled = isLimitDeletionEnabled;
        }

        @Override
        public ResourceChange createChangeForCreate(ClientQuotaEntity key, V1KafkaClientQuota after) {
            List changes = ChangeComputer.computeChanges(
                    Collections.emptyMap(),
                    toClientQuotaConfigs(after.getSpec().getConfigs()),
                    isLimitDeletionEnabled
            );
            return buildChange(Operation.CREATE, after, changes);
        }

        @Override
        public ResourceChange createChangeForDelete(ClientQuotaEntity key,
                                                    V1KafkaClientQuota before) {
            List changes = ChangeComputer.computeChanges(
                    toClientQuotaConfigs(before.getSpec().getConfigs()),
                    Collections.emptyMap(),
                    isLimitDeletionEnabled
            );

            return buildChange(Operation.DELETE, before, changes);
        }

        @Override
        public ResourceChange createChangeForUpdate(ClientQuotaEntity key,
                                                    V1KafkaClientQuota before,
                                                    V1KafkaClientQuota after) {
            List changes = ChangeComputer.computeChanges(
                    toClientQuotaConfigs(before.getSpec().getConfigs()),
                    toClientQuotaConfigs(after.getSpec().getConfigs()),
                    isLimitDeletionEnabled
            );

            Operation operation = Change.computeOperation(changes);
            return buildChange(operation, before, changes);
        }

        private static ResourceChange buildChange(Operation op,
                                                  V1KafkaClientQuota object,
                                                  List changes) {

            KafkaClientQuotaEntity entity = object.getSpec().getEntity();
            Map entities = object
                    .getSpec()
                    .getType()
                    .toEntities(entity);
            return GenericResourceChange
                    .builder(V1KafkaClientQuota.class)
                    .withMetadata(object.getMetadata())
                    .withSpec(ResourceChangeSpec
                            .builder()
                            .withOperation(op)
                            .withData(entities)
                            .withChanges(changes)
                            .build()
                    )
                    .build();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy