<p>On my helm chart I have a job with the <code>pre-install</code> hook where I need to use a property from my secrets. However when I try to install my helm chart I get the following error on my <code>pre-install</code> job:</p>

<blockquote>
  <p>Error: secret "SecretsFileName" not found</p>
</blockquote>

<p>Secrets aren't created before the pods execution? What's the problem here? How can I solve this?</p>

<p>Notes: </p>

<ul>
<li>I want to use secrets to have the properties encrypted. I don't want to use the decrypted value directly on my pod;</li>
<li>I already read <a href="https://stackoverflow.com/questions/51957676/helm-install-in-certain-order">Helm install in certain order</a> but I still not understanding the reason of this error;</li>
<li>I already tried to use <code>"helm.sh/hook": pre-install,post-delete</code> and <code>"helm.sh/hook-weight": "1"</code> on secrets, and <code>"helm.sh/hook-weight": "2"</code> on my pod but the problem remains.</li>
</ul>

<p><strong>My pre-install job:</strong></p>

<pre><code>apiVersion: batch/v1
kind: Job
metadata:
  name: "MyPodName"
  annotations:
    "helm.sh/hook": pre-install
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
  #some more code
spec:
  template:
    #some more code
    spec:
      dnsPolicy: {{ .Values.specPolicy.dnsPolicy }}
      restartPolicy: {{ .Values.specPolicy.restartPolicy }}
      volumes:
        - name: {{ .Values.volume.name }}
          persistentVolumeClaim:
            claimName: {{ .Values.volume.claimName }}
      securityContext:
        {{- toYaml .Values.securityContext | nindent 8 }}
      containers:
        - name: "MyContainerName"
          #some more code
          env:
            - name: SECRET_TO_USE
              valueFrom:
                secretKeyRef:
                  name: SecretsFileName
                  key: PROP_FROM_SCRETS
          #some more code
</code></pre>

<p><strong>My secrets file:</strong></p>

<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: "SecretsFileName"
  labels:
    app: "MyAppName"
    #some more code
type: Opaque
data:
    PROP_FROM_SCRETS: eHB0bw==
</code></pre>

Ninita
<p>While <a href="https://helm.sh/docs/topics/charts_hooks/" rel="noreferrer">Helm hooks</a> are typically Jobs, there's no requirement that they are, and Helm doesn't do any analysis on the contents of a hook object to see what else it might depend on.  If you read through the installation sequence described there, it is (7) install things tagged as hooks, (8) wait for those to be ready, then (9) install everything else; it waits for the Job to finish before it installs the Secret it depends on.</p>
<p>The first answer, then, is that you also need to tag your Secret as a hook for it to be installed during the pre-install phase, with a modified weight so that it gets installed before the main Job (smaller weight numbers happen sooner):</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Secret
annotations:
  &quot;helm.sh/hook&quot;: pre-install
  &quot;helm.sh/hook-weight&quot;: &quot;-5&quot;
</code></pre>
<p>The next question is when this Secret gets <em>deleted</em>.  The documentation <a href="https://helm.sh/docs/topics/charts_hooks/#hook-resources-are-not-managed-with-corresponding-releases" rel="noreferrer">notes</a> that <code>helm uninstall</code> won't delete hook resources; you need to add a separate <a href="https://helm.sh/docs/topics/charts_hooks/#hook-deletion-policies" rel="noreferrer"><code>helm.sh/hook-delete-policy</code> annotation</a>, or else it will stick around until the next time the hook is scheduled to be run.  This reads to me as saying that if you modify the Secret (or the values that make it up) and upgrade (not delete and reinstall) the chart, the Secret won't get updated.</p>
<p>I'd probably just create two copies of the Secret, one that's useful at pre-install time and one that's useful for the primary chart lifecycle.  You could create a template to render the Secret body and then call that twice:</p>
<pre><code>{{- define &quot;secret.content&quot; -}}
type: Opaque
data:
    PROP_FROM_SCRETS: eHB0bw==
{{- end -}}
---
apiVersion: v1
kind: Secret
metadata:
  name: &quot;SecretsFileName&quot;
  labels:
    app: &quot;MyAppName&quot;
{{ include &quot;secret.content&quot; . }}
---
apiVersion: v1
kind: Secret
metadata:
  name: &quot;SecretsFileName-preinst&quot;
  labels:
    app: &quot;MyAppName&quot;
  annotations:
    &quot;helm.sh/hook&quot;: pre-install
    &quot;helm.sh/hook-weight&quot;: &quot;-5&quot;
    &quot;helm.sh/hook-delete-policy&quot;: hook-succeeded
{{ include &quot;secret.content&quot; . }}
</code></pre>
