Search in sources :

Example 26 with OrderedProperties

use of io.strimzi.operator.common.model.OrderedProperties 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 27 with OrderedProperties

use of io.strimzi.operator.common.model.OrderedProperties in project strimzi by strimzi.

the class TestConfigurationWithoutDefaults method testConfigurationStringWithDuplicates.

@ParallelTest
public void testConfigurationStringWithDuplicates() {
    String configuration = "var1=aaa" + LINE_SEPARATOR + "var2=bbb" + LINE_SEPARATOR + "var3=ccc" + LINE_SEPARATOR + "var2=ddd" + LINE_SEPARATOR;
    OrderedProperties expectedConfiguration = createWithDefaults("var3", "ccc", "var2", "ddd", "var1", "aaa");
    AbstractConfiguration config = new TestConfiguration(configuration);
    assertThat(config.asOrderedProperties(), is(expectedConfiguration));
}
Also used : OrderedProperties(io.strimzi.operator.common.model.OrderedProperties) ParallelTest(io.strimzi.test.annotations.ParallelTest)

Example 28 with OrderedProperties

use of io.strimzi.operator.common.model.OrderedProperties in project strimzi by strimzi.

the class TestConfigurationWithoutDefaults method testNonEmptyJson.

@ParallelTest
public void testNonEmptyJson() {
    JsonObject configuration = new JsonObject().put("var1", "aaa").put("var2", "bbb").put("var3", "ccc");
    OrderedProperties expectedConfiguration = createWithDefaults("var3", "ccc", "var2", "bbb", "var1", "aaa");
    AbstractConfiguration config = new TestConfiguration(configuration);
    assertThat(config.asOrderedProperties(), is(expectedConfiguration));
}
Also used : JsonObject(io.vertx.core.json.JsonObject) OrderedProperties(io.strimzi.operator.common.model.OrderedProperties) ParallelTest(io.strimzi.test.annotations.ParallelTest)

Example 29 with OrderedProperties

use of io.strimzi.operator.common.model.OrderedProperties in project strimzi by strimzi.

the class TestConfigurationWithoutDefaults method testConfigurationStringWithForbiddenKeys.

@ParallelTest
public void testConfigurationStringWithForbiddenKeys() {
    String configuration = "var1=aaa" + LINE_SEPARATOR + "var2=bbb" + LINE_SEPARATOR + "var3=ccc" + LINE_SEPARATOR + "forbidden.option=ddd" + LINE_SEPARATOR;
    OrderedProperties expectedConfiguration = createWithDefaults("var3", "ccc", "var2", "bbb", "var1", "aaa");
    AbstractConfiguration config = new TestConfiguration(configuration);
    assertThat(config.asOrderedProperties(), is(expectedConfiguration));
}
Also used : OrderedProperties(io.strimzi.operator.common.model.OrderedProperties) ParallelTest(io.strimzi.test.annotations.ParallelTest)

Example 30 with OrderedProperties

use of io.strimzi.operator.common.model.OrderedProperties in project strimzi by strimzi.

the class TestConfigurationWithoutDefaults method testJsonWithForbiddenKeys.

@ParallelTest
public void testJsonWithForbiddenKeys() {
    JsonObject configuration = new JsonObject().put("var1", "aaa").put("var2", "bbb").put("var3", "ccc").put("forbidden.option", "ddd");
    OrderedProperties expectedConfiguration = createWithDefaults("var3", "ccc", "var2", "bbb", "var1", "aaa");
    AbstractConfiguration config = new TestConfiguration(configuration);
    assertThat(config.asOrderedProperties(), is(expectedConfiguration));
}
Also used : JsonObject(io.vertx.core.json.JsonObject) OrderedProperties(io.strimzi.operator.common.model.OrderedProperties) ParallelTest(io.strimzi.test.annotations.ParallelTest)

Aggregations

OrderedProperties (io.strimzi.operator.common.model.OrderedProperties)46 ParallelTest (io.strimzi.test.annotations.ParallelTest)24 Map (java.util.Map)20 Reconciliation (io.strimzi.operator.common.Reconciliation)16 JsonObject (io.vertx.core.json.JsonObject)16 ArrayList (java.util.ArrayList)14 List (java.util.List)14 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)12 LabelSelectorBuilder (io.fabric8.kubernetes.api.model.LabelSelectorBuilder)12 LocalObjectReference (io.fabric8.kubernetes.api.model.LocalObjectReference)12 Collections (java.util.Collections)10 Collections.emptyMap (java.util.Collections.emptyMap)10 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)8 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 HostAlias (io.fabric8.kubernetes.api.model.HostAlias)8 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)8 PodSecurityContextBuilder (io.fabric8.kubernetes.api.model.PodSecurityContextBuilder)8 Quantity (io.fabric8.kubernetes.api.model.Quantity)8 Secret (io.fabric8.kubernetes.api.model.Secret)8 ServiceAccount (io.fabric8.kubernetes.api.model.ServiceAccount)8