<p>I'm trying to use variables from a <code>secret</code> in a line in <code>confMap</code>. The deployment is deploying an app which needs to connect to a <code>MongoDB</code> server via a url. The url is in a <code>confMap</code> and the <code>username</code> and the <code>password</code> are variables stored in the <code>secret</code>.
I'm stuck too long in it and can't solve it. Would appreciate your help.Here are the files:</p>
<p>deployent.yaml:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo-deployment
  labels:
    app: echo-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: echo-app
  template:
    metadata:
      labels:
        app: echo-app
    spec:
      containers:
      - name: echo-app-container
        image: gcr.io/echo-project-320612/echo:v1.1
        ports:
          - containerPort: 3000
        volumeMounts:
          - mountPath: /app/config 
            name: echo-confmap123
        env:
          - name: username
            valueFrom:
              secretKeyRef:
                name: echo-secret
                key: username
          - name: password
            valueFrom:
              secretKeyRef:
                name: echo-secret
                key: password
      volumes:
        - name: echo-confmap123
          configMap:
            name: echo-confmap
</code></pre>
<p>secret.yaml:</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: echo-secret
type: Opaque
stringData:
  username: usr123
  password: 123
</code></pre>
<p>confMap:</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: echo-confmap
data:
  default.yaml: |
    port: 3000
    loglevel: info
    persist: true
    dbname: my-database
    mongodburl: mongodb://$(username):$(password)@my-database-mongodb-0.my-database-mongodb-headless.default.svc.cluster.local,my-database-mongodb-1.my-database-mongodb-headless.default.svc.cluster.local,my-database-mongodb-2.my-database-mongodb-headless.default.svc.cluster.local:27017/my-database
</code></pre>
<p>the error:</p>
<pre><code>[2021-07-29T17:54:41.949] [INFO] default - Starting echo app!
[2021-07-29T17:54:41.967] [INFO] default - Echo listening on port 3000!
[2021-07-29T17:54:41.989] [FATAL] default - Could not connect to MongoDB! MongoError: Authentication failed.
    at Function._getError (/app/node_modules/mongodb/lib/core/auth/scram.js:125:14)
    at /app/node_modules/mongodb/lib/core/auth/scram.js:175:31
    at Connection.messageHandler (/app/node_modules/mongodb/lib/core/connection/connect.js:334:5)
    at Connection.emit (events.js:314:20)
    at processMessage (/app/node_modules/mongodb/lib/core/connection/connection.js:364:10)
    at Socket.&lt;anonymous&gt; (/app/node_modules/mongodb/lib/core/connection/connection.js:533:15)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at Socket.Readable.push (_stream_readable.js:213:10) {
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {}
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
</code></pre>

DlekaShelHaHaim
<p>Please check your MongoDB URL connection string however if auth failing which means it cloud be an issue of username password.</p>
<pre><code>mongodb://myDBReader:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&amp;replicaSet=myRepl
</code></pre>
<p>ideally we pass the port number at the end of each service URL and also pass the default admin database if any.</p>
<p><a href="https://docs.mongodb.com/manual/reference/connection-string/" rel="nofollow noreferrer">https://docs.mongodb.com/manual/reference/connection-string/</a></p>
