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

Refactor workspaces into projects #3364

Open
wants to merge 37 commits into
base: develop
Choose a base branch
from
Open

Conversation

stefannica
Copy link
Contributor

@stefannica stefannica commented Feb 18, 2025

Describe changes

This mega-PR lands the preparation work for the ZenML Projects and clears out a huge chunk of technical debt in the process.

Introduce Global Resources

The following domain entities are refactored to live outside the workspace scope: stacks, stack components, flavors, service connectors and secrets:

[x] the database schema: removed the workspace reference from global resource types.
[x] the database migration: removed the workspace field from global resource types. This is conditioned by there being only one workspace, to avoid name clashes.
[x] the domain models: switched from workspace-scoped to user-scoped request/update/read/filter models for global resource types.

Refactor Workspace-Scoped Endpoints

This PR disbands the create/list workspace-scoped endpoints in the zenml.zen_server.routers.workspaces_endpoints module and distributes its endpoints to the other router modules that handle the corresponding resource types. The workspace scoped routers are still kept around for dashboard backwards compatibility, but are merely slapped on top of the regular endpoint implementations that use the standard pattern of setting the workspace field in the request models instead of URL path parameters, e.g.:

@router.post(
    "",
    response_model=PipelineResponse,
    responses={401: error_response, 409: error_response, 422: error_response},
)
# TODO: the workspace scoped endpoint is only kept for dashboard compatibility
# and can be removed after the migration
@workspace_router.post(
    "/{workspace_name_or_id}" + PIPELINES,
    response_model=PipelineResponse,
    responses={401: error_response, 409: error_response, 422: error_response},
    deprecated=True,
    tags=["pipelines"],
)
@handle_exceptions
def create_pipeline(
    pipeline: PipelineRequest,
    workspace_name_or_id: Optional[Union[str, UUID]] = None,
    _: AuthContext = Security(authorize),
) -> PipelineResponse:
    """Creates a pipeline, optionally in a specific workspace.

    Args:
        pipeline: Pipeline to create.
        workspace_name_or_id: Optional name or ID of the workspace.

    Returns:
        The created pipeline.
    """
    if workspace_name_or_id:
        workspace = zen_store().get_workspace(workspace_name_or_id)
        pipeline.workspace = workspace.id
...

List endpoints and other get/update/delete endpoints that use "name or ID" fields as input arguments and work with workspace-scoped resources have also been heavily refactored to enforce setting the workspace scope in the WorkspaceScopedFilter model. The following process is implemented to force the Python client to pass the workspace field in all these requests while at the same time keeping backwards-compatibility with the current dashboard:

  • some endpoints enforce passing the workspace scope as a parameter in addition to the regular arguments
  • for list endpoints, if the WorkspaceScopedFilter model has the workspace field set to a workspace UUID, use that as the workspace scope
  • for list endpoints, if the WorkspaceScopedFilter model has the workspace field set to a workspace name, resolve that to a workspace object and use its ID as the workspace scope
  • for list endpoints, if the WorkspaceScopedFilter model does not have the workspace field set at all, normally, we would raise an error. However, to maintain backwards-compatibility with the current dashboard, the "Default Workspace" mechanism described in the next session is implemented and used as a fallback.
  • the SQL Zen Store will fail all list requests applied to a workspace-scoped resource if the workspace scope is missing and cannot be resolved. Same goes for get/update/delete requests for workspace-scoped resources that are identified by name instead of UUID.
  • the RBAC checks will also fail if the workspace scope is missing from workspace scoped list filters

Default Workspace Provisional Mechanism

This is a newly implemented mechanism that comes in addition to the well known default workspace. It is applied whenever the client calling a REST endpoint or SQL Zen store request failed to supply the mandatory workspace scope for the workspace-scoped resource(s) to which the call refers. It goes as follows:

  • the user has a new optional "default workspace" field
  • the default workspace can be set for the active user via a REST API call to one of the existing workspaces
  • this workspace is used as a fall-back when the workspace isn't explicitly provided in inbound calls
  • if this field is also missing, the default workspace is used as a last resort

This mechanism is provisional and will be removed once the dashboard has been updated to include the workspace scope explicitly in all requests made to the API. The default workspace is also going to become a legacy concept, and no longer automatically created for new Pro ZenML servers.

Server-Side User Scoping

