Skip to content

Commit 4661b80

Browse files
add env & env_from spec
1 parent d2e311f commit 4661b80

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

sdk/python/v1beta1/kubeflow/katib/api/katib_client.py

+18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from kubeflow.katib.constants import constants
2626
from kubeflow.katib.utils import utils
2727
from kubernetes import client, config
28+
from kubernetes.client.models import V1EnvVar, V1EnvFromSource
2829

2930

3031
class KatibClient(object):
@@ -140,6 +141,8 @@ def tune(
140141
parameters: Dict[str, Any],
141142
base_image: str = constants.BASE_IMAGE_TENSORFLOW,
142143
namespace: Optional[str] = None,
144+
env: Union[Dict[str, str], List[V1EnvVar], None] = None,
145+
env_from: Union[V1EnvFromSource, List[V1EnvFromSource], None] = None,
143146
algorithm_name: str = "random",
144147
algorithm_settings: Union[dict, List[models.V1beta1AlgorithmSetting], None] = None,
145148
objective_metric_name: str = None,
@@ -172,6 +175,13 @@ def tune(
172175
objective function.
173176
base_image: Image to use when executing the objective function.
174177
namespace: Namespace for the Experiment.
178+
env: Environment variable(s) to be attached to each trial container. You can either specifiy
179+
a list of kubernetes.client.models.V1EnvVar (documented here:
180+
https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1EnvVar.md) or a dictionary
181+
corresponding to the environment variable name and value pair(s).
182+
env_from: Source(s) of environment variables to be populated in each trial container. You can either specify
183+
a kubernetes.client.models.V1EnvFromSource (documented here:
184+
https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1EnvFromSource.md) or a list of such a type.
175185
algorithm_name: Search algorithm for the HyperParameter tuning.
176186
algorithm_settings: Settings for the search algorithm given.
177187
For available fields, check this doc: https://www.kubeflow.org/docs/components/katib/experiment/#search-algorithms-in-detail.
@@ -319,6 +329,12 @@ def tune(
319329
limits=resources_per_trial,
320330
)
321331

332+
if isinstance(env, dict):
333+
env = [V1EnvVar(name=str(k), value=str(v)) for k, v in env.items()]
334+
335+
if isinstance(env_from, V1EnvFromSource):
336+
env_from = [env_from]
337+
322338
# Create Trial specification.
323339
trial_spec = client.V1Job(
324340
api_version="batch/v1",
@@ -336,6 +352,8 @@ def tune(
336352
image=base_image,
337353
command=["bash", "-c"],
338354
args=[exec_script],
355+
env=env,
356+
env_from=env_from,
339357
resources=resources_per_trial,
340358
)
341359
],

0 commit comments

Comments
 (0)