Skip to content

Commit 4081bcc

Browse files
authored
Allow field path env variables
Differential Revision: D59865500 Pull Request resolved: #935
1 parent 76ea111 commit 4081bcc

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

torchx/schedulers/kubernetes_scheduler.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,17 @@
169169

170170
LABEL_INSTANCE_TYPE = "node.kubernetes.io/instance-type"
171171

172+
# role.env translates to static env variables in the yaml
173+
# {"FOO" : "bar"} =====> - name: FOO
174+
# value: bar
175+
# unless this placeholder is present at the start of the role.env value then the env variable
176+
# in the yaml will be dynamically populated at runtime (placeholder is stripped out of the value)
177+
# {"FOO" : "[FIELD_PATH]bar"} =====> - name: FOO
178+
# valueFrom:
179+
# fieldRef:
180+
# fieldPath: bar
181+
PLACEHOLDER_FIELD_PATH = "[FIELD_PATH]"
182+
172183

173184
def sanitize_for_serialization(obj: object) -> object:
174185
from kubernetes import client
@@ -183,7 +194,9 @@ def role_to_pod(name: str, role: Role, service_account: Optional[str]) -> "V1Pod
183194
V1ContainerPort,
184195
V1EmptyDirVolumeSource,
185196
V1EnvVar,
197+
V1EnvVarSource,
186198
V1HostPathVolumeSource,
199+
V1ObjectFieldSelector,
187200
V1ObjectMeta,
188201
V1PersistentVolumeClaimVolumeSource,
189202
V1Pod,
@@ -303,9 +316,20 @@ def role_to_pod(name: str, role: Role, service_account: Optional[str]) -> "V1Pod
303316
image=role.image,
304317
name=name,
305318
env=[
306-
V1EnvVar(
307-
name=name,
308-
value=value,
319+
(
320+
V1EnvVar(
321+
name=name,
322+
value_from=V1EnvVarSource(
323+
field_ref=V1ObjectFieldSelector(
324+
field_path=value.strip(PLACEHOLDER_FIELD_PATH)
325+
)
326+
),
327+
)
328+
if value.startswith(PLACEHOLDER_FIELD_PATH)
329+
else V1EnvVar(
330+
name=name,
331+
value=value,
332+
)
309333
)
310334
for name, value in role.env.items()
311335
],

torchx/schedulers/test/kubernetes_scheduler_test.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
KubernetesOpts,
2929
KubernetesScheduler,
3030
LABEL_INSTANCE_TYPE,
31+
PLACEHOLDER_FIELD_PATH,
3132
role_to_pod,
3233
)
3334
from torchx.specs import AppState
@@ -75,7 +76,7 @@ def _test_app(num_replicas: int = 1) -> specs.AppDef:
7576
"--rank0-env",
7677
specs.macros.rank0_env,
7778
],
78-
env={"FOO": "bar"},
79+
env={"FOO": "bar", "FOO_FIELD_PATH": f"{PLACEHOLDER_FIELD_PATH}bar"},
7980
resource=specs.Resource(
8081
cpu=2,
8182
memMB=3000,
@@ -149,7 +150,9 @@ def test_role_to_pod(self) -> None:
149150
V1ContainerPort,
150151
V1EmptyDirVolumeSource,
151152
V1EnvVar,
153+
V1EnvVarSource,
152154
V1HostPathVolumeSource,
155+
V1ObjectFieldSelector,
153156
V1ObjectMeta,
154157
V1Pod,
155158
V1PodSpec,
@@ -188,7 +191,15 @@ def test_role_to_pod(self) -> None:
188191
],
189192
image="pytorch/torchx:latest",
190193
name="name",
191-
env=[V1EnvVar(name="FOO", value="bar")],
194+
env=[
195+
V1EnvVar(name="FOO", value="bar"),
196+
V1EnvVar(
197+
name="FOO_FIELD_PATH",
198+
value_from=V1EnvVarSource(
199+
field_ref=V1ObjectFieldSelector(field_path="bar")
200+
),
201+
),
202+
],
192203
resources=resources,
193204
ports=[V1ContainerPort(name="foo", container_port=1234)],
194205
security_context=V1SecurityContext(),
@@ -303,6 +314,10 @@ def test_submit_dryrun(self) -> None:
303314
env:
304315
- name: FOO
305316
value: bar
317+
- name: FOO_FIELD_PATH
318+
valueFrom:
319+
fieldRef:
320+
fieldPath: bar
306321
- name: TORCHX_RANK0_HOST
307322
value: localhost
308323
image: pytorch/torchx:latest

0 commit comments

Comments
 (0)