Home > OS >  Environment variables: Pod vs Container. Trying to access Container envvar with Python os.getenv
Environment variables: Pod vs Container. Trying to access Container envvar with Python os.getenv

Time:03-12

I have deployed a Pod with several containers. In my Pod I have certain environment variables that I can access in Python script with os.getenv(). However, if I try to use os.getenv to access the Container's environment variables I get an error stating they don't exist (NoneType). When I write 'kubectl describe pod <POD_Name>' I see that all the environment variables (both Pod and Container) are set.

Any ideas?

CodePudding user response:

The issue was in creating helm tests. In order to get the environment variables from the containers in a helm test then the environment variables need to be duplicated in the test.yaml file or injected from a shared configmap.

CodePudding user response:

According to your answer I would like to add a little theory.

See this documentation about ConfigMaps.

A ConfigMap is an API object used to store non-confidential data in key-value pairs. Pods can consume ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume.

Here one can find also an example of Pod that uses values from ConfigMap to configure a Pod:

      env:
        # Define the environment variable
        - name: PLAYER_INITIAL_LIVES # Notice that the case is different here
                                     # from the key name in the ConfigMap.
          valueFrom:
            configMapKeyRef:
              name: game-demo           # The ConfigMap this value comes from.
              key: player_initial_lives # The key to fetch.
  • Related