5

I am trying to do the following, so where MYVALUE in host needs to change to include Release Name. Can't figure how to do this, as you can't use env variables like {{ .Release.Name }} directly in to a values.yaml file.

I did do a fullnameOverride and put fullnameOverride: myrelease-mysql for the mysql pod and then jasper has host: myrelease-mysql that works but wanted to know if there was a clever way to put release name into a values.yaml file.

I assumed I would need to use a configMap as can use .Release.Name there and then embed that config key into values.yaml.

Values.yaml

jasperreports:
  mariadb:
    enabled: false
  externalDatabase:
    host: MYVALUE   // Also tried $MVALUE
    user: sqluser
    database: jasper
  jasperreportsUsername: jasper
  env:
      - name: MYVALUE
        valueFrom:
          configMapKeyRef:
              name: mysql-jasper
              key: mysql_releasename

ConfigMap

kind: ConfigMap
metadata:
  name: mysql-jasper
data:
  mysql_releasename: {{ .Release.Name }}-"mysql"
Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
sam
  • 109
  • 2
  • 3
  • 12
  • 1
    How is `.Values.jasperreports.externalDatabase` used? I'd probably fill in this value when I'm creating the ConfigMap with the application configuration. It's tricky to put Helm template code into the `values.yaml` file (the chart needs to be specifically aware of it) and you can't read back from a ConfigMap into Helm values. – David Maze Nov 04 '21 at 11:10
  • So its set in values file only as used for connectivity to mySQL database, I’m using bitnamis chart: https://github.com/bitnami/charts/tree/master/bitnami/jasperreports/#installing-the-chart – sam Nov 04 '21 at 13:14
  • Hi @sam, any progress? – Mikolaj S. Nov 08 '21 at 16:43
  • No, I just used the fullnameoverride for the pod name without the release name, couldn’t figure out how to embed env variables into the values file to keep the release name/tag. Don’t think its possible. – sam Nov 09 '21 at 12:12
  • Just to clarify - you want to to read variable from the ConfigMap (in your case it is `mysql-jasper` ConfigMap) in the `values.yaml` and set the value of the `MYVALUE`, and then use the `MYVALUE` to set value of the `jasperreports.externalDatabase.host` variable? – Mikolaj S. Nov 10 '21 at 16:16
  • Correct, wasn't sure if this was possible. At present, I have used a fullnameOverride so that the pod name is the host of the externalDatabase, weren't sure if I can retain the release name without doing this. Thanks – sam Nov 11 '21 at 18:55

1 Answers1

3

It seems that helm does not support any template rendering capabilities in a values.yaml file - there are multiple topics on the helm GitHub:

For now this feature is not implemented so you need to find a workaround - the suggestion from David Maze seems to be a good direction, but if you want to follow your approach you can use below workaround using --set flag in the helm install command or use sed command and pipe to helm install command.

First solution with --set flag.

My values.yaml file is little bit different than yours:

mariadb:
  enabled: false
externalDatabase:
  user: sqluser
  database: jasper
jasperreportsUsername: jasper

That's because when I was using your values.yaml I couldn't manage to apply these values to bitnami/jasperreports chart, the helm install command was using default values from here.

I'm setting a shell variable RELEASE_NAME which I will use both for setting chart name and externalDatabase.host value.

RELEASE_NAME=my-test-release
helm install $RELEASE_NAME bitnami/jasperreports -f values.yaml --set externalDatabase.host=$RELEASE_NAME-mysql

The above helm install command will override default values both by setting values from the values.yaml file + setting externalDatabase.host value.

Before applying you can check if this solution works as expected by using helm template command:

RELEASE_NAME=my-test-release
helm template $RELEASE_NAME bitnami/jasperreports -f values.yaml --set externalDatabase.host=$RELEASE_NAME-mysql
...
- name: MARIADB_HOST
  value: "my-test-release-mariadb"
...

Another approach is to set a bash variable RELEASE_NAME which will be used in the sed command to output modified values.yaml file (I'm not editing values.yaml file itself). This output will be pipe into a helm install command (where I also used theRELEASE_NAME variable).

values.yaml:

mariadb:
  enabled: false
externalDatabase:
  host: MYHOST
  user: sqluser
  database: jasper
jasperreportsUsername: jasper
RELEASE_NAME=my-test-release
sed "s/MYHOST/$RELEASE_NAME-mysql/g" values.yaml | helm install $RELEASE_NAME bitnami/jasperreports -f -

This approach will set chart configuration the same as in the first approach.

Mikolaj S.
  • 2,850
  • 1
  • 5
  • 17