Configmap data is blank - kubernetes

I am creating a ConfigMap using this YAML file:
apiVersion: v1
kind: ConfigMap
metadata:
name: "{{ .Values.configmapName}}-config"
labels:
app: "{{ .Values.configmapName}}"
data:
{{ (.Files.Glob "configmap/dev/*").AsConfig | indent 2 }}
The ConfigMap gets created but there is no data, the ConfigMap is blank. I have multiple files under a configmap/dev/ directory located at the root level of my Helm chart. I'm expecting to see the data from the files which are under this configmap/dev/ directory.

Related

Helm Variables inside ConfigMap File

So I had a ConfigMap with a json configuration file in it, like this:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config-map
data:
config.json: |+
{
"some-url": "{{ .Values.myApp.someUrl }}"
}
But I've moved to having my config files outside the ConfigMap's yaml, and just referencing them there, like this:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config-map
data:
config.json: |-
{{ .Files.Get .Values.myApp.configFile | indent 4 }}
But now I want my json to look like the following
{
"some-url": "{{ .Values.myApp.someUrl }}"
}
The only thing I tried is what I just showed. I 'm not even sure how to look for this answer.
Is it even possible?
At the time of reading the file, its content is a string. It's not evaluated as template, and therefore you cannot use variables like you do.
However, helm has a function for this purpose specifically called tpl:
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config-map
data:
config.json: |-
{{ tpl (.Files.Get .Values.myApp.configFile) $ | indent 4 }}
The tpl function takes a template string and renders it with some context. This is useful when you have template snippets in your values file or like in your case in some files content.

Config Map in Helm

I am creating a configMap from CSV file using helm tempate but it is getting create different from OC command.
Heml Template:
apiVersion: v1
kind: ConfigMap
metadata:
name: service-config
namespace: {{ .Values.namespace }}
labels:
app: {{ .Values.appname }}
data:
{{- $path := printf "%s/application-config/*" .Values.env }}
{{ (.Files.Glob $path).AsConfig | indent 2 }}
Generated Configmap
kind: ConfigMap
apiVersion: v1
metadata:
name: service-config
namespace: ''
labels:
app.kubernetes.io/managed-by: Helm
data:
esm-instrument-type-map.csv: |-
M-MKT,COB
CMO,COB
MUNI,COB
WARRNT,EQU
PFD,EQU
OC Command :
oc create configmap test-config --from-file= ./esm-instrument-type-map.csv
Generated ConfigMap
kind: ConfigMap
apiVersion: v1
metadata:
name: test-config
namespace: ''
data:
esm-instrument-type-map.csv: "CORP,COB\r\nEQUITY,EQU\r\nGOVT,TRY\r\nMBS,FNM\r\nST-PRD,COB\r\nM-MKT,COB\r\nCMO,COB\r\nMUNI,COB\r\nWARRNT,EQU\r\nPFD,EQU"
As we see, data from the CSV file is in double quotes, when generated by the OC command. I want the same in helm. How can I achieve this?

Usage of Variable Chart.Name in inherited Helm Chart

I've created a helm chart which contains some resources, which are reused in several other Helm charts:
base/templates/base.yaml
apiVersion: secrets-store.csi.x-k8s.io/v1alpha1
kind: SecretProviderClass
metadata:
name: {{ .Chart.Name }}
Then I've created a helm chart which inherits the base chart and contains some special resources:
sub1/templates/sub1.yaml
...
name: {{ .Chart.Name }}
Actual Output
In the actual output the resources of the base chart use always the chart name of the base chart.
---
# Source: sub1/templates/sub1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: sub1
---
# Source: sub1/charts/base/templates/base.yaml
apiVersion: secrets-store.csi.x-k8s.io/v1alpha1
kind: SecretProviderClass
metadata:
name: base
Wanted output
But I want the chart name of the sub chart to be used in the base chart resources.
# Source: sub1/charts/base/templates/base.yaml
...
kind: SecretProviderClass
metadata:
name: sub1
How can I achieve this?
A solution is to reuse the resources via named templates:
base/templates/base.yaml
{{- define "base-lib.secret-provider-class" -}}
apiVersion: secrets-store.csi.x-k8s.io/v1alpha1
kind: SecretProviderClass
metadata:
name: {{ .Chart.Name }}
{{- end -}}
sub1/templates/sub1.yaml
{{ include "base-lib.secret-provider-class" . }}
---
...

