<p>I am using K8S with helm.</p>
<p>I need to run pods and dependencies with a predefined flow order.</p>
<p>How can I create helm dependencies that run the pod only once (i.e - populate database for the first time), and exits after first success?</p>
<p>Also, if I have several pods, and I want to run the pod only on certain conditions occurs and after creating a pod.</p>
<p>Need to build 2 pods, as is described as following:</p>
<p>I have a database.</p>
<p><strong>1st step</strong> is to create the database.</p>
<p><strong>2nd step</strong> is to populate the db.</p>
<p>Once I populate the db, this job need to finish.</p>
<p><strong>3rd step</strong> is another pod (not the db pod) that uses that database, and always in listen mode (never stops).</p>
<p>Can I define in which order the dependencies are running (and not always parallel).</p>
<p>What I see for <code>helm create</code> command that there are templates for deployment.yaml and service.yaml, and maybe pod.yaml is better choice?</p>
<p>What are the best charts types for this scenario?</p>
<p>Also, need the to know what is the chart hierarchy.</p>
<p>i.e: when having a chart of type: listener, and one pod for database creation, and one pod for the database population (that is deleted when finished), I may have a chart tree hierarchy that explain the flow.</p>
<p><a href="https://i.stack.imgur.com/FpBys.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FpBys.png" alt="enter image description here" /></a></p>
<p>The main chart use the populated data (after all the sub-charts and templates are run properly - BTW, can I have several templates for same chart?).</p>
<p>What is the correct tree flow</p>
<p>Thanks.</p>

<p>There is a fixed order with which helm with create resources, which you cannot influence apart from <a href="https://helm.sh/docs/topics/charts_hooks/" rel="nofollow noreferrer">hooks</a>.</p>
<p>Helm hooks can cause more problems than they solve, in my experience. This is because most often they actually rely on resources which are only available after the hooks are done. For example, configmaps, secrets and service accounts / rolebindings. Leading you to move more and more things into the hook lifecycle, which isn't idiomatic IMO. It also leaves them dangling when uninstalling a release.</p>
<p>I tend to use jobs and init containers that blocks until the jobs are done.</p>
<pre class="lang-yaml prettyprint-override"><code>---
apiVersion: v1
kind: Pod
metadata:
  name: mysql
  labels:
    name: mysql
spec:
  containers:
    - name: mysql
      image: mysql
---
apiVersion: batch/v1
kind: Job
metadata:
  name: migration
spec:
  ttlSecondsAfterFinished: 100
  template:
    spec:
      initContainers:
        - name: wait-for-db
          image: bitnami/kubectl
          args:
            - wait
            - pod/mysql
            - --for=condition=ready
            - --timeout=120s
      containers:
        - name: migration
          image: myapp
          args: [--migrate]
      restartPolicy: Never
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  selector:
    matchLabels:
      app: myapp
  replicas: 3
  template:
    metadata:
      labels:
        app: myapp
    spec:
      initContainers:
        - name: wait-for-migration
          image: bitnami/kubectl
          args:
            - wait
            - job/migration
            - --for=condition=complete
            - --timeout=120s
      containers:
        - name: myapp
          image: myapp
          args: [--server]
</code></pre>
<p>Moving the migration into its own job, is beneficial if you want to scale your application horizontally. Your migration need to run only 1 time. So it doesn't make sense to run it for each deployed replica.</p>
<p>Also, in case a pod crashes and restarts, the migration doest need to run again. So having it in a separate one time job, makes sense.</p>
<p>The main chart structure would look like this.</p>
<pre><code>.
├── Chart.lock
├── charts
│   └── mysql-8.8.26.tgz
├── Chart.yaml
├── templates
│   ├── deployment.yaml    # waits for db migration job
│   └── migration-job.yaml # waits for mysql statefulset master pod
└── values.yaml
</code></pre>
