Repository: PrefectHQ/prefect

Issue:
Kubernetes Agent not using env-vars flag correctly
## Opened from the [Prefect Public Slack Community](https://join.slack.com/t/prefect-public/shared_invite/enQtNzE5OTU3OTQwNzc1LTQ5M2FkZmQzZjI0ODg1ZTBmOTc0ZjVjYWFjMWExZDAyYzBmYjVmMTE1NTQ1Y2IxZTllOTc4MmI3NzYxMDlhYWU)

**philip.bennett**: Hey all. I'm having problems where I set the environment variables using --env but I can't seem to access them from within the flow running on a Kubernetes agent

I'm running my agent using  `prefect agent install kubernetes --api "<http://10.154.15.199:4200>" --rbac --resource-manager --env DB_HOST=10.104.145.11 --env CRUNCHBASE_API_KEY=XYZ | kubectl apply -f -`

I try to access them using  `os.getenv()` but cannot access them.
Has anyone had similar issues?

**chris**: Hi Philip - can you confirm that those environment flags are also present in your k8s `yaml` file?  If you just inspect the output of `prefect agent install kubernetes --api "<http://10.154.15.199:4200>" --rbac --resource-manager --env DB_HOST=10.104.145.11 --env CRUNCHBASE_API_KEY=XYZ` and look for `DB_HOST`, etc.

**philip.bennett**: Hi Chris. Thanks for your quick response. Yes they look like this in the yaml file:
`- name: PREFECT__CLOUD__AGENT__ENV_VARS__DB_HOST`
     `value: 10.104.145.11`
`- name: PREFECT__CLOUD__AGENT__ENV_VARS__CRUNCHBASE_API_KEY`
     `value: XYZ`

**chris**: Ok great, and what Prefect environment are you using for your Flow?

**philip.bennett**: 11.5

**chris**: Ah I mean what environment class?  <https://docs.prefect.io/orchestration/execution/overview.html#environments>

Are you using the default?

**philip.bennett**: I believe so

**philip.bennett**: I will double check with the other developer

**chris**: AH I actually think I see the issue; I think this might be a bug in that yaml spec!  Let me open a GitHub issue for you and see if we can reproduce + fix it before our next release

**chris**: <@ULVA73B9P> open “Kubernetes Agent not using env-vars flag correctly”

Original thread can be found [here](https://prefect-community.slack.com/archives/CL09KU1K7/p1598969070112200?thread_ts=1598969070.112200&cid=CL09KU1K7).




Discussion:

Resolving patch:
diff --git a/src/prefect/agent/kubernetes/agent.py b/src/prefect/agent/kubernetes/agent.py
--- a/src/prefect/agent/kubernetes/agent.py
+++ b/src/prefect/agent/kubernetes/agent.py
@@ -3,6 +3,7 @@
 import uuid
 from typing import Iterable, List
 
+import json
 import yaml
 
 import prefect
@@ -444,10 +445,12 @@ def generate_deployment_yaml(
         agent_env[10]["value"] = service_account_name
 
         if env_vars:
-            for k, v in env_vars.items():
-                agent_env.append(
-                    {"name": f"PREFECT__CLOUD__AGENT__ENV_VARS__{k}", "value": v}
-                )
+            agent_env.append(
+                {
+                    "name": "PREFECT__CLOUD__AGENT__ENV_VARS",
+                    "value": json.dumps(env_vars),
+                }
+            )
 
         # Use local prefect version for image
         deployment["spec"]["template"]["spec"]["containers"][0][


Test patch:
diff --git a/tests/agent/test_k8s_agent.py b/tests/agent/test_k8s_agent.py
--- a/tests/agent/test_k8s_agent.py
+++ b/tests/agent/test_k8s_agent.py
@@ -556,18 +556,15 @@ def test_k8s_agent_generate_deployment_yaml_env_vars(monkeypatch, cloud_api):
     )
 
     agent = KubernetesAgent()
-    deployment = agent.generate_deployment_yaml(
-        env_vars={"test1": "test2", "test3": "test4"}
-    )
+    env_vars = {"test1": "test2", "test3": "test4"}
+    deployment = agent.generate_deployment_yaml(env_vars=env_vars)
 
     deployment = yaml.safe_load(deployment)
 
     agent_env = deployment["spec"]["template"]["spec"]["containers"][0]["env"]
 
-    assert agent_env[13]["name"] == "PREFECT__CLOUD__AGENT__ENV_VARS__test1"
-    assert agent_env[13]["value"] == "test2"
-    assert agent_env[14]["name"] == "PREFECT__CLOUD__AGENT__ENV_VARS__test3"
-    assert agent_env[14]["value"] == "test4"
+    assert agent_env[13]["name"] == "PREFECT__CLOUD__AGENT__ENV_VARS"
+    assert agent_env[13]["value"] == json.dumps(env_vars)
 
 
 def test_k8s_agent_generate_deployment_yaml_backend_default(monkeypatch, server_api):