Skip to content

Commit 4b791d2

Browse files
author
Arne-Christian Blystad
committed
feat(controller): add hostname exposing to units
This exposes a configurable hostname type to Docker container through fleet unit files. Three hostname types are supported: - default : standard Docker hostname - application : application hostname (e.g. my-app.v54.web.1) - server : Core OS hostname This fixes #2835 and is an alternative to #2428, which got rejected. It is necessary when using a monitoring solution like New Relic.
1 parent 14353e1 commit 4b791d2

4 files changed

Lines changed: 27 additions & 1 deletion

File tree

controller/bin/boot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ etcd_set_default secretKey ${DEIS_SECRET_KEY:-`openssl rand -base64 64 | tr -d '
4949
etcd_set_default builderKey ${DEIS_BUILDER_KEY:-`openssl rand -base64 64 | tr -d '\n'`}
5050
etcd_set_default registrationEnabled 1
5151
etcd_set_default webEnabled 0
52+
etcd_set_default unitHostname default
5253

5354
# safely create required keyspaces
5455
etcd_safe_mkdir /deis/services

controller/deis/settings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,13 @@
308308
# see https://docs.djangoproject.com/en/1.6/ref/settings/#secure-proxy-ssl-header
309309
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
310310

311+
# Unit Hostname handling.
312+
# Supports:
313+
# default - Docker generated hostname
314+
# application - Hostname based on application unit name (i.e. my-application.v2.web.1)
315+
# server - Hostname based on CoreOS server hostname
316+
UNIT_HOSTNAME = 'default'
317+
311318
# Create a file named "local_settings.py" to contain sensitive settings data
312319
# such as database configuration, admin email, or passwords and keys. It
313320
# should also be used for any settings which differ between development

controller/scheduler/fleet.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import socket
88
import re
99
import time
10+
from django.conf import settings
1011

1112

1213
MATCH = re.compile(
@@ -112,6 +113,8 @@ def _create_container(self, name, image, command, unit, **kwargs):
112113
l.update({'cpu': '-c {}'.format(cpu)})
113114
else:
114115
l.update({'cpu': ''})
116+
# set unit hostname
117+
l.update({'hostname': self._get_hostname(name)})
115118
# should a special entrypoint be used
116119
entrypoint = kwargs.get('entrypoint')
117120
if entrypoint:
@@ -137,6 +140,19 @@ def _create_container(self, name, image, command, unit, **kwargs):
137140
if attempt == (RETRIES - 1): # account for 0 indexing
138141
raise
139142

143+
def _get_hostname(self, application_name):
144+
hostname = settings.UNIT_HOSTNAME
145+
if hostname == "default":
146+
return ''
147+
elif hostname == "application":
148+
# replace underscore with dots, since underscore is not valid in DNS hostnames
149+
dns_name = application_name.replace("_", ".")
150+
return '-h ' + dns_name
151+
elif hostname == "server":
152+
return '-h %H'
153+
else:
154+
raise RuntimeError('Unsupported hostname: ' + hostname)
155+
140156
def start(self, name):
141157
"""Start a container"""
142158
self._wait_for_container(name)
@@ -294,7 +310,7 @@ def attach(self, name):
294310
{"section": "Unit", "name": "Description", "value": "{name}"},
295311
{"section": "Service", "name": "ExecStartPre", "value": '''/bin/sh -c "IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; docker pull $IMAGE"'''}, # noqa
296312
{"section": "Service", "name": "ExecStartPre", "value": '''/bin/sh -c "docker inspect {name} >/dev/null 2>&1 && docker rm -f {name} || true"'''}, # noqa
297-
{"section": "Service", "name": "ExecStart", "value": '''/bin/sh -c "IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; port=$(docker inspect -f '{{{{range $k, $v := .ContainerConfig.ExposedPorts }}}}{{{{$k}}}}{{{{end}}}}' $IMAGE | cut -d/ -f1) ; docker run --name {name} {memory} {cpu} -P -e PORT=$port $IMAGE {command}"'''}, # noqa
313+
{"section": "Service", "name": "ExecStart", "value": '''/bin/sh -c "IMAGE=$(etcdctl get /deis/registry/host 2>&1):$(etcdctl get /deis/registry/port 2>&1)/{image}; port=$(docker inspect -f '{{{{range $k, $v := .ContainerConfig.ExposedPorts }}}}{{{{$k}}}}{{{{end}}}}' $IMAGE | cut -d/ -f1) ; docker run --name {name} {memory} {cpu} {hostname} -P -e PORT=$port $IMAGE {command}"'''}, # noqa
298314
{"section": "Service", "name": "ExecStop", "value": '''/usr/bin/docker rm -f {name}'''},
299315
{"section": "Service", "name": "TimeoutStartSec", "value": "20m"},
300316
{"section": "Service", "name": "RestartSec", "value": "5"},

controller/templates/confd_settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@
4444
{{ if .deis_controller_webEnabled }}
4545
WEB_ENABLED = bool({{ .deis_controller_webEnabled }})
4646
{{ end }}
47+
48+
UNIT_HOSTNAME = '{{ or (.deis_controller_unitHostname) "default" }}'

0 commit comments

Comments
 (0)