Skip to content

Commit 5832277

Browse files
MinuraPunchihewaMatthew Hoffman
authored andcommitted
Pyflyte --env Option for Register and Serialize (flyteorg#1880)
Use with `pyflyte register --env key=val ...` Signed-off-by: Minura Punchihewa <minurapunchihewa17@gmail.com>
1 parent 88fe671 commit 5832277

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

flytekit/clis/sdk_in_container/register.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from flytekit.clis.sdk_in_container.utils import domain_option_dec, project_option_dec
1010
from flytekit.configuration import ImageConfig
1111
from flytekit.configuration.default_images import DefaultImages
12+
from flytekit.interaction.click_types import key_value_callback
1213
from flytekit.loggers import cli_logger
1314
from flytekit.tools import repo
1415

@@ -107,6 +108,15 @@
107108
is_flag=True,
108109
help="Activate newly registered Launchplans. This operation deactivates previous versions of Launchplans.",
109110
)
111+
@click.option(
112+
"--env",
113+
"--envvars",
114+
required=False,
115+
multiple=True,
116+
type=str,
117+
callback=key_value_callback,
118+
help="Environment variables to set in the container, of the format `ENV_NAME=ENV_VALUE`",
119+
)
110120
@click.argument("package-or-module", type=click.Path(exists=True, readable=True, resolve_path=True), nargs=-1)
111121
@click.pass_context
112122
def register(
@@ -124,6 +134,7 @@ def register(
124134
package_or_module: typing.Tuple[str],
125135
dry_run: bool,
126136
activate_launchplans: bool,
137+
env: typing.Optional[typing.Dict[str, str]],
127138
):
128139
"""
129140
see help
@@ -173,6 +184,7 @@ def register(
173184
fast=not non_fast,
174185
package_or_module=package_or_module,
175186
remote=remote,
187+
env=env,
176188
dry_run=dry_run,
177189
activate_launchplans=activate_launchplans,
178190
)

flytekit/clis/sdk_in_container/serialize.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
from flytekit.clis.sdk_in_container.constants import CTX_PACKAGES
1010
from flytekit.configuration import FastSerializationSettings, ImageConfig, SerializationSettings
1111
from flytekit.exceptions.scopes import system_entry_point
12+
from flytekit.interaction.click_types import key_value_callback
1213
from flytekit.tools.fast_registration import fast_package
1314
from flytekit.tools.repo import serialize_to_folder
1415

1516
CTX_IMAGE = "image"
1617
CTX_LOCAL_SRC_ROOT = "local_source_root"
1718
CTX_FLYTEKIT_VIRTUALENV_ROOT = "flytekit_virtualenv_root"
1819
CTX_PYTHON_INTERPRETER = "python_interpreter"
20+
CTX_ENV = "env"
1921

2022

2123
class SerializationMode(_Enum):
@@ -33,6 +35,7 @@ def serialize_all(
3335
flytekit_virtualenv_root: typing.Optional[str] = None,
3436
python_interpreter: typing.Optional[str] = None,
3537
config_file: typing.Optional[str] = None,
38+
env: typing.Optional[typing.Dict[str, str]] = None,
3639
):
3740
"""
3841
This function will write to the folder specified the following protobuf types ::
@@ -64,6 +67,7 @@ def serialize_all(
6467
),
6568
flytekit_virtualenv_root=flytekit_virtualenv_root,
6669
python_interpreter=python_interpreter,
70+
env=env,
6771
)
6872

6973
serialize_to_folder(pkgs, serialization_settings, local_source_root, folder)
@@ -106,9 +110,23 @@ def serialize_all(
106110
"installed inside your container. Required for running `pyflyte serialize` in out of container mode when "
107111
"your container installs the flytekit virtualenv outside of the default `/opt/venv`",
108112
)
113+
@click.option(
114+
"--env",
115+
"--envvars",
116+
required=False,
117+
multiple=True,
118+
type=str,
119+
callback=key_value_callback,
120+
help="Environment variables to set in the container, of the format `ENV_NAME=ENV_VALUE`",
121+
)
109122
@click.pass_context
110123
def serialize(
111-
ctx, image_config: ImageConfig, local_source_root, in_container_config_path, in_container_virtualenv_root
124+
ctx,
125+
image_config: ImageConfig,
126+
local_source_root,
127+
in_container_config_path,
128+
in_container_virtualenv_root,
129+
env: typing.Optional[typing.Dict[str, str]],
112130
):
113131
"""
114132
This command produces protobufs for tasks and templates.
@@ -119,6 +137,7 @@ def serialize(
119137
"""
120138
ctx.obj[CTX_IMAGE] = image_config
121139
ctx.obj[CTX_LOCAL_SRC_ROOT] = local_source_root
140+
ctx.obj[CTX_ENV] = env
122141
click.echo(f"Serializing Flyte elements with image {image_config}")
123142

124143
if in_container_virtualenv_root:
@@ -155,6 +174,7 @@ def workflows(ctx, folder=None):
155174
flytekit_virtualenv_root=ctx.obj[CTX_FLYTEKIT_VIRTUALENV_ROOT],
156175
python_interpreter=ctx.obj[CTX_PYTHON_INTERPRETER],
157176
config_file=ctx.obj.get(constants.CTX_CONFIG_FILE, None),
177+
env=ctx.obj.get(CTX_ENV, None),
158178
)
159179

160180

@@ -194,6 +214,7 @@ def fast_workflows(ctx, folder=None, deref_symlinks=False):
194214
flytekit_virtualenv_root=ctx.obj[CTX_FLYTEKIT_VIRTUALENV_ROOT],
195215
python_interpreter=ctx.obj[CTX_PYTHON_INTERPRETER],
196216
config_file=ctx.obj.get(constants.CTX_CONFIG_FILE, None),
217+
env=ctx.obj.get(CTX_ENV, None),
197218
)
198219

199220

flytekit/tools/repo.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ def register(
217217
fast: bool,
218218
package_or_module: typing.Tuple[str],
219219
remote: FlyteRemote,
220+
env: typing.Optional[typing.Dict[str, str]],
220221
dry_run: bool = False,
221222
activate_launchplans: bool = False,
222223
):
@@ -239,6 +240,7 @@ def register(
239240
version=version,
240241
image_config=image_config,
241242
fast_serialization_settings=fast_serialization_settings,
243+
env=env,
242244
)
243245

244246
if not version and fast:

0 commit comments

Comments
 (0)