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

Reflect env variables on global configuration #2371

Merged
merged 13 commits into from
Feb 28, 2024

Conversation

safoinme
Copy link
Contributor

@safoinme safoinme commented Jan 29, 2024

Describe changes

Fix for the following issue:
image

Details

This PR implements an environment variable overlay that is applied on top of the current store configuration, regardless of whether it is loaded from the global configuration file or explicitly configured using set_store. The environment variables are used to either replace the active store configuration or merged on top of them, depending on how complete they are.

Downside: if any environment variables are used to configure the zen store, they will also be reflected in the configuration file, thus replacing what was there before. There is no easy way to prevent this, because we rely on the actual running store configuration to be dumped back into the configuration file for persistence purposes (e.g. to persist the API token).

More changes included to clean up the already overloaded global configuration:

  • got rid of copy_configuration methods that are no longer used
  • got rid of any attributes and methods in the global configuration class that are no longer needed
  • implemented a store_configuration property that is the source of truth regarding the actual store configuration, with env variables applied and used it to replace any references to the .store property that is not always as accurate and does not include environment variable overrides.
  • moved the method that converts the global config into env variables (i.e. the opposite operation) into the global config class

Pre-requisites

Please ensure you have done the following:

  • I have read the CONTRIBUTING.md document.
  • If my change requires a change to docs, I have updated the documentation accordingly.
  • 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.
  • If my changes require changes to the dashboard, these changes are communicated/requested.

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)

Summary by CodeRabbit

  • Refactor
    • Improved the logic for accessing the Zen store, ensuring more reliable and direct checking for store availability.

Copy link
Contributor

coderabbitai bot commented Jan 29, 2024

Important

Auto 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.

Walkthrough

The recent update in the ZenML configuration involves a streamlined approach to accessing the Zen store. The process has been simplified by directly checking the presence of the Zen store and eliminating the previous configuration step. Additionally, an assertion ensures the Zen store's attribute is always set, enhancing the reliability of the system.

Changes

File Change Summary
src/zenml/.../global_config.py Modified Zen store access logic: removed _configure_store call, added direct check and assertion for _zen_store.

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>.
    • Generate unit-tests for this file.
  • 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 tests 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 generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@github-actions github-actions bot added internal To filter out internal PRs and issues bug Something isn't working labels Jan 29, 2024
@safoinme
Copy link
Contributor Author

@coderabbitai review

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: .coderabbit.yaml

Commits Files that changed from the base of the PR and between 9f31c6b and 3684698.
Files selected for processing (1)
  • src/zenml/config/global_config.py (1 hunks)
Additional comments: 1
src/zenml/config/global_config.py (1)
  • 672-672: The modification to the zen_store property to directly set the default store if _zen_store is None simplifies the logic. However, ensure that removing the _configure_store call does not omit necessary initializations that might have been performed within _configure_store. The added assertion ensures _zen_store is not None after attempting to set the default store, which is a good practice for ensuring the property's integrity.

@@ -669,11 +669,8 @@ def zen_store(self) -> "BaseZenStore":
Returns:
The current zen store.
"""
if not self.store:
if self._zen_store is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain how these changes actually solve the issue? It seems to me like we now always write the default config to disk, doesn't that lose any information that we store in the file?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

does adding a check on self.store after env variables solve the issue? since it will only go to return the get_default_store_config if no env variables where set

Copy link
Contributor

Choose a reason for hiding this comment

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

Updated now to do the right thing: use the merged configuration to initialize the store.

Comment on lines 566 to 570
config = StoreConfiguration(
**env_store_config,
)
elif self.store:
config = self.store
Copy link
Contributor

Choose a reason for hiding this comment

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

To add to Michael's comment: it seems to me that in this case either ENV go or File, but not a merger of them, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good point! maybe I understood the ticket differently. In that case, I imagine we can do something like the following, where we see if self.config is set and then update config based on existing env variables. @schustmi @avishniakov

# Update self.config based on environment variables
if self.config:
    # Update the store configuration from environment variables
    if env_store_config:
        if "type" not in env_store_config and "url" in env_store_config:
            env_store_config["type"] = BaseZenStore.get_store_type(env_store_config["url"])

        # Merge environment variables into self.config for store
        for key, value in env_store_config.items():
            self.config[key] = value

    # Update the secrets store configuration
    for key, value in env_secrets_store_config.items():
        self.config.secrets_store[key] = value

    # Update the backup secrets store configuration
    for key, value in env_backup_secrets_store_config.items():
        self.config.backup_secrets_store[key] = value

    logger.debug("Updated the configuration with environment variables.")
else:
    if len(env_store_config):
        if "type" not in env_store_config and "url" in env_store_config:
            env_store_config["type"] = BaseZenStore.get_store_type(env_store_config["url"])

        logger.debug("Using environment variables to configure the default store")

        self.config = StoreConfiguration(**env_store_config)
    else:
        self.config = BaseZenStore.get_default_store_config(
            path=os.path.join(self.local_stores_path, DEFAULT_STORE_DIRECTORY_NAME)
        )

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, something like this might do. Can you write some tests to cover this use-case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@avishniakov Looking back on this, we can't merge. With the env variables, we need to initialize a new zen store which in this case will be the RestZenStore if we use the already existing one which is SQLZenStore config and we just updated (merge) to initialize the rest one it will fail because rest zen store doesn't the same attributes as sql one and each of these stores has different attributes besides type, URL, and SSL.

Copy link
Contributor

Choose a reason for hiding this comment

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

I implemented a custom merge logic that in some cases replaces the config file store completely, in other cases it only overrides some of its attributes.

@safoinme safoinme requested review from avishniakov and removed request for bcdurak January 30, 2024 13:05
@avishniakov avishniakov marked this pull request as draft February 19, 2024 08:43
@stefannica stefannica marked this pull request as ready for review February 27, 2024 18:23
@safoinme safoinme removed the request for review from stefannica February 28, 2024 09:05
@safoinme safoinme requested review from bcdurak and removed request for schustmi February 28, 2024 09:05
@strickvl strickvl requested review from schustmi and removed request for schustmi February 28, 2024 09:57
Copy link
Contributor

@avishniakov avishniakov left a comment

Choose a reason for hiding this comment

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

🦭

Copy link
Contributor

@stefannica stefannica left a comment

Choose a reason for hiding this comment

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

FWIW this looks good to me

@safoinme safoinme merged commit ef94a48 into develop Feb 28, 2024
55 checks passed
@safoinme safoinme deleted the bugfix/OSSK-36-reflect-env-changes-on-globalconfig branch February 28, 2024 16:02
adtygan pushed a commit to adtygan/zenml that referenced this pull request Mar 21, 2024
* reflect env variables on global configuration

* add check on self.store

* Remove unneeded copy_configuration functions

* Cleaned up unneded attrs and methods from the global config

* Move conversion to env variables to global config

* Implemented store environment variables overlay support for global config

* Ensure external code uses the actual active store configuration, not the persisted one

* Fix docstrings

* Fix unit tests

---------

Co-authored-by: Stefan Nica <stefan@zenml.io>
Co-authored-by: Alex Strick van Linschoten <strickvl@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working internal To filter out internal PRs and issues run-slow-ci
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants