Skip to content

Commit

Permalink
feat: Helm support for Golang expressions
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Jun 9, 2021
1 parent d5eca53 commit c01c5c3
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Usage:
```
### 1.4.0-SNAPSHOT
* Fix #705: JIB assembly works on Windows
* Fix #714: feat: Helm support for Golang expressions

### 1.3.0
* Fix #497: Assembly descriptor removed but still in documentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.stream.Stream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.ResourceFileType;
import org.eclipse.jkube.kit.common.archive.ArchiveCompression;
Expand All @@ -47,6 +48,7 @@ public class HelmService {
private static final String CHART_API_VERSION = "v1";
private static final String CHART_FILENAME = "Chart" + YAML_EXTENSION;
private static final String VALUES_FILENAME = "values" + YAML_EXTENSION;
private static final String GOLANG_EXPRESSION_REGEX = "\\{\\{.+}}";

private HelmService() {}

Expand Down Expand Up @@ -199,20 +201,27 @@ private static String interpolateTemplateWithHelmParameter(String template, Helm
final String braceEnclosedFrom = "${" + name + "}";
final String quotedBraceEnclosedFrom = "\"" + braceEnclosedFrom + "\"";
String answer = template;
final String to = expression(parameter);
answer = answer.replace(quotedBraceEnclosedFrom, to);
answer = answer.replace(braceEnclosedFrom, to);
answer = answer.replace(from, to);
return answer;
}

private static String expression(HelmParameter parameter) {
final String value = Optional.ofNullable(parameter.getParameter().getValue()).map(StringUtils::trimToEmpty).orElse("");
if (value.matches(GOLANG_EXPRESSION_REGEX)) {
return value;
}
String defaultExpression = "";
String required = "";
String value = parameter.getParameter().getValue();
if (value != null) {
if (StringUtils.isNotBlank(value)) {
defaultExpression = " | default \"" + value + "\"";
}
if (Boolean.TRUE.equals(parameter.getParameter().getRequired())) {
required = "required \"A valid .Values." + parameter.getHelmName() + " entry required!\" ";
}
final String to = "{{ " + required + ".Values." + parameter.getHelmName() + defaultExpression + " }}";
answer = answer.replace(quotedBraceEnclosedFrom, to);
answer = answer.replace(braceEnclosedFrom, to);
answer = answer.replace(from, to);
return answer;
return "{{ " + required + ".Values." + parameter.getHelmName() + defaultExpression + " }}";
}

private static void interpolateTemplateParameterExpressionsWithHelmExpressions(File file, List<HelmParameter> helmParameters) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static void assertYamls() throws Exception {
final Map<String, ?> expectedContent = mapper.readValue(replacePlaceholders(expected), Map.class);
final Map<String, ?> actualContent =
mapper.readValue(replacePlaceholders(generatedYamls.resolve(expectations.relativize(expected))), Map.class);
assertThat(expectedContent).isEqualTo(actualContent);
assertThat(actualContent).isEqualTo(expectedContent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ items:
jkube/it: This is a test
helm-variable: {{ required "A valid .Values.global_template_env_var entry required!" .Values.global_template_env_var | default "This is a sample" }}
escape-test: "{{"{{"}} {{"}}"}} should be escaped to prevent helm errors"
helm-golang-expression: {{ .Values.unused_value | upper | quote }}
labels:
app: test
provider: jkube
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# Red Hat, Inc. - initial API and implementation
#

golang_expression: "{{ .Values.unused_value | upper | quote }}"
global_template_env_var: This is a sample
limits_memory: 512Mi
unused_value: the-value
requests_memory: 256Mi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# Red Hat, Inc. - initial API and implementation
#

golang_expression: "{{ .Values.unused_value | upper | quote }}"
global_template_env_var: This is a sample
limits_memory: 512Mi
unused_value: the-value
requests_memory: 256Mi
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ parameters:
- name: limits_memory
value: 512Mi
- name: requests_memory
value: 256Mi
value: 256Mi
- name: unused_value
value: the-value
- name: golang_expression
value: "{{ .Values.unused_value | upper | quote }}"
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ items:
jkube/it: This is a test
helm-variable: ${GLOBAL_TEMPLATE_ENV_VAR}
escape-test: "{{ }} should be escaped to prevent helm errors"
helm-golang-expression: ${golang_expression}
labels:
app: test
provider: jkube
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
jkube.helm.sh/provided-value: {{ .Values.i_should_be_quoted | upper | quote }}
jkube.helm.sh/provided-value-scalar: {{ .Values.i_should_be_quoted | upper | quote }}
jkube.io/git-url: "@ignore@"
jkube.io/git-commit: "@ignore@"
jkube.io/git-branch: "@ignore@"
Expand All @@ -26,6 +28,7 @@ metadata:
version: "@ignore@"
group: org.eclipse.jkube
name: helm-and-fragments
namespace: {{ .Release.Namespace }
spec:
replicas: 1
revisionHistoryLimit: 2
Expand All @@ -36,7 +39,10 @@ spec:
group: org.eclipse.jkube
template:
metadata:
namespace: {{ .Release.Namespace }
annotations:
jkube.helm.sh/provided-value: {{ .Values.i_should_be_quoted | upper | quote }}
jkube.helm.sh/provided-value-scalar: {{ .Values.i_should_be_quoted | upper | quote }}
jkube.io/git-url: "@ignore@"
jkube.io/git-commit: "@ignore@"
jkube.io/git-branch: "@ignore@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
#

---
golang_expression: "{{ .Values.i_should_be_quoted | upper | quote }}"
i_should_be_quoted: I'm a quoted uppercase value
golang_expression_scalar: "{{ .Values.i_should_be_quoted | upper | quote }}\n"
limits_memory: 512Mi
helm_namespace: "{{ .Release.Namespace }}\n"
requests_memory: 256Mi
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@

apiVersion: apps/v1
kind: Deployment
metadata:
namespace: ${helm_namespace}
annotations:
jkube.helm.sh/provided-value: ${golang_expression}
jkube.helm.sh/provided-value-scalar: ${golang_expression_scalar}
spec:
template:
metadata:
namespace: ${helm_namespace}
spec:
containers:
- env:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,14 @@ parameters:
- name: limits_memory
value: "512Mi"
- name: parameter_with_no_value
- name: unused_parameter
- name: helm_namespace
value: |
{{ .Release.Namespace }}
- name: i_should_be_quoted
value: I'm a quoted uppercase value
- name: golang_expression
value: "{{ .Values.i_should_be_quoted | upper | quote }}"
- name: golang_expression_scalar
value: >
{{ .Values.i_should_be_quoted | upper | quote }}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import org.eclipse.jkube.maven.it.Verify
}

// Verify k8s:helm output
["templates/helm-and-fragments-deployment"].each {
[
"Chart", "values", "templates/helm-and-fragments-deployment"
].each {
Verify.verifyResourceDescriptors(
new File(basedir, sprintf("/target/jkube/helm/helm-and-fragments/kubernetes/%s.yaml", it)),
new File(basedir, sprintf("/expected/helm/%s.yaml", it)))
Expand Down
17 changes: 17 additions & 0 deletions quickstarts/maven/spring-boot-helm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ Helm chart can also be customized through `pom.xml` plugin configuration:
</plugin>
```

