nginx-proxy / nginx-proxy

Automated nginx proxy for Docker containers using docker-gen
MIT License
18.13k stars 3k forks source link

refactor: template internal data structure #2405

Closed buchdag closed 2 months ago

buchdag commented 3 months ago

This PR refactors the template internals to facilitate #1504

It centralise vhosts and vpaths values precomputation in a single range loop and remove some duplicated code.

The data structure stored for now in $globals.vhosts look like this : ```json { "foo.bar.com": { "cert": "", "cert_ok": false, "default": false, "hsts": "max-age=31536000", "http2_enabled": true, "http3_enabled": false, "https_method": "redirect", "paths": { "/": { "containers": [{}], "dest": "", "network_tag": "external", "proto": "http", "upstream": "foo.bar.com" } }, "server_tokens": "", "ssl_policy": "", "vhost_root": "/var/www/public" },{ "multipath.bar.com": { "cert": "", "cert_ok": false, "default": false, "hsts": "max-age=31536000", "http2_enabled": true, "http3_enabled": false, "https_method": "redirect", "paths": { "/": { "containers": [{}], "dest": "", "network_tag": "external", "proto": "http", "upstream": "multipath.bar.com-somesha" }, "/anotherpath": { "containers": [{}], "dest": "", "network_tag": "external", "proto": "http", "upstream": "multipath.bar.com-anothersha" } }, "server_tokens": "", "ssl_policy": "", "vhost_root": "/var/www/public" } } ```
Prior to this PR, it looked like this: ```json { "foo.bar.com": { "cert": "", "cert_ok": false, "containers": [{}], "default": false, "http3": false, "https_method": "redirect", },{ "multipath.bar.com": { "cert": "", "cert_ok": false, "containers": [{}], "default": false, "http3": false, "https_method": "redirect", } } ```

At this point containers contains the list of containers objects (as generated by docker-gen) corresponding to each host / path combinations.

They look like this: ```json { "ID": "68aafce89930f680cb3714f4c1f4348058eb646abf5f69e84065fa7464437fa2", "Created": "2024-02-24T20:40:11.669487339Z", "Addresses": [ { "IP": "172.17.0.3", "IP6LinkLocal": "", "IP6Global": "", "Port": "80", "HostPort": "", "Proto": "tcp", "HostIP": "" } ], "Networks": [ { "IP": "172.17.0.3", "Name": "bridge", "Gateway": "172.17.0.1", "EndpointID": "20fdad73206d1b67ec068597c892515f3ec6af1f731d966e45a0db8c8ad50ba6", "IPv6Gateway": "", "GlobalIPv6Address": "", "MacAddress": "02:42:ac:11:00:03", "GlobalIPv6PrefixLen": 0, "IPPrefixLen": 16, "Internal": false } ], "Gateway": "172.17.0.1", "Name": "wonderful_ardinghelli", "Hostname": "68aafce89930", "NetworkMode": "default", "Image": { "Registry": "", "Repository": "nginx", "Tag": "" }, "Env": { "NGINX_VERSION": "1.25.4", "NJS_VERSION": "0.8.3", "PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "PKG_RELEASE": "1~bookworm", "VIRTUAL_HOST": "foo.bar.com" }, "Volumes": {}, "Node": { "ID": "", "Name": "", "Address": { "IP": "", "IP6LinkLocal": "", "IP6Global": "", "Port": "", "HostPort": "", "Proto": "", "HostIP": "" } }, "Labels": { "maintainer": "NGINX Docker Maintainers " }, "IP": "172.17.0.3", "IP6LinkLocal": "", "IP6Global": "", "Mounts": null, "State": { "Running": true, "Health": { "Status": "" } } } ```

ping @pini-gh

pini-gh commented 3 months ago

Looks good. I'll need some time to try to rebase my fork on top of it.

buchdag commented 3 months ago

Thanks for the approval @SchoNie

@pini-gh is it okay with you if I merge this now ?

pini-gh commented 2 months ago

Yes, please go ahead. Thanks!