Skip to content

Commit 9031f2f

Browse files
committed
feat: add support for specifying kube-version in install and template commands (review)
1 parent d24dcc3 commit 9031f2f

File tree

10 files changed

+83
-9
lines changed

10 files changed

+83
-9
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ Release result = installCommand
124124
.withNamespace("namespace")
125125
// Optionally create the namespace if not present
126126
.createNamespace()
127-
// Optionally, specify the kubernetes version
128-
.withKubeVersion("1.21.0")
127+
// Optionally specify the Kubernetes version used for capabilities and deprecation checks (e.g. "v1.21.0", "1.21.0")
128+
.withKubeVersion("v1.21.0")
129129
// Optionally, if set, the installation process deletes the installation on failure
130130
.atomic()
131131
// Optionally specify a custom description for the release
@@ -596,8 +596,8 @@ String result = templateCommand
596596
.withVersion("^1.0.0")
597597
// Optionally specify the Kubernetes namespace for the release
598598
.withNamespace("namespace")
599-
// Optionally, specify the kubernetes version
600-
.withKubeVersion("1.21.0")
599+
// Optionally specify the Kubernetes version used for capabilities and deprecation checks (e.g. "v1.21.0", "1.21.0")
600+
.withKubeVersion("v1.21.0")
601601
// Optionally update dependencies if they are missing before installing the chart
602602
.dependencyUpdate()
603603
// Optionally set values for the chart
@@ -696,6 +696,8 @@ Release result = upgradeCommand
696696
.withNamespace("namespace")
697697
// Optionally run an installation if a release by this name doesn't already exist
698698
.install()
699+
// Optionally specify the Kubernetes version for capabilities checks (only used with install() when release doesn't exist)
700+
.withKubeVersion("v1.21.0")
699701
// Optionally force resource updates through a replacement strategy
700702
.force()
701703
// Optionally reset the values to the ones built into the chart when upgrading

helm-java/src/main/java/com/marcnuri/helm/InstallCommand.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,14 @@ public InstallCommand withNamespace(String namespace) {
187187
}
188188

