Skip to content

Commit

Permalink
Use SSA to patch Kafka resource annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Oct 15, 2024
1 parent 34e6eb9 commit 2c83d2a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.fabric8.kubernetes.client.informers.cache.Cache;
import io.strimzi.api.ResourceAnnotations;
import io.strimzi.api.kafka.model.kafka.Kafka;
import io.strimzi.api.kafka.model.kafka.KafkaBuilder;
import io.strimzi.api.kafka.model.kafka.KafkaStatus;
import io.strimzi.api.kafka.model.kafka.listener.GenericKafkaListener;
import io.strimzi.api.kafka.model.kafka.listener.GenericKafkaListenerConfiguration;
Expand Down Expand Up @@ -154,16 +155,30 @@ public KafkaCluster patchCluster(String id, KafkaCluster cluster) {
Kafka resource = kafkaContext.resource();

if (resource != null) {
var annotations = resource.getMetadata().getAnnotations();
var paused = cluster.reconciliationPaused();
var paused = Optional.ofNullable(cluster.reconciliationPaused())
.map(Object::toString)
.orElse(null);

var patch = new KafkaBuilder()
.withNewMetadata()
.withNamespace(resource.getMetadata().getNamespace())
.withName(resource.getMetadata().getName())
.withAnnotations(resource.getMetadata().getAnnotations())
.endMetadata()
.withSpec(resource.getSpec());

if (paused != null) {
annotations.put(ResourceAnnotations.ANNO_STRIMZI_IO_PAUSE_RECONCILIATION, paused.toString());
patch.editMetadata()
.addToAnnotations(ResourceAnnotations.ANNO_STRIMZI_IO_PAUSE_RECONCILIATION, paused)
.endMetadata();
} else {
annotations.remove(ResourceAnnotations.ANNO_STRIMZI_IO_PAUSE_RECONCILIATION);
patch.editMetadata()
.removeFromAnnotations(ResourceAnnotations.ANNO_STRIMZI_IO_PAUSE_RECONCILIATION)
.endMetadata();
}

resource = client.resource(resource).patch();
resource = client.resource(patch.build()).forceConflicts().serverSideApply();

return toKafkaCluster(resource);
} else {
throw new BadRequestException("Kafka cluster is not associated with a Strimzi Kafka resource");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,8 @@ void testPatchClusterReconciliationPaused(Boolean paused) {
.get();

assertEquals(paused.toString(), kafkaCR.getMetadata().getAnnotations().get(ResourceAnnotations.ANNO_STRIMZI_IO_PAUSE_RECONCILIATION));
// Test custom annotation was untouched
assertEquals("value-1", kafkaCR.getMetadata().getAnnotations().get("x-custom-annotation"));

whenRequesting(req -> req
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
Expand All @@ -788,6 +790,8 @@ void testPatchClusterReconciliationPaused(Boolean paused) {
.get();

assertNull(kafkaCR.getMetadata().getAnnotations().get(ResourceAnnotations.ANNO_STRIMZI_IO_PAUSE_RECONCILIATION));
// Test custom annotation was untouched
assertEquals("value-1", kafkaCR.getMetadata().getAnnotations().get("x-custom-annotation"));
}

// Helper methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public Kafka buildKafkaResource(String name, String id, URI bootstrapServers, Ka
.withNewMetadata()
.withNamespace("default")
.withName(name)
.addToAnnotations("x-custom-annotation", "value-1")
.endMetadata()
.withNewSpec()
.withNewKafka()
Expand Down

0 comments on commit 2c83d2a

Please sign in to comment.