Skip to content

sorted keys for non core. grains - #54853

Closed
jtraub91 wants to merge 3 commits into
saltstack:masterfrom
jtraub91:54694_core_napalm_grains_loaded_after_custom_grains
Closed

jtraub91 wants to merge 3 commits into
saltstack:masterfrom
jtraub91:54694_core_napalm_grains_loaded_after_custom_grains

Conversation

@jtraub91

@jtraub91 jtraub91 commented Oct 2, 2019

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes problem due to grains module evaluation. Specifically for proxyenabled grains modules, the salt-core included non-core grains were being evaluated after custom grains. So for example, the grains included in grains/napalm.py were being evaluated after my custom grains included in /srv/salt/_grains. This was a problem since I wished to overwrite grains according to the precedence define in the docs and it was not behaving the way it should.

The way I solved this (while not perfect) was to sort the non-core (not core.xxx) grains so that if you wish to overwrite a non-core salt-core included grain's key, simply use a __virtualname__ that has a later key alphabetically. E.g. my custom grain module 'napalm_mod' will overwrite similar keys in 'napalm' grains module

What issues does this PR fix or reference?

#54694

Previous Behavior

Non-core salt-core include grains module would be evaluated last, thus taking precedence over custom grains

New Behavior

Custom grain will take precedence if virtualname has a later key alphabetically

Tests written?

No

Commits signed with GPG?

No

@jtraub91
jtraub91 requested a review from a team as a code owner October 2, 2019 18:39
@ghost
ghost requested a review from xeacott October 2, 2019 18:39
@xeacott

xeacott commented Oct 2, 2019

Copy link
Copy Markdown
Contributor

I do not know the implications of this change, but there are some lint issues you'll need to fix first. Then I'll have someone else on the core team review this.

@jtraub91

jtraub91 commented Oct 2, 2019

Copy link
Copy Markdown
Contributor Author

Thanks @xeacott ! I fixed the lint and build errors. Looking forward to feedback.

@xeacott
xeacott requested a review from dwoz October 3, 2019 15:44
@xeacott

xeacott commented Oct 4, 2019

Copy link
Copy Markdown
Contributor

Before we can get this merged, you'll need to change the target branch and add a test. This means re-basing and targeting master branch. I believe there should be some unit tests for example purposes in tests\unit\test_loader.py

@cmcmarrow cmcmarrow added the needs-testcase PR needs test cases written, or the issue is about a bug/feature that needs test cases label Oct 4, 2019
@cmcmarrow

Copy link
Copy Markdown
Contributor

This should go into 2019.2.1.

@cmcmarrow cmcmarrow left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the code, but salt now want tests with every code change now.

Comment thread salt/loader.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

for key in sorted(funcs.keys()): sorted_keys is never used again. Also it does not change the readability of the code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Absolutely, was planning to do this. Less lines is better 🐍

@jtraub91
jtraub91 changed the base branch from develop to 2019.2.1 October 6, 2019 20:53
@jtraub91
jtraub91 changed the base branch from 2019.2.1 to master October 6, 2019 20:58
@jtraub91

jtraub91 commented Oct 6, 2019

Copy link
Copy Markdown
Contributor Author

Rebased on master. Will add test asap

@codecov

codecov Bot commented Oct 7, 2019

Copy link
Copy Markdown

Codecov Report

Merging #54853 into master will decrease coverage by 1.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #54853      +/-   ##
==========================================
- Coverage   11.67%   10.64%   -1.03%     
==========================================
  Files        1520     1519       -1     
  Lines      253244   252797     -447     
  Branches    54003    53891     -112     
==========================================
- Hits        29561    26908    -2653     
- Misses     221590   224212    +2622     
+ Partials     2093     1677     -416
Flag Coverage Δ
#archlts 10.64% <100%> (-0.3%) ⬇️
#centos7 ?
#py2 ?
#py3 10.64% <100%> (-0.72%) ⬇️
#ubuntu1604 ?
#zeromq 10.64% <100%> (-1.03%) ⬇️
Impacted Files Coverage Δ
salt/loader.py 53.4% <100%> (-6.45%) ⬇️
salt/config/schemas/esxdatacenter.py 0% <0%> (-100%) ⬇️
salt/ext/vsan/vsanmgmtObjects.py 0% <0%> (-100%) ⬇️
salt/proxy/dummy.py 0% <0%> (-80.87%) ⬇️
salt/proxy/docker.py 0% <0%> (-62.5%) ⬇️
salt/proxy/chronos.py 0% <0%> (-50%) ⬇️
salt/log/handlers/log4mongo_mod.py 0% <0%> (-50%) ⬇️
salt/proxy/marathon.py 0% <0%> (-50%) ⬇️
salt/proxy/cisconso.py 0% <0%> (-46.43%) ⬇️
salt/proxy/arista_pyeapi.py 0% <0%> (-45.66%) ⬇️
... and 174 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 532b2c3...6d7d17e. Read the comment docs.

@jtraub91

jtraub91 commented Oct 7, 2019

Copy link
Copy Markdown
Contributor Author

May I propose that this commit doesn't, in fact, need a test? The commit simply sorts the keys to execute grains modules in alphabetical order. What type of test is warranted here?

@waynew

waynew commented Oct 7, 2019

Copy link
Copy Markdown
Contributor

