use of io.strimzi.api.kafka.model.KafkaTopic in project strimzi by strimzi.
the class TopicOperatorIT method testKafkaTopicWithOwnerRef.
// TODO: What happens if we create and then change labels to the resource predicate isn't matched any more
// What then happens if we change labels back?
@Test
public void testKafkaTopicWithOwnerRef() throws InterruptedException, ExecutionException, TimeoutException {
String topicName = "test-kafka-topic-with-owner-ref-1";
// this CM is created to be the owner of the KafkaTopic we're about to create.
String cmName = "hodor";
HashMap<String, String> cmData = new HashMap<>();
cmData.put("strimzi", "rulez");
kubeClient.configMaps().inNamespace(NAMESPACE).create(new ConfigMapBuilder().withNewMetadata().withName(cmName).withNamespace(NAMESPACE).endMetadata().withApiVersion("v1").withData(cmData).build());
String uid = kubeClient.configMaps().inNamespace(NAMESPACE).withName(cmName).get().getMetadata().getUid();
ObjectMeta metadata = new ObjectMeta();
OwnerReference or = new OwnerReferenceBuilder().withName(cmName).withApiVersion("v1").withController(false).withBlockOwnerDeletion(false).withUid(uid).withKind("ConfigMap").build();
metadata.getOwnerReferences().add(or);
Map<String, String> annos = new HashMap<>();
annos.put("iam", "groot");
Map<String, String> lbls = new HashMap<>();
lbls.put("iam", "root");
metadata.setAnnotations(annos);
metadata.setLabels(lbls);
// create topic and test OR, labels, annotations
Topic topic = new Topic.Builder(topicName, 1, (short) 1, emptyMap(), metadata).build();
KafkaTopic topicResource = TopicSerialization.toTopicResource(topic, labels);
createKafkaTopicResource(topicResource);
assertThat(operation().inNamespace(NAMESPACE).withName(topicName).get().getMetadata().getOwnerReferences().size(), is(1));
assertThat(operation().inNamespace(NAMESPACE).withName(topicName).get().getMetadata().getOwnerReferences().get(0).getUid(), is(uid));
assertThat(operation().inNamespace(NAMESPACE).withName(topicName).get().getMetadata().getAnnotations().size(), is(1));
assertThat(operation().inNamespace(NAMESPACE).withName(topicName).get().getMetadata().getAnnotations().get("iam"), is("groot"));
assertThat(operation().inNamespace(NAMESPACE).withName(topicName).get().getMetadata().getLabels().size(), is(2));
assertThat(operation().inNamespace(NAMESPACE).withName(topicName).get().getMetadata().getLabels().get("iam"), is("root"));
// edit kafka topic
topicName = "test-kafka-topic-with-owner-ref-2";
Topic topic2 = new Topic.Builder(topicName, 1, (short) 1, emptyMap(), metadata).build();
KafkaTopic topicResource2 = TopicSerialization.toTopicResource(topic2, labels);
topicResource = TopicSerialization.toTopicResource(topic2, labels);
topicResource.getMetadata().getAnnotations().put("han", "solo");
createKafkaTopicResource(topicResource2);
assertThat(operation().inNamespace(NAMESPACE).withName(topicName).get().getMetadata().getOwnerReferences().get(0).getUid(), is(uid));
assertThat(operation().inNamespace(NAMESPACE).withName(topicName).get().getMetadata().getAnnotations().size(), is(2));
assertThat(operation().inNamespace(NAMESPACE).withName(topicName).get().getMetadata().getAnnotations().get("iam"), is("groot"));
assertThat(operation().inNamespace(NAMESPACE).withName(topicName).get().getMetadata().getAnnotations().get("han"), is("solo"));
// edit k8s topic
topicName = "test-kafka-topic-with-owner-ref-3";
Topic topic3 = new Topic.Builder(topicName, 1, (short) 1, emptyMap(), metadata).build();
topic3.getMetadata().getLabels().put("stan", "lee");
topicResource = TopicSerialization.toTopicResource(topic3, labels);
createKafkaTopicResource(topicResource);
assertThat(operation().inNamespace(NAMESPACE).withName(topicName).get().getMetadata().getOwnerReferences().get(0).getUid(), is(uid));
assertThat(operation().inNamespace(NAMESPACE).withName(topicName).get().getMetadata().getLabels().size(), is(3));
assertThat(operation().inNamespace(NAMESPACE).withName(topicName).get().getMetadata().getLabels().get("stan"), is("lee"));
assertThat(operation().inNamespace(NAMESPACE).withName(topicName).get().getMetadata().getLabels().get("iam"), is("root"));
}
use of io.strimzi.api.kafka.model.KafkaTopic in project strimzi by strimzi.
the class TopicOperatorIT method testKafkaTopicModifiedNameChanged.
@Test
public void testKafkaTopicModifiedNameChanged() throws Exception {
// create the topicResource
String topicName = "test-kafkatopic-modified-name-changed";
KafkaTopic topicResource = createKafkaTopicResource(topicName);
// now change the topicResource
String changedName = topicName.toUpperCase(Locale.ENGLISH);
LOGGER.info("Changing Topic Resource spec.topicName from {} to {}", topicName, changedName);
KafkaTopic changedTopic = new KafkaTopicBuilder(operation().inNamespace(NAMESPACE).withName(topicResource.getMetadata().getName()).get()).editOrNewSpec().withTopicName(changedName).endSpec().build();
operation().inNamespace(NAMESPACE).withName(topicResource.getMetadata().getName()).replace(changedTopic);
// We expect this to cause a warning event
waitForEvent(topicResource, "Kafka topics cannot be renamed, but KafkaTopic's spec.topicName has changed.", TopicOperator.EventType.WARNING);
}
use of io.strimzi.api.kafka.model.KafkaTopic in project strimzi by strimzi.
the class TopicOperatorIT method testKafkaTopicModifiedRetentionChanged.
@Test
public void testKafkaTopicModifiedRetentionChanged() throws Exception {
// create the topic
String topicName = "test-kafkatopic-modified-retention-changed";
KafkaTopic topicResource = createKafkaTopicResource(topicName);
String expectedValue = alterTopicConfigInKube(topicResource.getMetadata().getName(), "retention.ms", currentValue -> Integer.toString(Integer.parseInt(currentValue) + 1));
awaitTopicConfigInKafka(topicName, "retention.ms", expectedValue);
}
use of io.strimzi.api.kafka.model.KafkaTopic in project strimzi by strimzi.
the class TopicOperatorIT method testReconcile.
@Test
public void testReconcile() throws InterruptedException, ExecutionException, TimeoutException {
String topicName = "test-reconcile";
Topic topic = new Topic.Builder(topicName, 1, (short) 1, emptyMap()).build();
KafkaTopic topicResource = TopicSerialization.toTopicResource(topic, labels);
String resourceName = topicResource.getMetadata().getName();
operation().inNamespace(NAMESPACE).create(topicResource);
// Wait for the resource to be created
waitFor(() -> {
KafkaTopic createdResource = operation().inNamespace(NAMESPACE).withName(resourceName).get();
LOGGER.info("Polled kafkatopic {} waiting for creation", resourceName);
// modify resource
if (createdResource != null) {
createdResource.getSpec().setPartitions(2);
operation().inNamespace(NAMESPACE).withName(resourceName).patch(createdResource);
}
return createdResource != null;
}, "Expected the kafkatopic to have been created by now");
// trigger an immediate reconcile, while topic operator is dealing with resource modification
session.topicOperator.reconcileAllTopics("periodic");
// Wait for the topic to be created
waitForTopicInKafka(topicName);
assertStatusReady(topicName);
}
use of io.strimzi.api.kafka.model.KafkaTopic in project strimzi by strimzi.
the class TopicOperatorIT method testInvalidConfig.
@Test
public void testInvalidConfig() throws Exception {
String topicName = "topic-invalid-config";
String expectedMessage = "Invalid config value for resource ConfigResource(type=TOPIC, name='" + topicName + "'): Invalid value x for configuration min.insync.replicas: Not a number of type INT";
String resourceName = createTopic(topicName, new NewTopic(topicName, 2, (short) 1));
KafkaTopic changedTopic = new KafkaTopicBuilder(operation().inNamespace(NAMESPACE).withName(resourceName).get()).editOrNewSpec().addToConfig("min.insync.replicas", "x").endSpec().build();
KafkaTopic replaced = operation().inNamespace(NAMESPACE).withName(resourceName).replace(changedTopic);
assertStatusNotReady(topicName, InvalidRequestException.class, expectedMessage);
// Now modify Kafka-side to cause another reconciliation: We want the same status.
alterTopicConfigInKafka(topicName, "compression.type", value -> "snappy".equals(value) ? "lz4" : "snappy");
// Wait for a periodic reconciliation
Thread.sleep(RECONCILIATION_INTERVAL + 10_000);
assertStatusNotReady(topicName, InvalidRequestException.class, expectedMessage);
}
Aggregations