Skip to content

Commit

Permalink
Add missing goal helm:uninstall, fix #116 (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
sschnabe authored Aug 9, 2023
1 parent 7d247c7 commit 781a2eb
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ Parameter | Type | User Property | Required | Description
`<skipUpload>` | boolean | helm.upload.skip | false | skip upload goal
`<insecure>` | boolean | helm.upload.insecure | false | Skip tls certificate checks for the chart upload.
`<skipInstall>` | boolean | helm.install.skip | false | skip install goal
`<skipUninstall>` | boolean | helm.uninstall.skip | false | skip uninstall goal
`<security>` | string | helm.security | false | path to your [settings-security.xml](https://maven.apache.org/guides/mini/guide-encryption.html) (default: `~/.m2/settings-security.xml`)
`<keyring>` | string | helm.package.keyring | false | path to gpg secret keyring for signing
`<key>` | string | helm.package.key | false | name of gpg key in keyring
Expand All @@ -315,6 +316,11 @@ Parameter | Type | User Property | Required | Description
`<upgradeAtomic>` | boolean | helm.upgrade.atomic | false | Set this to `true` to rollback changes made in case of failed upgrade.
`<upgradeTimeout>` | boolean | helm.upgrade.imeout | false | Time in seconds to wait for any individual Kubernetes operation during upgrade process. The default is 300 seconds (from helm) if `upgradeTimeout` is set to `true`.
`<upgradeDryRun>` | boolean | helm.upgrade.dryRun | false | Run upgrade goal only in dry run mode
`<uninstallWait>` | boolean | helm.uninstall.wait | false | If set, will wait until all the resources are deleted before returning. It will wait for as long as `uninstallTimeout`.
`<uninstallTimeout>` | boolean | helm.uninstall.timeout | false | Time to wait for any individual Kubernetes operation (like Jobs for hooks) (default 5m0s).
`<uninstallNoHooks>` | boolean | helm.uninstall.no-hooks | false | Prevent hooks from running during uninstallation.
`<uninstallCascade>` | boolean | helm.uninstall.cascade | false | Must be "background", "orphan", or "foreground". Selects the deletion cascading strategy for the dependents. Defaults to background. (default "background" from helm)
`<uninstallKeepHistory>` | boolean | helm.uninstall.keep-history | false | Remove all associated resources and mark the release as deleted, but retain the release history.
`<templateOutputDir>` | file | helm.template.output-dir | false | Writes the executed templates to files in output-dir instead of stdout.
`<templateGenerateName>` | boolean | helm.template.generate-name | false | Generate the name (and omit the NAME parameter).
`<caFile>` | boolean | helm.push.caFile | false | Verify certificates of HTTPS-enabled servers using this CA bundle.
Expand Down
92 changes: 92 additions & 0 deletions src/main/java/io/kokuwa/maven/helm/UninstallMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
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;
import org.apache.maven.plugins.annotations.Parameter;

import lombok.Setter;

/**
* Mojo for executing "helm uninstall".
*
* @author [email protected]
* @see <a href="https://helm.sh/docs/helm/helm_uninstall">helm uninstall</a>
* @since 6.10.0
*/
@Mojo(name = "uninstall", defaultPhase = LifecyclePhase.DEPLOY, threadSafe = true)
@Setter
public class UninstallMojo extends AbstractHelmWithValueOverrideMojo {

/**
* Must be "background", "orphan", or "foreground". Selects the deletion cascading strategy for the dependents.
* Defaults to background. (default "background" from helm)
*
* @since 6.10.0
*/
@Parameter(property = "helm.uninstall.cascade")
private String uninstallCascade;

/**
* Prevent hooks from running during uninstallation.
*
* @since 6.10.0
*/
@Parameter(property = "helm.uninstall.no-hooks", defaultValue = "false")
private boolean uninstallNoHooks;

/**
* Remove all associated resources and mark the release as deleted, but retain the release history.
*
* @since 6.10.0
*/
@Parameter(property = "helm.uninstall.keep-history ", defaultValue = "false")
private boolean uninstallKeepHistory;

/**
* If set, will wait until all the resources are deleted before returning. It will wait for as long
* as"uninstallTimeout".
*
* @since 6.10.0
*/
@Parameter(property = "helm.uninstall.wait ", defaultValue = "false")
private boolean uninstallWait;

/**
* Time to wait for any individual Kubernetes operation (like Jobs for hooks) (default 5m0s).
*
* @since 6.10.0
*/
@Parameter(property = "helm.uninstall.timeout")
private Integer uninstallTimeout;

/**
* Set this to <code>true</code> to skip invoking uninstall goal.
*
* @since 6.10
*/
@Parameter(property = "helm.uninstall.skip", defaultValue = "true")
private boolean skipUninstall;

@Override
public void execute() throws MojoExecutionException {

if (skip || skipUninstall) {
getLog().info("Skip uninstall");
return;
}

for (Path chartDirectory : getChartDirectories()) {
getLog().info("Perform uninstall for chart " + chartDirectory);
helm().arguments("uninstall", chartDirectory.getFileName().toString())
.flag("wait", uninstallWait)
.flag("timeout", uninstallTimeout != null ? uninstallTimeout + "s" : null)
.flag("cascade", uninstallCascade)
.flag("no-hooks", uninstallNoHooks)
.flag("keep-history", uninstallKeepHistory)
.execute("Failed to deploy helm chart");
}
}
}
63 changes: 63 additions & 0 deletions src/test/java/io/kokuwa/maven/helm/UninstallMojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.kokuwa.maven.helm;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

/**
* Test for {@link UninstallMojoTest}
*
* @author [email protected]
* @since 6.10.0
*/
@DisplayName("helm:uninstall")
public class UninstallMojoTest extends AbstractMojoTest {

@DisplayName("default values")
@Test
void install(UninstallMojo mojo) {
mojo.setSkipUninstall(false);
assertHelm(mojo, "uninstall simple");
}

@DisplayName("with flag skip")
@Test
void skip(UninstallMojo mojo) {
assertHelm(mojo);
assertHelm(mojo.setSkipUninstall(false).setSkip(true));
assertHelm(mojo.setSkipUninstall(true).setSkip(false));
assertHelm(mojo.setSkipUninstall(true).setSkip(true));
}

@DisplayName("with flag keep-history")
@Test
void keepHistory(UninstallMojo mojo) {
mojo.setSkipUninstall(false);
mojo.setUninstallKeepHistory(true);
assertHelm(mojo, "uninstall simple --keep-history");
}

@DisplayName("with flag no-hooks")
@Test
void noHooks(UninstallMojo mojo) {
mojo.setSkipUninstall(false);
mojo.setUninstallNoHooks(true);
assertHelm(mojo, "uninstall simple --no-hooks");
}

@DisplayName("with flags cascade background")
@Test
void cascade(UninstallMojo mojo) {
mojo.setSkipUninstall(false);
mojo.setUninstallCascade("background");
assertHelm(mojo, "uninstall simple --cascade background");
}

@DisplayName("with flags wait and timeout")
@Test
void timeout(UninstallMojo mojo) {
mojo.setSkipUninstall(false);
mojo.setUninstallWait(true);
mojo.setUninstallTimeout(41);
assertHelm(mojo, "uninstall simple --wait --timeout 41s");
}
}

0 comments on commit 781a2eb

Please sign in to comment.