diff --git a/binderhub/app.py b/binderhub/app.py index c2404f62c..85872210c 100755 --- a/binderhub/app.py +++ b/binderhub/app.py @@ -378,7 +378,22 @@ def _default_hub_token(self): """, config=True, ) - @validate('hub_url') + + hub_url_local = Unicode( + help=""" + The base URL of the JupyterHub instance for local/internal traffic + + If local/internal network connections from the BinderHub process should access + JupyterHub using a different URL than public/external traffic set this, default + is hub_url + """, + config=True, + ) + @default('hub_url_local') + def _default_hub_url_local(self): + return self.hub_url + + @validate('hub_url', 'hub_url_local') def _add_slash(self, proposal): """trait validator to ensure hub_url ends with a trailing slash""" if proposal.value is not None and not proposal.value.endswith('/'): @@ -577,6 +592,7 @@ def initialize(self, *args, **kwargs): self.launcher = Launcher( parent=self, hub_url=self.hub_url, + hub_url_local=self.hub_url_local, hub_api_token=self.hub_api_token, create_user=not self.auth_enabled, ) @@ -665,7 +681,7 @@ def initialize(self, *args, **kwargs): tornado.web.StaticFileHandler, {'path': os.path.join(self.tornado_settings['static_path'], 'images')}), (r'/about', AboutHandler), - (r'/health', HealthHandler, {'hub_url': self.hub_url}), + (r'/health', HealthHandler, {'hub_url': self.hub_url_local}), (r'/', MainHandler), (r'.*', Custom404), ] diff --git a/binderhub/launcher.py b/binderhub/launcher.py index 63810a96a..9ee80e447 100644 --- a/binderhub/launcher.py +++ b/binderhub/launcher.py @@ -14,7 +14,7 @@ from tornado import web, gen from tornado.httpclient import AsyncHTTPClient, HTTPRequest, HTTPError from traitlets.config import LoggingConfigurable -from traitlets import Integer, Unicode, Bool +from traitlets import Integer, Unicode, Bool, default from jupyterhub.traitlets import Callable from jupyterhub.utils import maybe_future @@ -34,6 +34,10 @@ class Launcher(LoggingConfigurable): hub_api_token = Unicode(help="The API token for the Hub") hub_url = Unicode(help="The URL of the Hub") + hub_url_local = Unicode(help="The internal URL of the Hub if different") + @default('hub_url_local') + def _default_hub_url_local(self): + return self.hub_url create_user = Bool(True, help="Create a new Hub user") allow_named_servers = Bool( os.getenv('JUPYTERHUB_ALLOW_NAMED_SERVERS', "false") == "true", @@ -81,7 +85,7 @@ async def api_request(self, url, *args, **kwargs): """Make an API request to JupyterHub""" headers = kwargs.setdefault('headers', {}) headers.update({'Authorization': 'token %s' % self.hub_api_token}) - hub_api_url = os.getenv('JUPYTERHUB_API_URL', '') or self.hub_url + 'hub/api/' + hub_api_url = os.getenv('JUPYTERHUB_API_URL', '') or self.hub_url_local + 'hub/api/' request_url = hub_api_url + url req = HTTPRequest(request_url, *args, **kwargs) retry_delay = self.retry_delay diff --git a/helm-chart/binderhub/templates/deployment.yaml b/helm-chart/binderhub/templates/deployment.yaml index a4e553cde..4ca58e1ce 100644 --- a/helm-chart/binderhub/templates/deployment.yaml +++ b/helm-chart/binderhub/templates/deployment.yaml @@ -98,7 +98,7 @@ spec: key: "binder.hub-token" {{- if .Values.config.BinderHub.auth_enabled }} - name: JUPYTERHUB_API_URL - value: {{ (print (.Values.config.BinderHub.hub_url | trimSuffix "/") "/hub/api/") }} + value: {{ (print (.Values.config.BinderHub.hub_url_local | default .Values.config.BinderHub.hub_url | trimSuffix "/") "/hub/api/") }} - name: JUPYTERHUB_BASE_URL value: {{ .Values.jupyterhub.hub.baseUrl | quote }} - name: JUPYTERHUB_CLIENT_ID