From 6802a5de7e05b4eb94637686d258c0b93c15608b Mon Sep 17 00:00:00 2001 From: Nick Kalgin Date: Mon, 7 Aug 2023 21:00:21 +0300 Subject: [PATCH] Add flags to install and upgrade Helm releases consistently Usage of --atomic and --timeout options allows to rollback deployment automatically when it fails. --- README.md | 4 ++++ .../io/kokuwa/maven/helm/InstallMojo.java | 22 ++++++++++++++++++- .../io/kokuwa/maven/helm/UpgradeMojo.java | 22 ++++++++++++++++++- .../io/kokuwa/maven/helm/InstallMojoTest.java | 17 ++++++++++++++ .../io/kokuwa/maven/helm/UpgradeMojoTest.java | 21 ++++++++++++++++++ 5 files changed, 84 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f78e0bbc..79892fff 100644 --- a/README.md +++ b/README.md @@ -310,6 +310,10 @@ Parameter | Type | User Property | Required | Description `` | string | helm.kubeAsGroup | false | group to impersonate for the operation, this flag can be repeated to specify multiple groups `` | string | helm.kubeToken | false | bearer token used for authentication `` | string | helm.releaseName | false | Name of the release for upgrade goal +`` | boolean | helm.install.atomic | false | Set this to `true` to delete the installation on failure. +`` | boolean | helm.upgrade.imeout | false | Time to wait for any individual Kubernetes operation during install process. The default is 5 minutes if `installAtomic` is set to `true`. +`` | boolean | helm.upgrade.atomic | false | Set this to `true` to rollback changes made in case of failed upgrade. +`` | boolean | helm.upgrade.imeout | false | Time to wait for any individual Kubernetes operation during upgrade process. The default is 5 minutes if `upgradeTimeout` is set to `true`. `` | boolean | helm.upgrade.dryRun | false | Run upgrade goal only in dry run mode `` | 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). diff --git a/src/main/java/io/kokuwa/maven/helm/InstallMojo.java b/src/main/java/io/kokuwa/maven/helm/InstallMojo.java index ab04452b..885aa891 100644 --- a/src/main/java/io/kokuwa/maven/helm/InstallMojo.java +++ b/src/main/java/io/kokuwa/maven/helm/InstallMojo.java @@ -31,6 +31,22 @@ public class InstallMojo extends AbstractHelmWithValueOverrideMojo { @Parameter(property = "action", defaultValue = "install") private String action; + /** + * Set this to true to delete the installation on failure. + * + * @since 6.10.0 + */ + @Parameter(property = "helm.install.atomic") + private boolean installAtomic; + + /** + * Time to wait for any individual Kubernetes operation. + * + * @since 6.10.0 + */ + @Parameter(property = "helm.install.timeout") + private String installTimeout; + /** * Set this to true to skip invoking install goal. * @@ -48,9 +64,13 @@ public void execute() throws MojoExecutionException, MojoFailureException { } for (Path chartDirectory : getChartDirectories()) { - getLog().info(String.format("\n\nPerform install for chart %s...", chartDirectory)); + getLog().info(String.format("\n\nPerform install for chart %s...", chartDirectory) + + (installAtomic ? " with atomic" : "") + + (installTimeout != null ? String.format(" with timeout %s", installTimeout) : "")); helm() .arguments(action, chartDirectory.getFileName().toString(), chartDirectory) + .flag("atomic", installAtomic) + .flag("timeout", installTimeout) .execute("Failed to deploy helm chart"); } } diff --git a/src/main/java/io/kokuwa/maven/helm/UpgradeMojo.java b/src/main/java/io/kokuwa/maven/helm/UpgradeMojo.java index a4f5faa6..ab2b2a62 100644 --- a/src/main/java/io/kokuwa/maven/helm/UpgradeMojo.java +++ b/src/main/java/io/kokuwa/maven/helm/UpgradeMojo.java @@ -27,6 +27,22 @@ public class UpgradeMojo extends AbstractHelmWithValueOverrideMojo { @Parameter(property = "helm.upgrade.skip", defaultValue = "true") private boolean skipUpgrade; + /** + * Set this to true to rollback changes made in case of failed upgrade. + * + * @since 6.10.0 + */ + @Parameter(property = "helm.upgrade.atomic") + private boolean upgradeAtomic; + + /** + * Time to wait for any individual Kubernetes operation. + * + * @since 6.10.0 + */ + @Parameter(property = "helm.upgrade.timeout") + private String upgradeTimeout; + /** * Upgrade with install parameter. * @@ -60,14 +76,18 @@ public void execute() throws MojoExecutionException { } for (Path chartDirectory : getChartDirectories()) { - getLog().info("installing the chart " + + getLog().info("Upgrading the chart " + (upgradeWithInstall ? "with install " : "") + + (upgradeAtomic ? "with atomic " : "") + + (upgradeTimeout != null ? String.format("timeout %s ", upgradeTimeout) : "") + (upgradeDryRun ? "as dry run " : "") + chartDirectory); helm() .arguments("upgrade", releaseName, chartDirectory) .flag("install", upgradeWithInstall) .flag("dry-run", upgradeDryRun) + .flag("atomic", upgradeAtomic) + .flag("timeout", upgradeTimeout) .execute("Error occurred while upgrading the chart"); } } diff --git a/src/test/java/io/kokuwa/maven/helm/InstallMojoTest.java b/src/test/java/io/kokuwa/maven/helm/InstallMojoTest.java index da100535..33d9dbb5 100644 --- a/src/test/java/io/kokuwa/maven/helm/InstallMojoTest.java +++ b/src/test/java/io/kokuwa/maven/helm/InstallMojoTest.java @@ -25,6 +25,23 @@ void skip(InstallMojo mojo) { assertHelm(mojo.setSkipInstall(true).setSkip(true)); } + @DisplayName("with flag atomic") + @Test + void atomic(InstallMojo mojo) { + mojo.setSkipInstall(false); + mojo.setInstallAtomic(true); + assertHelm(mojo, "install simple src/test/resources/simple --atomic"); + } + + @DisplayName("with flags atomic and timeout") + @Test + void atomicAndTimeout(InstallMojo mojo) { + mojo.setSkipInstall(false); + mojo.setInstallAtomic(true); + mojo.setInstallTimeout("30s"); + assertHelm(mojo, "install simple src/test/resources/simple --atomic --timeout 30s"); + } + @DisplayName("with values overrides") @Test void valuesFile(InstallMojo mojo) { diff --git a/src/test/java/io/kokuwa/maven/helm/UpgradeMojoTest.java b/src/test/java/io/kokuwa/maven/helm/UpgradeMojoTest.java index 5a5718da..e1c52f14 100644 --- a/src/test/java/io/kokuwa/maven/helm/UpgradeMojoTest.java +++ b/src/test/java/io/kokuwa/maven/helm/UpgradeMojoTest.java @@ -33,6 +33,27 @@ void dryRun(UpgradeMojo mojo) { assertHelm(mojo, "upgrade foo src/test/resources/simple --install --dry-run"); } + @DisplayName("with flag atomic") + @Test + void atomic(UpgradeMojo mojo) { + mojo.setSkipUpgrade(false); + mojo.setReleaseName("foo"); + mojo.setUpgradeWithInstall(false); + mojo.setUpgradeAtomic(true); + assertHelm(mojo, "upgrade foo src/test/resources/simple --atomic"); + } + + @DisplayName("with flags atomic and timeout") + @Test + void atomicAndTimeout(UpgradeMojo mojo) { + mojo.setSkipUpgrade(false); + mojo.setReleaseName("foo"); + mojo.setUpgradeWithInstall(false); + mojo.setUpgradeAtomic(true); + mojo.setUpgradeTimeout("30s"); + assertHelm(mojo, "upgrade foo src/test/resources/simple --atomic --timeout 30s"); + } + @DisplayName("without flag install") @Test void install(UpgradeMojo mojo) {