Skip to content

Commit

Permalink
Added upload-dependencies mojo to upload chart dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Delametter committed Feb 21, 2024
1 parent 3e8f8df commit 1ae4d5c
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 57 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ and disables the auto-detection feature:
- `helm:template` Locally render templates
- `helm:dry-run` simulates an install
- `helm:upload` upload charts via HTTP PUT
- `helm:upload-dependencies` uploads dependency charts via HTTP PUT
- `helm:registry-login` login into docker registry
- `helm:registry-logout` login from docker registry
- `helm:push` push charts to OCI (docker registry)
Expand All @@ -260,8 +261,8 @@ and disables the auto-detection feature:

Parameter | Type | User Property | Required | Description
--- | --- | --- | --- | ---
`<chartDirectory>` | string | helm.chartDirectory | true | root directory of your charts
`<chartVersion>` | string | helm.chartVersion | true | Version of the charts. The version have to be in the [SEMVER-Format](https://semver.org/), required by helm.
`<chartDirectory>` | string | helm.chartDirectory | false | root directory of your charts
`<chartVersion>` | string | helm.chartVersion | false | Version of the charts. The version have to be in the [SEMVER-Format](https://semver.org/), required by helm.
`<appVersion>` | string | helm.appVersion | false | The version of the app. This needn't be SemVer.
`<helmDownloadUrl>` | string | helm.downloadUrl | false | URL to download helm. Leave empty to autodetect URL based upon OS and architecture.
`<helmDownloadUser>` | string | helm.downloadUser | false | Username used to authenticate while downloading helm binary package
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@
<dependencies>

<!-- maven -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/io/kokuwa/maven/helm/AbstractChartDirectoryMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.kokuwa.maven.helm;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.MatchPatterns;

import lombok.Setter;

@Setter
public abstract class AbstractChartDirectoryMojo extends AbstractHelmMojo {

/**
* Root directory of your charts.
*
* @since 1.0
*/
@Parameter(property = "helm.chartDirectory", required = true)
protected File chartDirectory;

/**
* List of chart directories to exclude.
*
* @since 1.0
*/
@Parameter(property = "helm.excludes")
protected String[] excludes;

List<Path> getChartDirectories() throws MojoExecutionException {

List<String> exclusions = new ArrayList<>();
if (excludes != null) {
exclusions.addAll(Arrays.asList(excludes));
}
exclusions.addAll(FileUtils.getDefaultExcludesAsList());
MatchPatterns exclusionPatterns = MatchPatterns.from(exclusions);

try (Stream<Path> files = Files.walk(chartDirectory.toPath(), FileVisitOption.FOLLOW_LINKS)) {
List<Path> chartDirectories = files
.filter(p -> p.getFileName().toString().equalsIgnoreCase("chart.yaml"))
.map(Path::getParent)
.filter(p -> !exclusionPatterns.matches(p.toString(), false))
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
if (chartDirectories.isEmpty()) {
getLog().warn("No Charts detected - no Chart.yaml files found below " + chartDirectory);
}
return chartDirectories;
} catch (IOException e) {
throw new MojoExecutionException("Unable to scan chart directory at " + chartDirectory, e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

/** Base class for dependency build and update mojos. */
@Setter
public abstract class AbstractDependencyMojo extends AbstractHelmMojo {
public abstract class AbstractDependencyMojo extends AbstractChartDirectoryMojo {

/**
* Controls whether a local path chart should be used for a chart dependency. When set to <code>true</code>, chart
Expand Down
53 changes: 3 additions & 50 deletions src/main/java/io/kokuwa/maven/helm/AbstractHelmMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
import java.io.File;
import java.io.IOException;
import java.net.PasswordAuthentication;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand All @@ -23,8 +19,6 @@
import org.apache.maven.project.MavenProjectHelper;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.MatchPatterns;
import org.codehaus.plexus.util.Os;
import org.codehaus.plexus.util.StringUtils;
import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
Expand Down Expand Up @@ -98,23 +92,7 @@ public abstract class AbstractHelmMojo extends AbstractMojo {
* @since 1.0
*/
@Parameter(property = "helm.outputDirectory", defaultValue = "${project.build.directory}/helm/repo")
private File outputDirectory;

/**
* List of chart directories to exclude.
*
* @since 1.0
*/
@Parameter(property = "helm.excludes")
private String[] excludes;

/**
* Root directory of your charts.
*
* @since 1.0
*/
@Parameter(property = "helm.chartDirectory", required = true)
private File chartDirectory;
protected File outputDirectory;

/**
* Version of the charts. The version have to be in the SEMVER-Format (https://semver.org/), required by helm.
Expand All @@ -130,15 +108,15 @@ public abstract class AbstractHelmMojo extends AbstractMojo {
* @since 1.10
*/
@Parameter
private HelmRepository uploadRepoStable;
protected HelmRepository uploadRepoStable;

/**
* Upload repository for snapshot charts (determined by version postfix 'SNAPSHOT').
*
* @since 1.10
*/
@Parameter
private HelmRepository uploadRepoSnapshot;
protected HelmRepository uploadRepoSnapshot;

/**
* Version of helm to download.
Expand Down Expand Up @@ -352,31 +330,6 @@ HelmExecutable helm() throws MojoExecutionException {
.flag("repository-config", repositoryConfig);
}

List<Path> getChartDirectories() throws MojoExecutionException {

List<String> exclusions = new ArrayList<>();
if (excludes != null) {
exclusions.addAll(Arrays.asList(excludes));
}
exclusions.addAll(FileUtils.getDefaultExcludesAsList());
MatchPatterns exclusionPatterns = MatchPatterns.from(exclusions);

try (Stream<Path> files = Files.walk(chartDirectory.toPath(), FileVisitOption.FOLLOW_LINKS)) {
List<Path> chartDirectories = files
.filter(p -> p.getFileName().toString().equalsIgnoreCase("chart.yaml"))
.map(Path::getParent)
.filter(p -> !exclusionPatterns.matches(p.toString(), false))
.sorted(Comparator.reverseOrder())
.collect(Collectors.toList());
if (chartDirectories.isEmpty()) {
getLog().warn("No Charts detected - no Chart.yaml files found below " + chartDirectory);
}
return chartDirectories;
} catch (IOException e) {
throw new MojoExecutionException("Unable to scan chart directory at " + chartDirectory, e);
}
}

List<Path> getChartArchives() throws MojoExecutionException {
try (Stream<Path> files = Files.walk(getOutputDirectory())) {
return files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import lombok.Setter;

@Setter
public abstract class AbstractHelmWithValueOverrideMojo extends AbstractHelmMojo {
public abstract class AbstractHelmWithValueOverrideMojo extends AbstractChartDirectoryMojo {

/**
* Additional values to set.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/kokuwa/maven/helm/PackageMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
@Mojo(name = "package", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true)
@Setter
public class PackageMojo extends AbstractHelmMojo {
public class PackageMojo extends AbstractChartDirectoryMojo {

/**
* Set this to <code>true</code> to skip invoking package goal.
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/io/kokuwa/maven/helm/UploadDependencies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.kokuwa.maven.helm;

import java.io.File;
import java.nio.file.Paths;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.kokuwa.maven.helm.pojo.HelmRepository;
import io.kokuwa.maven.helm.pojo.RepoType;

@Mojo(name = "upload-dependencies")
public class UploadDependencies extends UploadMojo {

private static final Logger logger = LoggerFactory.getLogger(UploadDependencies.class);

@Parameter(property = "url", required = true)
private String url;

@Parameter(property = "id", required = true)
private String id;

@Parameter(property = "type", required = true)
private RepoType type;

@Parameter(defaultValue = "${project}", required = true, readonly = true)
private MavenProject project;

@Override
public void execute() throws MojoExecutionException {
String projectPath = project.getBasedir().getAbsolutePath();
if (!isHelmChart(projectPath)) {
return;
}
uploadRepoStable = createHelmRepository(id, url, type);
uploadRepoSnapshot = createHelmRepository(id, url, type);
outputDirectory = new File(Paths.get(projectPath, "charts").toString());
if (!outputDirectory.exists()) {
return;
}
try {
super.execute();
} catch (Exception e) {
logger.info(String.format("%s:%s", e.getMessage(), e.getCause()));
}
}

private boolean isHelmChart(String path) {
File chartFile = new File(Paths.get(path, "Chart.yaml").toString());
return chartFile.exists();
}

private HelmRepository createHelmRepository(String id, String url, RepoType type) {
HelmRepository repo = new HelmRepository();
repo.setName(id);
repo.setUrl(url);
repo.setType(type);
return repo;
}

}
1 change: 0 additions & 1 deletion src/main/java/io/kokuwa/maven/helm/UploadMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public void execute() throws MojoExecutionException {
getLog().info("Skip upload");
return;
}

getLog().info("Uploading to " + getHelmUploadUrl() + "\n");
for (Path chart : getChartArchives()) {
getLog().info("Uploading " + chart + "...");
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/io/kokuwa/maven/helm/junit/MojoExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;

import io.kokuwa.maven.helm.AbstractChartDirectoryMojo;
import io.kokuwa.maven.helm.AbstractHelmMojo;

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -113,7 +114,10 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte

// preconfigure

mojo.setChartDirectory(new File("src/test/resources/simple")); // set some sane defaults for tests
if (mojo instanceof AbstractChartDirectoryMojo) {
((AbstractChartDirectoryMojo) mojo)
.setChartDirectory(new File("src/test/resources/simple")); // set some sane defaults for tests
}
mojo.setHelmExecutableDirectory(determineHelmExecutableDirectory().toFile()); // avoid download helm
mojo.setHelmVersion("3.12.0"); // avoid github api

Expand Down

0 comments on commit 1ae4d5c

Please sign in to comment.