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

feature: apply env expansion consistently by moving it into a decode hook #60

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

devnev
Copy link

@devnev devnev commented Aug 21, 2024

Reason for This PR

Addresses roadrunner-server/roadrunner#1989 and likely other sources of confusion where certain strings in the config file might not have been getting the expand-env-with-default treatment.

Description of Changes

The code before this change iterates through configuration values with viper's AllKeys() method, and tries to apply the env expansion in cases it understands. However, AllKeys() doesn't descend into sequences/slices, which the previous code tried to handle by checking if the slice contained strings, but its also possible to have slices of more complex values, e.g. subobjects, which would be completely ignored by this case.

By moving the logic for expanding environment variables into the unmarshal decoder, it is applied to any string encountered during unmarshaling (at least I think thats the case based on my reading of how viper does things).

This will cause problems if other plugins apply their own form of env expansion, e.g. the server plugin doing its own os.Expand() on environment variable values. So this is probably a breaking change, and at the very least requires the server plugin to be updated accordingly. It also changes the semantics of the plugins Get() method, which will return values where the expansion has not been applied, and it changes Overwrite() because the values set using Overwrite will have env expansion applied to them.

An alternative might be to still iterate through viper.AllKeys(), but for each key use UnmarshalKey with the decode option to consistently apply env substitution, and and call viper.Set() with the result to write it back.

License Acceptance

By submitting this pull request, I confirm that my contribution is made under
the terms of the MIT license.

PR Checklist

[Author TODO: Meet these criteria.]
[Reviewer TODO: Verify that these criteria are met. Request changes if not]

  • All commits in this PR are signed (git commit -s).
  • The reason for this PR is clearly provided (issue no. or explanation).
  • The description of changes is clear and encompassing.
  • Any required documentation changes (code and docs) are included in this PR.
  • Any user-facing changes are mentioned in CHANGELOG.md.
  • All added/changed functionality is tested.

Summary by CodeRabbit

  • New Features

    • Enhanced error handling for configuration retrieval processes.
    • Introduced custom decoding options for unmarshalling configuration data.
  • Bug Fixes

    • Improved robustness in handling environment variable management by eliminating automatic injection and ensuring proper error reporting.
  • Refactor

    • Simplified the logic of environment variable processing and updated related methods for clarity and efficiency.

Copy link

coderabbitai bot commented Aug 21, 2024

Walkthrough

The recent changes streamline configuration handling within the codebase by removing the expandEnvViper function and enhancing error handling in the include.go and plugin.go files. This simplifies how environment variables are managed and improves the robustness of configuration retrieval processes, facilitating better integration with the viper library.

Changes

Files Change Summary
expand.go Removed the expandEnvViper function, simplifying environment variable management.
include.go Modified getConfiguration and handleInclude functions to improve error handling and logic for environment variable retrieval.
plugin.go Enhanced Plugin struct by replacing automatic environment injection with direct flag assignments and updated UnmarshalKey and Unmarshal methods to support custom decoding options.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ConfigManager
    participant Viper

    User->>ConfigManager: Request Configuration
    ConfigManager->>Viper: Retrieve Configuration
    Viper-->>ConfigManager: Return Config Data
    ConfigManager-->>User: Provide Config
Loading

Poem

🐰 In fields of code, the changes bloom,
With new paths forged, dispelling gloom.
The rabbit hops with joy so bright,
For cleaner code brings sheer delight!
Simplified the flow, the errors fade,
In this code garden, progress made! 🌼


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 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

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.

Copy link

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1d40bd9 and bd39d11.

Files ignored due to path filters (1)
  • go.work.sum is excluded by !**/*.sum
Files selected for processing (3)
  • expand.go (2 hunks)
  • include.go (2 hunks)
  • plugin.go (4 hunks)
Files skipped from review due to trivial changes (1)
  • expand.go
Additional comments not posted (6)
include.go (2)

33-37: Improved error handling in handleInclude.

The initialization of ifiles as an empty slice and the use of UnmarshalKey with error handling enhance the robustness of the function. This prevents potential nil pointer dereference issues.


61-63: Enhanced error handling in handleEnvFile.

The initialization of envFile as an empty string and the use of UnmarshalKey with error handling improve the function's reliability by ensuring envFile is always initialized.

plugin.go (4)

83-83: Simplified environment variable handling in Init.

The removal of automatic environment variable injection and the use of handleEnvFile for environment file handling simplify the logic and improve error handling.


135-137: Custom decoding options in UnmarshalKey.

The inclusion of p.unmarshalOpts() for custom decoding options enhances the flexibility and robustness of configuration handling.


144-146: Custom decoding options in Unmarshal.

The inclusion of p.unmarshalOpts() for custom decoding options enhances the flexibility and robustness of configuration handling.


151-161: Improved type conversion and environment variable parsing in unmarshalOpts.

The use of decode hooks for type conversion and environment variable parsing improves the handling of complex data types and aligns with the PR objectives.

@devnev
Copy link
Author

devnev commented Aug 21, 2024

This also affects the semantics of the Get() and Overwrite() methods of the config plugin, so it might not quite be the right approach.

@rustatian rustatian self-requested a review August 21, 2024 07:00
@rustatian rustatian added the enhancement New feature or request label Aug 21, 2024
@rustatian
Copy link
Member

Hey @devnev 👋
Sorry for the late response, I'm on vacation 😄

You may introduce new methods, but yeah, we should not introduce breaking changes.

By moving the logic for expanding environment variables into the unmarshal decoder, it is applied to any string encountered during unmarshaling (at least I think thats the case based on my reading of how viper does things).

I already thought about that, but didn't want to introduce a BC 😞

However, endure, our internal plugins' container, works nice with such cases. If you add new methods, this won't break any of the existing plugins. Moreover, if you update the Configurer interface in the server plugin, you'll be able to use these new methods, while other plugins, might use the old ones, and we may slowly replace all places where they used.

@rustatian rustatian changed the title Apply env expansion consistently by moving it into a decode hook feature: apply env expansion consistently by moving it into a decode hook Aug 21, 2024
@devnev
Copy link
Author

devnev commented Aug 28, 2024

It'll take me a while to circle back to this. Making it behave in a compatible fashion while also handling calls to Overwrite could be tricky.

@rustatian
Copy link
Member

This is the case 😃 You don't need to be compatible. You may create an OverwriteWithEnv or whatever you choose the name. Put the new logic into that new method and that's it 😃
All existing plugins will continue to work. All community plugins would work.
But, if you needed to use the new method, you would need to add the name to the Configurer interface in the particular plugin you wanted to use the new method.

@rustatian rustatian marked this pull request as draft October 26, 2024 09:31
@rustatian
Copy link
Member

Hey @devnev 👋
Some tests are failing (also in CI):
image

Do you have plans to continue working on this?

@devnev
Copy link
Author

devnev commented Nov 1, 2024

@rustatian I got really stuck on disentangling the various configs and overrides, and now I have other priorieties although I'd like to get back to this eventually. Maybe its better to close for now, to avoid any confusion, and I can re-open if and when I get back to it?

@rustatian
Copy link
Member

@devnev np at all 😃 I'm not going to close this PR, just take your time and when you would have more time feel free to return to it 😃 Thank you for your work 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants