Top SLS compilation does not behave the same as Docs describe
Hi
(This is taken from my comments on closed issue #5440 - @basepi recommended a new issue so here it is! Thanks.)
I have 4 environments, "base", "qa", "dev", "master", in that order defined in file_roots. These are branches in a git and I have my own deployment of this to the salt server (I do not use gitfs).
Each has a version of the top.sls file containing all environments (as you'd expect since it same file, just different revisions different branches)
The documentation describes that if an environment has info in top.sls in "base", it will always override all other top.sls. This is not the case - and checking the code there is no distinction between "base" and any other name, and no specific decision on ordering (and it seems alphabetical) - so in my case, even though in the top.sls and file_roots, "qa" is second down, because it is last alphabetically, that overrides all other top.sls files in all other environments.
Based on the docs, it seems to give the idea that "base" is a special name and is always authoritative. It also explicitly says that if an environment is defined in multiple top.sls files - the definition in the top.sls for that environment will be preferred. But the code seems to say otherwise.
The code seems to simply merge and overwrite, in alphabetical order. Which contradicts entirely the documentation.
For reference, from http://docs.saltstack.com/en/latest/ref/states/top.html:
The following is what lead me to think "base" was a special name (it doesn't say the FIRST environment wins, it says the "base" environment wins.)
21.24.15.3. HOW TOP FILES ARE COMPILED ... 1 . The base environment's top file is processed first. Any environment which is defined in the base top.sls as well as another environment's top file, will use the instance of the environment configured in base and ignore all other instances. In other words, the base top file is authoritative when defining environments.
The following is the bit saying a top.sls in an environment is authoritative for that environment if it contains config for it:
3 . For environments other than base, the top file in a given environment will be checked for a section matching the environment's name. If one is found, then it is used. Otherwise, the remaining (non-base) environments will be checked in alphabetical order. In the below example, the qa section in /srv/salt/dev/top.sls will be ignored, but if /srv/salt/qa/top.sls were cleared or removed, then the states configured for the qa environment in /srv/salt/dev/top.sls will be applied.
Although if I'm honest, point 3 does seem to contradict point 2 (below) - I would've expected point 2 to at least make an exception for point 3 - or point 3 come first
2 . If, for some reason, the base environment is not configured in the base environment's top file, then the other environments will be checked in alphabetical order. The first top file found to contain a section for the base environment wins, and the other top files' base sections are ignored. So, provided there is no base section in the base top file, with the below two top files the dev environment would win out, and the common.centos SLS would not be applied to CentOS hosts.
@basepi:
@driskell Could I convince you to open a new issue and insert all of this awesome information and detective work into that new issue? I think what we will want to do is bring the code in line with the documentation (I still haven't tested this myself, so I haven't verified that I see the same thing you do) if possible. I think the documentation's way is a good way, but we definitely need to get them in line with each other one way or another.
I agree the documentation is (mostly) a good way, and the behaviour should match. Assuming I haven't read the doc wrong though, and "base" is meant to be special and authoritative and overriding, then I think that should be configurable so I can call it something else, so maybe a "main_env: base" option with that as default.
Thanks guys!
Thanks for filing this!
Proposed a fix if it helps!
Awesome! I'll leave review of that to @thatch45. =)
Flagged as 'Fixed Pending Verification' while we wait for that review.
OK so the fix is not viable as I expected - though I think it was a good POC of what the docs say.
Has there been any discussions on what should happen? Or what recommended approach for me to take?
I think I can rename my environments aDev bQA cProd and things will work better, but that just seems a hack. Or I would need to take my top.sls out of Git and hold them separately, so that each one only contains directives for servers in its environments (thus merging doesn't overwrite anything). But I still worry about someone with access to QA accidentally using a production server name instead of QA and it overriding - so I'd have to use the aDev bQA cProd still.
Maybe I'm approaching this completely wrong though.
I think what it comes down to is that we've never really had testing in place for this kind of workflow. In general we just recommend that people don't have conflicts across environments for their top.sls files, because it tends to be implicit, rather than explicit, which in my opinion isn't good (and is harder to maintain).
So we just need to deep dive this and either change the documentation or get the actual compilation process in line with the documentation.
It seems that #9616 is a variation of this issue?
Please let me know if you start discussing this. I am more than willing to accept something where you have an option to enable the top compilation described in the docs.
I think it is important that not only states can be version controlled, but top.sls also. This workflow I used is exactly how I would proceed when using GitFS backend. But I do require top.sls to be in each branch and version controlled, so changes can be promoted along with state changes. Otherwise I end up merging in state changes from QA to Production but have to prevent top.sls merging too somehow... not even sure how you do this without a dirty hack :-(
Jason
Also - I'm willing to help implement the proposed solution when one is decided.
No worries, we'll keep you posted. Just need to find a good time to have a discussion about this.
I would strongly suggest that a note is added to the docs right now that they are incorrect. We just set up a new system that strongly relied on the behaviour described in the documentation, and now have to find out that is not implemented that way.
You're right, we need to update the docs, especially since it's taken so long for us to have time to investigate this.
ditto; been scratching my head as to why my env ordering isn't working. thanks for updating the docs - but finding that the docs say one thing and saltstack does something else is a sad trend i keep running into :(
cheers
@tehmaspc Thanks for the feedback. We've recently hired some services people and should have more core engineering time in the coming months to fix issues like this and get the general bug count under control.
@basepi - awesome; for the record Helium fixed up a bunch of things for us - so it's not all bad :)
cheers,
For anyone arriving here needing a limited alternative, I name the per-env top.sls "_top.sls" in the repository. And I made "top.sls" a symbolic link to "../../base/states/_top.sls" (adjust as necessary) This makes the top.sls in every env point to the _top.sls in the base environment, always. And you keep the ability to merge top changes between environments. Of course, it means base always wins so not exactly a workaround - just a limited alternative.
It'll be good to get something in place to fix it up completely though! I'm watching in the sidelines if you need any help.
Thanks @driskell - I'll give that a shot; I think I'm having the issue as xcorvis on the other issue linked here: https://github.com/saltstack/salt/issues/5440 ; but I need to peruse that better. I did notice yesterday after trying to debug on one of my minion that it indeed was pulling env info from an env I didn't expect and everything I'm trying to do formula wise is via the git backend. Hopefully I can find a solution that works because I'm trying to create a base, dev, stg, prd environment setup like yourself and I have been trying to put a separate top.sls file in each of these env dirs.
@tehmaspc The current functionality just browses the environment top.sls in order, overwriting envs as it goes along. So it will probably pull the top.sls data from "stg" as it's alphabetically last. For me it was pulling qa.
@tehmaspc I can see that other ticket was 2013 - I think when I last looked at this it used to be random (unordered dictionary) so probably based on some hash of some sort, but at some point the code was changed to be an ordered dictionary. So if you tried to reproduce #5440 now it might be it's reverse alphabetical order.
@driskell
so one thing i'm pretty sure is broken and I just verified in my setup is that gitfs backends are not being searched properly in environments OTHER than 'base'. In my non-base environment I get the following error when trying to verify the state of my minion:
root@REDACTED:/srv/salt/base# salt 'utopia-REDACTED' state.show_highstate utopia-REDACTED: - No matching sls found for 'mongodb' in env 'utopia'
My 'utopia' env top.sls file:
root@REDACTED:/srv/salt/base# cat ../utopia/top.sls utopia: 'utopia-REDACTED-*': - mongodb
BUT, if I have this same mongodb in base I am able to get the highstate info. I can also clone the mongodb formula into my utopia env dir completely and get the highstate info as well. Something is up w/ gitfs backends in non-base envs.
This is a different problem than this original issue but because we are all doing gitfs backends it becomes part of the problem w/ gitfs backends and multiple envs. I'll be doing more testing w/ envs today to see what also doesn't work in my desired setup. FYI - I didn't mention above but my full list of envs is base, dev, stg, prd, utopia.
UPDATE:
some more info - I believe the following is a good sign - the minions I want in my utopia env are listing their states from the env properly. I can also see this reflected on the minion's cache dir as well - which is expected from the show_top execution below:
root@REDACTED:/srv/salt# salt '*' state.show_top utopia-REDACTED: ---------- base: - core utopia: - mongodb utopia-REDACTED: ---------- base: - core utopia: - mongodb utopia-REDACTED: ---------- base: - core utopia: - mongodb dns1-REDACTED: ---------- base: - core dns2-REDACTED: ---------- base: - core
The following is my file_roots config and the reason it has base in each environment is because I went with this setup based on the info here - http://docs.saltstack.com/en/latest/ref/file_server/file_roots.html#directory-overlay:
file_roots: base: - /srv/salt/base dev: - /srv/salt/dev - /srv/salt/base stg: - /srv/salt/stg - /srv/salt/base prd: - /srv/salt/prd - /srv/salt/base utopia: - /srv/salt/utopia - /srv/salt/base
UPDATE2: Well I just realized that if I'm trying to apply a formula to a minion in an environment other than base than that formula's git repo needs a branch other than master. For my case my mongodb salt formula repo needed a 'utopia' branch and after adding that remote branch to the git repo I can properly do show_top. I totally overlooked this since I assumed even in a non-base environment the master branch of any git repo would be used. Thus, it appears for the time being my use case is working for me unless I'm missing something else.
Thanks, Tehmasp
Count me among those who found this too late after painfully discovering that these don't work they way I'd expect.
I think a simpler scheme would be that an environments definition must be in that environments top file, and all other environments in that top file are ignored. So in the base top.sls, the contents of the base environment is read, and everything else is ignored. In the dev environment's top.sls the dev environment is read and everything else is ignored.
This would make the merging of top files much simpler and intuitive, while still allowing them to be version controlled and merged with a normal git workflow.