User-scoping for create requests used to be the responsibility of the client, while the server only verified that the user is set properly to match the active (authorized) users. This PR makes a fundamental change to this responsibility model:

  • clients no longer have to set the user field in the create requests (it's optional and no longer listed as a OpenAPI model field).
  • the SQL Zen store populates this field automatically for all incoming "create" requests to the active user (exception: default stacks and components and builtin flavors)
  • the Client code is updated to not set the user field in and of the "create" requests.

RBAC Improvements

The RBAC utilities used in API endpoints were improved with the following:

  • add workspace-scoped checks and use new workspace-scoped resource definitions
  • verify the proper scopes are set in all calls
  • reduce the amount of arguments needed to call the utilities

Refactor Secret Scopes to Private Secrets

The workspace/user scoping feature associated with secrets has been refactored into a "Private Secrets" feature. This was necessary given that secrets are no longer workspace-scoped entities.

Secrets can be "private" or "public". Private secrets are only visible by their owner.

Technical Debt / Bugs Removal and Quality of Life Improvements

  • removed the duplicated user_id field from the user scoped base filter model (merged into user)
  • removed the duplicated workspace_id field from the workspace scoped base filter model (merged into `workspace)
  • refactored and unified the name uniqueness verification logic in the SQLZenStore to use the same logic for all resource types and take into account the scope of the resource
  • unified "get-by-name-or-id" scoping checks for SQLZenStore methods to include workspace scoping rules: all get requests that have a name_or_id argument targeting a workspace-scoped resource must also have a workspace ID argument
  • unified "get-by-id" SQLZenStore calls: all get requests that have an id argument call the same method in the SQLZenStore.
  • unified reference scope fetch/verifications in SQLZenStore calls: fetching and verifying the scoping rules for referenced resources in all create/update requests is done using the same pattern
  • refactored the flavor name uniqueness to no longer be user-scoped (i.e. scoped only to name+type) @bcdurak
  • make tags user-scoped (they were previously not owned by any user) @bcdurak
  • make artifacts workspace and user-scoped (they were previously not scoped to either of these)
  • all filters inherited from TaggableFilter were previously broken because they didn't handle the double-inheritance at all when calling super(). This is fixed now.
  • updated the existing DB unique constraints and added new ones to be aligned with the the scopes and name uniqueness checks enforced through code in the SQLZenStore
  • refactored the model/model version Zen store API and endpoints to be aligned with the existing patterns and to use workspace scoping:
    • get_model/delete_model: use the model ID instead of the id_or_name argument
    • list_model_versions(): use filter with model scoping instead of the model name_or_id argument

Breaking Changes

  • the exceptions StackComponentExistsError, StackExistsError and SecretExistsError have been removed. You should use the common EntityExistsError exception class instead.
  • the get_run REST API call has been merged into the existing get_or_create_run API call.
  • if your ZenML installation has more than one workspace, the DB migration will fail during the upgrade. You need to list/delete the additional workspaces using code or the OpenAPI dashboard beforehand, e.g.:
(TODO: include code instructions)

Pre-requisites

Please ensure you have done the following:

  • I have read the CONTRIBUTING.md document.
  • I have added tests to cover my changes.
  • I have based my new branch on develop and the open PR is targeting develop. If your branch wasn't based on develop read Contribution guide on rebasing branch to develop.
  • IMPORTANT: I made sure that my changes are reflected properly in the following resources:
    • ZenML Docs
    • Dashboard: Needs to be communicated to the frontend team.
    • Templates: Might need adjustments (that are not reflected in the template tests) in case of non-breaking changes and deprecations.
    • Projects: Depending on the version dependencies, different projects might get affected.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Other (add details above)

Copy link
Contributor

coderabbitai bot commented Feb 18, 2025

Important

Review skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@stefannica stefannica changed the title Refactor workspaces into projects and Refactor workspaces into projects Feb 18, 2025
@github-actions github-actions bot added internal To filter out internal PRs and issues enhancement New feature or request labels Feb 18, 2025
@stefannica stefannica force-pushed the feature/zenml-projects branch 4 times, most recently from cc149c0 to b31ab41 Compare February 21, 2025 19:11
Whether the resource type is workspace scoped.
"""
return resource_type in [
ResourceType.STACK,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems wrong still?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good eye! However, this was just a left-over function that was completely unused. I already moved everything to the ResourceType enum class.

It's gone now.

@stefannica stefannica force-pushed the feature/zenml-projects branch from b31ab41 to f820ab2 Compare February 24, 2025 19:59
schustmi and others added 20 commits February 28, 2025 09:17
* includes handling default stack / components
* includes removing the client code that sets the user field in requests
  to the active user
…ce scoping

Also removed all *ExistsError exceptions, since they were completely
unused, and replaced the all with EntityExistsError
@stefannica stefannica marked this pull request as ready for review February 28, 2025 08:18
@stefannica stefannica force-pushed the feature/zenml-projects branch from 9404ab4 to 669359a Compare February 28, 2025 08:18
@stefannica stefannica requested a review from bcdurak February 28, 2025 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change enhancement New feature or request internal To filter out internal PRs and issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants