Handle ipv6 literal zookeeper addresses

If you use a ipv6 literal as the host: parameter in zookeeper-services
section, it gets incorretly concatentated with the port when building
the host list.  Detect ipv6 literals and quote them with rfc2732 []'s
when building the host string.

Change-Id: I49a749142e8031be37a08960bf645faca4e97149
This commit is contained in:
Ian Wienand 2020-05-04 13:31:44 +10:00 committed by James E. Blair
parent fd5915a070
commit ae784c57a2
3 changed files with 37 additions and 1 deletions

View File

@ -43,6 +43,23 @@ class TestZooKeeper(tests.DBTestCase):
self.assertEqual('127.0.0.1:2181/test1,127.0.0.2:2182/test2',
zk.buildZooKeeperHosts(hosts))
def test_buildZooKeeperHosts_ipv6(self):
hosts = [
zk.ZooKeeperConnectionConfig(
'2001:4800:7817:103:be76:4eff:fe04:e359', port=2181,
chroot='/test1'),
zk.ZooKeeperConnectionConfig(
'[2002:4800:7817:103:be76:4eff:fe04:e359]', port=2181,
chroot='/test2'),
zk.ZooKeeperConnectionConfig('127.0.0.2', port=2182,
chroot='/test3')
]
self.assertEqual((
'[2001:4800:7817:103:be76:4eff:fe04:e359]:2181/test1,'
'[2002:4800:7817:103:be76:4eff:fe04:e359]:2181/test2,'
'127.0.0.2:2182/test3'
), zk.buildZooKeeperHosts(hosts))
def test_imageBuildLock(self):
path = self.zk._imageBuildLockPath("ubuntu-trusty")
with self.zk.imageBuildLock("ubuntu-trusty", blocking=False):

View File

@ -13,6 +13,7 @@
from contextlib import contextmanager
from copy import copy
import abc
import ipaddress
import json
import logging
import time
@ -101,6 +102,10 @@ class ZooKeeperConnectionConfig(object):
self.port = port
self.chroot = chroot or ''
def __repr__(self):
return "host=%s port=%s chroot=%s" % \
(self.host, self.port, self.chroot)
def buildZooKeeperHosts(host_list):
'''
@ -114,7 +119,16 @@ def buildZooKeeperHosts(host_list):
raise Exception("'host_list' must be a list")
hosts = []
for host_def in host_list:
host = '%s:%s%s' % (host_def.host, host_def.port, host_def.chroot)
h = host_def.host
# If this looks like a ipv6 literal address, make sure it's
# quoted in []'s
try:
addr = ipaddress.ip_address(host_def.host)
if addr.version == 6:
h = '[%s]' % addr
except ValueError:
pass
host = '%s:%s%s' % (h, host_def.port, host_def.chroot)
hosts.append(host)
return ",".join(hosts)

View File

@ -0,0 +1,5 @@
---
fixes:
- |
Zookeeper hosts specified as IPv6 literals will now be configured
correctly.