Skip to content
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

fix: main caddy service should send MFE requests to the mfe containers #23

Closed
wants to merge 1 commit into from

Conversation

jfavellar90
Copy link
Contributor

Since Nginx service and containers were removed in overhangio/tutor@e19f334, MFE traffic should be routed from caddy main service to caddy process running inside mfe containers.

@regisb regisb added this to Waiting for review in Tutor project management (obsolete) Nov 8, 2021
@regisb
Copy link
Contributor

regisb commented Nov 8, 2021

Thanks for the PR @jfavellar90! Since this change is only required in the nightly branch, could you please rebase your branch?

Also, the "caddyfile" patch should probably look like this: (to be verified)

{{ MFE_HOST }}{{ port }} {
    import proxy "mfe:8000"
}

@regisb regisb moved this from Waiting for review to In Progress in Tutor project management (obsolete) Nov 8, 2021
@jfavellar90 jfavellar90 changed the base branch from master to nightly November 8, 2021 14:14
@jfavellar90
Copy link
Contributor Author

@regisb I already rebased the branch over the nightly branch. When trying the change you proposed to the "caddyfile" patch:

{{ MFE_HOST }}{{ port }} {
    import proxy "mfe:8000"
}

It fails when running tutor config save since port variable is not in the context of that patch rendering. The variable is defined here in the main Caddyfile. is this error expected?

On the other hand, port 8000 is not open in the MFE docker compose definition. Should we open it and serve the traffic through or use the 80 port (already open because of caddy service running in the MFE container I think)?

@regisb
Copy link
Contributor

regisb commented Nov 9, 2021

It fails when running tutor config save since port variable is not in the context of that patch rendering.

This is very annoying. I created this port variable precisely to make it easy for plugin developers to add their own patches to the Caddyfile (see this pr).

The problem is that currently patches are rendered outside of the scope of the parent template. We could change the patch function to something like:

def patch(self, name: str, separator: str = "\n", suffix: str = "") -> str:
    patches = [patch.strip() for _, patch in plugins.iter_patches(self.config, name)]
    rendered = separator.join(patches)
    if rendered:
        rendered += suffix
    return rendered

And then patches would not be rendered outside of the parent context. We would lose the error message in case the plugin patch is invalid. But, more importantly, we would also lose the ability to have patches that themselves include patches (so called "recursive patches"). I verified the latter problem with the following unit test:

def test_recursive_patching(self) -> None:
    patches = {
        "1": {
            "plugin1": "{{ patch('2') }}",
        },
        "2": {"plugin2": "abcd"},
    }

    def mock_iter_patches(config, name):
        yield from patches[name].items()

    with patch.object(env.plugins, "iter_patches", new=mock_iter_patches):
        rendered = env.render_str({}, '{{ patch("1") }}')
    self.assertEqual("abcd", rendered)

Currently, this test passes. But with the proposed change, I get the following failure:

======================================================================
FAIL: test_recursive_patching (test_env.EnvTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/regis/projets/overhang/repos/overhang/tutor/tests/test_env.py", line 129, in test_recursive_patching
    self.assertEqual("abcd", rendered)
AssertionError: 'abcd' != "{{ patch('2') }}"
- abcd
+ {{ patch('2') }}

Another option is to create a global CADDY_SITE_PORT = "{% if not ENABLE_HTTPS or not ENABLE_WEB_PROXY %}:80{% endif %}" variable. But I'm reluctant to create a new variable for such a specific scenario.

Another option is to pass the port as an environment variable, and then use it as {{ MFE_HOST }}{$default_site_port}. According to the Caddy docs and my experiments, this should work.

I'll push this fix right now in the nightly and maple branches, so that you can update this PR.

On the other hand, port 8000 is not open in the MFE docker compose definition. Should we open it and serve the traffic through or use the 80 port (already open because of caddy service running in the MFE container I think)?

You don't need to open the port in docker-compose. Port 8002 (and not 8000 as I wrote earlier) is open inside the docker-compose (and k8s) network.

regisb added a commit to overhangio/tutor that referenced this pull request Nov 9, 2021
When nginx was removed in favour of caddy, we decided that plugin
implementations of the "caddyfile" patch should make use of the "port" local
variable. However, local variables are not available from inside plugin
patches, which are rendered outside of the context of the parent templates.

For a more extensive description of the problem, see:
overhangio/tutor-mfe#23 (comment)

We still want to make it easy for developers to decide what should the port be
for caddy hosts. To do so, we make use of environment variables that are passed
at runtime to the caddy container.

Thus, a regular plugin patch should look like this:

    {{ PLUGIN_HOST }}{$default_site_port} {
        import proxy "myplugin:8000"
    }
@regisb
Copy link
Contributor

regisb commented Nov 9, 2021

Here is the commit in the Maple branch: overhangio/tutor@72baae0

@regisb
Copy link
Contributor

regisb commented Nov 15, 2021

Fixed by this commit. Sorry to close this @jfavellar90 but I needed this change for local testing.

@regisb regisb closed this Nov 15, 2021
@regisb regisb moved this from In Progress to Blocked in Tutor project management (obsolete) Nov 15, 2021
@regisb regisb moved this from Blocked to Won't fix in Tutor project management (obsolete) Nov 15, 2021
regisb added a commit to overhangio/tutor that referenced this pull request Dec 20, 2021
When nginx was removed in favour of caddy, we decided that plugin
implementations of the "caddyfile" patch should make use of the "port" local
variable. However, local variables are not available from inside plugin
patches, which are rendered outside of the context of the parent templates.

For a more extensive description of the problem, see:
overhangio/tutor-mfe#23 (comment)

We still want to make it easy for developers to decide what should the port be
for caddy hosts. To do so, we make use of environment variables that are passed
at runtime to the caddy container.

Thus, a regular plugin patch should look like this:

    {{ PLUGIN_HOST }}{$default_site_port} {
        import proxy "myplugin:8000"
    }
regisb added a commit to overhangio/tutor that referenced this pull request Dec 20, 2021
When nginx was removed in favour of caddy, we decided that plugin
implementations of the "caddyfile" patch should make use of the "port" local
variable. However, local variables are not available from inside plugin
patches, which are rendered outside of the context of the parent templates.

For a more extensive description of the problem, see:
overhangio/tutor-mfe#23 (comment)

We still want to make it easy for developers to decide what should the port be
for caddy hosts. To do so, we make use of environment variables that are passed
at runtime to the caddy container.

Thus, a regular plugin patch should look like this:

    {{ PLUGIN_HOST }}{$default_site_port} {
        import proxy "myplugin:8000"
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Won't fix
Development

Successfully merging this pull request may close these issues.

None yet

2 participants