Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch Helm dependencies before packaging #1067

Merged
merged 1 commit into from
Sep 30, 2022
Merged
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 @@ -18,6 +18,7 @@

import static io.dekorate.helm.util.HelmTarArchiver.createTarBall;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -35,7 +36,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -57,6 +57,7 @@
import io.dekorate.helm.model.Maintainer;
import io.dekorate.helm.util.HelmExpressionParser;
import io.dekorate.project.Project;
import io.dekorate.utils.Exec;
import io.dekorate.utils.Maps;
import io.dekorate.utils.Serialization;
import io.dekorate.utils.Strings;
Expand Down Expand Up @@ -129,16 +130,19 @@ public Map<String, String> writeHelmFiles(Session session, Project project,
valuesByProfile));
artifacts.putAll(createChartYaml(helmConfig, project, outputDir));
artifacts.putAll(createValuesYaml(helmConfig, valuesReferences, inputDir, outputDir, prodValues, valuesByProfile));
if (helmConfig.isCreateTarFile()) {
artifacts.putAll(createTarball(helmConfig, project, outputDir, artifacts, valuesByProfile.keySet()));
}

// To follow Helm file structure standards:
artifacts.putAll(createEmptyChartFolder(helmConfig, outputDir));
artifacts.putAll(addNotesIntoTemplatesFolder(helmConfig, inputDir, outputDir));
artifacts.putAll(addResourceIfExists(helmConfig, LICENSE, inputDir, outputDir));
artifacts.putAll(addResourceIfExists(helmConfig, README, inputDir, outputDir));

// Final step: packaging
if (helmConfig.isCreateTarFile()) {
fetchDependencies(helmConfig, outputDir);
artifacts.putAll(createTarball(helmConfig, project, outputDir, artifacts));
}

} catch (IOException e) {
throw new RuntimeException("Error writing resources", e);
}
Expand All @@ -147,6 +151,22 @@ public Map<String, String> writeHelmFiles(Session session, Project project,
return artifacts;
}

private void fetchDependencies(HelmChartConfig helmConfig, Path outputDir) {
if (helmConfig.getDependencies() != null && helmConfig.getDependencies().length > 0) {
Path chartFolder = getChartOutputDir(helmConfig, outputDir);
ByteArrayOutputStream out = new ByteArrayOutputStream();
boolean success = Exec.inPath(chartFolder)
.redirectingOutput(out)
.commands("helm", "dependency", "build");

if (success) {
LOGGER.info("Dependencies successfully fetched");
} else {
throw new RuntimeException("Error fetching Helm dependencies. Cause: " + new String(out.toByteArray()));
}
}
}

