Skip to content

Commit

Permalink
FLYTECTL_CONFIG env var higher precedence, config flag respected in p…
Browse files Browse the repository at this point in the history
…yflyte package (#1662)

Signed-off-by: Yee Hing Tong <[email protected]>
  • Loading branch information
wild-endeavor authored May 31, 2023
1 parent f671fb6 commit 484a221
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
8 changes: 8 additions & 0 deletions flytekit/clis/sdk_in_container/pyflyte.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import typing

import grpc
Expand All @@ -17,6 +18,7 @@
from flytekit.clis.sdk_in_container.run import run
from flytekit.clis.sdk_in_container.serialize import serialize
from flytekit.clis.sdk_in_container.serve import serve
from flytekit.configuration.file import FLYTECTL_CONFIG_ENV_VAR, FLYTECTL_CONFIG_ENV_VAR_OVERRIDE
from flytekit.configuration.internal import LocalSDK
from flytekit.exceptions.base import FlyteException
from flytekit.exceptions.user import FlyteInvalidInputException
Expand Down Expand Up @@ -120,6 +122,12 @@ def main(ctx, pkgs: typing.List[str], config: str, verbose: bool):
if config:
ctx.obj[CTX_CONFIG_FILE] = config
cfg = configuration.ConfigFile(config)
# Set here so that if someone has Config.auto() in their user code, the config here will get used.
if FLYTECTL_CONFIG_ENV_VAR in os.environ:
cli_logger.info(
f"Config file arg {config} will override env var {FLYTECTL_CONFIG_ENV_VAR}: {os.environ[FLYTECTL_CONFIG_ENV_VAR]}"
)
os.environ[FLYTECTL_CONFIG_ENV_VAR_OVERRIDE] = config
if not pkgs:
pkgs = LocalSDK.WORKFLOW_PACKAGES.read(cfg)
if pkgs is None:
Expand Down
22 changes: 16 additions & 6 deletions flytekit/configuration/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

# This is the env var that the flytectl sandbox instructions say to set
FLYTECTL_CONFIG_ENV_VAR = "FLYTECTL_CONFIG"
# This is an explicit override only to be used by pyflyte and takes precedence in get_config_file over the main
# environment variable.
# This env var should not be set by users
FLYTECTL_CONFIG_ENV_VAR_OVERRIDE = "_FLYTECTL_CONFIG_PYFLYTE_OVERRIDE"


def _exists(val: typing.Any) -> bool:
Expand Down Expand Up @@ -239,6 +243,17 @@ def get_config_file(c: typing.Union[str, ConfigFile, None]) -> typing.Optional[C
Checks if the given argument is a file or a configFile and returns a loaded configFile else returns None
"""
if c is None:
# Pyflyte override env var takes highest precedence
# Env var takes second highest precedence
flytectl_path_from_env = getenv(FLYTECTL_CONFIG_ENV_VAR_OVERRIDE, getenv(FLYTECTL_CONFIG_ENV_VAR, None))
if flytectl_path_from_env:
flytectl_path = Path(flytectl_path_from_env)
if flytectl_path.exists():
logger.info(f"Using flytectl/YAML config {flytectl_path.absolute()}")
return ConfigFile(str(flytectl_path.absolute()))
else:
logger.warning(f"flytectl config file {flytectl_path.absolute()} does not exist, ignoring...")

# See if there's a config file in the current directory where Python is being run from
current_location_config = Path("flytekit.config")
if current_location_config.exists():
Expand All @@ -251,13 +266,8 @@ def get_config_file(c: typing.Union[str, ConfigFile, None]) -> typing.Optional[C
logger.info(f"Using configuration from home directory {home_dir_config.absolute()}")
return ConfigFile(str(home_dir_config.absolute()))

# If not, see if the env var that flytectl sandbox tells the user to set is set,
# or see if there's something in the default home directory location
# If not see if there's something in the default home directory location
flytectl_path = Path(Path.home(), ".flyte", "config.yaml")
flytectl_path_from_env = getenv(FLYTECTL_CONFIG_ENV_VAR, None)

if flytectl_path_from_env:
flytectl_path = Path(flytectl_path_from_env)
if flytectl_path.exists():
logger.info(f"Using flytectl/YAML config {flytectl_path.absolute()}")
return ConfigFile(str(flytectl_path.absolute()))
Expand Down

0 comments on commit 484a221

Please sign in to comment.