Files.Get concatenated string value appears empty after helm template in Kubernetes Configmap

I'm using a configmap with a dynamic filename defined as below. However, after I do helm template the value for the filename is empty:
apiVersion: v1
kind: ConfigMap
metadata:
name: krb5-configmap
data:
krb5.conf: |-
{{ .Files.Get (printf "%s//krb5-%s.conf" .Values.kerberosConfigDirectory .Values.environment) | indent 4 }}
kerberosConfigDirectory: kerberos-configs (set in Values.yaml)
Folder structure:
k8s:
templates
configmap.yaml
kerberos-configs
krb5-dev.conf
After helm template the data value looks like this:
data:
krb5.conf: |-
--
I can't figure out why the value for the filename is empty. Note that I'm able to run the helm template command successfully.
You have an extra / and extra indentation in you file. Working example:
apiVersion: v1
kind: ConfigMap
metadata:
name: krb5-configmap
data:
krb5.conf: |-
{{ .Files.Get (printf "%s/krb5-%s.conf" .Values.kerberosConfigDirectory .Values.environment) | indent 4 }}

How can I generate a configmap from a directory of files that need to be templated?

I can generate the ConfigMap from the directory but they are not translating the template directives or values. Below is an example of the Release.Namespace template directive not being output in the ConfigMap.
.
|____Chart.yaml
|____charts
|____.helmignore
|____templates
| |____my-scripts.yaml
|____values.yaml
|____test-files
|____test1.txt
---
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-scripts
namespace: {{ .Release.Namespace }}
data:
test.txt: |-
{{ .Files.Get "test-files/test1.txt" | indent 4}}
# test-files/test1.txt
test file
{{ .Release.Namespace }}
When I run helm install . --dry-run --debug --namespace this-should-print here's what I'm getting vs what I'm expecting:
Actual:
---
# Source: test/templates/my-scripts.yaml
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-scripts
namespace: test
data:
test.txt: |-
# test-files/test1.txt
test file
{{ .Release.Namespace }}
Expected:
---
# Source: test/templates/my-scripts.yaml
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-scripts
namespace: test
data:
test.txt: |-
# test-files/test1.txt
test file
this-should-print
Alternatively, I would be interested in every file in a specified directory being output in the format like:
<filename>: |-
<content>
I've found a way of doing it using the tpl function:
---
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-scripts
namespace: {{ .Release.Namespace }}
data:
test.txt: |-
{{ tpl ( .Files.Get "test-files/test1.txt" ) . | indent 4 }}
The new output is exactly as expected:
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-scripts
namespace: this-should-print
data:
test.txt: |-
# test-files/test1.txt
test file
this-should-print
And for bonus points, getting all files from a directory without having to update this list within the config map:
---
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-scripts
namespace: {{ .Release.Namespace }}
data:
{{ tpl (.Files.Glob "groovy-scripts/*").AsConfig . | indent 4 }}
.Files.Get will take the raw contents of those files and dump it into your resulting YAMLs, if you want those file contents to be subject to Helm template rendering themselves, this approach won't work. You can instead create named templates and then include them within other templates.
Directory Structure: tree
.
├── Chart.yaml
├── templates
│   ├── _test1.tpl
│   └── my-scripts.yaml
└── values.yaml
Template: cat templates/my-scripts.yaml
---
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-scripts
namespace: {{ .Release.Namespace }}
data:
test.txt: |-
{{- include "mychart.test1" . | indent 4 }}
Helper: cat templates/_test1.tpl
{{- define "mychart.test1" }}
test file
{{ .Release.Namespace }}
{{- end }}
Result: helm template . --namespace this-should-print
---
# Source: helm/templates/my-scripts.yaml
---
# templates/myscripts.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-scripts
namespace: this-should-print
data:
test.txt: |-
test file
this-should-print