Skip to content

Enable Router to be configured with multiple domains - #188

Merged
bilbof merged 1 commit into
masterfrom
bilbof/router-subdomain
Feb 24, 2021
Merged

Enable Router to be configured with multiple domains#188
bilbof merged 1 commit into
masterfrom
bilbof/router-subdomain

Conversation

@bilbof

@bilbof bilbof commented Feb 22, 2021

Copy link
Copy Markdown
Contributor

Problem

During the migration to ECS, Replatforming intends to run router in ECS and EC2, using the same backing database managed by Router API, hosted in EC2.

One problem with this approach is that ECS and EC2 apps are served at different domains (as below):

Router looks up the entire backend_url from its database. This currently makes it impossible to run two different Router applications which serve different domains, but use the same database.

Solution

Rather than store the backend_url for a backend in Router API's database, we'll provide backend URLs as environment variables such as

BACKEND_URL_content-store=https://content-store.gov.uk/
BACKEND_URL_search-api=https://search.gov.uk/

If an environment variable matching a backend_id is set Router will use the value of the variable instead of the FQDN backend_url stored in the DB.

This change should have no impact on the current platform since we won't set any new environment variables there.

As it is trivial to bring up new containers in ECS with different sets of environment variables, this will enable us to make router work across domains while using the same database (for workspaces).

Migration path:

  1. Push this change out
  2. Add env var for backends to router instances in ECS

Other approaches

  • Provide an env var for the host domain and add subdomains to backends in Router API (overly complex)
  • write duplicate backend records to the db (unnecessary)

Trello: https://trello.com/c/qtAZfkjg/403-work-out-whether-we-can-make-router-work-with-multiple-domains-and-a-single-database
Design doc: https://docs.google.com/document/d/147FSn4sd80iGZ1KxcJ7OYG2cW_uY-Gc7e7laLhkkMJo/edit#

@bilbof
bilbof force-pushed the bilbof/router-subdomain branch from f7adb1d to 27f2908 Compare February 24, 2021 10:41
@bilbof
bilbof marked this pull request as ready for review February 24, 2021 10:54
@bilbof bilbof changed the title Enable Router to be configured with a domain Enable Router to be configured with multiple domains Feb 24, 2021
Comment thread router.go
Comment thread integration_tests/route_loading_test.go Outdated
@richardTowers

Copy link
Copy Markdown

I think you might need to update the PR description to match the commit too :)

Problem
-------

During the migration to ECS, Replatforming intends to run router
in ECS and EC2, using the same backing database managed by
Router API, hosted in EC2.

One problem with this approach is that ECS and EC2 apps
are served at different domains (as below),
- https://calendars.integration.govuk-internal.digital/
- http://calendars.mesh.govuk-internal.digital/

Router looks up the entire backend_url from its database.
This currently makes it impossible to run two different Router
applications which serve different domains, but use the same database.

Solution
--------

Rather than store the backend_url for a backend in Router API's
database, we'll provide backend URLs as environment variables
such as

```
BACKEND_URL_content-store=https://content-store.gov.uk/
BACKEND_URL_search-api=https://search.gov.uk/
```

If an environment variable matching a backend_id is set Router will
use the value of the variable instead of the FQDN backend_url stored
in the DB.

This change should have no impact on the current platform since
we won't set any new environment variables there.

As it is trivial to bring up new containers in ECS with different
sets of environment variables, this will enable us to make router
work across domains while using the same database (for workspaces).

Migration path:

1. Push this change out
2. Add env var for backends to router instances in ECS

**Other approaches**

* Provide an env var for the host domain and add subdomains to backends
  in Router API (overly complex)
* write duplicate backend records to the db (unnecessary)

Trello: https://trello.com/c/qtAZfkjg/403-work-out-whether-we-can-make-router-work-with-multiple-domains-and-a-single-database
Design doc: https://docs.google.com/document/d/147FSn4sd80iGZ1KxcJ7OYG2cW_uY-Gc7e7laLhkkMJo/edit#
@bilbof
bilbof force-pushed the bilbof/router-subdomain branch from 27f2908 to 9e1c0d5 Compare February 24, 2021 11:19
@bilbof

bilbof commented Feb 24, 2021

Copy link
Copy Markdown
Contributor Author

@richardTowers thanks for your feedback! I've improved the test and formatted.

@richardTowers richardTowers left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Beautiful, thanks Bill!

@bilbof
bilbof merged commit 7a4cdd6 into master Feb 24, 2021
@bilbof
bilbof deleted the bilbof/router-subdomain branch February 24, 2021 16:56
bilbof pushed a commit to alphagov/govuk-infrastructure that referenced this pull request Apr 16, 2021
This will enable router to route requests to instances in
its own mesh. This follows on from this PR:
alphagov/router#188

I've added the other backends in as placeholders,
acquired using this RouterAPI query:

```ruby
Route.collection.aggregate([
    {"$group" => {"_id" => "$backend_id", "count" => {"$sum" => 1}}}
]).each { |res|
    puts "#{res["_id"]}: #{res["count"]}"
}
```
bilbof pushed a commit to alphagov/govuk-infrastructure that referenced this pull request Apr 16, 2021
This will enable router to route requests to instances in
its own mesh. This follows on from this PR:
alphagov/router#188

I've added the other backends in as placeholders,
acquired using this RouterAPI query:

```ruby
Route.collection.aggregate([
    {"$group" => {"_id" => "$backend_id", "count" => {"$sum" => 1}}}
]).each { |res|
    puts "#{res["_id"]}: #{res["count"]}"
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants