Hey there, I've been having trouble setting up env...
# ask-the-community
x
Hey there, I've been having trouble setting up environment variables in Flyte pods. It doesn't seem to be working at all. I have tried the following code:
Copy code
PRIMARY_CONTAINER_NAME = "primary"
FLYTE_POD_SPEC = V1PodSpec(
    containers=[
        V1Container(
            env=[
                V1EnvVar(
                    name="K8S_POD_NAME",
                    value_from=V1EnvVarSource(
                        field_ref=V1ObjectFieldSelector(
                            field_path="metadata.name",
                        )
                    ),
                ),
                V1EnvVar(
                    name="K8S_POD_UID",
                    value_from=V1EnvVarSource(
                        field_ref=V1ObjectFieldSelector(
                            field_path="metadata.uid",
                        )
                    ),
                ),
                V1EnvVar(
                    name="TEST_ENV",
                    value="test-test",
                ),
            ],
        )
    ],
)
task_config = Pod(pod_spec=FLYTE_POD_SPEC, primary_container_name=PRIMARY_CONTAINER_NAME)
Unfortunately, none of the environment variables were set using the above code. I'm not sure if I'm doing something wrong or if there's something missing. Is it possible to set up env vars this way?
y
what does the task definition look like?
like what’s on the Task tab when you launch it?
can you copy/paste a redacted version of that here please?
d
@Xiangxiang Ma running the above code with a task:
Copy code
PRIMARY_CONTAINER_NAME = "primary"
FLYTE_POD_SPEC = V1PodSpec(
    containers=[
        V1Container(
            env=[
                V1EnvVar(
                    name="K8S_POD_NAME",
                    value_from=V1EnvVarSource(
                        field_ref=V1ObjectFieldSelector(
                            field_path="metadata.name",
                        )
                    ),
                ),
                V1EnvVar(
                    name="K8S_POD_UID",
                    value_from=V1EnvVarSource(
                        field_ref=V1ObjectFieldSelector(
                            field_path="metadata.uid",
                        )
                    ),
                ),
                V1EnvVar(
                    name="TEST_ENV",
                    value="test-test",
                ),
            ],
        )
    ],
)
task_config = Pod(pod_spec=FLYTE_POD_SPEC, primary_container_name=PRIMARY_CONTAINER_NAME)

@task(task_config=task_config)
def my_pod_task_with_env() -> str:
    return "hello world!"
results in :
Copy code
(.venv) hamersaw@ragnarok:~/Development/flytetest$ pyflyte run --remote pod.py my_pod_task_with_env
Failed with Unknown Exception <class 'ValueError'> Reason: Invalid value for `name`, must not be `None`
Invalid value for `name`, must not be `None`
however, when I add the name to the
V1Container
like:
Copy code
PRIMARY_CONTAINER_NAME = "primary"
FLYTE_POD_SPEC = V1PodSpec(
    containers=[
        V1Container(
            name=PRIMARY_CONTAINER_NAME,
            env=[
                V1EnvVar(
                    name="K8S_POD_NAME",
                    value_from=V1EnvVarSource(
                        field_ref=V1ObjectFieldSelector(
                            field_path="metadata.name",
                        )
                    ),
                ),
                V1EnvVar(
                    name="K8S_POD_UID",
                    value_from=V1EnvVarSource(
                        field_ref=V1ObjectFieldSelector(
                            field_path="metadata.uid",
                        )
                    ),
                ),
                V1EnvVar(
                    name="TEST_ENV",
                    value="test-test",
                ),
            ],
        )
    ],
)
task_config = Pod(pod_spec=FLYTE_POD_SPEC, primary_container_name=PRIMARY_CONTAINER_NAME)

@task(task_config=task_config)
def my_pod_task_with_env() -> str:
    return "hello world!"
then all of the environment variables are correctly set on the k8s Pod:
Copy code
hamersaw@ragnarok:~$ kubectl -n flytesnacks-development get pods f617b2923f70244caa47-podmypodtaskwithenv-0 -o yaml
...
spec:
  affinity: {}
  containers:
...
    env:
    - name: K8S_POD_NAME
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: metadata.name
    - name: K8S_POD_UID
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: metadata.uid
    - name: TEST_ENV
      value: test-test
v
Hey I’m also using pod spec with task config to set env-vars and more You can see my working example here for comparison https://flyte-org.slack.com/archives/C01P3B761A6/p1687256261978239?thread_ts=1687244445.667749&amp;cid=C01P3B761A6
x
Update: it seems that by updating flytekitplugins-pod==1.2.0 to 1.2.4 solved the issue :)