Search in sources :

Example 1 with JsonDiff

use of io.fabric8.zjsonpatch.JsonDiff in project strimzi by strimzi.

the class KafkaBrokerConfigurationDiff method diff.

/**
 * Computes diff between two maps. Entries in IGNORABLE_PROPERTIES are skipped
 * @param brokerId id of compared broker
 * @param desired desired configuration, may be null if the related ConfigMap does not exist yet or no changes are required
 * @param brokerConfigs current configuration
 * @param configModel default configuration for {@code kafkaVersion} of broker
 * @return Collection of AlterConfigOp containing all entries which were changed from current in desired configuration
 */
private Collection<AlterConfigOp> diff(int brokerId, String desired, Config brokerConfigs, Map<String, ConfigModel> configModel) {
    if (brokerConfigs == null || desired == null) {
        return Collections.emptyList();
    }
    Map<String, String> currentMap;
    Collection<AlterConfigOp> updatedCE = new ArrayList<>();
    currentMap = brokerConfigs.entries().stream().collect(Collectors.toMap(ConfigEntry::name, configEntry -> configEntry.value() == null ? "null" : configEntry.value()));
    OrderedProperties orderedProperties = new OrderedProperties();
    orderedProperties.addStringPairs(desired);
    Map<String, String> desiredMap = orderedProperties.asMap();
    fillPlaceholderValue(desiredMap, Integer.toString(brokerId));
    JsonNode source = patchMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true).valueToTree(currentMap);
    JsonNode target = patchMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true).valueToTree(desiredMap);
    JsonNode jsonDiff = JsonDiff.asJson(source, target);
    for (JsonNode d : jsonDiff) {
        String pathValue = d.get("path").asText();
        String pathValueWithoutSlash = pathValue.substring(1);
        Optional<ConfigEntry> optEntry = brokerConfigs.entries().stream().filter(configEntry -> configEntry.name().equals(pathValueWithoutSlash)).findFirst();
        String op = d.get("op").asText();
        if (optEntry.isPresent()) {
            ConfigEntry entry = optEntry.get();
            if ("remove".equals(op)) {
                removeProperty(configModel, updatedCE, pathValueWithoutSlash, entry);
            } else if ("replace".equals(op)) {
                // entry is in the current, desired is updated value
                updateOrAdd(entry.name(), configModel, desiredMap, updatedCE);
            }
        } else {
            if ("add".equals(op)) {
                // entry is not in the current, it is added
                updateOrAdd(pathValueWithoutSlash, configModel, desiredMap, updatedCE);
            }
        }
        if ("remove".equals(op)) {
            // there is a lot of properties set by default - not having them in desired causes very noisy log output
            LOGGER.traceCr(reconciliation, "Kafka Broker {} Config Differs : {}", brokerId, d);
            LOGGER.traceCr(reconciliation, "Current Kafka Broker Config path {} has value {}", pathValueWithoutSlash, lookupPath(source, pathValue));
            LOGGER.traceCr(reconciliation, "Desired Kafka Broker Config path {} has value {}", pathValueWithoutSlash, lookupPath(target, pathValue));
        } else {
            LOGGER.debugCr(reconciliation, "Kafka Broker {} Config Differs : {}", brokerId, d);
            LOGGER.debugCr(reconciliation, "Current Kafka Broker Config path {} has value {}", pathValueWithoutSlash, lookupPath(source, pathValue));
            LOGGER.debugCr(reconciliation, "Desired Kafka Broker Config path {} has value {}", pathValueWithoutSlash, lookupPath(target, pathValue));
        }
    }
    return updatedCE;
}
Also used : Scope(io.strimzi.kafka.config.model.Scope) AbstractJsonDiff(io.strimzi.operator.common.operator.resource.AbstractJsonDiff) Config(org.apache.kafka.clients.admin.Config) ReconciliationLogger(io.strimzi.operator.common.ReconciliationLogger) OrderedProperties(io.strimzi.operator.common.model.OrderedProperties) Collection(java.util.Collection) KafkaVersion(io.strimzi.operator.cluster.model.KafkaVersion) ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) JsonDiff(io.fabric8.zjsonpatch.JsonDiff) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ConfigModel(io.strimzi.kafka.config.model.ConfigModel) Reconciliation(io.strimzi.operator.common.Reconciliation) AlterConfigOp(org.apache.kafka.clients.admin.AlterConfigOp) PatchUtils.patchMapper(io.fabric8.kubernetes.client.internal.PatchUtils.patchMapper) KafkaConfiguration(io.strimzi.operator.cluster.model.KafkaConfiguration) Map(java.util.Map) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) ArrayList(java.util.ArrayList) AlterConfigOp(org.apache.kafka.clients.admin.AlterConfigOp) OrderedProperties(io.strimzi.operator.common.model.OrderedProperties) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 2 with JsonDiff

use of io.fabric8.zjsonpatch.JsonDiff in project strimzi-kafka-operator by strimzi.

the class KafkaBrokerConfigurationDiff method diff.

/**
 * Computes diff between two maps. Entries in IGNORABLE_PROPERTIES are skipped
 * @param brokerId id of compared broker
 * @param desired desired configuration, may be null if the related ConfigMap does not exist yet or no changes are required
 * @param brokerConfigs current configuration
 * @param configModel default configuration for {@code kafkaVersion} of broker
 * @return Collection of AlterConfigOp containing all entries which were changed from current in desired configuration
 */
