Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FlyteCTL Feature] save registration process results into result_dir #1904

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions flytekit/clis/sdk_in_container/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@
callback=key_value_callback,
help="Environment variables to set in the container, of the format `ENV_NAME=ENV_VALUE`",
)
@click.option(
"--result_dir",
required=False,
type=click.Path(dir_okay=True, file_okay=False, writable=True, resolve_path=True),
default=None,
help="Directory to write the registration process results",
)
@click.option(
"-f",
"--format",
required=False,
type=click.Choice(["json", "yaml"], case_sensitive=False),
default="json",
help="Results file format",
)
@click.argument("package-or-module", type=click.Path(exists=True, readable=True, resolve_path=True), nargs=-1)
@click.pass_context
def register(
Expand All @@ -135,6 +150,8 @@ def register(
dry_run: bool,
activate_launchplans: bool,
env: typing.Optional[typing.Dict[str, str]],
result_dir: str,
format: str,
):
"""
see help
Expand Down Expand Up @@ -187,6 +204,8 @@ def register(
env=env,
dry_run=dry_run,
activate_launchplans=activate_launchplans,
result_dir=result_dir,
format=format,
)
except Exception as e:
raise e
1 change: 1 addition & 0 deletions flytekit/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
FUTURES_FILE_NAME = "futures.pb"
ERROR_FILE_NAME = "error.pb"
REQUIREMENTS_FILE_NAME = "requirements.txt"
REGISTRATION_RESULT_FILENAME = "output.{}"

CONTAINER_ARRAY_TASK = "container_array"
GLOBAL_INPUT_NODE_ID = ""
Expand Down
24 changes: 24 additions & 0 deletions flytekit/tools/repo.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import json
import os
import tarfile
import tempfile
import typing
from pathlib import Path

import click
import yaml

from flytekit.configuration import FastSerializationSettings, ImageConfig, SerializationSettings
from flytekit.core.constants import REGISTRATION_RESULT_FILENAME
from flytekit.core.context_manager import FlyteContextManager
from flytekit.loggers import logger
from flytekit.models import launch_plan
Expand Down Expand Up @@ -218,6 +221,8 @@ def register(
package_or_module: typing.Tuple[str],
remote: FlyteRemote,
env: typing.Optional[typing.Dict[str, str]],
result_dir: str,
format: str,
dry_run: bool = False,
activate_launchplans: bool = False,
):
Expand Down Expand Up @@ -266,6 +271,7 @@ def register(
click.secho("No Flyte entities were detected. Aborting!", fg="red")
return

registration_results = []
for cp_entity in registrable_entities:
is_lp = False
if isinstance(cp_entity, launch_plan.LaunchPlan):
Expand All @@ -286,6 +292,24 @@ def register(
secho(i, reason="activated", op="Activation")
else:
secho(og_id, reason="Dry run Mode!")
status = "SUCCESS"
except RegistrationSkipped:
secho(og_id, "failed")
status = "FAILED"

registration_results.append(
{
"id": og_id.name,
"type": og_id.resource_type_name(),
"version": og_id.version,
"status": status,
}
)
if result_dir:
with open(os.path.join(result_dir, REGISTRATION_RESULT_FILENAME.format(format)), "w") as f:
if format == "yaml":
txt = yaml.dump(registration_results)
else:
txt = json.dumps(registration_results)
f.write(txt)
click.secho(f"Successfully registered {len(registrable_entities)} entities", fg="green")
36 changes: 36 additions & 0 deletions tests/flytekit/unit/cli/pyflyte/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ def reset_flytectl_config_env_var() -> pytest.fixture():
return os.environ[FLYTECTL_CONFIG_ENV_VAR]


@pytest.fixture
def expected_result_file_content():
expected_result_json = '[{"id": "core_json.sample.sum", "type": "TASK", "version": "dummy_version_from_hash", "status": "SUCCESS"}, {"id": "core_json.sample.square", "type": "TASK", "version": "dummy_version_from_hash", "status": "SUCCESS"}, {"id": "core_json.sample.my_workflow", "type": "WORKFLOW", "version": "dummy_version_from_hash", "status": "SUCCESS"}, {"id": "core_json.sample.my_workflow", "type": "LAUNCH_PLAN", "version": "dummy_version_from_hash", "status": "SUCCESS"}]'
expected_result_yaml = "- id: core_yaml.sample.sum\n status: SUCCESS\n type: TASK\n version: dummy_version_from_hash\n- id: core_yaml.sample.square\n status: SUCCESS\n type: TASK\n version: dummy_version_from_hash\n- id: core_yaml.sample.my_workflow\n status: SUCCESS\n type: WORKFLOW\n version: dummy_version_from_hash\n- id: core_yaml.sample.my_workflow\n status: SUCCESS\n type: LAUNCH_PLAN\n version: dummy_version_from_hash\n"
return {
"json": expected_result_json,
"yaml": expected_result_yaml,
}


@mock.patch("flytekit.clis.sdk_in_container.helpers.FlyteRemote")
def test_get_remote(mock_remote, reset_flytectl_config_env_var):
r = get_remote(None, "p", "d")
Expand Down Expand Up @@ -159,3 +169,29 @@ def test_non_fast_register_require_version(mock_client, mock_remote):
assert result.exit_code == 1
assert str(result.exception) == "Version is a required parameter in case --non-fast is specified."
shutil.rmtree("core3")


@mock.patch("flytekit.clis.sdk_in_container.helpers.FlyteRemote", spec=FlyteRemote)
@mock.patch("flytekit.clients.friendly.SynchronousFlyteClient", spec=SynchronousFlyteClient)
@pytest.mark.parametrize("format", ["json", "yaml"])
def test_register_result_output(mock_client, mock_remote, format, expected_result_file_content):
ctx = FlyteContextManager.current_context()
mock_remote._client = mock_client
mock_remote.return_value.context = ctx
mock_remote.return_value._version_from_hash.return_value = "dummy_version_from_hash"
mock_remote.return_value.fast_package.return_value = "dummy_md5_bytes", "dummy_native_url"
runner = CliRunner()
context_manager.FlyteEntities.entities.clear()
dirname = f"core_{format}"
with runner.isolated_filesystem():
out = subprocess.run(["git", "init"], capture_output=True)
assert out.returncode == 0
os.makedirs(dirname, exist_ok=True)
with open(os.path.join(dirname, "sample.py"), "w") as f:
f.write(sample_file_contents)
result = runner.invoke(pyflyte.main, ["register", f"--result_dir={dirname}", f"--format={format}", dirname])
assert "Successfully registered 4 entities" in result.output
with open(os.path.join(dirname, f"output.{format}"), "r") as f:
omg = f.read()
assert expected_result_file_content[format] == omg
shutil.rmtree(dirname)