<p>I'm trying to mount a secret as a file</p>
<pre><code>apiVersion: v1
data:
  credentials.conf: &gt;-
    dGl0bGU6IHRoaYWwpCg==
kind: Secret
metadata:
  name: address-finder-secret
type: Opaque
</code></pre>
<pre><code>kind: DeploymentConfig
    apiVersion: v1
    metadata:
      name: app-sample
    spec:
      replicas: 1
      selector:
        app: app-sample
      template:
        metadata:
          labels:
            app: app-sample
        spec:
          volumes:
            - name: app-sample-vol
              configMap:
                name: app-sample-config
            - name: secret
              secret:
                secretName: address-finder-secret
          containers:
            - name: app-sample
              volumeMounts:
                - mountPath: /config
                  name: app-sample-vol
                - mountPath: ./secret/credentials.conf
                  name: secret
                  readOnly: true
                  subPath: credentials.conf
</code></pre>
<p>I need to add the <code>credentials.conf</code> file to a directory where there are already other files. I'm trying to use <code>subPath</code>, but I get <code>'Error: failed to create subPath directory for volumeMount &quot;secret&quot; of container &quot;app-sample&quot;'</code>
If I remove the subPath, I will lose all other files in the directory.</p>
<p>Where did I go wrong?</p>

Violetta
<p>Hello, Hope you are enjoying your kubernetes journey !</p>
<p>It would have been better if you have given your image name to try it out but however, i decided to create on custom image.</p>
<p>I created a simple file named file1.txt and copied it to the image, here is my dockefile:</p>
<pre><code>FROM nginx

COPY file1.txt /secret/
</code></pre>
<p>I built it simply with:</p>
<pre><code>❯ docker build -t test-so-mount-file .
</code></pre>
<p>I just checked if my file were here before going further:</p>
<pre><code>❯ docker run -it test-so-mount-file bash
root@1c9cebc4884c:/# ls
bin                   etc    mnt   sbin    usr
boot                  home   opt   secret  var
dev                   lib    proc  srv
docker-entrypoint.d   lib64  root  sys
docker-entrypoint.sh  media  run   tmp
root@1c9cebc4884c:/# cd secret/
root@1c9cebc4884c:/secret# ls
file1.txt
root@1c9cebc4884c:/secret#
</code></pre>
<p>perfect. now lets deploy it on kubernetes.</p>
<p>For this test, since i'm using kind (kubernetes in docker) I just used this command to upload my image to the cluster:</p>
<pre><code>❯ kind load docker-image test-so-mount-file --name so-cluster-1
</code></pre>
<p>It seems that you are deploying on openshift regarding to your &quot;Deploymentconfig&quot; kind. however, once my image has been added to my cluster i modified your deployment with it :</p>
<p>first without volumes, to check that the file1 is in the container:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-sample
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app-sample
  template:
    metadata:
      labels:
        app: app-sample
    spec:
      containers:
        - name: app-sample
          image: test-so-mount-file
          imagePullPolicy: Never
          # volumeMounts:
</code></pre>
<p>yes it is:</p>
<pre><code>❯ k exec -it app-sample-7b96558fdf-hn4qt -- ls /secret
file1.txt
</code></pre>
<p>Before going further, when i tried to deploy your secret i got this:</p>
<pre><code>Error from server (BadRequest): error when creating &quot;manifest.yaml&quot;: Secret in version &quot;v1&quot; cannot be handled as a Secret: illegal base64 data at input byte 20
</code></pre>
<p>This is linked to your base64 string that actually contains illegal base64 data, here it is:</p>
<pre><code>❯ base64 -d &lt;&lt;&lt; &quot;dGl0bGU6IHRoaYWwpCg==&quot;
title: thi���(base64: invalid input
</code></pre>
<p>No pb, I used another string in b64:</p>
<pre><code>❯ base64 &lt;&lt;&lt; test
dGVzdAo=
</code></pre>
<p>and added it to the secret. since I want this data to be in a file, i replaced the '&gt;-' by a '|-'  (<a href="https://stackoverflow.com/questions/72082343/what-is-the-difference-between-and-in-yaml">What is the difference between &#39;&gt;-&#39; and &#39;|-&#39; in yaml?</a>),however it works, with or without it.
Now, lets add the secret to our deployment. I replaced the &quot;./secret/credentials.conf&quot; by &quot;/secret/credentials.conf&quot; (it works with or without but i prefer to remove the &quot;.&quot;). since I don't have your confimap datas, I commented out this part. However, Here is the deployment manifest of my file manifest.yaml:</p>
<pre><code>apiVersion: v1
kind: Secret
type: Opaque
metadata:
  name: address-finder-secret
data:
  credentials.conf: |-
    dGVzdAo=

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-sample
spec:
  replicas: 1
  selector:
    matchLabels:
      app: app-sample
  template:
    metadata:
      labels:
        app: app-sample
    spec:
      containers:
        - name: app-sample
          image: test-so-mount-file
          imagePullPolicy: Never
          volumeMounts:
            # - mountPath: /config
            #   name: app-sample-vol
            - mountPath: /secret/credentials.conf
              name: secret
              readOnly: true
              subPath: credentials.conf
      volumes:
        # - name: app-sample-vol
        #   configMap:
        #     name: app-sample-config
        - name: secret
          secret:
            secretName: address-finder-secret
</code></pre>
<p>Lets deploy this:</p>
<pre><code>❯ kaf manifest.yaml
secret/address-finder-secret created
deployment.apps/app-sample created



❯ k get pod
NAME                                READY   STATUS    RESTARTS   AGE
app-sample-c45ff9d58-j92ct          1/1     Running   0          31s



❯ k exec -it app-sample-c45ff9d58-j92ct -- ls /secret
credentials.conf  file1.txt



❯ k exec -it app-sample-c45ff9d58-j92ct -- cat /secret/credentials.conf
test
</code></pre>
<p>It worked perfectly, Since I havent modified big things from you manifest, i think the pb comes from the deploymentConfig, I suggest you to use deployment instead of deploymentConfig, That way it will works (I hope) and if someday you decide to migrate from openshift to another kubernetes cluster your manifest will be compatible.</p>
<p>bguess</p>
