Home > front end >  NodeJs throwing undefined error while reading the secret value from kubernetes yaml file
NodeJs throwing undefined error while reading the secret value from kubernetes yaml file

Time:03-15

I need to read a Kubernetes key and value from NodeJs. But am getting an undefined error.

Please find the below code.

deployment.yaml

containers:
-   name: server
    env:
    -name: CLIENT_DEV
        valueFrom:
            secretKeyRef:
                name: dev1-creds-config
                key: clientId

The secretKeyRef value will be available in another yaml file. This will get read properly when the dev/local minikube build is running based on the region we are running.

secrets.enc.yaml

apiVersion: v1
kind: Secret
metadata:
    name: dev1-creds-config
type: Opaque
data:
    clientId: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    username: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    password: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

The above one contains the encrypted values. This is created to ensure security.

The index.js file of NodeJs to read the value

require("dotenv").config();
const express = require("express");
const app = express();

console.log("value.." processs.env.CLIENT_DEV);
const host = customHost || "localhost";

app.listen(port,host,err => {
    if(err){
        return logger.error(err.message);
    }
    logger.appStarted("3000", host);
});

console.log("value.." processs.env.CLIENT_DEV); this line is giving me "undefined"

My query is,

  1. is it possible to the yaml encrypted value from deployment yaml using Node js
  2. is it possible to configure yaml key, value in .env file of Node Js

Am not able to read this secret value from yaml file in my node js.

Please help me to resolve this issue.

Thanks in advance.

CodePudding user response:

apiVersion: v2 of secret yaml is corerct?

CodePudding user response:

Check the indentation in your deployment.yaml, it should be like this:

containers:
- name: server
  env:
    - name: CLIENT_DEV
      valueFrom:
        secretKeyRef:
          name: dev1-creds-config
          key: clientId

In your question the indentation is incorrect. But as long as your nodejs pods are running well, I think you just pasted it not very accurately.

Second, there is a typo processs in your JavaScript code. Correct the line:

console.log("value.." process.env.CLIENT_DEV);

After verifying all of these, your NodeJs application will read the Kubernetes secret value.

  • Related