From ce12e0284b075c4a4fcc75bb92619982e30395ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Silva?= Date: Sat, 13 Jun 2026 00:45:09 +0200 Subject: [PATCH] fix(msk): populate currentBrokerSoftwareInfo in cluster describe response (#1238) * fix(msk): populate currentBrokerSoftwareInfo in cluster describe response * fix(msk): map provisioned.kafkaVersion in CreateClusterV2 Addresses review feedback: V2 create requests carry kafkaVersion under provisioned, not at the top level, so it was always falling back to the default. Adds integration test coverage for the V1 path, V2 with provisioned.kafkaVersion, and V2 default fallback. * fix(msk): update RedpandaManagerTest for MskCluster 3-arg constructor --- .../floci/services/msk/MskController.java | 8 +- .../floci/services/msk/MskService.java | 8 +- .../msk/model/BrokerSoftwareInfo.java | 20 +++++ .../floci/services/msk/model/MskCluster.java | 9 ++- .../msk/MskControllerIntegrationTest.java | 74 +++++++++++++++++++ .../floci/services/msk/MskServiceTest.java | 14 ++++ .../services/msk/RedpandaManagerTest.java | 2 +- 7 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/services/msk/model/BrokerSoftwareInfo.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/msk/MskControllerIntegrationTest.java diff --git a/src/main/java/io/github/hectorvent/floci/services/msk/MskController.java b/src/main/java/io/github/hectorvent/floci/services/msk/MskController.java index af920df7b9..84e3ad8fdd 100644 --- a/src/main/java/io/github/hectorvent/floci/services/msk/MskController.java +++ b/src/main/java/io/github/hectorvent/floci/services/msk/MskController.java @@ -23,16 +23,20 @@ public class MskController { @Path("/v1/clusters") public Response createCluster(Map request) { String clusterName = (String) request.get("clusterName"); - MskCluster cluster = mskService.createCluster(clusterName); + String kafkaVersion = (String) request.get("kafkaVersion"); + MskCluster cluster = mskService.createCluster(clusterName, kafkaVersion); return Response.ok(Map.of("clusterArn", cluster.getClusterArn(), "clusterName", cluster.getClusterName(), "state", cluster.getState())).build(); } @POST @Path("/api/v2/clusters") + @SuppressWarnings("unchecked") public Response createClusterV2(Map request) { // Simple mapping to V1 for now String clusterName = (String) request.get("clusterName"); - MskCluster cluster = mskService.createCluster(clusterName); + Map provisioned = (Map) request.get("provisioned"); + String kafkaVersion = provisioned != null ? (String) provisioned.get("kafkaVersion") : null; + MskCluster cluster = mskService.createCluster(clusterName, kafkaVersion); return Response.ok(Map.of("clusterArn", cluster.getClusterArn(), "clusterName", cluster.getClusterName(), "state", cluster.getState())).build(); } diff --git a/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java b/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java index 0d1605a176..b12441980e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java +++ b/src/main/java/io/github/hectorvent/floci/services/msk/MskService.java @@ -29,6 +29,7 @@ import java.util.concurrent.TimeUnit; public class MskService { private static final Logger LOG = Logger.getLogger(MskService.class); + private static final String DEFAULT_KAFKA_VERSION = "3.6.0"; private final StorageBackend storage; private final EmulatorConfig config; private final RegionResolver regionResolver; @@ -60,6 +61,10 @@ public class MskService { } public MskCluster createCluster(String clusterName) { + return createCluster(clusterName, DEFAULT_KAFKA_VERSION); + } + + public MskCluster createCluster(String clusterName, String kafkaVersion) { if (storage.scan(k -> true).stream().anyMatch(c -> c.getClusterName().equals(clusterName))) { throw new AwsException("ConflictException", "Cluster already exists: " + clusterName, 409); } @@ -67,7 +72,8 @@ public class MskService { String accountId = regionResolver.getAccountId(); String clusterArn = AwsArnUtils.Arn.of("kafka", config.defaultRegion(), accountId, "cluster/" + clusterName + "/" + java.util.UUID.randomUUID()).toString(); - MskCluster cluster = new MskCluster(clusterArn, clusterName); + String resolvedKafkaVersion = (kafkaVersion == null || kafkaVersion.isBlank()) ? DEFAULT_KAFKA_VERSION : kafkaVersion; + MskCluster cluster = new MskCluster(clusterArn, clusterName, resolvedKafkaVersion); cluster.setAccountId(accountId); cluster.setVolumeId(String.format("%06x", new SecureRandom().nextInt(0xFFFFFF))); diff --git a/src/main/java/io/github/hectorvent/floci/services/msk/model/BrokerSoftwareInfo.java b/src/main/java/io/github/hectorvent/floci/services/msk/model/BrokerSoftwareInfo.java new file mode 100644 index 0000000000..5779e9eb2a --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/msk/model/BrokerSoftwareInfo.java @@ -0,0 +1,20 @@ +package io.github.hectorvent.floci.services.msk.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.quarkus.runtime.annotations.RegisterForReflection; + +@RegisterForReflection +public class BrokerSoftwareInfo { + + @JsonProperty("kafkaVersion") + private String kafkaVersion; + + public BrokerSoftwareInfo() {} + + public BrokerSoftwareInfo(String kafkaVersion) { + this.kafkaVersion = kafkaVersion; + } + + public String getKafkaVersion() { return kafkaVersion; } + public void setKafkaVersion(String kafkaVersion) { this.kafkaVersion = kafkaVersion; } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/msk/model/MskCluster.java b/src/main/java/io/github/hectorvent/floci/services/msk/model/MskCluster.java index ed081def37..b43592f507 100644 --- a/src/main/java/io/github/hectorvent/floci/services/msk/model/MskCluster.java +++ b/src/main/java/io/github/hectorvent/floci/services/msk/model/MskCluster.java @@ -33,6 +33,9 @@ public class MskCluster { @JsonProperty("zookeeperConnectString") private String zookeeperConnectString; + @JsonProperty("currentBrokerSoftwareInfo") + private BrokerSoftwareInfo currentBrokerSoftwareInfo; + // Internal field, not directly in AWS response but needed for GetBootstrapBrokers private String bootstrapBrokers; @@ -47,7 +50,7 @@ public class MskCluster { public MskCluster() {} - public MskCluster(String clusterArn, String clusterName) { + public MskCluster(String clusterArn, String clusterName, String kafkaVersion) { this.clusterArn = clusterArn; this.clusterName = clusterName; this.state = ClusterState.CREATING; @@ -55,6 +58,7 @@ public class MskCluster { this.currentVersion = "K3V6I1"; // Example version this.numberOfBrokerNodes = 1; this.zookeeperConnectString = "localhost:2181"; // Mock ZK + this.currentBrokerSoftwareInfo = new BrokerSoftwareInfo(kafkaVersion); } public String getClusterArn() { return clusterArn; } @@ -81,6 +85,9 @@ public class MskCluster { public String getZookeeperConnectString() { return zookeeperConnectString; } public void setZookeeperConnectString(String zookeeperConnectString) { this.zookeeperConnectString = zookeeperConnectString; } + public BrokerSoftwareInfo getCurrentBrokerSoftwareInfo() { return currentBrokerSoftwareInfo; } + public void setCurrentBrokerSoftwareInfo(BrokerSoftwareInfo currentBrokerSoftwareInfo) { this.currentBrokerSoftwareInfo = currentBrokerSoftwareInfo; } + public String getBootstrapBrokers() { return bootstrapBrokers; } public void setBootstrapBrokers(String bootstrapBrokers) { this.bootstrapBrokers = bootstrapBrokers; } diff --git a/src/test/java/io/github/hectorvent/floci/services/msk/MskControllerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/msk/MskControllerIntegrationTest.java new file mode 100644 index 0000000000..ae6dbf947f --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/msk/MskControllerIntegrationTest.java @@ -0,0 +1,74 @@ +package io.github.hectorvent.floci.services.msk; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +@QuarkusTest +class MskControllerIntegrationTest { + + @Test + void createClusterV1EchoesRequestedKafkaVersion() { + String clusterArn = given() + .contentType("application/json") + .body(""" + {"clusterName": "v1-version-test", "kafkaVersion": "3.5.1"} + """) + .when() + .post("/v1/clusters") + .then() + .statusCode(200) + .extract().path("clusterArn"); + + given() + .when() + .get("/v1/clusters/{clusterArn}", clusterArn) + .then() + .statusCode(200) + .body("clusterInfo.currentBrokerSoftwareInfo.kafkaVersion", equalTo("3.5.1")); + } + + @Test + void createClusterV2EchoesRequestedKafkaVersionFromProvisioned() { + String clusterArn = given() + .contentType("application/json") + .body(""" + {"clusterName": "v2-version-test", "provisioned": {"kafkaVersion": "3.5.1"}} + """) + .when() + .post("/api/v2/clusters") + .then() + .statusCode(200) + .extract().path("clusterArn"); + + given() + .when() + .get("/api/v2/clusters/{clusterArn}", clusterArn) + .then() + .statusCode(200) + .body("clusterInfo.currentBrokerSoftwareInfo.kafkaVersion", equalTo("3.5.1")); + } + + @Test + void createClusterV2WithoutProvisionedFallsBackToDefaultKafkaVersion() { + String clusterArn = given() + .contentType("application/json") + .body(""" + {"clusterName": "v2-default-version-test"} + """) + .when() + .post("/api/v2/clusters") + .then() + .statusCode(200) + .extract().path("clusterArn"); + + given() + .when() + .get("/api/v2/clusters/{clusterArn}", clusterArn) + .then() + .statusCode(200) + .body("clusterInfo.currentBrokerSoftwareInfo.kafkaVersion", equalTo("3.6.0")); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/msk/MskServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/msk/MskServiceTest.java index 4879a556cb..5ad3b7d301 100644 --- a/src/test/java/io/github/hectorvent/floci/services/msk/MskServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/msk/MskServiceTest.java @@ -51,6 +51,20 @@ class MskServiceTest { assertTrue(cluster.getClusterArn().contains("test-cluster")); } + @Test + void createClusterPopulatesCurrentBrokerSoftwareInfoWithDefaultKafkaVersion() { + MskCluster cluster = mskService.createCluster("test-cluster"); + assertNotNull(cluster.getCurrentBrokerSoftwareInfo()); + assertEquals("3.6.0", cluster.getCurrentBrokerSoftwareInfo().getKafkaVersion()); + } + + @Test + void createClusterEchoesRequestedKafkaVersion() { + MskCluster cluster = mskService.createCluster("test-cluster", "3.5.1"); + assertNotNull(cluster.getCurrentBrokerSoftwareInfo()); + assertEquals("3.5.1", cluster.getCurrentBrokerSoftwareInfo().getKafkaVersion()); + } + @Test void describeCluster() { MskCluster created = mskService.createCluster("test-cluster"); diff --git a/src/test/java/io/github/hectorvent/floci/services/msk/RedpandaManagerTest.java b/src/test/java/io/github/hectorvent/floci/services/msk/RedpandaManagerTest.java index d53bb56e4a..9ca39204ff 100644 --- a/src/test/java/io/github/hectorvent/floci/services/msk/RedpandaManagerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/msk/RedpandaManagerTest.java @@ -53,7 +53,7 @@ class RedpandaManagerTest { } private static MskCluster newCluster() { - return new MskCluster("arn:aws:kafka:us-east-1:000000000000:cluster/test-cluster/abc", "test-cluster"); + return new MskCluster("arn:aws:kafka:us-east-1:000000000000:cluster/test-cluster/abc", "test-cluster", "3.6.0"); } private int startFakeAdminServer() throws Exception {