Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions helm-java/src/main/java/com/marcnuri/helm/HelmCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ Result run(Function<HelmLib, Result> function) {
return result;
}

static String urlEncode(Map<String, String> values) {
static <T> String urlEncode(Map<String, T> entries, Function<T, String> valueMapper) {
final StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : values.entrySet()) {
for (Map.Entry<String, T> entry : entries.entrySet()) {
if (sb.length() > 0) {
sb.append("&");
}
try {
sb.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.name()))
.append("=")
.append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.name()));
.append(URLEncoder.encode(valueMapper.apply(entry.getValue()), StandardCharsets.UTF_8.name()));
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Invalid entry: " + entry.getKey() + "=" + entry.getValue(), e);
}
Expand Down
20 changes: 19 additions & 1 deletion helm-java/src/main/java/com/marcnuri/helm/InstallCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;

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

Expand Down Expand Up @@ -53,6 +54,7 @@ public class InstallCommand extends HelmCommand<Release> {
private boolean wait;
private int timeout;
private final Map<String, String> values;
private final Map<String, Path> setFiles;
private final List<Path> valuesFiles;
private Path kubeConfig;
private String kubeConfigContents;
Expand All @@ -74,6 +76,7 @@ public InstallCommand(HelmLib helmLib, Path chart) {
super(helmLib);
this.chart = toString(chart);
this.values = new LinkedHashMap<>();
this.setFiles = new LinkedHashMap<>();
this.valuesFiles = new ArrayList<>();
}

Expand All @@ -96,7 +99,8 @@ public Release call() {
dryRunOption == null ? null : dryRunOption.name().toLowerCase(Locale.ROOT),
toInt(wait),
timeout,
urlEncode(values),
urlEncode(values, Function.identity()),
urlEncode(setFiles, HelmCommand::toString),
toString(valuesFiles),
toString(kubeConfig),
kubeConfigContents,
Expand Down Expand Up @@ -302,6 +306,20 @@ public InstallCommand set(String key, Object value) {
return this;
}

/**
* Set a value for the chart by reading it from a file.
* <p>
* The file contents will be used as the value for the specified key.
*
* @param key the key.
* @param file the path to the file containing the value.
* @return this {@link InstallCommand} instance.
*/
public InstallCommand setFile(String key, Path file) {
this.setFiles.put(key, file);
return this;
}

/**
* Adds a values (YAML) file to source values for the chart (can specify multiple).
*
Expand Down
20 changes: 19 additions & 1 deletion helm-java/src/main/java/com/marcnuri/helm/TemplateCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
* @author Marc Nuri
Expand All @@ -37,6 +38,7 @@ public class TemplateCommand extends HelmCommand<String> {
private String namespace;
private boolean dependencyUpdate;
private final Map<String, String> values;
private final Map<String, Path> setFiles;
private final List<Path> valuesFiles;
private Path certFile;
private Path keyFile;
Expand All @@ -55,6 +57,7 @@ public TemplateCommand(HelmLib helmLib, Path chart) {
super(helmLib);
this.chart = toString(chart);
this.values = new LinkedHashMap<>();
this.setFiles = new LinkedHashMap<>();
this.valuesFiles = new ArrayList<>();
}

Expand All @@ -66,7 +69,8 @@ public String call() {
chart,
namespace,
toInt(dependencyUpdate),
urlEncode(values),
urlEncode(values, Function.identity()),
urlEncode(setFiles, HelmCommand::toString),
toString(valuesFiles),
toString(certFile),
toString(keyFile),
Expand Down Expand Up @@ -149,6 +153,20 @@ public TemplateCommand set(String key, Object value) {
return this;
}

/**
* Set a value for the chart by reading it from a file.
* <p>
* The file contents will be used as the value for the specified key.
*
* @param key the key.
* @param file the path to the file containing the value.
* @return this {@link TemplateCommand} instance.
*/
public TemplateCommand setFile(String key, Path file) {
this.setFiles.put(key, file);
return this;
}

/**
* Adds a values (YAML) file to source values for the chart (can specify multiple).
*
Expand Down
20 changes: 19 additions & 1 deletion helm-java/src/main/java/com/marcnuri/helm/UpgradeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;

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

Expand Down Expand Up @@ -57,6 +58,7 @@ public class UpgradeCommand extends HelmCommand<Release> {
private boolean wait;
private int timeout;
private final Map<String, String> values;
private final Map<String, Path> setFiles;
private final List<Path> valuesFiles;
private Path kubeConfig;
private String kubeConfigContents;
Expand All @@ -78,6 +80,7 @@ public UpgradeCommand(HelmLib helmLib, Path chart) {
super(helmLib);
this.chart = toString(chart);
this.values = new LinkedHashMap<>();
this.setFiles = new LinkedHashMap<>();
this.valuesFiles = new ArrayList<>();
}

Expand All @@ -104,7 +107,8 @@ public Release call() {
dryRunOption == null ? null : dryRunOption.name().toLowerCase(Locale.ROOT),
toInt(wait),
timeout,
urlEncode(values),
urlEncode(values, Function.identity()),
urlEncode(setFiles, HelmCommand::toString),
toString(valuesFiles),
toString(kubeConfig),
kubeConfigContents,
Expand Down Expand Up @@ -354,6 +358,20 @@ public UpgradeCommand set(String key, Object value) {
return this;
}

/**
* Set a value for the chart by reading it from a file.
* <p>
* The file contents will be used as the value for the specified key.
*
* @param key the key.
* @param file the path to the file containing the value.
* @return this {@link UpgradeCommand} instance.
*/
public UpgradeCommand setFile(String key, Path file) {
this.setFiles.put(key, file);
return this;
}

/**
* Adds a values (YAML) file to source values for the chart (can specify multiple).
*
Expand Down
18 changes: 18 additions & 0 deletions helm-java/src/test/java/com/marcnuri/helm/HelmInstallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ void withValuesFile() throws IOException {
);
}

@Test
void withSetFile() throws IOException {
final Path configFile = Files.write(tempDir.resolve("config.txt"),
"foobar".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
final Release result = helm.install()
.clientOnly()
.debug()
.withName("test")
.setFile("configData", configFile)
.call();
assertThat(result)
.extracting(Release::getOutput).asString()
.contains(
"NAME: test\n",
"configData: foobar"
);
}

@Test
void withDisableOpenApiValidation() {
final Release result = helm.install()
Expand Down
18 changes: 18 additions & 0 deletions helm-java/src/test/java/com/marcnuri/helm/HelmKubernetesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,24 @@ void withWaitAndCustomTimeout() {
"beginning wait for 3 resources with timeout of 5m30s"
);
}

@Test
void withSetFile(@TempDir Path tempDir) throws IOException {
final Path configFile = Files.write(tempDir.resolve("upgrade-config.txt"),
"foobar".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
helm.install().withName("upgrade-with-set-file").withKubeConfig(kubeConfigFile).call();
final Release result = helm.upgrade()
.withKubeConfig(kubeConfigFile)
.withName("upgrade-with-set-file")
.setFile("configData", configFile)
.debug()
.call();
assertThat(result)
.returns("2", Release::getRevision)
.returns("deployed", Release::getStatus)
.extracting(Release::getOutput).asString()
.contains("configData: foobar");
}
}

@Nested
Expand Down
11 changes: 11 additions & 0 deletions helm-java/src/test/java/com/marcnuri/helm/HelmTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ void withInvalidValuesAndDebug() {
.hasMessageContaining("# Source: local-chart-test")
.hasMessageContaining("name: release-name-local-chart-test");
}

@Test
void withSetFile() throws IOException {
final Path replicaFile = Files.write(tempDir.resolve("replica-count.txt"),
"42".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE);
final String result = helm.template()
.setFile("replicaCount", replicaFile)
.call();
assertThat(result)
.contains("replicas: 42");
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"wait",
"timeout",
"values",
"setFiles",
"valuesFiles",
"kubeConfig",
"kubeConfigContents",
Expand Down Expand Up @@ -74,6 +75,7 @@ public class InstallOptions extends Structure {
public int wait;
public int timeout;
public String values;
public String setFiles;
public String valuesFiles;
public String kubeConfig;
public String kubeConfigContents;
Expand Down Expand Up @@ -105,6 +107,7 @@ public InstallOptions(
int wait,
int timeout,
String values,
String setFiles,
String valuesFiles,
String kubeConfig,
String kubeConfigContents,
Expand Down Expand Up @@ -135,6 +138,7 @@ public InstallOptions(
this.wait = wait;
this.timeout = timeout;
this.values = values;
this.setFiles = setFiles;
this.valuesFiles = valuesFiles;
this.kubeConfig = kubeConfig;
this.kubeConfigContents = kubeConfigContents;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"namespace",
"dependencyUpdate",
"values",
"setFiles",
"valuesFiles",
"certFile",
"keyFile",
Expand All @@ -46,6 +47,7 @@ public class TemplateOptions extends Structure {
public String namespace;
public int dependencyUpdate;
public String values;
public String setFiles;
public String valuesFiles;
public String certFile;
public String keyFile;
Expand All @@ -63,6 +65,7 @@ public TemplateOptions(
String namespace,
int dependencyUpdate,
String values,
String setFiles,
String valuesFiles,
String certFile,
String keyFile,
Expand All @@ -79,6 +82,7 @@ public TemplateOptions(
this.namespace = namespace;
this.dependencyUpdate = dependencyUpdate;
this.values = values;
this.setFiles = setFiles;
this.valuesFiles = valuesFiles;
this.certFile = certFile;
this.keyFile = keyFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"wait",
"timeout",
"values",
"setFiles",
"valuesFiles",
"kubeConfig",
"kubeConfigContents",
Expand Down Expand Up @@ -81,6 +82,7 @@ public class UpgradeOptions extends Structure {
public int wait;
public int timeout;
public String values;
public String setFiles;
public String valuesFiles;
public String kubeConfig;
public String kubeConfigContents;
Expand Down Expand Up @@ -116,6 +118,7 @@ public UpgradeOptions(
int wait,
int timeout,
String values,
String setFiles,
String valuesFiles,
String kubeConfig,
String kubeConfigContents,
Expand Down Expand Up @@ -150,6 +153,7 @@ public UpgradeOptions(
this.wait = wait;
this.timeout = timeout;
this.values = values;
this.setFiles = setFiles;
this.valuesFiles = valuesFiles;
this.kubeConfig = kubeConfig;
this.kubeConfigContents = kubeConfigContents;
Expand Down
10 changes: 8 additions & 2 deletions native/internal/helm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type InstallOptions struct {
Wait bool
Timeout time.Duration
Values string
SetFiles string
ValuesFiles string
KubeConfig string
KubeConfigContents string
Expand Down Expand Up @@ -161,7 +162,7 @@ func install(options *InstallOptions) (*release.Release, *installOutputs, error)
return nil, outputs, invalidDryRun
}
// Values
vals, err := mergeValues(options.Values, options.ValuesFiles)
vals, err := mergeValues(options.Values, options.SetFiles, options.ValuesFiles)
if err != nil {
return nil, outputs, err
}
Expand Down Expand Up @@ -294,11 +295,15 @@ func parseValuesSet(values string) ([]string, error) {
}

// mergeValues returns a map[string]interface{} with the provided processed values
func mergeValues(encodedValuesMap, encodedValuesFiles string) (map[string]interface{}, error) {
func mergeValues(encodedValuesMap, encodedSetFiles, encodedValuesFiles string) (map[string]interface{}, error) {
valuesSet, err := parseValuesSet(encodedValuesMap)
if err != nil {
return nil, err
}
setFiles, err := parseValuesSet(encodedSetFiles)
if err != nil {
return nil, err
}
valueFiles := make([]string, 0)
if encodedValuesFiles != "" {
for _, valuesFile := range strings.Split(encodedValuesFiles, ",") {
Expand All @@ -307,6 +312,7 @@ func mergeValues(encodedValuesMap, encodedValuesFiles string) (map[string]interf
}
return (&values.Options{
Values: valuesSet,
FileValues: setFiles,
ValueFiles: valueFiles,
}).MergeValues(make(getter.Providers, 0))
}
Loading