Skip to content

Commit

Permalink
fix(helm): HelmService should check in templates/test for template in…
Browse files Browse the repository at this point in the history
…terpolation (3398)

fix (jkube-kit/helm) : HelmService should check in nested directories while interpolating chart templates

Follow up to eclipse-jkube#3346

+ Modify HelmService's interpolateChartTemplates method to look for
  chart templates recursively as now chart templates can also be test
  templates that usually reside in `templates/tests/` directory.
+ We already have a method named `listYamls` in YamlUtil that lists yaml
  files in a specified directory. Add another method that would do the
  recursive listing based on boolean flag

Signed-off-by: Rohan Kumar <[email protected]>
---
review: helm test directory file interpolation

Signed-off-by: Marc Nuri <[email protected]>

Co-authored-by: Marc Nuri <[email protected]>
  • Loading branch information
rohanKanojia and manusa authored Sep 25, 2024
1 parent dad4cc6 commit 72bac63
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,11 @@ private static void interpolateTemplateParameterExpressionsWithHelmExpressions(F
}

private static void interpolateChartTemplates(List<HelmParameter> helmParameters, File templatesDir) throws IOException {
// now lets replace all the parameter expressions in each template
for (File file : listYamls(templatesDir)) {
interpolateTemplateParameterExpressionsWithHelmExpressions(file, helmParameters);
// now let's replace all the parameter expressions in each template
for (File directory : new File[]{templatesDir, new File(templatesDir, "tests")}) {
for (File file : listYamls(directory)) {
interpolateTemplateParameterExpressionsWithHelmExpressions(file, helmParameters);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,37 @@ void whenHelmTestFragmentProvided_thenFragmentCopiedToTemplatesTestDir() throws
.hasFieldOrPropertyWithValue("command", Collections.singletonList("wget"))
.hasFieldOrPropertyWithValue("args", Collections.singletonList("name-configured-via-properties:79"));
}

@Test
@DisplayName("when helm test fragment provided with helm parameters, then helm test resource added and interpolated to templates/tests directory")
void whenHelmTestFragmentAndHelmParameters_thenFragmentCopiedToTemplatesTestDirAndHelmParametersInterpolated() throws IOException {
// Given
helmConfig.parameters(Arrays.asList(
HelmParameter.builder().name("application.name").value("name-configured-via-parameters").build(),
HelmParameter.builder().name("application.port").value("79").build(),
HelmParameter.builder().name("image").value("{{ .Values.image | default \"busybox\" }}").build()
));
resourceServiceConfig = ResourceServiceConfig.builder().resourceDirs(Collections.singletonList(
new File(Objects.requireNonNull(getClass().getResource("/helm-test-fragment-with-parameters")).getFile())))
.build();
// When
new HelmService(jKubeConfiguration, resourceServiceConfig, new KitLogger.SilentLogger())
.generateHelmCharts(helmConfig.build());
// Then
assertThat(new String(Files.readAllBytes(helmOutputDirectory.resolve("kubernetes").resolve("templates").resolve("tests").resolve("test-connection.yaml"))))
.isEqualTo(String.format("---%n" +
"apiVersion: v1%n" +
"kind: Pod%n" +
"metadata:%n" +
" annotations:%n" +
" helm.sh/hook: test%n" +
" name: \"{{ .Values.application.name }}-test-connection\"%n" +
"spec:%n" +
" containers:%n" +
" - image: {{ .Values.image | default \"busybox\" }}%n" +
" args:%n" +
" - \"{{ .Values.application.name }}:{{ .Values.application.port }}\"%n"));
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Pod
metadata:
name: ${application.name}-test-connection
annotations:
"helm.sh/hook": test
spec:
containers:
- image: ${image}
args: ['${application.name}:${application.port}']

0 comments on commit 72bac63

Please sign in to comment.