How using Kustomize partially change values in overlay

368 Views Asked by At

I'm looking for an elegant solution for handling different values of helm packages on the overlay level:

  • We have a base folder and, overlay folders stg and prod corresponding to environments
  • I would like to use a helm chart with a long list of values.
    • Part of those values are the same for all environments - common values.
    • Part of those values are different in each environment.

I want to have a value file with common values and a file with additional environment-specific values.

The goal here is to avoid copy-paste common values in each directory.

What I already tried:

  1. Have common_config.yaml in /base/common_config.yaml and in /stg/kustomize.yaml set in helmCharts field valuesFile to ../base/common_config.yaml but it will fail when --load-restrictor is enabled (which is the default).
  2. Use HelmChartInflationGenerator on base kustomize and use patching on this resource to add custom value files - but this has the same issue as above.
  3. I'm aware that I can render helm on the base image and then use patching to change things - the issue is that it requires changing many many fields and is not maintainable.

Perhaps there is a better way to handle such scenarios.

1

There are 1 best solutions below

4
VonC On

We don't use Helm CLI, we use kubectl kustomize test --enable-helm and use helmCharts section of kustomize.yaml

Given that you are using Kustomize's helmCharts and you want to avoid duplication of common values across different environments, you might consider:

  • storing your common configuration in a YAML file that will be used as the base values for the Helm chart.
  • creating, for each environment (e.g., staging, production), overlay directories that include additional, environment-specific values.
  • specifying, in each overlay's kustomization.yaml, the common values file and the environment-specific values file. That will allow Kustomize to merge these values when generating the final configuration for the Helm chart.

That is inspired from "Generate Kubernetes Manifests with Helm Charts using Kustomize" from Tharuka Mannapperuma.

Assuming a project structure similar to this:

project/
│
├── base/
│   └── common_values.yaml
│
├── overlays/
│   ├── stg/
│   │   ├── kustomization.yaml
│   │   └── stg_values.yaml
│   └── prod/
│       ├── kustomization.yaml
│       └── prod_values.yaml

You would have:

  • common_values.yaml, for the values that common across all environments.

  • overlay kustomizations with:

    • stg/kustomization.yaml:

      ```yaml
      resources:
      - ../../base/common_values.yaml
      helmCharts:
      - name: your-helm-chart
          releaseName: chart-release-stg
          valuesFile: stg_values.yaml
      ```
      
    • prod/kustomization.yaml:

      ```yaml
      resources:
      - ../../base/common_values.yaml
      helmCharts:
      - name: your-helm-chart
          releaseName: chart-release-prod
          valuesFile: prod_values.yaml
      ```
      

Kustomize will use the values from common_values.yaml as the base and merge them with the values specified in stg_values.yaml or prod_values.yaml for the respective environments. That is achieved through the valuesFile field in the helmCharts section of the overlay kustomization.yaml. See HelmChartInflationGenerator.

When using kubectl kustomize, the --load-restrictor flag can prevent loading files from outside the kustomization root.

Here, your project is structured in a way where the common values file is always within the kustomization root of each environment.

When you run kubectl kustomize overlays/stg or kubectl kustomize overlays/prod from the project directory, the project directory becomes the root for the kustomization. As a result, referencing ../../base/common_values.yaml from the overlays/stg/kustomization.yaml or overlays/prod/kustomization.yaml is allowed because the base directory is within the scope of the kustomization root (project).