-
Notifications
You must be signed in to change notification settings - Fork 5.6k
[master] Fix 64433: Add dynamic loading of file_roots, pillar_roots, and thorium_roots #64434
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
Open
bluesliverx
wants to merge
11
commits into
saltstack:master
Choose a base branch
from
bluesliverx:dynamic-roots
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4a375fd
Add dynamic loading of file_roots, pillar_roots, and thorium_roots
1e1b5ca
Add integration tests for dynamic configuration and improve docs
566ea12
Fix merge conflicts
7bb20ef
Fix formatting
677d684
Cleanup salt minion key after test
38f813a
Update test to copy config first
5d0e3dc
Fix testing for yaml dumper
9f88817
Update version to specify v3008 for dynamic file roots
942e308
Address review feedback on DynamicDict: fix items()/deepcopy, add TTL…
fbfa837
Fix import order in yamldumper.py (isort)
d65831f
Merge remote-tracking branch 'upstream/master' into dynamic-roots
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added dynamic loading of file_roots, pillar_roots, and thorium_roots to salt config |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| """ | ||
| A dictionary with optionally dynamic values, used for dynamic configuration such | ||
| as file roots. | ||
| """ | ||
|
|
||
| import copy | ||
| import time | ||
|
|
||
| __all__ = ["DynamicDict"] | ||
|
|
||
| #: Default number of seconds a dynamic value is cached before being | ||
| #: re-evaluated. Callers (e.g. ``salt.config``) may override this per | ||
| #: instance via the ``ttl`` argument. | ||
| DEFAULT_TTL = 5.0 | ||
|
|
||
|
|
||
| class DynamicDict(dict): | ||
| """ | ||
| A dictionary that can mix static and dynamic values. | ||
| """ | ||
|
|
||
| def __init__(self, *args, ttl=DEFAULT_TTL, **argv): | ||
| self._func_dict = {} | ||
| self._cache = {} | ||
| self._ttl = ttl | ||
| super().__init__(*args, **argv) | ||
|
|
||
| def __getitem__(self, key): | ||
| val = super().__getitem__(key) | ||
| if key in self._func_dict: | ||
| now = time.time() | ||
| cached = self._cache.get(key) | ||
| if self._ttl and cached is not None and (now - cached[1]) < self._ttl: | ||
| return cached[0] | ||
| val = self._func_dict[key](val, dyn_dict=self, key=key) | ||
| self._cache[key] = (val, now) | ||
| return val | ||
|
bluesliverx marked this conversation as resolved.
|
||
|
|
||
| def __delitem__(self, key): | ||
| if key in self._func_dict: | ||
| del self._func_dict[key] | ||
| self._cache.pop(key, None) | ||
| super().__delitem__(key) | ||
|
|
||
| def get(self, key, default=None): | ||
| if key not in self: | ||
| return default | ||
| return self[key] | ||
|
|
||
| def pop(self, key, default=None): | ||
| if key in self: | ||
| val = self[key] | ||
| del self[key] | ||
| else: | ||
| val = default | ||
| return val | ||
|
|
||
| def values(self): | ||
| keys = super().keys() | ||
| for key in keys: | ||
| yield self[key] | ||
|
bluesliverx marked this conversation as resolved.
|
||
|
|
||
| def items(self): | ||
| keys = super().keys() | ||
| for key in keys: | ||
| yield key, self[key] | ||
|
|
||
| def copy(self): | ||
| new_dd = DynamicDict(ttl=self._ttl) | ||
| for key, val in super().items(): | ||
| if key in self._func_dict: | ||
| func = self._func_dict[key] | ||
| data = super().__getitem__(key) | ||
| new_dd.add_dyn(key, func, data) | ||
| else: | ||
| new_dd[key] = val | ||
| return new_dd | ||
|
|
||
| def __copy__(self): | ||
| return self.copy() | ||
|
|
||
| def __deepcopy__(self, memo): | ||
| rdd = DynamicDict(ttl=self._ttl) | ||
| memo[id(self)] = rdd | ||
| for key in super().keys(): | ||
| if key in self._func_dict: | ||
| func = self._func_dict[key] | ||
| data = copy.deepcopy(super().__getitem__(key), memo) | ||
| rdd.add_dyn(key, func, data) | ||
| else: | ||
| copied_key = copy.deepcopy(key, memo) | ||
| copied_value = copy.deepcopy(super().__getitem__(key), memo) | ||
| rdd[copied_key] = copied_value | ||
| return rdd | ||
|
|
||
| def static_dict(self): | ||
| new_dict = {} | ||
| for key in super().keys(): | ||
| new_dict[key] = self[key] | ||
| return new_dict | ||
|
|
||
| def is_dyn_key(self, key): | ||
| return key in self._func_dict | ||
|
|
||
| def add_dyn(self, key, func, data=None): | ||
| if not hasattr(func, "__call__"): | ||
| raise ValueError(f"Value for key '{key}' is not a function") | ||
| self._func_dict[key] = func | ||
| self._cache.pop(key, None) | ||
| if data is not None or key not in self: | ||
| self[key] = data | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.