private Collection<AlterConfigOp> diff(int brokerId, String desired, Config brokerConfigs, Map<String, ConfigModel> configModel) {
    if (brokerConfigs == null || desired == null) {
        return Collections.emptyList();
    }
    Map<String, String> currentMap;
    Collection<AlterConfigOp> updatedCE = new ArrayList<>();
    currentMap = brokerConfigs.entries().stream().collect(Collectors.toMap(ConfigEntry::name, configEntry -> configEntry.value() == null ? "null" : configEntry.value()));
    OrderedProperties orderedProperties = new OrderedProperties();
    orderedProperties.addStringPairs(desired);
    Map<String, String> desiredMap = orderedProperties.asMap();
    fillPlaceholderValue(desiredMap, Integer.toString(brokerId));
    JsonNode source = patchMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true).valueToTree(currentMap);
    JsonNode target = patchMapper().configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true).valueToTree(desiredMap);
    JsonNode jsonDiff = JsonDiff.asJson(source, target);
    for (JsonNode d : jsonDiff) {
        String pathValue = d.get("path").asText();
        String pathValueWithoutSlash = pathValue.substring(1);
        Optional<ConfigEntry> optEntry = brokerConfigs.entries().stream().filter(configEntry -> configEntry.name().equals(pathValueWithoutSlash)).findFirst();
        String op = d.get("op").asText();
        if (optEntry.isPresent()) {
            ConfigEntry entry = optEntry.get();
            if ("remove".equals(op)) {
                removeProperty(configModel, updatedCE, pathValueWithoutSlash, entry);
            } else if ("replace".equals(op)) {
                // entry is in the current, desired is updated value
                updateOrAdd(entry.name(), configModel, desiredMap, updatedCE);
            }
        } else {
            if ("add".equals(op)) {
                // entry is not in the current, it is added
                updateOrAdd(pathValueWithoutSlash, configModel, desiredMap, updatedCE);
            }
        }
        if ("remove".equals(op)) {
            // there is a lot of properties set by default - not having them in desired causes very noisy log output
            LOGGER.traceCr(reconciliation, "Kafka Broker {} Config Differs : {}", brokerId, d);
            LOGGER.traceCr(reconciliation, "Current Kafka Broker Config path {} has value {}", pathValueWithoutSlash, lookupPath(source, pathValue));
            LOGGER.traceCr(reconciliation, "Desired Kafka Broker Config path {} has value {}", pathValueWithoutSlash, lookupPath(target, pathValue));
        } else {
            LOGGER.debugCr(reconciliation, "Kafka Broker {} Config Differs : {}", brokerId, d);
            LOGGER.debugCr(reconciliation, "Current Kafka Broker Config path {} has value {}", pathValueWithoutSlash, lookupPath(source, pathValue));
            LOGGER.debugCr(reconciliation, "Desired Kafka Broker Config path {} has value {}", pathValueWithoutSlash, lookupPath(target, pathValue));
        }
    }
    return updatedCE;
}
Also used : Scope(io.strimzi.kafka.config.model.Scope) AbstractJsonDiff(io.strimzi.operator.common.operator.resource.AbstractJsonDiff) Config(org.apache.kafka.clients.admin.Config) ReconciliationLogger(io.strimzi.operator.common.ReconciliationLogger) OrderedProperties(io.strimzi.operator.common.model.OrderedProperties) Collection(java.util.Collection) KafkaVersion(io.strimzi.operator.cluster.model.KafkaVersion) ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) JsonDiff(io.fabric8.zjsonpatch.JsonDiff) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ConfigModel(io.strimzi.kafka.config.model.ConfigModel) Reconciliation(io.strimzi.operator.common.Reconciliation) AlterConfigOp(org.apache.kafka.clients.admin.AlterConfigOp) PatchUtils.patchMapper(io.fabric8.kubernetes.client.internal.PatchUtils.patchMapper) KafkaConfiguration(io.strimzi.operator.cluster.model.KafkaConfiguration) Map(java.util.Map) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) ArrayList(java.util.ArrayList) AlterConfigOp(org.apache.kafka.clients.admin.AlterConfigOp) OrderedProperties(io.strimzi.operator.common.model.OrderedProperties) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)2 PatchUtils.patchMapper (io.fabric8.kubernetes.client.internal.PatchUtils.patchMapper)2 JsonDiff (io.fabric8.zjsonpatch.JsonDiff)2 ConfigModel (io.strimzi.kafka.config.model.ConfigModel)2 Scope (io.strimzi.kafka.config.model.Scope)2 KafkaConfiguration (io.strimzi.operator.cluster.model.KafkaConfiguration)2 KafkaVersion (io.strimzi.operator.cluster.model.KafkaVersion)2 Reconciliation (io.strimzi.operator.common.Reconciliation)2 ReconciliationLogger (io.strimzi.operator.common.ReconciliationLogger)2 OrderedProperties (io.strimzi.operator.common.model.OrderedProperties)2 AbstractJsonDiff (io.strimzi.operator.common.operator.resource.AbstractJsonDiff)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Pattern (java.util.regex.Pattern)2 Collectors (java.util.stream.Collectors)2 AlterConfigOp (org.apache.kafka.clients.admin.AlterConfigOp)2