Skip to content

Commit 30dfcd6

Browse files
Add version parameter to helm3 fluent API (#276)
* Added version parameter to InstallFluent * Added version parameter to InstallFluent and JavaDoc * Add test to ensure version parameter is applied --------- Co-authored-by: Alex Stockinger <[email protected]>
1 parent 9007666 commit 30dfcd6

File tree

2 files changed

+79
-9
lines changed

2 files changed

+79
-9
lines changed

src/main/java/com/dajudge/kindcontainer/helm/InstallFluent.java

+54-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
import com.dajudge.kindcontainer.BaseSidecarContainer.ExecInContainer;
44
import com.dajudge.kindcontainer.exception.ExecutionException;
5+
import org.testcontainers.containers.ContainerState;
6+
import org.testcontainers.utility.MountableFile;
57

68
import java.io.IOException;
7-
import java.util.ArrayList;
8-
import java.util.HashMap;
9-
import java.util.List;
10-
import java.util.Map;
9+
import java.util.*;
1110

1211
import static java.util.Arrays.asList;
1312

@@ -17,6 +16,7 @@ public class InstallFluent<P> {
1716
private final P parent;
1817
private String namespace;
1918
private boolean createNamespace;
19+
private String version;
2020
private final Map<String, String> params = new HashMap<>();
2121

2222
private final List<String> values = new ArrayList<>();
@@ -26,31 +26,77 @@ public InstallFluent(final ExecInContainer c, final P parent) {
2626
this.parent = parent;
2727
}
2828

29+
/**
30+
* Adds the given key/value as --set parameter to the Helm install command.
31+
* @param key required
32+
* @param value required
33+
* @return The fluent API
34+
*/
2935
public InstallFluent<P> set(final String key, final String value) {
3036
params.put(key, value);
3137
return this;
3238
}
3339

40+
/**
41+
* Adds the given values file as -f parameter to the Helm install command.
42+
* Make sure the values file is available in the Helm container.
43+
* @see ContainerState#copyFileToContainer(MountableFile, String)
44+
* @param path Path to the values file in the Helm container.
45+
* @return The fluent API
46+
*/
3447
public InstallFluent<P> values(final String path) {
3548
values.add(path);
3649
return this;
3750
}
3851

52+
/**
53+
* Sets the given namespace as target namespace (--namespace parameter) for the Helm install command.
54+
* @param namespace required
55+
* @return The fluent API
56+
*/
3957
public InstallFluent<P> namespace(final String namespace) {
4058
this.namespace = namespace;
4159
return this;
4260
}
4361

62+
/**
63+
* Enables or disables the creation of the target namespace for the Helm install command. (--create-namespace parameter)
64+
* @param createNamespace true to enable creation, false otherwise
65+
* @return The fluent API
66+
*/
4467
public InstallFluent<P> createNamespace(final boolean createNamespace) {
4568
this.createNamespace = createNamespace;
4669
return this;
4770
}
4871

4972

73+
/**
74+
* Enables the creation of the target namespace for the Helm install command.
75+
* @return The fluent API
76+
*/
5077
public InstallFluent<P> createNamespace() {
5178
return createNamespace(true);
5279
}
5380

81+
/**
82+
* Sets the version for the Helm chart to be installed. (--version parameter)
83+
* @param version required
84+
* @return The fluent API
85+
*/
86+
public InstallFluent<P> version(final String version) {
87+
this.version = version;
88+
return this;
89+
}
90+
91+
/**
92+
* Runs the Helm install command.
93+
* @param releaseName The release name of the Helm installation
94+
* @param chart The chart name of the Helm installation
95+
* @return Parent container
96+
* @throws IOException
97+
* @throws InterruptedException
98+
* @throws ExecutionException
99+
*/
54100
public P run(final String releaseName, final String chart) throws IOException, InterruptedException, ExecutionException {
55101
try {
56102
final List<String> cmdline = new ArrayList<>(asList("helm", "install"));
@@ -60,6 +106,9 @@ public P run(final String releaseName, final String chart) throws IOException, I
60106
if (createNamespace) {
61107
cmdline.add("--create-namespace");
62108
}
109+
if (version != null) {
110+
cmdline.addAll(asList("--version", version));
111+
}
63112
params.forEach((k, v) -> cmdline.addAll(asList("--set", String.format("%s=%s", k, v))));
64113
cmdline.addAll(asList(releaseName, chart));
65114
values.forEach(v -> {
@@ -70,6 +119,7 @@ public P run(final String releaseName, final String chart) throws IOException, I
70119
} finally {
71120
createNamespace = false;
72121
namespace = null;
122+
version = null;
73123
params.clear();
74124
}
75125
}

src/test/java/com/dajudge/kindcontainer/Helm3Test.java

+25-5
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,57 @@
33
import com.dajudge.kindcontainer.util.ContainerVersionHelpers.KubernetesTestPackage;
44
import org.junit.jupiter.api.DynamicTest;
55
import org.junit.jupiter.api.TestFactory;
6+
import org.testcontainers.utility.MountableFile;
67

78
import java.util.stream.Stream;
8-
import org.testcontainers.utility.MountableFile;
99

1010
import static com.dajudge.kindcontainer.util.ContainerVersionHelpers.allContainers;
1111
import static com.dajudge.kindcontainer.util.ContainerVersionHelpers.runWithK8s;
1212
import static com.dajudge.kindcontainer.util.TestUtils.runWithClient;
1313
import static org.junit.jupiter.api.Assertions.assertFalse;
14+
import static org.junit.jupiter.api.Assertions.assertThrows;
1415

1516
public class Helm3Test {
1617

18+
private static final String NAMESPACE = "hello";
19+
1720
@TestFactory
1821
public Stream<DynamicTest> can_install_something() {
1922
return allContainers(this::assertCanInstallSomething);
2023
}
2124

25+
@TestFactory
26+
public Stream<DynamicTest> assertInvalidVersionFails() {
27+
return allContainers(this::assertInvalidVersionFails);
28+
}
29+
2230
private void assertCanInstallSomething(final KubernetesTestPackage<? extends KubernetesContainer<?>> testPkg) {
23-
runWithK8s(configureContainer(testPkg.newContainer()), k8s -> runWithClient(k8s, client -> {
24-
assertFalse(client.apps().deployments().inNamespace("hello").list().getItems().isEmpty());
31+
assertCanInstallVersion(testPkg, "0.1.0");
32+
}
33+
34+
private void assertInvalidVersionFails(final KubernetesTestPackage<? extends KubernetesContainer<?>> testPkg) {
35+
assertThrows(RuntimeException.class, () -> assertCanInstallVersion(testPkg, "42.42.42"));
36+
}
37+
38+
private void assertCanInstallVersion(
39+
final KubernetesTestPackage<? extends KubernetesContainer<?>> testPkg,
40+
final String version
41+
) {
42+
runWithK8s(configureContainer(testPkg.newContainer(), version), k8s -> runWithClient(k8s, client -> {
43+
assertFalse(client.apps().deployments().inNamespace(NAMESPACE).list().getItems().isEmpty());
2544
}));
2645
}
2746

28-
private KubernetesContainer<?> configureContainer(KubernetesContainer<?> container) {
47+
private KubernetesContainer<?> configureContainer(final KubernetesContainer<?> container, final String version) {
2948
return container.withHelm3(helm -> {
3049
helm.copyFileToContainer(MountableFile.forClasspathResource("hello-values.yaml"), "/apps/values.yaml");
3150
helm.repo.add.run("examples", "https://helm.github.io/examples");
3251
helm.repo.update.run();
3352
helm.install
34-
.namespace("hello")
53+
.namespace(NAMESPACE)
3554
.createNamespace()
3655
.values("/apps/values.yaml")
56+
.version(version)
3757
.run("hello", "examples/hello-world");
3858
});
3959
}

0 commit comments

Comments
 (0)