@jtraub91 you kind of stated it yourself:

So for example, the grains included in grains/napalm.py were being evaluated after my custom grains included in /srv/salt/_grains. This was a problem since I wished to overwrite grains according to the precedence define in the docs and it was not behaving the way it should.

I would assert that there should be tests written to ensure that grains are evaluated in the correct order. TBH it's really weird that there aren't (apparently) any tests covering this situation.

But if there's a faulty behavior, we shouldn't add code to salt that fixes the faulty behavior unless we can write a test that fail when the faulty behavior exists. Otherwise someone may come in and change the code later, re-introducing this error.

@jtraub91

jtraub91 commented Oct 9, 2019 •

Copy link
Copy Markdown
Contributor Author

@waynew understood.

I added better testing for this situation. The real issue is that there are 'core' grains that aren't in the 'core' grains module. If you look at loader.py core grains will always run first however 'non-core' grains that are included in the repo can only be overwritten with an alphabetically-superseding module name (with this change).

Perhaps the better way to handle this is to change all the non-core grains included in the repo (e.g. napalm, zfs, etc.) to have __virtualname__ = 'core'. Then they would always get run first no matter custom module name. <- that sounds like a good way to go now that I say it, although there may be other complications introduced. I'm going to take a break for today, but interested to hear your thought if any, as I mull this over and test it soon.

@jtraub91

jtraub91 commented Oct 9, 2019

Copy link
Copy Markdown
Contributor Author

Another issue with grains precedence, /etc/salt/minion grains take precedence over custom _grains
Appeared to be this way via testing but also quite evident by examining loader.py (https://github.com/saltstack/salt/blob/master/salt/loader.py#L846)

We will want to either make a change to the code, or the docs (https://docs.saltstack.com/en/latest/topics/grains/#precedence). This probably belongs in a separate issue / PR.

@waynew

waynew commented Oct 14, 2019

Copy link
Copy Markdown
Contributor

I'll bring this up with the core team - it seems like we will probably want to take a step back and figure out a long-term strategy for these.

@jtraub91

Copy link
Copy Markdown
Contributor Author

I've spoke with some of my colleagues and the path forward that makes most sense to us, is the following:

Since there are grains included in this repo which aren't under the modules scope 'core.' (e.g. napalm, zfs, etc) and changing these to be __virtualname__ = 'core' could be a breaking change, the grains evaluation should be as follows:

  1. Evaluate grains modules contained in salt/grains
    a. First execute core. grains
    b. Then execute other grains modules in alphabetical order
  2. Evaluate custom _grains in alphabetical order

@jtraub91

Copy link
Copy Markdown
Contributor Author

I'm willing to investigate further and make this change (and add a test), but have been unsuccessful so far. The logic to execute the modules within salt.loader.grains is not sufficient, and perhaps this could be fixed there, but maybe it is better to be fixed before this point, perhaps in the ordering of modules via the _module_dirs function in loader.py ?

@waynew

waynew commented Nov 8, 2019

Copy link
Copy Markdown
Contributor

@jtraub91 Sorry for the delay - we finally had a chance to discuss things, and from what I can tell what you're heading after seems like the right direction.

I'd say it's "sort of" a breaking change - those grains are salt core grains even though they don't live in the .py file. But it could break some things for people who are relying on the incorrect behavior. Because of that we'll make sure to call special attention to it when we release the fixes - hopefully that's enough for people to be aware that we're fixing broken behavior they could have been relying on.

We'd love to see a solid set of unit tests that verify that grains are evaluated in the order the documentation says they are. If you're not able to, that's understandable. But it would be awesome and appreciated!

If not, at least some minimal test to verify that your code does what it says it will will be 👍 .

@jtraub91

jtraub91 commented Nov 8, 2019

Copy link
Copy Markdown
Contributor Author

But if those grains are 'core' grains then there should be no worries aliasing them under the __virtualname__ = 'core' namespace. Any reliance would be purely an error in overwriting an existing grain of the same name.

Accidental reliance aside, from what you are saying, it sounds like, to me, all 'core' grains that aren't namespaced under 'core' should be. After all that is the criteria that salt.loader.grains is using when determining if it is in fact a core grain: https://github.com/saltstack/salt/blob/master/salt/loader.py#L783

@waynew

waynew commented Dec 12, 2019

Copy link
Copy Markdown
Contributor

Accidental reliance aside, from what you are saying, it sounds like, to me, all 'core' grains that aren't namespaced under 'core' should be.

That's the way I understand it, yes!

@waynew waynew removed the needs-testcase PR needs test cases written, or the issue is about a bug/feature that needs test cases label Mar 19, 2020
@xeacott

xeacott commented May 27, 2020

Copy link
Copy Markdown
Contributor

@jtraub91 Would you mind also resolving the merge conflict?

@sagetherage
sagetherage removed the request for review from xeacott December 3, 2020 17:40
@dwoz

dwoz commented Dec 10, 2023

Copy link
Copy Markdown
Contributor

Closing this due to inactivity. Anyone should feel free to re-open it if they want to see it through to the end in one release cycle.

@dwoz dwoz closed this Dec 10, 2023
@dwoz dwoz added help-wanted Community help is needed to resolve this Abandoned labels Dec 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Abandoned help-wanted Community help is needed to resolve this

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants