<p>I have a k8s cronjob which exports metrics periodically and there's k8s secret.yaml and I execute a script from it, called run.sh
Within run.sh, I would like to refer another script and I can't seem to find a right way to access this script or specify it in cronjob.yaml</p>
<p>cronjob.yaml</p>
<pre><code>apiVersion: batch/v1
kind: CronJob
metadata:
  name: exporter
  labels:
    app: metrics-exporter

spec:
  schedule: &quot;* * * * *&quot;
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: exporter
        spec:
          volumes:
            - name: db-dir
              emptyDir: { }
            - name: home-dir
              emptyDir: { }
            - name: run-sh
              secret:
                secretName: exporter-run-sh
            - name: clusters
              emptyDir: { }
          containers:
            - name: stats-exporter
              image: XXXX
              imagePullPolicy: IfNotPresent
              command:
                - /bin/bash
                - /home/scripts/run.sh
              resources: { }
              volumeMounts:
                - name: db-dir
                  mountPath: /.db
                - name: home-dir
                  mountPath: /home
                - name: run-sh
                  mountPath: /home/scripts
                - name: clusters
                  mountPath: /db-clusters
              terminationMessagePath: /dev/termination-log
              terminationMessagePolicy: File
              securityContext:
                capabilities:
                  drop:
                    - ALL
                privileged: false
                runAsUser: 1000
                runAsNonRoot: true
                readOnlyRootFilesystem: false
                allowPrivilegeEscalation: false
          terminationGracePeriodSeconds: 30
          restartPolicy: OnFailure
</code></pre>
<p>Here's how in secret.yaml, I run script run.sh and refer to another script inside /db-clusters.</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: exporter-run-sh
type: Opaque
stringData:
  run.sh: |
    #!/bin/sh
    source $(dirname $0)/db-clusters/cluster1.sh
    # further work here

</code></pre>
<p>Here's the <a href="https://i.stack.imgur.com/IqJh5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IqJh5.png" alt="directory structure" /></a></p>
<p>Error Message:</p>
<pre><code>/home/scripts/run.sh: line 57: /home/scripts/db-clusters/cluster1.sh: No such file or directory
</code></pre>

flowAlong
<p>As per <a href="https://kubernetes.io/docs/tasks/configmap-secret/managing-secret-using-config-file/" rel="nofollow noreferrer">secret yaml</a>,  In string data you need to mention “run-sh” as you need to include the secret in the run-sh volume mount.</p>
<p>Have a try by using <code>run-sh</code> in stringData as below :</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: exporter-run-sh
type: Opaque
stringData:
  run-sh: |
    #!/bin/sh
    source $(dirname $0)/db-clusters/cluster1.sh
    # further work here
</code></pre>
