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

Add back support for reading from parent command, config file, or subcommand in flyte-cli #890

Merged
merged 2 commits into from
Mar 16, 2022
Merged
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
41 changes: 35 additions & 6 deletions flytekit/clis/flyte_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,13 @@ def _render_schedule_expr(lp):

def _get_client(host: str, insecure: bool) -> _friendly_client.SynchronousFlyteClient:
parent_ctx = _click.get_current_context(silent=True)
kwargs = {}
if parent_ctx.obj["cacert"]:
kwargs["root_certificates"] = parent_ctx.obj["cacert"]
cfg = parent_ctx.obj["config"]
cfg = cfg.with_parameters(endpoint=host, insecure=insecure)
return _friendly_client.SynchronousFlyteClient(cfg, root_certificates=parent_ctx.obj["cacert"])

return _friendly_client.SynchronousFlyteClient(cfg, **kwargs)


_PROJECT_FLAGS = ["-p", "--project"]
Expand Down Expand Up @@ -477,6 +481,7 @@ class _FlyteSubCommand(_click.Command):
}

def make_context(self, cmd_name, args, parent=None):
# This list represents the set of args/flags that can be, and are, set on both the parent and subcommand.
prefix_args = []
for param in self.params:
if (
Expand All @@ -499,14 +504,38 @@ def make_context(self, cmd_name, args, parent=None):
if cmd_name == "setup-config":
return ctx

config = parent.params["config"]
if config is None:
config_file = parent.params["config"]
if config_file is None:
# Run this as the module is loading to pick up settings that click can
# then use when constructing the commands
config = _detect_default_config_file()
config_file = _detect_default_config_file()

config_obj = configuration.PlatformConfig.auto(config_file=config_file)

# These two flags are special in that they are specifiable in both the user's default ~/.flyte/config file,
# and in the flyte-cli command itself, both in the parent-command position (flyte-cli) , and in the
# child-command position (e.g. list-task-names).
# For both host and insecure, command line values will override the setting in a config file.
#
# The host url option is a required setting, so if missing it will fail, but it may be set in the click command,
# so we don't have to check now. It will be checked later.

# If the host was not specified in the parent command, and also not in the subcommand, but is in the file,
# then add in the switch and value before creating the context
if _HOST_FLAGS[0] not in prefix_args and _HOST_FLAGS[0] not in args and config_obj.endpoint:
prefix_args.extend([_HOST_FLAGS[0], config_obj.endpoint])

# If insecure was not in the parent command and not in the subcommand, but is true in the config object (the
# default is False), then add the flag to the args before creating the context.
if _INSECURE_FLAGS[0] not in prefix_args and _INSECURE_FLAGS[0] not in args and config_obj.insecure:
prefix_args.append(_INSECURE_FLAGS[0])

# Create context object and fill in with additional objects
ctx = super(_FlyteSubCommand, self).make_context(cmd_name, prefix_args + args, parent=parent)
ctx.obj = ctx.obj or {}
ctx.obj["cacert"] = parent.params["cacert"] or None
ctx.obj["config"] = config_obj

print("Config loading")
ctx.obj["config"] = configuration.PlatformConfig.auto(config_file=config)
return ctx


Expand Down
1 change: 1 addition & 0 deletions flytekit/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ def with_parameters(
) -> PlatformConfig:
return PlatformConfig(
endpoint=endpoint,
insecure=insecure,
command=command,
client_id=client_id,
client_credentials_secret=client_credentials_secret,
Expand Down