Helm post install hook on subchart

10/25/2021

I try to create an umbrella helm chart for a complex solution that is made from few components. One of them is a database, for which I use a mariadb-galera chart. The problem I face is that I want to execute a flyway migration once my DB is available, and I can't find a way to do it properly. First, I want to use range version, and I don't know how to indicate my hook to match the DB subchart without telling it the full version. Second, I recently added an alias for my subcharts, and I haven't been able to trigger the hook properly since: it just trigger at installation and fails again and again until the DB became eventually available.

My Chart.yaml looks a bit like that :

apiVersion: v2
name: myApp
description: umbrella chart 
type: application
version: 0.1.0
appVersion: "0.1-dev"

dependencies:
  - name: "portal"
    version: "0.1-dev"
    alias: "portal"
  - name: "mariadb-galera"
    version: "~5.11"
    repository: "https://charts.bitnami.com/bitnami"
    alias: "database"
#...More dependencies...

My hook is defined as follow:

apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "myApp.fullname" . }}-migration
  labels:
    {{- include "myApp.labels" . | nindent 4 }}
  annotations:
    "helm.sh/hook": post-install,post-upgrade
    "helm.sh/hook-weight": "-5"
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
  template:
    metadata:
      name: "{{ .Release.Name }}-migration"
      labels:
        app.kubernetes.io/managed-by: {{ .Release.Service | quote }}
        app.kubernetes.io/instance: {{ .Release.Name | quote }}
        helm.sh/chart: "database-5.11.2"
    spec:
      containers:
      - name: flyway-migration
        image: flyway/flyway
        args:
        - "migrate"
        - "-password=$(DB_PASS)"
        volumeMounts:
        - name: migration-files
          mountPath: /flyway/sql/
        - name: flyway-conf
          mountPath: /flyway/conf/
        env:
        - name: DB_PASS
          valueFrom:
            secretKeyRef:
              name: mariadb-secret
              key: mariadb-password
      volumes:
        - name: migration-files
          configMap:
             name: migration-files
        - name: flyway-conf
          configMap:
             name: flyway-conf

Before using aliases, the helm.sh annotation used to look like :

helm.sh/chart: "mariadb-galera-5.11.2"

As you can see, it requires a full version which I don't want to include manually.

I tried to use somthing like :

{{ template ".Chart.name" .Subcharts.database }}

but it seems that it can't access the .Chart values of the subchart.

Is there something I missed ?

-- NainzguL
helm3
kubernetes
kubernetes-helm

1 Answer

11/8/2021

I found a way to get the proper name for the dependency thanks to variables embedded inside the package :

helm.sh/chart: "{{ template "common.names.chart" .Subcharts.database }}"

This produce the exact line I needed, but the job still starts without waiting for mariadb to be ready.

-- NainzguL
Source: StackOverflow