diff --git a/README.md b/README.md index a3abc208..493e5731 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,7 @@ Parameter | Type | User Property | Required | Description `` | boolean | helm.upload.skip | false | skip upload goal `` | boolean | helm.upload.insecure | false | Skip tls certificate checks for the chart upload. `` | boolean | helm.install.skip | false | skip install goal +`` | boolean | helm.uninstall.skip | false | skip uninstall goal `` | string | helm.security | false | path to your [settings-security.xml](https://maven.apache.org/guides/mini/guide-encryption.html) (default: `~/.m2/settings-security.xml`) `` | string | helm.package.keyring | false | path to gpg secret keyring for signing `` | string | helm.package.key | false | name of gpg key in keyring @@ -315,6 +316,11 @@ Parameter | Type | User Property | Required | Description `` | boolean | helm.upgrade.atomic | false | Set this to `true` to rollback changes made in case of failed upgrade. `` | 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`. `` | boolean | helm.upgrade.dryRun | false | Run upgrade goal only in dry run mode +`` | 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`. +`` | boolean | helm.uninstall.timeout | false | Time to wait for any individual Kubernetes operation (like Jobs for hooks) (default 5m0s). +`` | boolean | helm.uninstall.no-hooks | false | Prevent hooks from running during uninstallation. +`` | 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) +`` | boolean | helm.uninstall.keep-history | false | Remove all associated resources and mark the release as deleted, but retain the release history. `` | file | helm.template.output-dir | false | Writes the executed templates to files in output-dir instead of stdout. `` | boolean | helm.template.generate-name | false | Generate the name (and omit the NAME parameter). `` | boolean | helm.push.caFile | false | Verify certificates of HTTPS-enabled servers using this CA bundle. diff --git a/src/main/java/io/kokuwa/maven/helm/UninstallMojo.java b/src/main/java/io/kokuwa/maven/helm/UninstallMojo.java new file mode 100644 index 00000000..7714963d --- /dev/null +++ b/src/main/java/io/kokuwa/maven/helm/UninstallMojo.java @@ -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 stephan.schnabel@posteo.de + * @see helm uninstall + * @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 true 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"); + } + } +} diff --git a/src/test/java/io/kokuwa/maven/helm/UninstallMojoTest.java b/src/test/java/io/kokuwa/maven/helm/UninstallMojoTest.java new file mode 100644 index 00000000..f4ca5357 --- /dev/null +++ b/src/test/java/io/kokuwa/maven/helm/UninstallMojoTest.java @@ -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 stephan.schnabel@posteo.de + * @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"); + } +}