Skip to content

Commit b21ec86

Browse files
feat: add support for --set-file flag (#319)
1 parent 6c0fef4 commit b21ec86

File tree

14 files changed

+139
-9
lines changed

14 files changed

+139
-9
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ Result run(Function<HelmLib, Result> function) {
5555
return result;
5656
}
5757

58-
static String urlEncode(Map<String, String> values) {
58+
static <T> String urlEncode(Map<String, T> entries, Function<T, String> valueMapper) {
5959
final StringBuilder sb = new StringBuilder();
60-
for (Map.Entry<String, String> entry : values.entrySet()) {
60+
for (Map.Entry<String, T> entry : entries.entrySet()) {
6161
if (sb.length() > 0) {
6262
sb.append("&");
6363
}
6464
try {
6565
sb.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.name()))
6666
.append("=")
67-
.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.name()));
67+
.append(URLEncoder.encode(valueMapper.apply(entry.getValue()), StandardCharsets.UTF_8.name()));
6868
} catch (UnsupportedEncodingException e) {
6969
throw new IllegalArgumentException("Invalid entry: " + entry.getKey() + "=" + entry.getValue(), e);
7070
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626
import java.util.Locale;
2727
import java.util.Map;
28+
import java.util.function.Function;
2829

2930
import static com.marcnuri.helm.Release.parseSingle;
3031

@@ -56,6 +57,7 @@ public class InstallCommand extends HelmCommand<Release> {
5657
private boolean wait;
5758
private int timeout;
5859
private final Map<String, String> values;
60+
private final Map<String, Path> setFiles;
5961
private final List<Path> valuesFiles;
6062
private Path kubeConfig;
6163
private String kubeConfigContents;
@@ -77,6 +79,7 @@ public InstallCommand(HelmLib helmLib, Path chart) {
7779
super(helmLib);
7880
this.chart = toString(chart);
7981
this.values = new LinkedHashMap<>();
82+
this.setFiles = new LinkedHashMap<>();
8083
this.valuesFiles = new ArrayList<>();
8184
}
8285

@@ -101,7 +104,8 @@ public Release call() {
101104
toInt(skipCrds),
102105
toInt(wait),
103106
timeout,
104-
urlEncode(values),
107+
urlEncode(values, Function.identity()),
108+
urlEncode(setFiles, HelmCommand::toString),
105109
toString(valuesFiles),
106110
toString(kubeConfig),
107111
kubeConfigContents,
@@ -335,6 +339,20 @@ public InstallCommand set(String key, Object value) {
335339
return this;
336340
}
337341

342+
/**
343+
* Set a value for the chart by reading it from a file.
344+
* <p>
345+
* The file contents will be used as the value for the specified key.
346+
*
347+
* @param key the key.
348+
* @param file the path to the file containing the value.
349+
* @return this {@link InstallCommand} instance.
350+
*/
351+
public InstallCommand setFile(String key, Path file) {
352+
this.setFiles.put(key, file);
353+
return this;
354+
}
355+
338356
/**
339357
* Adds a values (YAML) file to source values for the chart (can specify multiple).
340358
*

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.LinkedHashMap;
2525
import java.util.List;
2626
import java.util.Map;
27+
import java.util.function.Function;
2728

2829
/**
2930
* @author Marc Nuri
@@ -40,6 +41,7 @@ public class TemplateCommand extends HelmCommand<String> {
4041
private boolean dependencyUpdate;
4142
private boolean skipCrds;
4243
private final Map<String, String> values;
44+
private final Map<String, Path> setFiles;
4345
private final List<Path> valuesFiles;
4446
private Path certFile;
4547
private Path keyFile;
@@ -58,6 +60,7 @@ public TemplateCommand(HelmLib helmLib, Path chart) {
5860
super(helmLib);
5961
this.chart = toString(chart);
6062
this.values = new LinkedHashMap<>();
63+
this.setFiles = new LinkedHashMap<>();
6164
this.valuesFiles = new ArrayList<>();
6265
}
6366

@@ -71,7 +74,8 @@ public String call() {
7174
kubeVersion,
7275
toInt(dependencyUpdate),
7376
toInt(skipCrds),
74-
urlEncode(values),
77+
urlEncode(values, Function.identity()),
78+
urlEncode(setFiles, HelmCommand::toString),
7579
toString(valuesFiles),
7680
toString(certFile),
7781
toString(keyFile),
@@ -182,6 +186,20 @@ public TemplateCommand set(String key, Object value) {
182186
return this;
183187
}
184188

189+
/**
190+
* Set a value for the chart by reading it from a file.
191+
* <p>
192+
* The file contents will be used as the value for the specified key.
193+
*
194+
* @param key the key.
195+
* @param file the path to the file containing the value.
196+
* @return this {@link TemplateCommand} instance.
197+
*/
198+
public TemplateCommand setFile(String key, Path file) {
199+
this.setFiles.put(key, file);
200+
return this;
201+
}
202+
185203
/**
186204
* Adds a values (YAML) file to source values for the chart (can specify multiple).
187205
*

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626
import java.util.Locale;
2727
import java.util.Map;
28+
import java.util.function.Function;
2829

2930
import static com.marcnuri.helm.Release.parseSingle;
3031

@@ -60,6 +61,7 @@ public class UpgradeCommand extends HelmCommand<Release> {
6061
private boolean wait;
6162
private int timeout;
6263
private final Map<String, String> values;
64+
private final Map<String, Path> setFiles;
6365
private final List<Path> valuesFiles;
6466
private Path kubeConfig;
6567
private String kubeConfigContents;
@@ -81,6 +83,7 @@ public UpgradeCommand(HelmLib helmLib, Path chart) {
8183
super(helmLib);
8284
this.chart = toString(chart);
8385
this.values = new LinkedHashMap<>();
86+
this.setFiles = new LinkedHashMap<>();
8487
this.valuesFiles = new ArrayList<>();
8588
}
8689

@@ -109,7 +112,8 @@ public Release call() {
109112
toInt(skipCrds),
110113
toInt(wait),
111114
timeout,
112-
urlEncode(values),
115+
urlEncode(values, Function.identity()),
116+
urlEncode(setFiles, HelmCommand::toString),
113117
toString(valuesFiles),
114118
toString(kubeConfig),
115119
kubeConfigContents,
@@ -387,6 +391,20 @@ public UpgradeCommand set(String key, Object value) {
387391
return this;
388392
}
389393

394+
/**
395+
* Set a value for the chart by reading it from a file.
396+
* <p>
397+
* The file contents will be used as the value for the specified key.
398+
*
399+
* @param key the key.
400+
* @param file the path to the file containing the value.
401+
* @return this {@link UpgradeCommand} instance.
402+
*/
403+
public UpgradeCommand setFile(String key, Path file) {
404+
this.setFiles.put(key, file);
405+
return this;
406+
}
407+
390408
/**
391409
* Adds a values (YAML) file to source values for the chart (can specify multiple).
392410
*

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,24 @@ void withValuesFile() throws IOException {
217217
);
218218
}
219219

220+
@Test
221+
void withSetFile() throws IOException {
222+
final Path configFile = Files.write(tempDir.resolve("config.txt"),
223+
"foobar".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
224+
final Release result = helm.install()
225+
.clientOnly()
226+
.debug()
227+
.withName("test")
228+
.setFile("configData", configFile)
229+
.call();
230+
assertThat(result)
231+
.extracting(Release::getOutput).asString()
232+
.contains(
233+
"NAME: test\n",
234+
"configData: foobar"
235+
);
236+
}
237+
220238
@Test
221239
void withDisableOpenApiValidation() {
222240
final Release result = helm.install()

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,24 @@ void skipCrdsWithoutCrdsInChart() {
646646
.returns("2", Release::getRevision)
647647
.returns("deployed", Release::getStatus);
648648
}
649+
650+
@Test
651+
void withSetFile(@TempDir Path tempDir) throws IOException {
652+
final Path configFile = Files.write(tempDir.resolve("upgrade-config.txt"),
653+
"foobar".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
654+
helm.install().withName("upgrade-with-set-file").withKubeConfig(kubeConfigFile).call();
655+
final Release result = helm.upgrade()
656+
.withKubeConfig(kubeConfigFile)
657+
.withName("upgrade-with-set-file")
658+
.setFile("configData", configFile)
659+
.debug()
660+
.call();
661+
assertThat(result)
662+
.returns("2", Release::getRevision)
663+
.returns("deployed", Release::getStatus)
664+
.extracting(Release::getOutput).asString()
665+
.contains("configData: foobar");
666+
}
649667
}
650668

651669
@Nested

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

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

112+
@Test
113+
void withSetFile() throws IOException {
114+
final Path replicaFile = Files.write(tempDir.resolve("replica-count.txt"),
115+
"42".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
116+
final String result = helm.template()
117+
.setFile("replicaCount", replicaFile)
118+
.call();
119+
assertThat(result)
120+
.contains("replicas: 42");
121+
}
122+
112123
@Test
113124
void withKubeVersion() {
114125
final String result = helm.template()

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"wait",
4646
"timeout",
4747
"values",
48+
"setFiles",
4849
"valuesFiles",
4950
"kubeConfig",
5051
"kubeConfigContents",
@@ -79,6 +80,7 @@ public class InstallOptions extends Structure {
7980
public int wait;
8081
public int timeout;
8182
public String values;
83+
public String setFiles;
8284
public String valuesFiles;
8385
public String kubeConfig;
8486
public String kubeConfigContents;
@@ -112,6 +114,7 @@ public InstallOptions(
112114
int wait,
113115
int timeout,
114116
String values,
117+
String setFiles,
115118
String valuesFiles,
116119
String kubeConfig,
117120
String kubeConfigContents,
@@ -144,6 +147,7 @@ public InstallOptions(
144147
this.wait = wait;
145148
this.timeout = timeout;
146149
this.values = values;
150+
this.setFiles = setFiles;
147151
this.valuesFiles = valuesFiles;
148152
this.kubeConfig = kubeConfig;
149153
this.kubeConfigContents = kubeConfigContents;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"dependencyUpdate",
3333
"skipCRDs",
3434
"values",
35+
"setFiles",
3536
"valuesFiles",
3637
"certFile",
3738
"keyFile",
@@ -51,6 +52,7 @@ public class TemplateOptions extends Structure {
5152
public int dependencyUpdate;
5253
public int skipCRDs;
5354
public String values;
55+
public String setFiles;
5456
public String valuesFiles;
5557
public String certFile;
5658
public String keyFile;
@@ -70,6 +72,7 @@ public TemplateOptions(
7072
int dependencyUpdate,
7173
int skipCRDs,
7274
String values,
75+
String setFiles,
7376
String valuesFiles,
7477
String certFile,
7578
String keyFile,
@@ -88,6 +91,7 @@ public TemplateOptions(
8891
this.dependencyUpdate = dependencyUpdate;
8992
this.skipCRDs = skipCRDs;
9093
this.values = values;
94+
this.setFiles = setFiles;
9195
this.valuesFiles = valuesFiles;
9296
this.certFile = certFile;
9397
this.keyFile = keyFile;

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
@@ -49,6 +49,7 @@
4949
"wait",
5050
"timeout",
5151
"values",
52+
"setFiles",
5253
"valuesFiles",
5354
"kubeConfig",
5455
"kubeConfigContents",
@@ -86,6 +87,7 @@ public class UpgradeOptions extends Structure {
8687
public int wait;
8788
public int timeout;
8889
public String values;
90+
public String setFiles;
8991
public String valuesFiles;
9092
public String kubeConfig;
9193
public String kubeConfigContents;
@@ -123,6 +125,7 @@ public UpgradeOptions(
123125
int wait,
124126
int timeout,
125127
String values,
128+
String setFiles,
126129
String valuesFiles,
127130
String kubeConfig,
128131
String kubeConfigContents,
@@ -159,6 +162,7 @@ public UpgradeOptions(
159162
this.wait = wait;
160163
this.timeout = timeout;
161164
this.values = values;
165+
this.setFiles = setFiles;
162166
this.valuesFiles = valuesFiles;
163167
this.kubeConfig = kubeConfig;
164168
this.kubeConfigContents = kubeConfigContents;

0 commit comments

Comments
 (0)