Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> buildProperties;

Expand Down Expand Up @@ -465,51 +474,11 @@ private List<ConfigReference> 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<String, Map<String, Object>> values = Serialization.unmarshal(Files.readString(valuesYamlPath), Map.class);
ValuesSchema valuesSchema = Serialization.unmarshal(Files.readString(valuesSchemaPath), ValuesSchema.class);

Map<String, String> 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<String> notes = Files.exists(notesPath) ? Optional.of(Files.readString(notesPath)) : Optional.empty();
Optional<String> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ public static Builder builder() {
}

public static HelmChartBuildItem read(Path dir, Function<String, Object> 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spite of you added this interface to avoid adding the io.dekorate.utils.Serialization dependency in the spi module, in practical, this method is useless unless you provide one implementation of this interface which can be hard to understand at first.

Instead, we should provide an utility maybe in the deployment module to read the HelmChartBuildItem instead of adding this method in here and avoid complicating the spi module.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 2 implementations of that interface in this PR 😉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, what I disliked it more :)
Let's wait for @iocanel's comment before moving forward.
But these methods should be moved away from the builditem class.

try {
Path chartYamlPath = dir.resolve("Chart.yaml");
Path valuesYamlPath = dir.resolve("values.yaml");
Expand All @@ -98,11 +106,11 @@ public static HelmChartBuildItem read(Path dir, Function<String, Object> 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<String, Map<String, Object>> values = (Map<String, Map<String, Object>>) deserializer
.apply(Files.readString(valuesYamlPath));
ValuesSchema valuesSchema = (ValuesSchema) deserializer.apply(Files.readString(valuesSchemaPath));
.deserialize(valuesYamlPath);
ValuesSchema valuesSchema = (ValuesSchema) deserializer.deserialize(valuesSchemaPath);

Map<String, String> templates = new HashMap<>();
if (Files.isDirectory(templatesDir)) {
Expand All @@ -126,8 +134,7 @@ public static HelmChartBuildItem read(Path dir, Function<String, Object> 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);
Expand Down