Helm 3: Access /files of library chart - kubernetes-helm

I have a library chart that is used as a dependency in multiple other charts. I already share a few named templates using this. I have a few files that I also want to share using the library chart. The content of the file should end up in a config map.
I tried adding a /files directory to the library chart (next to the /templates directory), placed the files inside and used the following named template
{{- define "lib-chart.all-files-as-data" -}}
data:
{{- range $path, $bytes := $.Files.Glob "files/*" }}
{{ $path | base }}: |{{ (tpl ($bytes | toString) $ ) | nindent 6 }}
{{- end -}}
{{- end -}}
This however only picks up files from the chart which uses the library chart and not the library chart itself.
Is there a way to access the content of the /files folder of a library chart?
Thanks!

I too came across similar kind of scenario
As i checked in the internet i could able to find a way to access files in library chart
Best possible way is you can create a subchart and extract them to your chart
How to create subchart in helm you see on 1
How to access subchart variables you can read 2

Related

What is the difference between helm syntax {{ something }} and {{- something }}?

I'm trying to understand helm templates and found syntax like this:
{{- if not .Values.autoscaling.enabled }}
replicas: {{ .Values.replicaCount }}
{{- end }}
So I thought every thing had to start with {{- but then I found other syntax that did not have that syntax:
- name: {{ .Chart.Name }}
So my question is what is the difference between those two syntaxs? What does the dash do? When is it needed?
The Helm template syntax is based on the Go programming language's text/template package.
The braces {{ and }} are the opening and closing brackets to enter and exit template logic.
The Helm documentation at https://helm.sh/docs/chart_template_guide/control_structures/
discusses why this syntax is needed in an example.
YAML ascribes meaning to whitespace, so managing the whitespace becomes pretty important. [...] the curly brace syntax {{ of template declarations can be modified with special characters to tell the template engine to chomp whitespace. {{- (with the dash and space added) indicates that whitespace should be chomped left, while -}} means whitespace to the right should be consumed. Be careful! Newlines are whitespace!
So the answer is this. The difference between the {{ syntax and the {{- syntax is that the {{- something }} will result in space on the left being removed. Without this any extra space would be included which could result in incorrectly formatted YAML.
Refer to the Helm documentation which goes into great length about how this syntax works and removes extra spaces.
You'll frequently see the dash showing up in control structures because without this extra space would be added to your YAML file which could result in invalid syntax being created. So, for example,
{{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
apiVersion: networking.k8s.io/v1beta1
{{- else -}}
apiVersion: extensions/v1beta1
{{- end }}
Causes the property apiVersion to be output (in the YAML file) without adding blank lines before and after the property.
Simple example
The Go templating documentation says
when executing the template whose source is
"{{23 -}} < {{- 45}}"
the generated output would be
"23<45"
This shows that the dash syntax causes white space to be removed.
Learning to experiment with Helm syntax
Below I'll explain how you can begin experimenting with helm syntax
using a simple throw away project.
The commands below I create a temp directory testhelm and go into it, and then run create helm mytest to create a helm application.
Next, I create a sample helm YAML file. This is the file you want to put what you want to test inside of. Below I used the file mytest/templates/my.yaml but any file can be created.
Helm apparently takes all of the files in the templates directory and parses/processes them to create the YAML output (which is used to create a Kubernetes YAML file to configure a K8S application).
In our case, we just leverage the helm command create a test bed for us to play around with.
If you are on a UNIX-based system you should be able to copy and paste the entire code sample below to create the testbed to begin experimenting.
mkdir testhelm
cd testhelm
helm create mytest
cat <<EOF > mytest/templates/my.yaml
expression1: "{{ 23 }} < {{ 45 }}"
expression2: "{{ 23 -}} < {{- 45 }}"
aTest0: ArgWithNoSpace
aTest1: Arg with spaces on left and right
aTest2: " spaces-on-left-and-right "
aTest3: {{ " spaces-on-left-and-right " }}
aTest4: {{ " spaces-on-left-and-right " | trim | quote }}
aTest5: Some
{{- "Thing Funky is" -}} goingOn
{{- " here"}}
drink2: {{ .Values.drink2 | default "coffee" | quote }}
aTest6: Some {{ "Thing Funky is" }}goingOn {{ " here"}}
aTest7: Some {{ "Thing Funky is" }}goingOn {{ " here"}}
EOF
Then run run the helm template command as shown below, and study the output that you get.
helm template myproj ./mychart | less
. . . output trimmed . . .
# Source: mychart/templates/my.yaml
expression1: "23 < 45"
expression2: "23<45"
aTest0: ArgWithNoSpace
aTest1: Arg with spaces on left and right
aTest2: " spaces-on-left-and-right "
aTest3: spaces-on-left-and-right
aTest4: "spaces-on-left-and-right"
aTest5: SomeThing Funky isgoingOn here
drink2: "coffee"
aTest6: Some Thing Funky isgoingOn here
aTest7: Some Thing Funky isgoingOn here
The first two name/value pairs expression1 and expression2 show the difference with and without dash syntax being used.
Notice also the syntax for aTest5 which resulted in several lines being merged into a single line of output.
Notice also aTest6 and aTest7 look different in the source but produce the same output; spaces within the {{ }} are not output unless they within quotes.
Using this approach digest the Helm syntax in bite sized chunks and so when you need to fix something you can understand what you are seeing.

can't access helm .Values from a named template with non global context passed in

I'm trying to use a Helm named template that I plan to include with several different contexts, and the template has many values that are the same for all contexts.
Whenever I pass a context to template or include to invoke the named template, the references to .Values do not work, which is understandable because I'm explicitly setting a lower context.
In the Helm documentation for with, it claims there is a "global" variable $ that will allow reference to the global .Values, e.g., {{ $.Values... }}. This does not work (the example below shows the error).
I've also tried defining variables (using :=) and "enclosing" the include inside that variable definition (via indentation - I don't know if it matters) to make that variable available within the named template, but this doesn't work either.
I've also tried putting these in "globals" as described here which is more of a subchart thing and this doesn't work either.
So, I'm out of Helm tricks to make this work and will sadly have to re-define these many same variable many times - which makes the entire named template solution a bit less elegant - or just go back to having largely duplicate partially-parameterized templates.
What am I missing?
$ helm version
Client: &version.Version{SemVer:"v2.9+unreleased", GitCommit:"", GitTreeState:"clean"}
Values.yaml:
---
commonSetting1: "common1"
commonSetting2: "common2"
context1:
setting1: "c1s1"
setting2: "c1s2"
context2:
setting1: "c2s1"
setting2: "c2s2"
deployment.yaml:
---
{{- define "myNamedTemplate" }}
- name: {{ .setting1 }}
image: {{ $.Values.commonSetting1 }}
{{- include "myNamedTemplate" .Values.context1 }}
{{- include "myNamedTemplate" .Values.context2 }}
$ helm template test-0.1.0.tgz
Error: render error in "test/templates/deployment.yaml": template: test/templates/deployment.yaml:7:4: executing "test/templates/deployment.yaml" at <include "myNamedTemp...>: error calling include: template: test/templates/deployment.yaml:4:19: executing "myNamedTemplate" at <$.Values.commonSetti...>: can't evaluate field commonSetting1 in type interface {}
When I do this, I tend to explicitly pass in the top-level context object as a parameter. This gets a little tricky because the Go text/template templates only take a single parameter, so you need to use the (Helm/Sprig) list function to package multiple parameters together, and then the (standard text/template) index function to unpack them.
The template definition would look like:
{{- define "myNamedTemplate" }}
{{- $top := index . 0 }}
{{- $context := index . 1 }}
- name: {{ $context.setting1 }}
image: {{ $top.Values.commonSetting1 }}
{{ end }}
When you invoke it, you would then need to explicitly pass the current context as a parameter:
{{ include "myNamedTemplate" (list . .Values.context1) }}

Use variable in HELM template define function

Is it possible to use a variable inside the template define function? I attempted to wrap the variable in brackets but it seems to fail. Example
{{- define {{ .Chart.Name }}.deployment -}}
The names of template functions are always fixed strings. (This is common with almost all programming languages.) Since these names don't appear anywhere in the rendered YAML, it doesn't really matter what they're called. The only place there's a potential conflict is if your chart includes other subcharts as dependencies, or is included as a subchart; in that case all template functions share the same function namespace.
A common convention is to name templates following the current chart name; that is, matching the fixed string in the Chart.yaml file
{{- define "mychart.deployment" -}}
Using the Helm include function you can call templates with a dynamic name, but this is a somewhat unusual use.
values.yaml
global:
key: {}
deployment: appdeployv1
If you were to expand the name of the chart, you would do it like this
_helpers.tpl
{{- define "key.name" -}}
{{- default .Chart.Name .Values.deployment | trunc 63 | trimSuffix "-" -}}
{{- end -}}
That's all I can understand from your question. I hope this helps. You can try exploring docs and check Declaring and using templates with define and template heading for detailed info.

How to use templates inside templates in helm chart?

I want to use template inside template with "." operator in my helm chart.
Suppose this is my values.yaml
component: my-component
my-component-arguments:
heap_opts: "heap-opts"
Now I want to get "heap_opts", but component name is dynamic.
How can I do something like this using template?
{{ .Values."{{.Values.component}}-arguments".heap_opts }}
You can do this using get spring function combined with index and printf from text/template package
{{ get (index .Values (printf "%s-arguments" .Values.component)) "heap_opts" }}

How can we specify custom path to .Files.Get when creating ConfigMap with Helm

I am creating a config map as below
kubectl create configmap testconfigmap --from-file=testkey=/var/opt/testfile.txt
As I am using helm charts, I would like to create the config map using YAML file instead of running kubectl.
I went through Kubernetes - How to define ConfigMap built using a file in a yaml? and we can use .Files.Get to access the files.
But then testfile.txt needs to be a part of helm. I would like to have something like
kind: ConfigMap
metadata:
name: testconfigmap
data:
fromfile: |-
{{ .Files.Get "/var/opt/testfile.txt" | indent 4 }}
It works when "testfile.txt" is under the main helm directory. So, {{ .Files.Get "testfile.txt" | indent 4 }} works but {{ .Files.Get "/var/opt/testfile.txt" | indent 4 }} doesn't. With custom path, the value for the ConfigMap is empty.
Is is possible to place the file at a custom path outside the helm folder, so I can define my path in Values.yaml and read it in my ConfigMap yaml ?
This is a Community Wiki answer so feel free to edit it and add any additional details you consider important.
As mdaniel has already stated in his comment:
Is is possible to place the file at a custom path outside the helm
folder no, because helm considers that a security risk – mdaniel 2
days ago
You can also compare it with this feature request on GitHub where you can find very similar requirement described in short e.g. in this comment:
I have this exact need. My chart publishes a secret read from file at
/keybase. This file is deliberately not in the chart.
I believe files for .Files.Get should not be assumed to be inside the
chart ...
One interesting comment:
lenalebt commented on Dec 23, 2017 I am quite sure .Files.Get not
being able to access the file system arbitrarily is a security
feature, so I don't think the current behaviour is wrong - it just
does not fulfill all use cases.
This issue was created quite long time ago (Dec 19, 2017) but has been recently reopened. There are even some specific proposals on how it could be handled:
titou10titou10 commented on Apr 2 #misberner can you confirm that
using--include-dir =will allow us to use
.Files.Glob().AsConfig(), and so create a ConfigMap with one
entry in the CM per file in?
#misberner misberner commented on Apr 2 Yeah that's the idea. An open
question from my point of view is whether an --include-dir with a
specified introduces an overlay, or shadows everything under
/ from previous args and from the bundle itself. I'm not super
opinionated on that one but would prefer the former.
The most recent comments give some hope that this feature might become available in future releases of helm.
As mdaniel and mario already mentioned, for now this is not possible, as it's considered a security risk.
But actually there is a workaround.
You can use Helm templating to parse your property file and load it into a ConfigMap.
# create the following ConfigMap in your Chart
# this is just a simple prototype
# it requires strict key=value syntax in your property file (no empty strings etc.)
# but it shows the idea - improve the syntax, if needed
apiVersion: v1
kind: ConfigMap
metadata:
name: example
data:
{{- if .Values.example.map }}
{{- range $line := splitList "\n" .Values.example.map }}
{{- $words := splitList "=" $line }}
{{- $key := index $words 0 | trim }}
{{- $value := rest $words | join "=" | trim }}
{{ $key }}: "{{ $value }}"
{{- end }}
{{- end }}
{{- end }}
And after that you may load your properties file into this ConfigMap.
helm install mychart --set-file example.map="/test/my.properties"
Of course it is safe to use ONLY if you fully control the input, i. e. how each and every line of your property file is populated.