Skip to content

Commit

Permalink
Merge pull request #188 from deviceinsight/feature/extra-values-files
Browse files Browse the repository at this point in the history
add option to provide additional values files
  • Loading branch information
pvorb authored Mar 7, 2023
2 parents 9718f51 + db9cd79 commit 6af00d5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

* Converted README and CHANGELOG to Markdown
* Add option to provide additional values files

## Version 2.11.1

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ that the correct docker image is used. An example snippet:
| stableRepoUrl | `"https://charts.helm.sh/stable"` | For helm 2.x: Can be used to overwrite the default URL for stable repository during `helm init` |
| strictLint | `false` | If true, linting fails on warnings (see: [Lint](#lint)) |
| valuesFile | None | values file that should be used for goals [Lint](#lint), [Template](#template) |
| 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. |

Expand Down
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<maven.version>3.6.2</maven.version>
<maven-project.version>2.2.1</maven-project.version>

<jackson-bom.version>2.14.2</jackson-bom.version>
<httpclient.version>4.5.14</httpclient.version>
<plexus-sec-dispatcher.version>1.4</plexus-sec-dispatcher.version>

Expand All @@ -44,6 +45,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>${jackson-bom.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -95,6 +103,15 @@
<version>${plexus-sec-dispatcher.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
42 changes: 42 additions & 0 deletions src/main/kotlin/com/deviceinsight/helm/PackageMojo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
package com.deviceinsight.helm

import com.deviceinsight.helm.util.ServerAuthentication
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
import org.apache.maven.plugin.MojoExecutionException
import org.apache.maven.plugins.annotations.Component
import org.apache.maven.plugins.annotations.LifecyclePhase
Expand All @@ -25,6 +29,8 @@ import org.apache.maven.plugins.annotations.Parameter
import org.apache.maven.settings.Settings
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher
import java.io.File
import java.io.IOException


/**
* Packages helm charts
Expand Down Expand Up @@ -70,6 +76,9 @@ class PackageMojo : ResolveHelmMojo(), ServerAuthentication {
@Parameter(defaultValue = "\${settings}", readonly = true)
override lateinit var settings: Settings

@Parameter(property = "extraValuesFiles")
private val extraValuesFiles: List<String> = emptyList()

@Throws(MojoExecutionException::class)
override fun execute() {

Expand Down Expand Up @@ -98,6 +107,7 @@ class PackageMojo : ResolveHelmMojo(), ServerAuthentication {
log.info("Created target helm directory")

processHelmConfigFiles(targetHelmDir)
mergeValuesFiles(targetHelmDir, extraValuesFiles)

val helmAddFlags = if (isHelm2 || !forceAddRepos) emptyList() else listOf("--force-update")

Expand Down Expand Up @@ -183,6 +193,38 @@ class PackageMojo : ResolveHelmMojo(), ServerAuthentication {
}
}

private fun mergeValuesFiles(targetHelmDir: File, extraValuesFiles: List<String>) {

if (extraValuesFiles.isEmpty()) {
return
}

val missingFiles = extraValuesFiles.filter { !File(it).exists() }
if (missingFiles.isNotEmpty()) {
throw IllegalStateException("extraValueFiles not found: $missingFiles")
}

val allValuesFiles = extraValuesFiles.toMutableList()
val valuesFile = targetHelmDir.resolve("values.yaml")

if (valuesFile.exists()) {
allValuesFiles.add(0, valuesFile.absolutePath)
}

val yamlMapper = ObjectMapper(YAMLFactory().enable(YAMLGenerator.Feature.MINIMIZE_QUOTES))
val values: ObjectNode = yamlMapper.createObjectNode()
val yamlReader = yamlMapper.setDefaultMergeable(true).readerForUpdating(values)

try {
for (file in allValuesFiles) {
yamlReader.readValue(File(file), ObjectNode::class.java)
}
yamlMapper.writeValue(valuesFile, values)
} catch (e: IOException) {
throw RuntimeException(e)
}
}

private fun findPropertyValue(property: String, fileName: String): CharSequence? {
val result = when (property) {
"project.version" -> project.version
Expand Down

0 comments on commit 6af00d5

Please sign in to comment.