189189
/**
190-
* Kubernetes version for this request.
190+
* Kubernetes version used for capabilities and deprecation checks.
191+
* <p>
192+
* This is useful when rendering charts without connecting to a cluster, or when you want to
193+
* validate chart compatibility with a specific Kubernetes version.
194+
* <p>
195+
* Accepts versions with or without the "v" prefix (e.g., "v1.21.0" or "1.21.0").
191196
*
192-
* @param kubeVersion the Kubernetes version for this request.
197+
* @param kubeVersion the Kubernetes version to use (e.g., "v1.21.0", "1.21.0").
193198
* @return this {@link InstallCommand} instance.
194199
*/
195200
public InstallCommand withKubeVersion(String kubeVersion) {

helm-java/src/main/java/com/marcnuri/helm/TemplateCommand.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,14 @@ public TemplateCommand withNamespace(String namespace) {
133133
}
134134

135135
/**
136-
* Kubernetes version for this request.
136+
* Kubernetes version used for capabilities and deprecation checks.
137+
* <p>
138+
* This is useful when rendering charts without connecting to a cluster, or when you want to
139+
* validate chart compatibility with a specific Kubernetes version.
140+
* <p>
141+
* Accepts versions with or without the "v" prefix (e.g., "v1.21.0" or "1.21.0").
137142
*
138-
* @param kubeVersion the Kubernetes version for this request.
143+
* @param kubeVersion the Kubernetes version to use (e.g., "v1.21.0", "1.21.0").
139144
* @return this {@link TemplateCommand} instance.
140145
*/
141146
public TemplateCommand withKubeVersion(String kubeVersion) {

helm-java/src/main/java/com/marcnuri/helm/UpgradeCommand.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class UpgradeCommand extends HelmCommand<Release> {
4141
private String version;
4242
private String chart;
4343
private String namespace;
44+
private String kubeVersion;
4445
private boolean install;
4546
private boolean force;
4647
private boolean resetValues;
@@ -90,6 +91,7 @@ public Release call() {
9091
version,
9192
chart,
9293
namespace,
94+
kubeVersion,
9395
toInt(install),
9496
toInt(force),
9597
toInt(resetValues),
@@ -171,6 +173,22 @@ public UpgradeCommand withNamespace(String namespace) {
171173
return this;
172174
}
173175

176+
/**
177+
* Kubernetes version used for capabilities and deprecation checks.
178+
* <p>
179+
* This is only used when the release doesn't exist and {@link #install()} is set,
180+
* in which case an installation is performed instead of an upgrade.
181+
* <p>
182+
* Accepts versions with or without the "v" prefix (e.g., "v1.21.0" or "1.21.0").
183+
*
184+
* @param kubeVersion the Kubernetes version to use (e.g., "v1.21.0", "1.21.0").
185+
* @return this {@link UpgradeCommand} instance.
186+
*/
187+
public UpgradeCommand withKubeVersion(String kubeVersion) {
188+
this.kubeVersion = kubeVersion;
189+
return this;
190+
}
191+
174192
/**
175193
* If a release by this name doesn't already exist, run an installation.
176194
*

helm-java/src/test/java/com/marcnuri/helm/HelmInstallTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,19 @@ void withDisableOpenApiValidation() {
230230
.hasFieldOrPropertyWithValue("status", "deployed");
231231
}
232232

233+
@Test
234+
void withKubeVersion() {
235+
final Release result = helm.install()
236+
.clientOnly()
237+
.debug()
238+
.withName("test-kube-version")
239+
.withKubeVersion("1.21.0")
240+
.call();
241+
assertThat(result)
242+
.returns("test-kube-version", Release::getName)
243+
.returns("deployed", Release::getStatus);
244+
}
245+
233246
@Test
234247
void skipCrdsWithoutCrdsInChart() {
235248
final Release result = helm.install()
@@ -301,6 +314,17 @@ void fromRepoWithInvalidVersion(@TempDir Path tempDir) {
301314
);
302315
}
303316

317+
@Test
318+
void withInvalidKubeVersion() {
319+
final InstallCommand install = helm.install()
320+
.clientOnly()
321+
.withName("test-invalid-kube-version")
322+
.withKubeVersion("invalid");
323+
assertThatThrownBy(install::call)
324+
.isInstanceOf(IllegalStateException.class)
325+
.hasMessageContaining("Invalid semantic version");
326+
}
327+
304328
// @Test
305329
// void withDevelopmentVersionInChart() throws IOException {
306330
// final Path chartYaml = tempDir.resolve("test").resolve("Chart.yaml");

helm-java/src/test/java/com/marcnuri/helm/HelmTemplateTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ void withInvalidValuesAndDebug() {
109109
.hasMessageContaining("name: release-name-local-chart-test");
110110
}
111111

112+
@Test
113+
void withKubeVersion() {
114+
final String result = helm.template()
115+
.withKubeVersion("1.21.0")
116+
.call();
117+
assertThat(result)
118+
.contains("name: release-name-local-chart-test");
119+
}
120+
112121
@Test
113122
void skipCrdsWithoutCrdsInChart() {
114123
final String result = helm.template()

lib/api/src/main/java/com/marcnuri/helm/jni/UpgradeOptions.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"version",
3131
"chart",
3232
"namespace",
33+
"kubeVersion",
3334
"install",
3435
"force",
3536
"resetValues",
@@ -66,6 +67,7 @@ public class UpgradeOptions extends Structure {
6667
public String version;
6768
public String chart;
6869
public String namespace;
70+
public String kubeVersion;
6971
public int install;
7072
public int force;
7173
public int resetValues;
@@ -102,6 +104,7 @@ public UpgradeOptions(
102104
String version,
103105
String chart,
104106
String namespace,
107+
String kubeVersion,
105108
int install,
106109
int force,
107110
int resetValues,
@@ -137,6 +140,7 @@ public UpgradeOptions(
137140
this.version = version;
138141
this.chart = chart;
139142
this.namespace = namespace;
143+
this.kubeVersion = kubeVersion;
140144
this.install = install;
141145
this.force = force;
142146
this.resetValues = resetValues;

native/internal/helm/install.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ func install(options *InstallOptions) (*release.Release, *installOutputs, error)
128128
client.ReleaseName = name
129129
client.Namespace = options.Namespace
130130
if options.KubeVersion != "" {
131-
client.KubeVersion, err = chartutil.ParseKubeVersion(options.KubeVersion)
131+
client.KubeVersion, err = chartutil.ParseKubeVersion(options.KubeVersion)
132+
if err != nil {
133+
return nil, outputs, err
134+
}
132135
}
133136
client.Atomic = options.Atomic
134137
client.CreateNamespace = options.CreateNamespace

native/internal/helm/upgrade.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type UpgradeOptions struct {
3030
Version string
3131
Chart string
3232
Namespace string
33+
KubeVersion string
3334
Install bool
3435
Force bool
3536
ResetValues bool
@@ -96,6 +97,7 @@ func Upgrade(options *UpgradeOptions) (string, error) {
9697
Version: options.Version,
9798
Chart: options.Chart,
9899
Namespace: options.Namespace,
100+
KubeVersion: options.KubeVersion,
99101
CreateNamespace: options.CreateNamespace,
100102
Description: options.Description,
101103
Devel: options.Devel,

native/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ struct UpgradeOptions {
212212
char* version;
213213
char* chart;
214214
char* namespace;
215+
char* kubeVersion;
215216
int install;
216217
int force;
217218
int resetValues;
@@ -684,6 +685,7 @@ func Upgrade(options *C.struct_UpgradeOptions) C.Result {
684685
Version: C.GoString(options.version),
685686
Chart: C.GoString(options.chart),
686687
Namespace: C.GoString(options.namespace),
688+
KubeVersion: C.GoString(options.kubeVersion),
687689
Install: options.install == 1,
688690
Force: options.force == 1,
689691
ResetValues: options.resetValues == 1,

0 commit comments

Comments
 (0)