From 8a679c906c51d65c3d51ea9f139076367215c6c3 Mon Sep 17 00:00:00 2001 From: Simon Flandergan Date: Mon, 2 Oct 2023 17:13:55 +0200 Subject: [PATCH 1/2] Allow excluding files from property replacement --- CHANGELOG.md | 2 + README.md | 51 +++++++++++++++++-- pom.xml | 6 +++ .../com/deviceinsight/helm/PackageMojo.kt | 6 ++- .../deviceinsight/helm/PropertyReplacement.kt | 35 +++++++++++++ .../helm/PropertyReplacementTest.kt | 35 +++++++++++++ 6 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/com/deviceinsight/helm/PropertyReplacement.kt create mode 100644 src/test/kotlin/com/deviceinsight/helm/PropertyReplacementTest.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index f45593d..e6b25c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +* Support excluding files from property replacement. + ## Version 2.13.0 * Support escaping placeholders with backslash e.g. `\${MY_ENV_VAR}` will no longer be expanded diff --git a/README.md b/README.md index b0f441b..69be082 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ that the correct docker image is used. An example snippet: ## Configuration | Property | Default | Description | -|---|---|---| +|---|--|---| | chartName | The Maven `artifactId` | The name of the chart | | chartVersion | `${project.model.version}` | The version of the chart | | chartRepoUrl | `null` | The URL of the Chart repository where dependencies are required from and where charts should be published to | @@ -95,6 +95,8 @@ that the correct docker image is used. An example snippet: | extraValuesFiles | None | a list of additional values files that can be generated dynamically and will be merged with the values.yaml during [Package](#package). | | outputFile | target/test-classes/helm.yaml | output file for [template goal](#template) | | deployAtEnd | `false` | If true, the helm chart is deployed at the end of a multi-module Maven build. This option does not make sense for single-module Maven projects. | +| propertyReplacement | Empty | Structure to configure property replacement performed on the chart files | +| propertyReplacement.exclusions | Empty | List of file extensions that should be excluded from property replacement. The expressions are by default glob expressions | ## Goals @@ -131,11 +133,11 @@ To use the `deployAtEnd` functionality it's mandatory to put the Helm Maven Plug com.deviceinsight.helm helm-maven-plugin - 2.11.1 + 2.14.0 my-chart https://charts.helm.sh/stable - 3.5.2 + 3.13.0 true src/test/helm/my-chart/values.yaml true @@ -155,6 +157,49 @@ To use the `deployAtEnd` functionality it's mandatory to put the Helm Maven Plug ``` +### Exclude files from property replacement + +To exclude files from property replacement, you can use the `propertyReplacement` configuration. +Any `exclusion` matching will exclude the file from property replacement. +By default glob expressions are used. +You can also use regular expressions by prefixing the expression with `regex`. + +```xml + + + ... + + com.deviceinsight.helm + helm-maven-plugin + 2.14.0 + + my-chart + https://charts.helm.sh/stable + 3.13.0 + true + src/test/helm/my-chart/values.yaml + + + **/grafana-dashboards/**/*.json + regex:.*/grafana-dashboards/.*\.json + + + + + + + package + lint + template + deploy + + + + + + +``` + ## Troubleshooting 1. _**Problem**_ diff --git a/pom.xml b/pom.xml index 9189b45..ac7c2c2 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,7 @@ 1.20.0 1.6.13 3.1.0 + 2.22.2 @@ -238,6 +239,11 @@ + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + diff --git a/src/main/kotlin/com/deviceinsight/helm/PackageMojo.kt b/src/main/kotlin/com/deviceinsight/helm/PackageMojo.kt index f38363c..03c7009 100644 --- a/src/main/kotlin/com/deviceinsight/helm/PackageMojo.kt +++ b/src/main/kotlin/com/deviceinsight/helm/PackageMojo.kt @@ -40,7 +40,6 @@ class PackageMojo : ResolveHelmMojo(), ServerAuthentication { companion object { private val PLACEHOLDER_REGEX = Regex("""(\\?)\$\{(.*?)}""") - private val SUBSTITUTED_EXTENSIONS = setOf("json", "tpl", "yml", "yaml") } @Component(role = SecDispatcher::class, hint = "default") @@ -79,6 +78,9 @@ class PackageMojo : ResolveHelmMojo(), ServerAuthentication { @Parameter(property = "extraValuesFiles") private val extraValuesFiles: List = emptyList() + @Parameter(property = "propertyReplacement") + private val propertyReplacement = PropertyReplacement() + @Throws(MojoExecutionException::class) override fun execute() { @@ -164,7 +166,7 @@ class PackageMojo : ResolveHelmMojo(), ServerAuthentication { parentFile.mkdirs() } - if (!SUBSTITUTED_EXTENSIONS.contains(file.extension.lowercase())) { + if (!propertyReplacement.isPropertyReplacementCandidate(file)) { file.copyTo(targetFile, true) return@onEach } diff --git a/src/main/kotlin/com/deviceinsight/helm/PropertyReplacement.kt b/src/main/kotlin/com/deviceinsight/helm/PropertyReplacement.kt new file mode 100644 index 0000000..64d7105 --- /dev/null +++ b/src/main/kotlin/com/deviceinsight/helm/PropertyReplacement.kt @@ -0,0 +1,35 @@ +package com.deviceinsight.helm + +import org.apache.maven.plugins.annotations.Parameter +import java.io.File +import java.nio.file.FileSystems +import java.nio.file.PathMatcher + +class PropertyReplacement { + + @Parameter(property = "exclusions") + var exclusions: List = emptyList() + + private val exclusionPatchMatchers = lazy { exclusions.toPathMatchers() } + + companion object { + private val SUBSTITUTED_EXTENSIONS = setOf("json", "tpl", "yml", "yaml") + private val PATH_MATCHER_PATTERN = Regex("^(glob|regex):.*$") + } + + fun isPropertyReplacementCandidate(file: File): Boolean { + val extension = file.extension.lowercase() + return SUBSTITUTED_EXTENSIONS.contains(extension) + && file.doesNotMatchAnyExclusion() + } + + private fun File.doesNotMatchAnyExclusion() = exclusionPatchMatchers.value.none { it.matches(this.toPath()) } + + private fun List.toPathMatchers(): List = this + .map { prependGlobIfMissing(it) } + .map { FileSystems.getDefault().getPathMatcher(it) } + + private fun prependGlobIfMissing(path: String): String = if (PATH_MATCHER_PATTERN.matches(path)) path else "glob:$path" + +} + diff --git a/src/test/kotlin/com/deviceinsight/helm/PropertyReplacementTest.kt b/src/test/kotlin/com/deviceinsight/helm/PropertyReplacementTest.kt new file mode 100644 index 0000000..97e47f9 --- /dev/null +++ b/src/test/kotlin/com/deviceinsight/helm/PropertyReplacementTest.kt @@ -0,0 +1,35 @@ +package com.deviceinsight.helm + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import java.io.File + +class PropertyReplacementTest { + + @ParameterizedTest + @ValueSource(strings = ["test.txt", "test.xml"]) + fun `isPropertyReplacementCandidate should return false if file extension is not in inclusion list`(filename: String) { + val file = File(filename) + val propertyReplacement = PropertyReplacement() + assertThat(propertyReplacement.isPropertyReplacementCandidate(file)).isFalse + } + + @ParameterizedTest + @ValueSource(strings = ["test.json", "test.tpl", "test.yml", "test.yaml"]) + fun `isPropertyReplacementCandidate should return true if file extension is in inclusion list`(filename: String) { + val file = File(filename) + val propertyReplacement = PropertyReplacement() + assertThat(propertyReplacement.isPropertyReplacementCandidate(file)).isTrue + } + + @ParameterizedTest + @ValueSource(strings = ["dashboards/test.json", "templates/test.json", "templates/default/test.json"]) + fun `isPropertyReplacementCandidate should return false if file matches exclusion pattern`(filename: String) { + val file = File(filename) + val propertyReplacement = PropertyReplacement() + propertyReplacement.exclusions = + listOf("regex:dashboards/.*\\.json", "glob:templates/*.json", "templates/**/*.json") + assertThat(propertyReplacement.isPropertyReplacementCandidate(file)).isFalse + } +} From 3937c325e981c2d2e77fbaaf48d1fe4e6ed40942 Mon Sep 17 00:00:00 2001 From: Simon Flandergan Date: Mon, 2 Oct 2023 17:44:12 +0200 Subject: [PATCH 2/2] Add missing comma to README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 69be082..de12db6 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ To use the `deployAtEnd` functionality it's mandatory to put the Helm Maven Plug To exclude files from property replacement, you can use the `propertyReplacement` configuration. Any `exclusion` matching will exclude the file from property replacement. -By default glob expressions are used. +By default, glob expressions are used. You can also use regular expressions by prefixing the expression with `regex`. ```xml