Repository navigation
feat: Allow traefik merged dynamic configuration override with <project>.yaml - #6375
nickchomey wants to merge 31 commits into
Conversation
|
Download the artifacts for this pull request:
See Testing a PR. |
|
Looks like the next hurdle is just TestCmdRestartJSON, https://buildkite.com/ddev/ddev-macos-arm64-mutagen/builds/8124#01907be5-07b3-4384-a0e9-aa1b1b01f567/15650-15654
Looks like a secondary failure, first the .traefik-dynamic-config-extensions-template.yaml doesn't exist, second the output of that causes the TestCmdRestartJSON to fail. You can run tests manually really easily in vscode, just click the little arrowhead. Since this is a TestCmd test, make sure you have the correct built version of ddev in path. |
|
I don't understand why you didn't fill in the PR template here. Please update it to explain. (But thanks for the manual testing instructions!) Please edit the OP with the required information:
Explain there why this PR is necessary, `## How this PR solves the problem.
|
rfay
left a comment
There was a problem hiding this comment.
Just beginning to look at this, but your comment about the creation of traefik-dynamic-config-extensions-template.yaml.example says:
I don't believe that existing projects will receive the /project/traefik/.traefik-dynamic-config-extensions-template.yaml.example file, as it is created with ddev config so that the file would be available prior to the first ddev start. Should that stay there, but also add some conditional logic to traefik.go to check for its existence, creating it if it doesn't exist?
This creation is being done in the wrong place. It should be processed configureTraefikForApp() in traefik.go, along with the other project-level traefik configuration.
Also, you're giving it an inappropriate name. .traefik_dynamic_config_extensions_template.yaml.example is a hidden file. We don't want an example to be hidden. I imagine it shouldn't have the name template in it either?
|
Please improve the testing instructions above. I took a look at the example file and it has all kinds of go templating in it. Do we expect users to be able to do that? Or was the example file supposed to have been processed? |
|
Your comments say
Shouldn't |
rfay
left a comment
There was a problem hiding this comment.
I'll take another look when I can understand the PR testing instructions and when Dockerfile stuff is removed and the example makes sense (I assume without go templating requirements?)
I believe when we created a PR to your PR together via zoom, I asked what to do about the template and you said just delete it. I'll create a new PR and copy the template from it to here and fill it out |
As I said in the comment that you quoted, I very much do think that this file should be created in config.go, so that it is there prior to any Yet, I suppose very few other files are created prior to the first So, sure I suppose I could just move the creation of this file from
I'm happy to rename it to whatever you like.
Do you have any specific qualms and suggestions?
It contains a template because it needs to be able to populate some app-specific things. Or at least that is the case for my redirect url stuff, which I showed you via zoom and we discussed I could probably reconfigure it such that the App.Name stuff is prepended in Go, while maintaining the possibility for people to define templates in it. Yet, the status-quo to implement any middleware is to literally edit Also, we've discussed a few times that this is something that very few people will or should ever touch - it would be better interacted with/populated by an addon or other core code. And if anyone does have the courage or competence to be messing with plugins, then I don't see it being particularly onerous for them to make sure to add App.Name where appropriate (especially when there's already an example and documentation). I'm open to any specific suggestions though. |
As far as I can tell, you simply have to wait for traefik to start up and try to load the middleware in order to find out if it was defined with proper yaml and plugin structure, no issues with url or other parameters, etc Though, correct me if I'm wrong, but I don't think there's anything like that right now to validate the core traefik yaml - if you use wrong spacing, indents, hyphen etc everything will start up but the router will fail in some way. The issue I had with v2 vs v3 syntax is a good example. I see no problem with this though - it fails, you can inspect why and then solve it. But, again, very few people should ever be touching any of this - and if they do, its at their own risk, as is documented throughout ddev But if you know how to validate traefik yaml before loading traefik, I'd be happy to try to implement something - for both this PR and for traefik yaml more generally (both the static and dynamic config). But I dont see it as being feasible (or at least practical), or even necessary. |
|
You don't have to create a new PR. Just edit the OP. Pull down the little editor in the upper right of the OP :) |
Yes, what I meant is I'll just start a new PR to get the template and copy it to here. I wouldn't actually submit (or even fill out) the new pr |
e344e44 to
35193d8
Compare
832b2ae to
e7d6da4
Compare
This reverts commit 35359b3.
3d189cf to
b1bd39b
Compare
rfay
left a comment
There was a problem hiding this comment.
Lots of comments, most super-easy. The part that isn't quite as easy is simplifying the flow of ConfigureTraefikForApp(). It does what it needs to do now, but it works a little too hard at it, resulting in too many lines of code and a harder flow. If I'm not mistaken, we can do the templating just once, and we can remove a number of if/else cases. I'll be happy to help with this if it's not obvious enough from the comments. I know you've already worked hard on this, you're almost to the finish line!
| } | ||
|
|
||
| err = t.Execute(f, templateData) | ||
| err = t.Execute(dynamicConfigTemp, templateData) |
There was a problem hiding this comment.
We don't need to do this whole templating stage do we, since we template the results of the merge?
There was a problem hiding this comment.
No, it is necessary because, as I'm sure I explained before, we can't unmarshal traefik_config_template.yaml to merge it because it is full of templates (e.g. to define router names). So, no, it needs to be as it is - parse the template, merge it with the extra files, parse template again, write to the final file.
The only question, i suppose, is whether we parse it into a temp file or a map to avoid a temp file. Yet, as explained various times, if we are to use MergeYamlFiles, we need to generate a file. Moreover, the base config comes from traefik_config_template.yaml, which is in bundledAssets via go:embed, and it cannot be passed to that function. So we'd need an entirely different mergeyamlmapbundledassets function. Not at all worth it. It is more than fine as it is
On a related note, are the 7 nanoseconds of processing that we might have possibly been able to save here worth the many hours (or even days) of time we've spent on this? Let alone the social friction getting augered in on these minute details has caused...
There was a problem hiding this comment.
You're already creating a temp file with the contents if I'm not mistaken.
There was a problem hiding this comment.
If it's not valid yaml with the templates in it, then you're right. But how is that going to work below, where you have the same problem? All of the files would have to be preprocessed in that case.
There was a problem hiding this comment.
The temp file gets its contents VIA the parsing of the template. If I just read the contents of the template into the temp file without parsing the template, then merging breaks because it cant unmarshal all of the templates in the string. (I actually have tried precisely this and it does, in fact, break).
As for the other files, yes, I initially preprocessed all of the templates precisely for this reason, prior to merging them all. But you insisted that i not do that anymore. So, it must be done like this and comes with the restriction (which i've moved from the docs to the readme) that you can't put a template at the start of a YAML key - it must be preceded by a character, or used in a value.
I could solve this problem by changing traefik_config_template.yaml to not use templates at the start of the lines
e.g. {{ .App.Name }}-web-80-http: is invalid, but web-80-http-{{ .App.Name }}: is valid.
Except the template is actually like this
{{ $appname }}-{{ $s.Service.InternalServiceName }}-{{ $s.Service.InternalServicePort }}-http:
So it would need to be
http-{{ $s.Service.InternalServiceName }}-{{ $s.Service.InternalServicePort }}-{{ $appname }}:
I actually would be quite happy to do that, precisely because it would make merging my rewriteurls middleware stuff considerably easier.
Yet, I suspect all the go template logic (if, range etc...) would break it still (or at least disappear). So, no point in trying
Perhaps we can just leave it all as is, and move on to far more important things?
"Premature optimization is the root of all evil"
|
I have found a pretty meaningful bug/gotcha in this. If you have two files that set middlewares, the second one overwrites the router assignments of the first e.g. you get this test4-web-80-http:
middlewares:
- test4-relative-location-header
- test4-relative-body-urlsinstead of: test4-web-80-http:
middlewares:
- test4-my-fail2ban
- test4-redirectHttps
- test4-relative-location-header
- test4-relative-body-urlsI just tried running this without test4-web-80-http:
middlewares:
- test4-my-fail2ban
- test4-redirectHttpsThe only reasonable solution I see, therefore, is to add documentation in the readme, example extra config file header, and website docs to the effect of "all middlewares must be defined in It'll make it a bit trickier for my wordpress addon to add/merge what it needs into that single middlewares config (as opposed to just copying a config into the directory). But I dont see a reasonable way around this. So, I'll add the docs soon. I'm also working through some ideas to improve things in other ways that I think will make everyone happier. I'll report back on that when ready |
|
Could you please provide the example file that demonstrates this problem with middlewares assignments? Thanks! (I think you're saying that a second router of the same name overrides the middlewares assignments from the first one if they exist, and I'd expect that to be normal behavior) |
… configs. Considerable refactoring of functions
My wsl2 is in the process of crashing for reasons unknown - i'll sort it out. Anyway, i cant generate any files for you but it shouldnt be needed. Just use the default The definitions will merge just fine, but the assignments from the 2nd one will override the 1st. |
Isn't that the intended behavior? |
|
I just pushed a big commit that does the following:
I timed how long the dynamic config merging and generations takes before and after this and both are in the realm of 1-2 milliseconds - so any qualms about extra processing are completely irrelevant, especially when the rest of I'm sure you'll have suggestions for improvement - function and parameter names, where to put them, etc... But I REALLY hope you'll accept it. If not, I dont see any possibility for my continued involvement in this PR or ddev contributions altogether. |
In some sense, perhaps. But lets say one config file defines and assigns fail2ban and another separate file defines and assigns redirectHttps (or anything else. The definitions will both be merged as they are different names/text. BUt the assignments will not merge - only the last-to-be-touched middleware file will actually be assigned. So, as described, you have to define all middlewares in one Again, something liek this is only possible if you define and assign them all from one file. test4-web-80-http:
middlewares:
- test4-my-fail2ban
- test4-redirectHttps
- test4-relative-location-header
- test4-relative-body-urlsIf you separate any of those 4 middlewares into different files, they overwrite each other. I forgot to update the docs/comments due to my refactoring work, but i'll do that when wsl2 comes back to life |
|
I think what you're describing is the intended override behavior. In config.yaml we have two modes. The first (default) is merging, so |
That's fine. I'll just add some details so that people understand it better. If even I was mistaken about this, then others definitely would be. |
|
Looks like all tests are passing just fine with the big refactoring (except wsl2 and colima - they've failed a couple times without actually giving any useful information). I see no reason to believe that this is due to the code - must be the build machines. It is working exceptionally well with the other stuff I'm working on (e.g. wordpress addon is seamlessly rewriting absolute urls to relative paths) - very helpful to have full go template capabilities available in the extra config files. Please let me know what changes you'd like made, and I'll make them along with push some other minor edits that I've committed locally, and rebase from master. |
|
I have decided not to accept this PR, mostly because of the explicit threat. I wish you well on your journey, and am sorry that this was such a difficult process. |
The Issue
Currently the only way to define any Traefik Middleware (or modify anything at all in the Traefik configs) is by removing
#ddev-generatedfrom the/pkg/ddevapp/traefik_config_template.yamland~/.ddev/traefik/static_configwhich, as described in #6357, invites all sorts of other issues. The solution is to make all#ddev-generatedfiles extensible.This was recently implemented for the static config via #6317. This PR is for the dynamic config
How This PR Solves The Issue
This PR adds a generalized merge mechanism to allow extra
/project/.ddev/traefik/dynamic_config.*.yamlfiles to be merged into the<project>.yamlfile, with the last alphanumeric file "winning". This can be used for adding middlewares as well as overriding any field in the dynamic config.The
dynamic_config.*.yamlfiles will also be able to contain {{go templates}} so that middlewares can dynamically use app variables to do things like rewrite app URLs in the response body (useful for Wordpress and Magento), or automatically "namespace" the middleware per active DDEV project.Most users should never need to touch this - it would be best implemented via addons or other core code.
Manual Testing Instructions
ddev config/project/traefik/dynamic_config.middleware.yaml.exampleto remove.exampleextension, which will use the fail2ban pluginddev start{{ .App.Name }}-prefixestls:Automated Testing Overview
A test has been added that injects a middleware definition via an extra config file.
Release/Deployment Notes
This should not affect anything else. It adds opt-in functionality, which would be best implemented via addons or other built-in mechanisms.