Skip to content

Instantly share code, notes, and snippets.

@Camusensei
Last active June 22, 2020 22:22
Show Gist options
  • Save Camusensei/20c5b051f32f6aa20efbedc359e3b3de to your computer and use it in GitHub Desktop.
Save Camusensei/20c5b051f32f6aa20efbedc359e3b3de to your computer and use it in GitHub Desktop.

I have an issue when trying to use a file from a library chart. Helm fails when I try to access a file from the library. I have followed the example from theΒ library_charts documentation

Everything is the same as the documentation except two parts: I have added the file mylibchart/files/foo.conf and this file is referenced in mylibchart/templates/_configmap.yaml's data key (in the documentation, data is an empty object)

.
β”œβ”€β”€ mychart
β”‚   β”œβ”€β”€ Chart.yaml
β”‚   └── templates
β”‚       └── configmap.yaml
└── mylibchart
    β”œβ”€β”€ Chart.yaml
    β”œβ”€β”€ files
    β”‚   └── foo.conf
    └── templates
        β”œβ”€β”€ _configmap.yaml
        └── _util.yaml

When I run the following commands, I would expect to see:

$ cd mychart
$ helm dependency update
Saving 1 charts
Deleting outdated charts
$ helm template .
---
# Source: mychart/templates/configmap.yaml
apiVersion: v1
data:
  foo.conf: mylibchart/files/foo.conf's content
  fromlib: true
  myvalue: Hello World
kind: ConfigMap
metadata:
  name: mychart-RELEASE-NAME

But what I get is:

$ helm template .
---
# Source: mychart/templates/configmap.yaml
Error: 'error converting YAML to JSON: yaml: line 8: did not find expected key'
data:
  myvalue: Hello World

This error is caused by the fact that mychart/files/foo.conf does not exist. If I create it, it yields the expected result... But it's not mychart/files/foo.conf's content that I want but mylibchart/files/foo.conf's content! I checked and the file foo.conf does exist inside the file generated by "helm dependency update":

$ tar -tf mychart/charts/mylibchart-0.1.0.tgz
mylibchart/Chart.yaml
mylibchart/templates/_configmap.yaml
mylibchart/templates/_utils.yaml
mylibchart/files/foo.conf

How can I use that file from the .tgz file?

apiVersion: v2
name: mychart
description: A Helm chart for 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.1.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.
appVersion: 1.16.0
dependencies:
- name: mylibchart
version: 0.1.0
repository: file://../mylibchart
{{- include "mylibchart.configmap" (list . "mychart.configmap") -}}
{{- define "mychart.configmap" -}}
data:
myvalue: "Hello World"
{{- end -}}
apiVersion: v2
name: mylibchart
description: A Helm chart for 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: library
# 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.1.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.
appVersion: 1.16.0
mylibchart/files/foo.conf's content
{{- define "mylibchart.configmap.tpl" -}}
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ .Release.Name | printf "%s-%s" .Chart.Name }}
data:
myvalue: "from mylibchart"
fromlib: yes
{{ (.Files.Glob "files/foo.conf").AsConfig | nindent 2 }}
{{- end -}}
{{- define "mylibchart.configmap" -}}
{{- include "mylibchart.util.merge" (append . "mylibchart.configmap.tpl") -}}
{{- end -}}
{{- /*
mylibchart.util.merge will merge two YAML templates and output the result.
This takes an array of three values:
- the top context
- the template name of the overrides (destination)
- the template name of the base (source)
*/ -}}
{{- define "mylibchart.util.merge" -}}
{{- $top := first . -}}
{{- $overrides := fromYaml (include (index . 1) $top) | default (dict ) -}}
{{- $tpl := fromYaml (include (index . 2) $top) | default (dict ) -}}
{{- toYaml (merge $overrides $tpl) -}}
{{- end -}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment