Skip to content

Commit 74bd971

Browse files
authored
Add current_project helper and update examples (#645)
## Summary - Add flyte.current_project() helper to complement the existing flyte.current_domain() method, providing a symmetric API for resolving the current project from runtime context or init config. - Update webhook examples to use flyte.current_project() and flyte.current_domain() instead of manually reading FLYTE_INTERNAL_EXECUTION_* env vars. ## Test Plan Run pytest tests/user_api/test_initialize.py --------- Signed-off-by: M. Adil Fayyaz <62440954+AdilFayyaz@users.noreply.github.com>
1 parent 86a57d6 commit 74bd971

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

examples/apps/run_webhook.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,10 @@ async def lifespan(app: FastAPI):
3434
This initializes Flyte with passthrough authentication, allowing the app to
3535
pass user credentials from incoming requests to the Flyte control plane.
3636
"""
37-
PROJECT_NAME_ENV_VAR = "FLYTE_INTERNAL_EXECUTION_PROJECT"
38-
DOMAIN_NAME_ENV_VAR = "FLYTE_INTERNAL_EXECUTION_DOMAIN"
39-
4037
# Startup: Initialize Flyte with passthrough authentication
4138
await flyte.init_passthrough.aio(
42-
project=os.getenv(PROJECT_NAME_ENV_VAR, None),
43-
domain=os.getenv(DOMAIN_NAME_ENV_VAR, None),
39+
project=flyte.current_project(),
40+
domain=flyte.current_domain(),
4441
)
4542
logger.info("Initialized Flyte passthrough auth")
4643
yield

examples/genai/n8n/flyte_webhook_app.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,9 @@
3939
# ---------------------------------------------------------------------------
4040
@asynccontextmanager
4141
async def lifespan(app: FastAPI):
42-
PROJECT_NAME_ENV_VAR = "FLYTE_INTERNAL_EXECUTION_PROJECT"
43-
DOMAIN_NAME_ENV_VAR = "FLYTE_INTERNAL_EXECUTION_DOMAIN"
44-
4542
await flyte.init_passthrough.aio(
46-
project=os.getenv(PROJECT_NAME_ENV_VAR, None),
47-
domain=os.getenv(DOMAIN_NAME_ENV_VAR, None),
43+
project=flyte.current_project(),
44+
domain=flyte.current_domain(),
4845
)
4946
logger.info("Initialized Flyte passthrough auth")
5047
yield

src/flyte/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@
1515
from ._excepthook import custom_excepthook
1616
from ._group import group
1717
from ._image import Image
18-
from ._initialize import current_domain, init, init_from_api_key, init_from_config, init_in_cluster, init_passthrough
18+
from ._initialize import (
19+
current_domain,
20+
current_project,
21+
init,
22+
init_from_api_key,
23+
init_from_config,
24+
init_in_cluster,
25+
init_passthrough,
26+
)
1927
from ._link import Link
2028
from ._logging import logger
2129
from ._map import map
@@ -97,6 +105,7 @@ def version() -> str:
97105
"build_images",
98106
"ctx",
99107
"current_domain",
108+
"current_project",
100109
"custom_context",
101110
"deploy",
102111
"get_custom_context",

src/flyte/_initialize.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,3 +788,32 @@ def current_domain() -> str:
788788
" or Call flyte.init_from_config() with a valid path to the config file",
789789
)
790790
return cfg.domain
791+
792+
793+
def current_project() -> str:
794+
"""
795+
Returns the current project from the Runtime environment (on the cluster) or from the initialized configuration.
796+
This is safe to be used during `deploy`, `run` and within `task` code.
797+
798+
NOTE: This will not work if you deploy a task to a project and then run it in another project.
799+
800+
Raises InitializationError if the configuration is not initialized or project is not set.
801+
:return: The current project
802+
"""
803+
from ._context import ctx
804+
805+
tctx = ctx()
806+
if tctx is not None:
807+
project = tctx.action.project
808+
if project is not None:
809+
return project
810+
811+
cfg = _get_init_config()
812+
if cfg is None or cfg.project is None:
813+
raise InitializationError(
814+
"ProjectNotInitializedError",
815+
"user",
816+
"Project has not been initialized. Call flyte.init() with a valid project before using this function"
817+
" or Call flyte.init_from_config() with a valid path to the config file",
818+
)
819+
return cfg.project

0 commit comments

Comments
 (0)