Skip to content

Commit

Permalink
Add releaseName handling for install, uninstall and upgrade, fix #350
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Schnabel committed Feb 6, 2024
1 parent 838aa42 commit 922db36
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ Parameter | Type | User Property | Required | Description
`<kubeAsUser>` | string | helm.kubeAsUser | false | username to impersonate for the operation
`<kubeAsGroup>` | string | helm.kubeAsGroup | false | group to impersonate for the operation, this flag can be repeated to specify multiple groups
`<kubeToken>` | string | helm.kubeToken | false | bearer token used for authentication
`<releaseName>` | string | helm.releaseName | false | Name of the release for upgrade goal
`<releaseName>` | string | helm.releaseName | false | Name of the release to handle.
`<installForce>` | boolean | helm.install.force | false | Force resource updates through a replacement strategy.
`<installAtomic>` | boolean | helm.install.atomic | false | Set this to `true` to delete the installation on failure.
`<installTimeout>` | boolean | helm.upgrade.imeout | false | Time in seconds to wait for any individual Kubernetes operation during install process. The default is 300 seconds (from helm) if `installAtomic` is set to `true`.
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/io/kokuwa/maven/helm/AbstractHandleMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.kokuwa.maven.helm;

import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;

import lombok.Setter;
import lombok.Value;

/** Base class for dependency build and update mojos. */
@Setter
public abstract class AbstractHandleMojo extends AbstractHelmWithValueOverrideMojo {

/**
* Name of the release to handle.
*
* @since 6.4.0
*/
@Parameter(property = "helm.releaseName")
private String releaseName;

List<Chart> getCharts() throws MojoExecutionException {

List<Path> chartDirectories = getChartDirectories();
if (releaseName != null && chartDirectories.size() > 1) {
throw new MojoExecutionException("For multiple charts releaseName is not supported.");
}

return chartDirectories.stream()
.map(p -> new Chart(releaseName == null ? p.getFileName().toString() : releaseName, p))
.collect(Collectors.toList());
}

@Value
class Chart {
private final String releaseName;
private final Path directory;
}
}
13 changes: 6 additions & 7 deletions src/main/java/io/kokuwa/maven/helm/InstallMojo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.kokuwa.maven.helm;

