George
George

Reputation: 63

trigger pod restart on configmap change

I have a configmap file defined as follows

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ .Release.Name }}-dashboard-cm-conf
  namespace: {{ .Release.Namespace }}
data:
{{ (tpl (.Files.Glob "conf/*").AsConfig . ) | indent 2 }}

As you can see above, it is taking up the content of the "conf" folder as a data. Everything works fine, however the issue is observed when i am trying to implement "annotations" to trigger a "POD-restart" with the below lines in my "deployment file.

      annotations:
        checksum/config-map: {{ include (print .Template.BasePath "/dashboard-conf-map.yaml") . | sha256sum }}

The POD is not getting restarted, even when content of "conf" folder is changed && a "helm upgrade" is performed.

If i use a simple configmap with data defined as below, a change in parameter and "helm upgrade" does result in POD-restart

data:
  parameter1: testparam1
  parameter2: testparam2

I have the following question in here

  1. Is there a way to perform checksum for the folder.

  2. Is there a way get the output of linux-command like below in the configmap.

find dashboard/conf/ -type f -exec md5sum {} \; |md5sum

  1. Can we perform checksum for a non-YAML file.
      annotations:
        checksum: {{ include (print .Template.BasePath "/logback-spring.xml") . | sha256sum }}

I get the below error Error: YAML parse error on chart-2/charts/dashboar/templates/conf1/logback-spring.xml: error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go value of type releaseutil.SimpleHead

  1. Can we perform checksum on file one directory above the ".Template.BasePath", i tried below combination all of them result in error.
checksum/config: {{ include (print .BasePath "../conf/values1.yaml") . | sha256sum }}

checksum/config: {{ include (print .Template.BasePath "/../../conf/values1.yaml") . | sha256sum }}

checksum/config: {{ include (print .Template.BasePath "/../conf/values1.yaml") . | sha256sum }} 

error calling include: template: no template "chart-2/charts/dashboard/templates../conf/values1.yaml" associated with template "gotpl"

error calling include: template: no template "chart-2/charts/dashboard/templates/../conf/values1.yaml" associated with template "gotpl"

error calling include: template: no template "chart-2/charts/dashboard/templates/../../conf/values1.yaml" associated with template "gotpl"

Upvotes: 1

Views: 6270

Answers (2)

maxisam
maxisam

Reputation: 22705

If you want to do this with sub-chart.

kind: <ResourceKind> # Deployment, StatefulSet, etc
spec:
  template:
    metadata:
      annotations:
        checksum/config: {{ include ("mylibchart.configmap") . | sha256sum }}

Another option is https://github.com/stakater/Reloader

You can use regex to select the config based on this PR https://github.com/stakater/Reloader/pull/314

Upvotes: 0

David Maze
David Maze

Reputation: 158647

The include template function is a Helm extension that executes a named template, just like the standard template directive, but returns the template's content as a string. It has nothing to do with reading files.

To read back files, you need to use the top-level .Files object. That specifically comes with a caveat that it cannot read files inside the chart's templates directory; prefixing file names with .Template.BasePath will not work, and you need to move the included files somewhere else.

Let's say you do create a subdirectory conf inside your main chart directory:

Chart.yaml
values.yaml
conf/
  logback.xml
templates/
  configmap.yaml

Many of the functions you list here – .Files.Get, .Files.Glob.AsConfig, tpl – return strings, so you can combine them together. For example, you can compute a checksum for the rendered content of the ConfigMap as:

annotations:
  checksum/config-map: {{ (tpl (.Files.Glob "conf/*").AsConfig . ) | sha256sum }}

You could do it on a single non-YAML file if you wanted, too: .Files.Get returns a string, so

.Files.Get "conf/logback-spring.xml" | sha256sum

The root path for .Files.Get is the root of the chart. You can retrieve any file not in the templates directory, but you can't reach outside the chart.

Upvotes: 6

Related Questions