## Development environment

This quickstart assumes the scenario where you have a project that will be deployed to production (Outer loop) using
Helm charts. For this purpose you would run the previously mentioned goals (`k8s:resource k8s:helm`) to generate the
charts. You might even want to configure your CI pipeline to publish the charts for you (`k8s:helm-push`)

However, developer might want to use JKube to test and deploy your project to a development environment
cluster (Inner loop). For this purpose, the placeholders defined for your Helm variables need to be completed with some
values. This showcases the power of JKube, because the same fragments and generated YAMLs can be used for those purposes.

In this example you'll find an additional Maven profile `dev` that includes the values for those placeholders, so in case
you are using this in a development environment, you can also take advantage of JKube.

```shell
$ mvn k8s:resource k8s:apply -Pdev -Pkubernetes
```

## How to test

### Docker build strategy (default)
Expand Down
10 changes: 10 additions & 0 deletions quickstarts/maven/spring-boot-helm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@
</build>

<profiles>
<profile>
<id>dev</id>
<properties>
<helm_namespace>default</helm_namespace>
<golang_expression>n/a development</golang_expression>
<golang_expression_scalar>n/a development</golang_expression_scalar>
<limits_memory/>
<requests_memory/>
</properties>
</profile>
<profile>
<id>kubernetes</id>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@
#

metadata:
namespace: ${helm_namespace}
labels:
project: ${project.artifactId}
hystrix.enabled: true
hystrix.cluster: default
version: ${project.version}
annotations:
jkube.helm.sh/expression-example: ${golang_expression}
jkube.helm.sh/expression-example-scalar: ${golang_expression_scalar}
spec:
replicas: 1
template:
metadata:
namespace: ${helm_namespace}
labels:
project: ${project.artifactId}
hystrix.enabled: true
Expand Down
12 changes: 11 additions & 1 deletion quickstarts/maven/spring-boot-helm/src/main/jkube/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ parameters:
- name: limits_memory
value: "512Mi"
- name: requests_memory
value: "256Mi"
value: "256Mi"
- name: helm_namespace
value: |
{{ .Release.Namespace }}
- name: i_should_be_quoted
value: I need quotes
- name: golang_expression
value: "{{ .Values.i_should_be_quoted | upper | quote }}"
- name: golang_expression_scalar
value: >
{{ .Values.i_should_be_quoted | upper | quote }}

0 comments on commit c01c5c3

Please sign in to comment.