0

I am passing some test variables to my pod which has code in nodejs like this:

apiVersion: v1
kind: Pod
metadata:
  name: envar-demo
  labels:
    purpose: demonstrate-envars
spec:
  containers:
  - name: envar-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    send:
    - name: DEMO_GREETING
      value: "Hello from the environment"
    - name: DEMO_FAREWELL
      value: "Such a sweet sorrow"

I am trying to access the value of these variables from the package.json that I have inside my pod but I'm not sure how to do it, if I don't have an .env file from which I can read these variables using grep

  • Wondering What's `send` in your manifest?? – P.... Jul 27 '22 at 18:43
  • I can access the variables from js code using `process.env.DEMO_GREETING`, but I don't know how to do it from package.json – Carlos Daniel Ospina Salazar Jul 27 '22 at 18:46
  • The manifest looks incorrect. So basically you want the content of a file to be set as environment variable? – P.... Jul 27 '22 at 18:56
  • I was already able to send the environment variable and retrieve it from the pod, but only from the .js files and I need to retrieve it from the package.json. Something like this but with a kubernetes .env: https://stackoverflow.com/questions/34650527/how-to-use-environment-variables-in-package-json – Carlos Daniel Ospina Salazar Jul 27 '22 at 18:59
  • Once your process starts running, these are ordinary environment variables, exactly the same thing as if you ran `export DEMO_GREETING='hello world'` in a local shell. Does that simplify the setup for you? What exactly are you trying to do with these variables in `package.json`? – David Maze Jul 28 '22 at 00:35

1 Answers1

1

No matter how you have set the variable through Kubernetes, export, or just before calling a command. You can get access to it as in the usual bash script.

"scripts": {
  "envvar": "echo $TEST_ENV_VAR"
},

Then we can run it

➜ TEST_ENV_VAR=4342 npm run envvar

> envvar
> echo $TEST_ENV_VAR

4342