private void validateHelmConfig(HelmChartConfig helmConfig) {
if (Strings.isNullOrEmpty(helmConfig.getName())) {
throw new RuntimeException("Helm Chart name is required!");
Expand Down Expand Up @@ -309,7 +329,7 @@ private boolean startWithDependencyPrefix(String property, io.dekorate.helm.conf
}

private Map<String, String> createTarball(HelmChartConfig helmConfig, Project project, Path outputDir,
Map<String, String> artifacts, Set<String> profiles) throws IOException {
Map<String, String> artifacts) throws IOException {

File tarballFile = outputDir.resolve(String.format("%s-%s-%s.%s",
helmConfig.getName(), getVersion(helmConfig, project), getHelmClassifier(artifacts), helmConfig.getExtension()))
Expand All @@ -319,16 +339,17 @@ private Map<String, String> createTarball(HelmChartConfig helmConfig, Project pr

Path helmSources = getChartOutputDir(helmConfig, outputDir);

List<File> yamls = new ArrayList<>();
yamls.add(helmSources.resolve(CHART_FILENAME).toFile());
yamls.add(helmSources.resolve(VALUES + YAML).toFile());
for (String profile : profiles) {
yamls.add(helmSources.resolve(VALUES + "." + profile + YAML).toFile());
List<File> files = new ArrayList<>();
for (String filePath : artifacts.keySet()) {
File file = new File(filePath);
if (file.isDirectory()) {
files.addAll(Arrays.asList(file.listFiles()));
} else {
files.add(file);
}
}

yamls.addAll(listYamls(helmSources.resolve(TEMPLATES)));

createTarBall(tarballFile, helmSources.toFile(), yamls, helmConfig.getExtension(),
createTarBall(tarballFile, helmSources.toFile(), files, helmConfig.getExtension(),
tae -> tae.setName(String.format("%s/%s", helmConfig.getName(), tae.getName())));

return Collections.singletonMap(tarballFile.toString(), null);
Expand All @@ -346,6 +367,7 @@ private Map<String, String> processSourceFiles(HelmChartConfig helmConfig, Path
List<ConfigReference> valuesReferences, Map<String, Object> prodValues,
Map<String, Map<String, Object>> valuesByProfile) throws IOException {

Map<String, String> templates = new HashMap<>();
Path templatesDir = getChartOutputDir(helmConfig, outputDir).resolve(TEMPLATES);
Files.createDirectories(templatesDir);
List<Map<Object, Object>> resources = replaceValuesInYamls(helmConfig, generatedFiles, valuesReferences, prodValues,
Expand All @@ -363,9 +385,10 @@ private Map<String, String> processSourceFiles(HelmChartConfig helmConfig, Path
.replaceAll("\\\\\\n(\\s)*\\\\(\\s)*}}", " }}");

writeFile(adaptedString, targetFile);
templates.put(targetFile.toString(), adaptedString);
}

return Collections.emptyMap();
return templates;
}

private List<Map<Object, Object>> replaceValuesInYamls(HelmChartConfig helmConfig,
Expand Down
8 changes: 4 additions & 4 deletions docs/documentation/helm.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,10 @@ Let's now see how you can add this configuration using the Dekorate Helm extensi

`application.properties`:
```
dekorate.helm.dependencies.postgresql.alias=database
dekorate.helm.dependencies.postgresql.name=postgresql
dekorate.helm.dependencies.postgresql.version=11.6.22
dekorate.helm.dependencies.postgresql.repository=https://charts.bitnami.com/bitnami
dekorate.helm.dependencies[0].postgresql.alias=database
dekorate.helm.dependencies[0].postgresql.name=postgresql
dekorate.helm.dependencies[0].postgresql.version=11.6.22
dekorate.helm.dependencies[0].postgresql.repository=https://charts.bitnami.com/bitnami

dekorate.helm.values[0].property=database.global.postgresql.auth.postgresPassword
dekorate.helm.values[0].value=secret
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
dekorate.helm.name=myChart
# Produce tar file
dekorate.helm.createTarFile=true
# Dependencies
dekorate.helm.dependencies[0].name=dependencyNameA
dekorate.helm.dependencies[0].version=0.0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,10 @@ public void shouldHelmManifestsBeGenerated() throws IOException {
assertNotNull(Main.class.getClassLoader().getResourceAsStream(CHART_OUTPUT_LOCATION + "/templates/deployment.yaml"));
assertNotNull(Main.class.getClassLoader().getResourceAsStream(CHART_OUTPUT_LOCATION + "/templates/ingress.yaml"));
assertNotNull(Main.class.getClassLoader().getResourceAsStream(CHART_OUTPUT_LOCATION + "/templates/service.yaml"));
// empty charts folder
// charts folder
assertNotNull(Main.class.getClassLoader().getResourceAsStream(CHART_OUTPUT_LOCATION + "/charts"));
// notes
assertNotNull(Main.class.getClassLoader().getResourceAsStream(CHART_OUTPUT_LOCATION + "/templates/NOTES.txt"));
// zip manifest
String zipName = String.format("META-INF/dekorate/helm/%s-%s-helm.tar.gz", chart.getName(), chart.getVersion());
assertNotNull(Main.class.getClassLoader().getResourceAsStream(zipName), "File '" + zipName + "' not found!");
// optional resources
assertNotNull(Main.class.getClassLoader().getResourceAsStream(CHART_OUTPUT_LOCATION + "/LICENSE"));
assertNotNull(Main.class.getClassLoader().getResourceAsStream(CHART_OUTPUT_LOCATION + "/README.md"));
Expand Down
72 changes: 72 additions & 0 deletions tests/issue-fetch-helm-dependencies/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>dekorate-tests</artifactId>
<groupId>io.dekorate</groupId>
<version>3.1-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

<groupId>io.dekorate</groupId>
<artifactId>issue-fetch-helm-dependencies</artifactId>
<name>Dekorate :: Tests :: Annotations :: Helm :: Fetch dependencies</name>
<description></description>

<dependencies>
<dependency>
<groupId>io.dekorate</groupId>
<artifactId>kubernetes-annotations</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.dekorate</groupId>
<artifactId>dekorate-spring-boot</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.dekorate</groupId>
<artifactId>helm-annotations</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${version.spring-boot}</version>
</dependency>

<!-- Testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${version.junit-jupiter}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${version.junit-jupiter}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<inherited>true</inherited>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${version.spring-boot}</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright 2018 The original authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.dekorate.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright 2018 The original authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/

package io.dekorate.example;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

private static final String HELLO = "hello world!";

@RequestMapping("/")
public String hello() {
return HELLO;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dekorate.helm.name=issue-fetch-deps
dekorate.helm.dependencies[0].alias=db
dekorate.helm.dependencies[0].name=postgresql
dekorate.helm.dependencies[0].version=11.9.1
dekorate.helm.dependencies[0].repository=https://charts.bitnami.com/bitnami
# Produce tar file: It needs to be enabled to fetch dependencies
dekorate.helm.createTarFile=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright 2018 The original authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/

package io.dekorate.example;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;

public class IssueHelmFetchDependencies {

private static final String CHART_NAME = "issue-fetch-deps";

@Test
public void shouldFetchDependencies() throws FileNotFoundException {
assertTrue(Stream.of(Paths.get("target", "helm", "kubernetes").toFile().listFiles())
.anyMatch(f -> f.getName().startsWith(CHART_NAME) && f.getName().endsWith(".tar.gz")));

assertNotNull(getResourceAsStream("charts/postgresql-11.6.22.tgz"));
}

private final InputStream getResourceAsStream(String file) throws FileNotFoundException {
return new FileInputStream(Paths.get("target", "helm", "kubernetes").resolve(CHART_NAME).resolve(file).toFile());
}
}
1 change: 1 addition & 0 deletions tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<module>issue-1009-probe-tcp-socket</module>
<module>feat-kubernetes-emptydir-volumes</module>
<module>issue-spring-boot-openshift-named-port</module>
<module>issue-fetch-helm-dependencies</module>
</modules>

</project>