diff --git a/deployment/src/main/java/io/quarkiverse/helm/deployment/HelmProcessor.java b/deployment/src/main/java/io/quarkiverse/helm/deployment/HelmProcessor.java index c85c685..affa1cd 100644 --- a/deployment/src/main/java/io/quarkiverse/helm/deployment/HelmProcessor.java +++ b/deployment/src/main/java/io/quarkiverse/helm/deployment/HelmProcessor.java @@ -77,6 +77,15 @@ public class HelmProcessor { private static final String SPLIT = ":"; private static final String PROPERTIES_CONFIG_SOURCE = "PropertiesConfigSource"; private static final String YAML_CONFIG_SOURCE = "YamlConfigSource"; + private static final HelmChartBuildItem.Deserializer DESERIALIZER = path -> { + final Class type = switch (path.getFileName().toString()) { + case "Chart.yaml" -> Chart.class; + case "values.yaml" -> Map.class; + case "values.schema.json" -> ValuesSchema.class; + default -> throw new IllegalArgumentException("Unsupported path: " + path); + }; + return Serialization.unmarshal(Files.readString(path), type); + }; // Lazy loaded when calling `isBuildTimeProperty(xxx)`. private static Set buildProperties; @@ -465,51 +474,11 @@ private List getConfigReferencesFromSession(String deploymentTa } private static HelmChartBuildItem read(Path dir) { - try { - Path chartYamlPath = dir.resolve("Chart.yaml"); - Path valuesYamlPath = dir.resolve("values.yaml"); - Path valuesSchemaPath = dir.resolve("values.schema.json"); - Path templatesDir = dir.resolve("templates"); - Path notesPath = templatesDir.resolve("NOTES.txt"); - Path readmePath = dir.resolve("README.md"); - - Chart chart = Serialization.unmarshal(Files.readString(chartYamlPath), Chart.class); - @SuppressWarnings("unchecked") - Map> values = Serialization.unmarshal(Files.readString(valuesYamlPath), Map.class); - ValuesSchema valuesSchema = Serialization.unmarshal(Files.readString(valuesSchemaPath), ValuesSchema.class); - - Map templates = new HashMap<>(); - if (Files.isDirectory(templatesDir)) { - Files.list(templatesDir).forEach(templatePath -> { - try { - if (!Files.isDirectory(templatePath)) { - templates.put(templatePath.getFileName().toString(), Files.readString(templatePath)); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - }); - } - - Optional notes = Files.exists(notesPath) ? Optional.of(Files.readString(notesPath)) : Optional.empty(); - Optional readme = Files.exists(readmePath) ? Optional.of(Files.readString(readmePath)) : Optional.empty(); - Path targetDir = dir.getParent(); - Path helmDir = targetDir.getParent(); - String deploymentTarget = helmDir.relativize(targetDir).getFileName().toString(); - - return HelmChartBuildItem.builder() - .withDeploymentTarget(deploymentTarget) - .withChart(chart) - .withValues(values) - .withValuesSchema(valuesSchema) - .withTemplates(templates) - .withNotes(notes) - .withReadme(readme) - .build(); - - } catch (IOException e) { - throw new RuntimeException("Failed to read HelmChartBuildItem from file system", e); - } + final var helmChartBuilder = HelmChartBuildItem.readAsBuilder(dir, DESERIALIZER); + Path targetDir = dir.getParent(); + Path helmDir = targetDir.getParent(); + String deploymentTarget = helmDir.relativize(targetDir).getFileName().toString(); + return helmChartBuilder.withDeploymentTarget(deploymentTarget).build(); } } diff --git a/spi/src/main/java/io/quarkiverse/helm/spi/HelmChartBuildItem.java b/spi/src/main/java/io/quarkiverse/helm/spi/HelmChartBuildItem.java index c280018..d2b6198 100644 --- a/spi/src/main/java/io/quarkiverse/helm/spi/HelmChartBuildItem.java +++ b/spi/src/main/java/io/quarkiverse/helm/spi/HelmChartBuildItem.java @@ -90,6 +90,14 @@ public static Builder builder() { } public static HelmChartBuildItem read(Path dir, Function deserializer) { + return readAsBuilder(dir, path -> deserializer.apply(Files.readString(path))).build(); + } + + public interface Deserializer { + Object deserialize(Path path) throws IOException; + } + + public static HelmChartBuildItem.Builder readAsBuilder(Path dir, Deserializer deserializer) { try { Path chartYamlPath = dir.resolve("Chart.yaml"); Path valuesYamlPath = dir.resolve("values.yaml"); @@ -98,11 +106,11 @@ public static HelmChartBuildItem read(Path dir, Function deseria Path notesPath = templatesDir.resolve("NOTES.txt"); Path readmePath = dir.resolve("README.md"); - Chart chart = (Chart) deserializer.apply(Files.readString(chartYamlPath)); + Chart chart = (Chart) deserializer.deserialize(chartYamlPath); @SuppressWarnings("unchecked") Map> values = (Map>) deserializer - .apply(Files.readString(valuesYamlPath)); - ValuesSchema valuesSchema = (ValuesSchema) deserializer.apply(Files.readString(valuesSchemaPath)); + .deserialize(valuesYamlPath); + ValuesSchema valuesSchema = (ValuesSchema) deserializer.deserialize(valuesSchemaPath); Map templates = new HashMap<>(); if (Files.isDirectory(templatesDir)) { @@ -126,8 +134,7 @@ public static HelmChartBuildItem read(Path dir, Function deseria .withValuesSchema(valuesSchema) .withTemplates(templates) .withNotes(notes) - .withReadme(readme) - .build(); + .withReadme(readme); } catch (IOException e) { throw new RuntimeException("Failed to read HelmChartBuildItem from file system", e);