1

I am using kubernetes and trying to call the following command:
kubectl set env deployment/server A=a B=b ...etc My env variables are located in .env file, and each value can contain spaces, non-escaped chars, comments, empty lines and so on:

## Common variables
NODE_ENV=production


## Server variables
SERVER_PORT=8009
CORS_ORIGIN=https://www.example.io,http://www.example.io,http://localhost:3000
SESSION_SECRET=/qm%7HLw"pk(8@"pja#I9CbN#2Lg[%d>5{CDA_9g|ZvZmuZ$]=';EhA#g+C;1>&

I am trying to parse the .env file so that I can integrate it in the command:

kubectl set env deployment/server $(do magic with .env file to extract the escaped variables)

Tried grep -v '^#' .env | xargs but it doesn't work with characters that need escaping or quotes. My bash abilities are not the strongest right now. Any idea how to solve this?

2
  • @prd New to kubernetes and didn't find well toturial that explains how to write .yml configs. Currency doing everyting via cli and kubectl. Commented Feb 13, 2019 at 8:50
  • you can use configmap to load envs to your pod Commented Feb 13, 2019 at 8:57

1 Answer 1

2
cat .env | grep -v '^#\|^$' | xargs -0 echo | tr '\n' ' '

This would print everything in one line: NODE_ENV=production SERVER_PORT=8009 CORS_ORIGIN=https://www.example.io,http://www.example.io,http://localhost:3000 SESSION_SECRET=/qm%7HLw"pk(8@"pja#I9CbN#2Lg[%d>5{CDA_9g|ZvZmuZ$]=';EhA#g+C;1>&

I am not sure if you need to keep the VAR= or if you also would like to get rid of that (pipe into sed or awk in the end then and replace it with whatever you need)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, this is a progress. I actually need the values escaped and wrapped with quotes since values contains slashes and quotes, this is the big issue. Any idea how to handle that?
| sed 's/=/="/g' | sed 's/ /" /g' this would add quotes to all your values. But you also need to look into your values and escape certain chars with a backslash \ ?
Exactly. Any idea how to do it?
Yeah, you can use one complex sed or just chain several ones to maintain readability. First add a backslash to all $-chars, then to all %-chars...change the char to whatever you need. Use a backslash in case it also used in Regex to escape it or sed will interpret it as a regex flag. sed 's/\$/\\$/g' | sed 's/\%/\\%/g' If this is what you meant I will update my main answer to make it more readable.
Thanks, looks like it is something I can continue with.

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.