helm trigger pod restart on parent-chart configmap change

Viewed 331

I'm facing the problem that we use an umbrella helm chart to deploy our services but our services are contained in subcharts. But there are some global configmaps that multiple services are using which are deployed by the umbrella chart. So a simplified structure looks like this:

├── Chart.yaml
├── templates
│   ├──  global-config.yaml
├── charts
│   ├── subchart1
│   │   ├── Chart.yaml
│   │   ├──    templates
│   │   │   ├──      deployment1.yaml
│   ├──  subchart2
│   │   ├──    Chart.yaml
│   │   ├──    templates
│   │   │   ├──      deployment2.yaml
 ...

I what I need is to put a checksum of global-config.yaml in deployment1.yaml and deployment2.yaml as annotation like in https://helm.sh/docs/howto/charts_tips_and_tricks/#automatically-roll-deployments

The problem is that I can define a template like this in the umbrella chart:

{{- define "configmap.checksum" }}{{ include (print $.Template.BasePath "/global-config.yaml") . | sha256sum }}{{ end }}

and then use {{ include "configmap.checksum" . }} in the deployment yamls as templates are global. But $.Template.BasePath is parsed when the include happens so it actually points to the template directory of the subchart where it is included. I have played around with '..' and the Files. and with passing another context in the template and also in the include like passing the global context $ but none of them were successful as I ways always locked in the specific subchart at rendering time.

Is there a solution to this or a different approach to solve this? Or is this something that cannot be done in helm? But I'm grateful for any solution

1 Answers

I found a workaround that requires you to refactor the contents of your global ConfigMap into a template in the umbrellas chart. You won't be able to use regular values from the umbrella chart in your ConfigMap, otherwise you'll get an error when trying to render the template in a subchart; you'll have to use global values instead.

{{/*
Create the contents of the global-config ConfigMap
*/}}
{{- define "my-umbrella-chart.global-config-contents" -}}
spring.activemq.user={{ .Values.global.activeMqUsername | quote }}
spring.activemq.password={{ .Values.global.activeMqPassword | quote }}
jms.destinations.itemCreationQueue={{ .Release.Namespace }}-itemCreationQueue
{{- end }}
kind: ConfigMap
apiVersion: v1
metadata:
  name: global-config
data:
  {{- include "my-umbrella-chart.global-config-contents" . | nindent 2 }}

Then you add a checksum of my-umbrella-chart.global-config-contents in deployment1.yaml and deployment2.yaml as an annotation like so:

annotations:
  # Automatically roll deployment when the configmap contents changes
  checksum/global-config: {{ include "my-umbrella-chart.global-config-contents" . | sha256sum }}
Related