Skip to content

Refactor param resolution#2476

Merged
carolynvs merged 6 commits intogetporter:mainfrom
carolynvs:refactor-param-resolution
Feb 2, 2023
Merged

Refactor param resolution#2476
carolynvs merged 6 commits intogetporter:mainfrom
carolynvs:refactor-param-resolution

Conversation

@carolynvs
Copy link
Copy Markdown
Member

@carolynvs carolynvs commented Dec 1, 2022

What does this change

Refactors how we resolve parameters so that it's easier to think about and maintain. Previously the logic for parsing and resolving parameters was spread out all over the place and was composed of many (unfortunately stateful) functions.

I've combined all that logic into a single function that resolves all user-defined parameters (defaults and parameter sources are still done separately since we need to be able to distinguish between the two, and the params are represented in each with different data types).

I had to cheat and use a bit of state to identify parameters that were set on the CLI or through a parameter set that really were parameters for a dependency of the bundle being executed. Dependencies v1 (the current implementation) allows the user to directly set a parameter for a dependency. The new implementation doesn't work that way (the parent bundle always manages passing data to the dependencies instead). Since eventually that code will go away and I really don't want to touch it, I went with the smallest possible fix in that area of the code.

This is refactoring for a) my happiness and that of future maintainers and b) to enable setting parameters differently for dependencies v2.

What issue does it fix

N/A, this is refactoring work to make PEP003 possible

Notes for the reviewer

N/A

Checklist

  • Did you write tests?
  • Did you write documentation?
  • Did you change porter.yaml or a storage document record? Update the corresponding schema file.
  • If this is your first pull request, please add your name to the bottom of our Contributors list. Thank you for making Porter better! 🙇‍♀️

Reviewer Checklist

  • Comment with /azp run test-porter-release if a magefile or build script was modified
  • Comment with /azp run porter-integration if it's a non-trivial PR

The sanitizer assumed that all param overrides on an installation were hard coded values because that's what the CLI allows. With recent changes to support dependencies and workflows, internal parameter sets can now resolve secrets from other sources such as env, file, etc.

Update the sanitizer to not assume they are all values that should be sanitized or edited.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
Do not re-parse parameters set on an installation resource as --param flags and instead apply any --param flag overrides on top of parameters defined on the installation. This changes the current behavior which would remove any parameter overrides set on an installation resource when the overrides were not re-specified with --param.

Example:

```
porter install mybuns --param logLevel=10

porter installation show mybuns -o yaml > mybuns.yaml

porter installation apply mybuns.yaml
```

A few other improvements are:
* We only resolve parameters once, regardless of code path.
* We persist the installation from a dry-run after comparing state now and only persist if modified.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
@carolynvs carolynvs force-pushed the refactor-param-resolution branch from c960ea4 to 113a0e5 Compare February 1, 2023 15:19
Avoid null reference exceptions by resolving and caching the bundle reference on the options, and not letting code directly access the "private" field that stored the resolved reference. Now we will resolve JIT if needed and cache the result for later use.

Ideally we would never have put state on the options in the first place but here we are. At least this will avoid accidental null references.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
I've combined applying options to an installation with calling sanitize so that it's easier to remember and never get accidentally skipped.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
@carolynvs carolynvs marked this pull request as ready for review February 1, 2023 16:52
Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
…OptionsToInstallation

Instead of setting the modified timestamp, tracking the bundle reference, etc individually in the install/upgrade/uninstall, etc actions, I've consolidated them into applyActionOptionsToInstallation, and ensure that we validate the installation after modifying it. Now we only need to validate in this function before saving the record.

The unit tests for IsInstallationInSync made a lot of simplifying assumptions that let it do less test setup that I have addressed so that it's safe to call the function under test without calling it from inside the install/upgade/uninstall/invoke actions which handle prepping the installation and options first.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
Copy link
Copy Markdown
Contributor

@VinozzZ VinozzZ left a comment

Choose a reason for hiding this comment

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

💯

@carolynvs carolynvs merged commit 69a1e27 into getporter:main Feb 2, 2023
@carolynvs carolynvs deleted the refactor-param-resolution branch February 2, 2023 17:18
bdegeeter pushed a commit to bdegeeter/porter that referenced this pull request May 11, 2023
* Update sanitizer logic to allow for non-value based parameter overrides

The sanitizer assumed that all param overrides on an installation were hard coded values because that's what the CLI allows. With recent changes to support dependencies and workflows, internal parameter sets can now resolve secrets from other sources such as env, file, etc.

Update the sanitizer to not assume they are all values that should be sanitized or edited.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>

* Improve applying installation parameters without reparsing as flags

Do not re-parse parameters set on an installation resource as --param flags and instead apply any --param flag overrides on top of parameters defined on the installation. This changes the current behavior which would remove any parameter overrides set on an installation resource when the overrides were not re-specified with --param.

Example:

```
porter install mybuns --param logLevel=10

porter installation show mybuns -o yaml > mybuns.yaml

porter installation apply mybuns.yaml
```

A few other improvements are:
* We only resolve parameters once, regardless of code path.
* We persist the installation from a dry-run after comparing state now and only persist if modified.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>

* Add guard rails to resolving the bundle reference

Avoid null reference exceptions by resolving and caching the bundle reference on the options, and not letting code directly access the "private" field that stored the resolved reference. Now we will resolve JIT if needed and cache the result for later use.

Ideally we would never have put state on the options in the first place but here we are. At least this will avoid accidental null references.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>

* Always sanitize the installation file

I've combined applying options to an installation with calling sanitize so that it's easier to remember and never get accidentally skipped.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>

* Do not modify the param varible in the sanitizer for loop

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>

* Consolidate modifying the installation based on opts into applyActionOptionsToInstallation

Instead of setting the modified timestamp, tracking the bundle reference, etc individually in the install/upgrade/uninstall, etc actions, I've consolidated them into applyActionOptionsToInstallation, and ensure that we validate the installation after modifying it. Now we only need to validate in this function before saving the record.

The unit tests for IsInstallationInSync made a lot of simplifying assumptions that let it do less test setup that I have addressed so that it's safe to call the function under test without calling it from inside the install/upgade/uninstall/invoke actions which handle prepping the installation and options first.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>

---------

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pep003-advanced-dependencies Implementation of the Advanced Dependencies proposal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants