I am currently using the helm .Release.IsInstall variable https://helm.sh/docs/chart_template_guide/builtin_objects/ in my helm chart. Here’s the snippet of code that I have:
{{- if .Release.IsInstall }}
command: [ "python", "main.py", "install"]
{{- else }}
command: [ "python", "main.py", "delete"]
{{- end }}
However regardless of whether I run helm install or helm uninstall it seems as though .Release.IsInstall returns a boolean of true. Basically I am attempting to detect whether the user is doing an install/uninstall. Maybe there is another variable that I should be using instead.
Thanks in advance.
>Solution :
.Release.IsInstall only works during the installation process. When you’re running helm uninstall, the Helm chart templates aren’t evaluated, so this variable isn’t relevant in that context. Therefore, you can’t use .Release.IsInstall to detect an uninstall operation directly.
Since Helm doesn’t evaluate templates on uninstall, it’s not straightforward to pass different commands for uninstalling using the chart templates.
One common approach to handle this is using Helm pre-uninstall and post-uninstall hooks. You can define specific jobs or pods that run before or after the uninstall process, for example you can use a pre-delete hook to perform actions before the chart is deleted, and post-delete for actions after the chart is deleted.
kind: Job
apiVersion: batch/v1
metadata:
name: "{{ .Release.Name }}-pre-delete"
annotations:
"helm.sh/hook": pre-delete
spec:
template:
spec:
containers:
- name: pre-delete
image: python:3.8
command: ["python", "main.py", "delete"]
restartPolicy: Never