microsoft / mindaro

Bridge to Kubernetes - for Visual Studio and Visual Studio Code
MIT License
307 stars 106 forks source link

Use environment variables from k8s deployment #175

Closed larsf96 closed 3 years ago

larsf96 commented 3 years ago

I don't know if this should already work, but I couldn't get it working though.

My scenario is that I try to start a service locally with Bridge2Kubernetes. The service already has a kubernetes deployment and service with some environment variables that are referring to configMaps and secrets. When I now start the launch config for Bridge2Kubernetes, my service is instantly crashing, because the environment variables could not be resolved. It would be very nice to just use the existing configuration to not have duplicates of those environment variables locally.

I've set "useKubernetesServiceEnvironmentVariables" to true, but this had no effect.

Am I doing something wrong or is this feature not yet supported?

irperez commented 3 years ago

We've had the same issue. To compensate, we've setup these variables on the local machine. Not ideal, but it works. I wish there was a way that these variables would get setup based on the configmap. That would make things ideal for sure.

pragyamehta commented 3 years ago

@larsf96 @irperez We do support this scenario through a file called KubernetesLocalProcessConfig.yaml Please go through this link for more details: https://docs.microsoft.com/en-us/visualstudio/containers/configure-bridge-to-kubernetes?view=vs-2019 Environment variables needed by the app can be set here. You can also specify volumes that your pod required which will be downloaded locally when debugging. Let me know if you have any questions.

larsf96 commented 3 years ago

@pragyamehta Thanks for the hint, but I'm not quite sure if this is exactly what I need. The article is telling about downloading a volume and setting the path as environment variable. What I need is "replicating" the environment variables from the current k8s cluster environment. Most of them are set as configMapValue's in the deployment and I want to use exactly the same env variables without having to setting up those locally. If that is possible with the above method, I might need a hint on how to do that

pragyamehta commented 3 years ago

@larsf96 Apologies for the confusion. It seems that you do not need KubernetesLocalProcessConfig.yaml at all for the scenario you are describing. What you are asking for is supported out of the box for both isolated and non-isolated modes.

Below is the env section of my deployment's container spec:

image

I modified the code to print this environment variable in my app code and I can see it getting printed once I debug with Bridge To Kubernetes. image

Can you try the scenario again and let me know if it works for you? If not, do provide the repro steps and I will try to unblock you. Thanks!

pragyamehta commented 3 years ago

@irperez Hello! Please see my reply above and let us know if it works for you! If not, please provide your repro steps and I will try to unblock you. Thanks!

larsf96 commented 3 years ago

@pragyamehta Thank you! I think it was my mistake, looked like I didn't start the service with Bridge to Kubernetes the first time. Everything is working not :)

pragyamehta commented 3 years ago

Closing this as resolved. Please feel free to reach out for any feedback! :)

irperez commented 3 years ago

@pragyamehta, @greenie-msft, in our case, the following scenario did not work when we pull environment variables with our configmap. This pulls out all the variables in the configmap and loads them into the env variables.

image

pragyamehta commented 3 years ago

@irperez Hi, I just tried this scenario on VSCode with the latest production bits. Below is my configmap: image

Below is my deployment showing the envFrom setup: image

Below is a screenshot of debugging with Bridge to Kubernetes showing the env var being printed: image

Let me know if you have any questions.