Skip to content

Subdomain options for multi-tenanted apps #4288

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

Closed
bartonip opened this issue Oct 1, 2022 · 11 comments
Closed

Subdomain options for multi-tenanted apps #4288

bartonip opened this issue Oct 1, 2022 · 11 comments

Comments

@bartonip
Copy link
Contributor

bartonip commented Oct 1, 2022

Now that coder supports wildcard domains, it would be awesome if we could add the ability to pre-define subdomains in the terraform config to particular ports/addresses.

The use case that we have is that we are developing a multi-tenanted app where each tenant has their own unique subdomain a-la https://tenant1.ourapp.com, https://tenant2.ourapp.com.

Right now, we have a real sketchy netcat listener that gets curl'd and then updates nginx config when new workspaces get spun up. It would be great if we could specify a default subdomain where the coder_agent could be accessed at and be able to define other subdomains that would point to this agent.

I think this would be advantageous to all companies who are developing apps that run on multiple subdomains.

Please let me know what you think.

@bpmct
Copy link
Member

bpmct commented Oct 3, 2022

I may be misunderstanding, but would something like this work in the template?

# inside a template main.tf

resource "coder_app" "tenant1" {
  url           = "http://localhost:8000"
  relative_path = false # host the app on a subdomain 
}

resource "coder_app" "tenant2" {
  url           = "http://localhost:8001"
  relative_path = false # host the app on a subdomain
}

# the tenant URLs could be shared with the workspace as environment variables
resource "coder_agent" "agentname" {
  env {
      name = "TENANT1_URL"
      value = `https://tenant1--agentname--${data.coder_workspace.me.name}-${data.coder_workspace.me.username}.wildcard-example-domain.com`
  }
  env {
      name = "TENANT2_URL"
      value = `https://tenant2--agentname--${data.coder_workspace.me.name}-${data.coder_workspace.me.username}.wildcard-example-domain.com`
  }
}

The subdomains are namespaced to include the workspace, agent name, and username to avoid conflicts, but the URL is predictable and can be dynamically represented in Terraform. This is in progress in #4263

Let me know if your use case is different though! I wasn't sure if you were looking for unique domains to access Coder or if your NGINX config is "global" outside each workspace, where you may need to specify unique routes.

@deansheather
Copy link
Member

Technically Ben's solution already works in the current version of Coder, however any buttons that link to the apps in the dashboard will incorrectly link to the path based app URLs rather than the subdomains you intended to use. That's fixed by #4263

@bartonip
Copy link
Contributor Author

bartonip commented Oct 8, 2022

@bpmct if i am able to access https://tenant2--agentname--${data.coder_workspace.me.name}-${data.coder_workspace.me.username}.wildcard-example-domain.com in a browser on my laptop and it gets pointed to http://localhost:8001 in the container, and the hostname is preserved in the headers that would be great.

I've tried the above code on 0.9.7 as per @deansheather message above but I am unable to access the services?

@deansheather
Copy link
Member

I think there's a mistake in the URL Ben posted, there should be double hyphens between each thing in the subdomain. So add an extra - between the workspace name and username and it should work.

The Host is preserved from the browser and passed all the way through I believe. If this is not the behavior you're seeing then we definitely need to resolve that.

@bartonip
Copy link
Contributor Author

bartonip commented Oct 8, 2022

Okay, just to throw a spanner in the works, our app uses the hostname header to determine which tenant the client is querying. So is it possible to point both the tenant1 and tenant2 domains to the same service on the same port?

@deansheather
Copy link
Member

@bartonip I don't believe there's any limitation on having two apps point to the same service/port inside the workspace

@bartonip
Copy link
Contributor Author

@deansheather I've just tried the above again and get a big ol' 404 when I try to access those subdomains. I am presuming that the environment variables are just the name of the coder_app uppercased with _URL appended to the name?

@deansheather
Copy link
Member

You can check the environment variables in your workspace by running the env command, if they're not showing up then it may be a bug in Coder @bartonip

@github-actions
Copy link

This issue is becoming stale. In order to keep the tracker readable and actionable, I'm going close to this issue in 7 days if there isn't more activity.

@github-actions github-actions bot added the stale This issue is like stale bread. label Dec 13, 2022
@kylecarbs
Copy link
Member

@bartonip were you able to get this working?

@kylecarbs kylecarbs added the waiting-for-info The issue creator is asked to provide more information. label Dec 14, 2022
@github-actions github-actions bot removed the stale This issue is like stale bread. label Dec 15, 2022
@bartonip
Copy link
Contributor Author

@kylecarbs yep this is all working now :)

@matifali matifali removed the waiting-for-info The issue creator is asked to provide more information. label Jun 30, 2023
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

No branches or pull requests

5 participants