Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running scripts via Helm hooks

I have written Pre- and Post-upgrade hooks for my Helm chart, which will get invoked when I do a helm upgrade. My Pre-upgrade hook is supposed to write some information to a file in the shared persistent storage volume. Somehow, I dont see this file getting created though I am able to see the hook getting invoked.

This is what my pre-upgrade hook looks like:

apiVersion: batch/v1
kind: Job
metadata:
  name: "{{.Release.Name}}-preupgrade"
  labels:
    heritage: {{.Release.Service | quote }}
    release: {{.Release.Name | quote }}
    chart: "{{.Chart.Name}}-{{.Chart.Version}}"
  annotations:
    "helm.sh/hook": pre-upgrade
    "helm.sh/hook-weight": "0"
    "helm.sh/hook-delete-policy": hook-succeeded
spec:
  template:
    metadata:
      name: "{{.Release.Name}}"
      labels:
        heritage: {{.Release.Service | quote }}
        release: {{.Release.Name | quote }}
        chart: "{{.Chart.Name}}-{{.Chart.Version}}"
  spec:
    restartPolicy: Never
    containers:
    - name: pre-upgrade-job
      image: {{ .Values.registry }}/{{ .Values.imageRepo }}:{{ .Values.imageTag }}
      imagePullPolicy: {{ .Values.imagePullPolicy }}
      volumeMounts:
      - mountPath: {{ .Values.pvc.shared_storage_path }}/{{ template "fullname" . }}
        name: shared-pvc
        command: ['/bin/sh -c scripts/preUpgradeScript.sh {{ .Values.pvc.shared_storage_path }}/{{ template "fullname" . }}']
  volumes:
    - name: shared-pvc
      persistentVolumeClaim:
        claimName: {{ template "fullname" . }}-shared-pv-claim

My expectation is that the hook should be able to write information to the PVC volume which was already created prior to the upgrade. When I did a describe on the upgrade pods, I could see the following error:

Error: failed to start container "pre-upgrade-job": Error response from daemon: oci runtime error: container_linux.go:247: starting container process caused "exec: \"/bin/sh -c scripts/preUpgradeScript.sh /opt/flink/share/myfl-flink\": stat /bin/sh -c scripts/preUpgradeScript.sh /opt/flink/share/myfl-flink: no such file or directory"

Doesn't the hook first mount the volume before running the command? Also, I'm packaging the script with the docker image, so I believe it should be there. I am unable to exec into the hook pod as it goes into the Failed state. Can anyone help me with this?

[Update] I added a sleep command to enter the pod and check if the script is available and if the mount path exists. All looks fine. I don't understand why this error would come up.

like image 497
James Isaac Avatar asked Jun 27 '18 17:06

James Isaac


People also ask

Why You Should not Use Helm?

Helm only adds value when you install community components. Otherwise you need to write yaml anyway. Leads to too much logic in the templates (not good for inexperienced k8s users) GitOps/Infrastructure as Code best practices are violated, because you version control before it has been templated.

Is Helm being deprecated?

helm/charts has been deprecated and will be obsolete by Nov 13 2020. For this reason, the datawire team as retaken ownership of this chart. The Ambassador Chart is now hosted at datawire/ambassador-chart.


1 Answers

Looks like I needed to give the command differently:

command: ["/bin/sh", "-c", "scripts/preUpgradeScript.sh","{{ .Values.pvc.shared_storage_path }}/{{ template "fullname" . }}"]
like image 133
James Isaac Avatar answered Oct 25 '22 23:10

James Isaac