Skip to content

Commit

Permalink
Add flags to install and upgrade Helm releases consistently
Browse files Browse the repository at this point in the history
Usage of --atomic and --timeout options allows to rollback
deployment automatically when it fails.
  • Loading branch information
nickkalgin committed Aug 7, 2023
1 parent ec2a37b commit 6802a5d
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ Parameter | Type | User Property | Required | Description
`<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
`<installAtomic>` | boolean | helm.install.atomic | false | Set this to `true` to delete the installation on failure.
`<installTimeout>` | 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`.
`<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 to wait for any individual Kubernetes operation during upgrade process. The default is 5 minutes if `upgradeTimeout` is set to `true`.
`<upgradeDryRun>` | boolean | helm.upgrade.dryRun | false | Run upgrade goal only in dry run mode
`<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).
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/io/kokuwa/maven/helm/InstallMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ public class InstallMojo extends AbstractHelmWithValueOverrideMojo {
@Parameter(property = "action", defaultValue = "install")
private String action;

/**
* Set this to <code>true</code> 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 <code>true</code> to skip invoking install goal.
*
Expand All @@ -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");
}
}
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/io/kokuwa/maven/helm/UpgradeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ public class UpgradeMojo extends AbstractHelmWithValueOverrideMojo {
@Parameter(property = "helm.upgrade.skip", defaultValue = "true")
private boolean skipUpgrade;

/**
* Set this to <code>true</code> 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.
*
Expand Down Expand Up @@ -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");
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/io/kokuwa/maven/helm/InstallMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/io/kokuwa/maven/helm/UpgradeMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6802a5d

Please sign in to comment.