<p>I have a number of secret "environment variables" I want to get into a kubernetes cluster. Currently, I have a directory of these secrets where each var has a corresponding file named the same way as the variable it is supposed to be assigned to. Using <code>docker-compose</code>, this gives me a script like <code>for secret in .secrets/*; do export "$(basename $secret)"="$(cat $secret)" ; done</code></p>

<p>I'm hoping to avoid explicitly specifying each one of these secrets in the kustomize file(s). Is there a simple way to create one secret per file by only specifying the directory?</p>

Ben
<p>Here is one way you could accomplish this outside of Kustomize.</p>

<p>Secrets can be consumed as environment variables in the Pod spec using the <code>valueFrom</code> keyword. Documentation about this is at <a href="https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-environment-variables" rel="noreferrer">https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-environment-variables</a>.</p>

<p>Knowing this, you could modify your script to create a main secret and a key per  file. An example (untested) might look something like:</p>

<pre><code>for secret in .secrets/*; do
    literals="$literals --from-literal=$(basename $secret)=$(cat $secret)"
done

kubectl create secret generic prod-secrets "$literals"
</code></pre>

<p>Then, in your specs reference the specific key from that secret to get the corresponding environment variable. Assuming one of them was <code>SECRET_USERNAME</code>:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: secret-env-pod
spec:
  containers:
  - name: mycontainer
    image: redis
    env:
      - name: SECRET_USERNAME
        valueFrom:
          secretKeyRef:
            name: prod-secrets
            key: SECRET_USERNAME
  restartPolicy: Never
</code></pre>
