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

5
  • Wondering What's send in your manifest?? Commented Jul 27, 2022 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 Commented Jul 27, 2022 at 18:46
  • The manifest looks incorrect. So basically you want the content of a file to be set as environment variable? Commented Jul 27, 2022 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: stackoverflow.com/questions/34650527/… Commented Jul 27, 2022 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? Commented Jul 28, 2022 at 0:35

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.