InitWriter { public boolean writeRack() { Map<String, String> nodeLabels = client.nodes().withName(config.getNodeName()).get().getMetadata().getLabels(); log.info("NodeLabels = {}", nodeLabels); String rackId = nodeLabels.get(config.getRackTopologyKey()); log.info("Rack: {} = {}", config.getRackTopologyKey(), rackId); if (rackId == null) { log.error("Node {} doesn't have the label {} for getting the rackid", config.getNodeName(), config.getRackTopologyKey()); return false; } return write(FILE_RACK_ID, rackId); } InitWriter(KubernetesClient client, InitWriterConfig config); boolean writeRack(); boolean writeExternalAddress();  }
@Test public void testWriteRackId() throws IOException { File kafkaFolder = new File(tempDir.getPath() + "opt/kafka"); String rackFolder = kafkaFolder.getAbsolutePath() + "/rack"; new File(rackFolder).mkdirs(); Map<String, String> envVars = new HashMap<>(InitWriterTest.envVars); envVars.put(InitWriterConfig.INIT_FOLDER, rackFolder); InitWriterConfig config = InitWriterConfig.fromMap(envVars); KubernetesClient client = mockKubernetesClient(config.getNodeName(), labels, Collections.EMPTY_LIST); InitWriter writer = new InitWriter(client, config); assertThat(writer.writeRack(), is(true)); assertThat(readFile(rackFolder + "/rack.id"), is("eu-zone1")); }
@Test public void testWriteRackFailWithMissingKubernetesZoneLabel() { Map<String, String> labels = new HashMap<>(InitWriterTest.labels); labels.remove("failure-domain.beta.kubernetes.io/zone"); InitWriterConfig config = InitWriterConfig.fromMap(envVars); KubernetesClient client = mockKubernetesClient(config.getNodeName(), labels, Collections.EMPTY_LIST); InitWriter writer = new InitWriter(client, config); assertThat(writer.writeRack(), is(false)); }
@Test public void testWriteRackFailsWhenInitFolderDoesNotExist() { Map<String, String> envVars = new HashMap<>(InitWriterTest.envVars); envVars.put(InitWriterConfig.INIT_FOLDER, "/no-folder"); InitWriterConfig config = InitWriterConfig.fromMap(envVars); KubernetesClient client = mockKubernetesClient(config.getNodeName(), labels, addresses); InitWriter writer = new InitWriter(client, config); assertThat(writer.writeRack(), is(false)); }