Helm `pre-install `hook calling to script during helm install

5/22/2019

I want to use the pre-install hook of helm,

https://github.com/helm/helm/blob/master/docs/charts_hooks.md

in the docs its written that you need to use annotation which is clear but what is not clear how to combine it ?

apiVersion: ...
kind: ....
metadata:
  annotations:
    "helm.sh/hook": "pre-install"

for my case I need to execute a bash script which create some env variable , where should I put this pre-hook script inside my chart that helm can use

before installation ?

I guess I need to create inside the templates folder a file which called: pre-install.yaml is it true? if yes where should I put the commands which create the env variables during the installation of the chart?

UPDATE The command which I need to execute in the pre-install is like:

export DB=prod_sales 
export DOMAIN=www.test.com
export THENANT=VBAS 
-- Jhon D
kubernetes
kubernetes-helm

2 Answers

5/22/2019

A Helm hook launches some other Kubernetes object, most often a Job, which will launch a separate Pod. Environment variable settings will only effect the current process and children it launches later, in the same Docker container, in the same Pod. That is: you can't use mechanisms like Helm pre-install hooks or Kubernetes initContainers to set environment variables like this.

If you just want to set environment variables to fixed strings like you show in the question, you can directly set that in a Pod spec. If the variables are, well, variable, but you don't want to hard-code them in your Pod spec, you can also put them in a ConfigMap and then set environment variables from that ConfigMap. You can also use Helm templating to inject settings from install-time configuration.

env:
  - name: A_FIXED_VARIABLE
    value: A fixed value
  - name: SET_FROM_A_CONFIG_MAP
    valueFrom:
      configMapKeyRef:
        name: the-config-map-name
        key: someKey
  - name: SET_FROM_HELM
    value: {{ .Values.environmentValue | quote }}

With the specific values you're showing, the Helm values path is probably easiest. You can run a command like

helm install --set db=prod_sales --set domain=www.test.com ...

and then have access to .Values.db, .Values.domain, etc. in your templates.

If the value is really truly dynamic and you can't set it any other way, you can use a Docker entrypoint script to set it at container startup time. In this answer I describe the generic-Docker equivalents to this, including the entrypoint script setup.

-- David Maze
Source: StackOverflow

5/22/2019

You can take as an example the built-in helm-chart from arc* project, here is the source code.
*Arc - kind of bootstraper for Laravel projects, that can Dockerize/Kubernetize existing apps written in this PHP framework.

-- Nepomucen
Source: StackOverflow