Skip to content

Commit b71eb94

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "utils: Remove periods from instance hostnames"
2 parents 0d0adff + 9046f0f commit b71eb94

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

nova/tests/unit/test_utils.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,25 @@ def test_parse_server_string(self):
7373
self.assertEqual(('', ''), result)
7474

7575
def test_hostname_unicode_sanitization(self):
76-
hostname = u"\u7684.test.example.com"
77-
self.assertEqual("test.example.com",
78-
utils.sanitize_hostname(hostname))
76+
hostname = u'\u7684myamazinghostname'
77+
self.assertEqual(
78+
'myamazinghostname', utils.sanitize_hostname(hostname))
7979

8080
def test_hostname_sanitize_periods(self):
81-
hostname = "....test.example.com..."
82-
self.assertEqual("test.example.com",
83-
utils.sanitize_hostname(hostname))
81+
hostname = '....test.example.com...'
82+
self.assertEqual(
83+
'test-example-com', utils.sanitize_hostname(hostname))
8484

8585
def test_hostname_sanitize_dashes(self):
86-
hostname = "----test.example.com---"
87-
self.assertEqual("test.example.com",
88-
utils.sanitize_hostname(hostname))
86+
hostname = '----my-amazing-hostname---'
87+
self.assertEqual(
88+
"my-amazing-hostname", utils.sanitize_hostname(hostname))
8989

9090
def test_hostname_sanitize_characters(self):
9191
hostname = "(#@&$!(@*--#&91)(__=+--test-host.example!!.com-0+"
92-
self.assertEqual("91----test-host.example.com-0",
93-
utils.sanitize_hostname(hostname))
92+
self.assertEqual(
93+
"91----test-host-example-com-0",
94+
utils.sanitize_hostname(hostname))
9495

9596
def test_hostname_translate(self):
9697
hostname = "<}\x1fh\x10e\x08l\x02l\x05o\x12!{>"
@@ -99,20 +100,20 @@ def test_hostname_translate(self):
99100
def test_hostname_has_default(self):
100101
hostname = u"\u7684hello"
101102
defaultname = "Server-1"
102-
self.assertEqual("hello", utils.sanitize_hostname(hostname,
103-
defaultname))
103+
self.assertEqual(
104+
"hello", utils.sanitize_hostname(hostname, defaultname))
104105

105106
def test_hostname_empty_has_default(self):
106107
hostname = u"\u7684"
107108
defaultname = "Server-1"
108-
self.assertEqual(defaultname, utils.sanitize_hostname(hostname,
109-
defaultname))
109+
self.assertEqual(
110+
defaultname, utils.sanitize_hostname(hostname, defaultname))
110111

111112
def test_hostname_empty_has_default_too_long(self):
112113
hostname = u"\u7684"
113114
defaultname = "a" * 64
114-
self.assertEqual("a" * 63, utils.sanitize_hostname(hostname,
115-
defaultname))
115+
self.assertEqual(
116+
"a" * 63, utils.sanitize_hostname(hostname, defaultname))
116117

117118
def test_hostname_empty_no_default(self):
118119
hostname = u"\u7684"

nova/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ def truncate_hostname(name):
372372
hostname = hostname.encode('latin-1', 'ignore').decode('latin-1')
373373

374374
hostname = truncate_hostname(hostname)
375-
hostname = re.sub('[ _]', '-', hostname)
375+
hostname = re.sub(r'[ _\.]', '-', hostname)
376376
hostname = re.sub(r'[^\w.-]+', '', hostname)
377377
hostname = hostname.lower()
378378
hostname = hostname.strip('.-')
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
fixes:
3+
- |
4+
Nova will now replace periods (``.``) with dashes (``-``) when santizing an
5+
instance's display name for use as a hostname.
6+
7+
Nova publishes hostnames for instances via the metadata service and config
8+
drives. This hostname is based on a sanitized version of the instance name
9+
combined with the domain value specified in ``[api] dhcp_domain``. The
10+
previous sanitization of the hostname included the replacement of whitespace
11+
and underscores with dashes and the stripping of unicode characters along
12+
with leading and trailing periods and dashes. It did not, however, include
13+
the removal of periods in the name. Periods are not valid in the hostname
14+
or, more specifically, in the host-specific or leaf label (the ``host`` in
15+
``host.example.com``) and their presence can cause conflicts when ``[api]
16+
dhcp_domain`` is configured, leading to instances being mistakenly
17+
configured with hostnames like ``host.example.com.example.com``. More
18+
pressingly, their use can result in a failure to boot instances if DNS
19+
integration is enabled in neutron, likely via designate, as the hostname is
20+
identified as a FQDN (fully-qualified domain name) by neutron and reasonable
21+
instance names like ``test-ubuntu20.04`` will be rejected as invalid FQDNs,
22+
in this case because the name would yield a TLD (top-level domain) of ``04``
23+
and TLDs cannot be entire numerical. To avoid these issues, periods are now
24+
replaced with dashes.

0 commit comments

Comments
 (0)