import java.nio.file.Path;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand All @@ -19,7 +17,7 @@
*/
@Mojo(name = "install", defaultPhase = LifecyclePhase.DEPLOY, threadSafe = true)
@Setter
public class InstallMojo extends AbstractHelmWithValueOverrideMojo {
public class InstallMojo extends AbstractHandleMojo {

/**
* Helm command to execute.
Expand Down Expand Up @@ -79,12 +77,13 @@ public void execute() throws MojoExecutionException, MojoFailureException {
return;
}

for (Path chartDirectory : getChartDirectories()) {
getLog().info(String.format("\n\nPerform install for chart %s...", chartDirectory) +
for (Chart chart : getCharts()) {
getLog().info("Perform install for chart " + chart.getReleaseName() + " " +
(installAtomic ? " with atomic" : "") +
(installTimeout != null ? installTimeout + "s" : ""));
(installTimeout != null ? installTimeout + "s" : "") +
chart.getDirectory());
helm()
.arguments(action, chartDirectory.getFileName().toString(), chartDirectory)
.arguments(action, chart.getReleaseName(), chart.getDirectory())
.flag("atomic", installAtomic)
.flag("force", installForce)
.flag("plain-http", isPlainHttp(installPlainHttp))
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/io/kokuwa/maven/helm/UninstallMojo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.kokuwa.maven.helm;

import java.nio.file.Path;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand All @@ -18,7 +16,7 @@
*/
@Mojo(name = "uninstall", defaultPhase = LifecyclePhase.DEPLOY, threadSafe = true)
@Setter
public class UninstallMojo extends AbstractHelmWithValueOverrideMojo {
public class UninstallMojo extends AbstractHandleMojo {

/**
* Must be "background", "orphan", or "foreground". Selects the deletion cascading strategy for the dependents.
Expand Down Expand Up @@ -86,9 +84,9 @@ public void execute() throws MojoExecutionException {
return;
}

for (Path chartDirectory : getChartDirectories()) {
getLog().info("Perform uninstall for chart " + chartDirectory);
helm().arguments("uninstall", chartDirectory.getFileName().toString())
for (Chart charts : getCharts()) {
getLog().info("Perform uninstall for chart with name " + charts.getReleaseName());
helm().arguments("uninstall", charts.getReleaseName())
.flag("wait", uninstallWait)
.flag("timeout", uninstallTimeout != null ? uninstallTimeout + "s" : null)
.flag("cascade", uninstallCascade)
Expand Down
19 changes: 5 additions & 14 deletions src/main/java/io/kokuwa/maven/helm/UpgradeMojo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.kokuwa.maven.helm;

import java.nio.file.Path;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand All @@ -17,7 +15,7 @@
*/
@Mojo(name = "upgrade", defaultPhase = LifecyclePhase.DEPLOY, threadSafe = true)
@Setter
public class UpgradeMojo extends AbstractHelmWithValueOverrideMojo {
public class UpgradeMojo extends AbstractHandleMojo {

/**
* Set this to <code>true</code> to skip invoking upgrade goal.
Expand Down Expand Up @@ -74,13 +72,6 @@ public class UpgradeMojo extends AbstractHelmWithValueOverrideMojo {
*/
@Parameter(property = "helm.upgrade.plain-http")
private Boolean upgradePlainHttp;
/**
* Name of the release for upgrade goal.
*
* @since 6.4.0
*/
@Parameter(property = "helm.releaseName")
private String releaseName;

@Override
public void execute() throws MojoExecutionException {
Expand All @@ -90,15 +81,15 @@ public void execute() throws MojoExecutionException {
return;
}

for (Path chartDirectory : getChartDirectories()) {
getLog().info("Upgrading the chart " +
for (Chart chart : getCharts()) {
getLog().info("Upgrading the chart " + chart.getReleaseName() + " " +
(upgradeWithInstall ? "with install " : "") +
(upgradeAtomic ? "with atomic " : "") +
(upgradeTimeout != null ? upgradeTimeout + "s" : "") +
(upgradeDryRun ? "as dry run " : "") +
chartDirectory);
chart.getDirectory());
helm()
.arguments("upgrade", releaseName, chartDirectory)
.arguments("upgrade", chart.getReleaseName(), chart.getDirectory())
.flag("install", upgradeWithInstall)
.flag("dry-run", upgradeDryRun)
.flag("atomic", upgradeAtomic)
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/io/kokuwa/maven/helm/InstallMojoTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package io.kokuwa.maven.helm;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.File;

import org.apache.maven.plugin.MojoExecutionException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -80,6 +84,24 @@ void valuesFile(InstallMojo mojo) {
assertHelm(mojo, "install simple src/test/resources/simple --values bar.yaml");
}

@DisplayName("with release name")
@Test
void releaseName(InstallMojo mojo) {
mojo.setSkipInstall(false);
mojo.setReleaseName("foo");
assertHelm(mojo, "install foo src/test/resources/simple");
}

@DisplayName("with release name and multiple charts")
@Test
void releaseNameWithMultipleCharts(InstallMojo mojo) {
mojo.setSkipInstall(false);
mojo.setReleaseName("foo");
mojo.setChartDirectory(new File("src/test/resources/dependencies"));
String message = assertThrows(MojoExecutionException.class, mojo::execute).getMessage();
assertEquals("For multiple charts releaseName is not supported.", message);
}

@DisplayName("with dependencies")
@Test
void dependencies(InstallMojo mojo) {
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/io/kokuwa/maven/helm/UninstallMojoTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package io.kokuwa.maven.helm;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.File;

import org.apache.maven.plugin.MojoExecutionException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -68,4 +74,22 @@ void timeout(UninstallMojo mojo) {
mojo.setUninstallTimeout(41);
assertHelm(mojo, "uninstall simple --wait --timeout 41s");
}

@DisplayName("with release name")
@Test
void releaseName(UninstallMojo mojo) {
mojo.setSkipUninstall(false);
mojo.setReleaseName("foo");
assertHelm(mojo, "uninstall foo");
}

@DisplayName("with release name and multiple charts")
@Test
void releaseNameWithMultipleCharts(UninstallMojo mojo) {
mojo.setSkipUninstall(false);
mojo.setReleaseName("foo");
mojo.setChartDirectory(new File("src/test/resources/dependencies"));
String message = assertThrows(MojoExecutionException.class, mojo::execute).getMessage();
assertEquals("For multiple charts releaseName is not supported.", message);
}
}
35 changes: 29 additions & 6 deletions src/test/java/io/kokuwa/maven/helm/UpgradeMojoTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package io.kokuwa.maven.helm;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.File;

import org.apache.maven.plugin.MojoExecutionException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -79,27 +85,44 @@ void plainHttp(UpgradeMojo mojo) {

mojo.setPlainHttp(false);
mojo.setUpgradePlainHttp(null);
assertHelm(mojo, "upgrade src/test/resources/simple --install");
assertHelm(mojo, "upgrade simple src/test/resources/simple --install");

mojo.setPlainHttp(true);
mojo.setUpgradePlainHttp(null);
assertHelm(mojo, "upgrade src/test/resources/simple --install --plain-http");
assertHelm(mojo, "upgrade simple src/test/resources/simple --install --plain-http");

mojo.setPlainHttp(false);
mojo.setUpgradePlainHttp(true);
assertHelm(mojo, "upgrade src/test/resources/simple --install --plain-http");
assertHelm(mojo, "upgrade simple src/test/resources/simple --install --plain-http");

mojo.setPlainHttp(false);
mojo.setUpgradePlainHttp(false);
assertHelm(mojo, "upgrade src/test/resources/simple --install");
assertHelm(mojo, "upgrade simple src/test/resources/simple --install");
}

@DisplayName("with release name")
@Test
void releaseName(UpgradeMojo mojo) {
mojo.setSkipUpgrade(false);
mojo.setReleaseName("foo");
assertHelm(mojo, "upgrade foo src/test/resources/simple --install");
}

@DisplayName("with release name and multiple charts")
@Test
void releaseNameWithMultipleCharts(UpgradeMojo mojo) {
mojo.setSkipUpgrade(false);
mojo.setReleaseName("foo");
mojo.setChartDirectory(new File("src/test/resources/dependencies"));
String message = assertThrows(MojoExecutionException.class, mojo::execute).getMessage();
assertEquals("For multiple charts releaseName is not supported.", message);
}

@DisplayName("with values overrides")
@Test
void valuesFile(UpgradeMojo mojo) {
mojo.setSkipUpgrade(false);
mojo.setReleaseName("foo");
mojo.setValues(new ValueOverride().setYamlFile("bar.yaml"));
assertHelm(mojo, "upgrade foo src/test/resources/simple --values bar.yaml --install");
assertHelm(mojo, "upgrade simple src/test/resources/simple --values bar.yaml --install");
}
}

0 comments on commit 922db36

Please sign in to comment.