unbroken-dome/gradle-helm-plugin

filtering doesn't work in Charts.yaml file

Closed this issue · 6 comments

A sample chart file


apiVersion: v2
name: ${chartName}
description: A Helm chart to deploy ${chartName} in Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.0.0

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: ${projectVersion}

${chartDependencies}

And in the build.gradle.kts file if you provide

filtering {
                values.put("chartDependencies", "chart-dependencies")
}

Getting the following exception

image

KK

The reason why its failing is because of the following code

image

The Chart.yaml cannot pass through

    fun filterSources() {
        val result = project.sync { spec ->
            spec.from(sourceDir)
            spec.into(targetDir)
            spec.applyChartInfoOverrides()  // Chart.yaml will only pass through this filter
            spec.applyFiltering() // It will not pass through this filter.
        }
        didWork = result.didWork
    }

One way to work around this issue is to do the following

tasks.withType(org.unbrokendome.gradle.plugins.helm.tasks.HelmFilterSources::class.java) {
    overrideChartInfo.set(false)
}

This will turn off the chartInfoOverride and it will pass through the second filter which will filter all values

However the Chart.yaml file will now show an error

image

I think thats ok since the final package file will be ok after running helmPackage

But the recommended approach or suggestion would be to be make

private fun CopySpec.applyChartInfoOverrides() {
        if (overrideChartInfo.get()) {
            filesMatching("Chart.yaml") { details ->
                details.filterYaml(
                    "name" to chartName.get(),
                    "version" to chartVersion.get()
                )
               
               
                val values = filtering.values.get()
                val valuesFromFiles = filtering.fileValues.get()
                     .mapValues { (_, value) ->
                          project.files(value).singleFile.readText()
                      }
                 details.expand(valuesFromFiles + values, true)
            }
        }
    }

This would make the Chart.yaml file be filtered by both filterYaml method and expand method.

I havent tested the above fix just my assumption.

I'd think this another occurence of the problems reported here: #117

Hi,

fixed several issues related to this in the above PR:

  • the "stream closed" error is caused by my implementation of filtering "swallowing" other errors when reading the file -- in your case the problem is that the insertion of ${chartDependencies} leads to an invalid YAML so it cannot be parsed
  • I added the overrideChartInfo property to the HelmChart as well, so you don't need to modify the HelmFilterSources task directly anymore
  • I changed the order of the filter operations so that the templating is applied before the YAML overrides (like you suggested in your code snippet). Still, with your example this would resolve ${chartDependencies} to the string "chart-dependencies" which would still result in an invalid YAML.

Running with these changes now I get a proper error message with your example:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':helmFilterExampleChartSources'.
> Could not copy file '/Users/till/git/unbroken-dome/helm-plugin-filtering-issue/src/main/example/Chart.yaml' to '/Users/till/git/unbroken-dome/helm-plugin-filtering-issue/build/tmp/helm/filtered/example/Chart.yaml'.
   > while scanning a simple key
      in 'reader', line 26, column 1:
         chart-dependencies
         ^
     could not find expected ':'
      in 'reader', line 27, column 1:
         
         ^