Docker Set Container name to Instance ID

Docker has named container since 0.7. In order to make it easier for the
admin to troubleshoot and find container set the container name to the nova
instance UUID. Addtional the name is prefixed with nova.
Example: nova-d7553842-2424-4471-98a7-92a1db70033b
DocImpact - Docker driver requires Docker 0.7 or newer.
Change-Id: I4f12734663e726b08f53400fdfcfb8d31401490d
Blueprint:
https://blueprints.launchpad.net/nova/+spec/docker-name-instance-id 
This commit is contained in:
Daniel Kuffner
2014年01月08日 13:14:08 +01:00
parent c2a204b79c
commit 2e12b46141

View File

@@ -25,6 +25,7 @@ import nova.virt.docker.client
class MockClient(object):
def __init__(self, endpoint=None):
self._containers = {}
self.name = None
def _fake_id(self):
return uuid.uuid4().hex + uuid.uuid4().hex
@@ -46,7 +47,8 @@ class MockClient(object):
})
return containers
def create_container(self, args):
def create_container(self, args, name):
self.name = name
data = {
'Hostname': '',
'User': '',

View File

@@ -15,6 +15,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import uuid
import mox
from nova.openstack.common import jsonutils
@@ -56,6 +58,7 @@ class DockerHTTPClientTestCase(test.NoDBTestCase):
def test_create_container(self):
mock_conn = self.mox.CreateMockAnything()
expected_uuid = uuid.uuid4()
expected_body = jsonutils.dumps({
'Hostname': '',
@@ -76,9 +79,11 @@ class DockerHTTPClientTestCase(test.NoDBTestCase):
'Volumes': {},
'VolumesFrom': '',
})
mock_conn.request('POST', '/v1.4/containers/create',
body=expected_body,
headers={'Content-Type': 'application/json'})
mock_conn.request('POST', '/v1.7/containers/create?name={0}'.format(
expected_uuid),
body=expected_body,
headers={'Content-Type': 'application/json'})
response = FakeResponse(201, data='{"id": "XXX"}',
headers={'Content-Type': 'application/json'})
mock_conn.getresponse().AndReturn(response)
@@ -86,14 +91,14 @@ class DockerHTTPClientTestCase(test.NoDBTestCase):
self.mox.ReplayAll()
client = nova.virt.docker.client.DockerHTTPClient(mock_conn)
container_id = client.create_container({})
container_id = client.create_container({}, expected_uuid)
self.assertEqual('XXX', container_id)
self.mox.VerifyAll()
def test_create_container_with_args(self):
mock_conn = self.mox.CreateMockAnything()
expected_uuid = uuid.uuid4()
expected_body = jsonutils.dumps({
'Hostname': 'marco',
'User': '',
@@ -113,9 +118,10 @@ class DockerHTTPClientTestCase(test.NoDBTestCase):
'Volumes': {},
'VolumesFrom': '',
})
mock_conn.request('POST', '/v1.4/containers/create',
body=expected_body,
headers={'Content-Type': 'application/json'})
mock_conn.request('POST', '/v1.7/containers/create?name={0}'.format(
expected_uuid),
body=expected_body,
headers={'Content-Type': 'application/json'})
response = FakeResponse(201, data='{"id": "XXX"}',
headers={'Content-Type': 'application/json'})
mock_conn.getresponse().AndReturn(response)
@@ -128,17 +134,20 @@ class DockerHTTPClientTestCase(test.NoDBTestCase):
'Memory': 512,
'Image': 'example',
}
container_id = client.create_container(args)
container_id = client.create_container(args, expected_uuid)
self.assertEqual('XXX', container_id)
self.mox.VerifyAll()
def test_create_container_no_id_in_response(self):
mock_conn = self.mox.CreateMockAnything()
expected_uuid = uuid.uuid4()
mock_conn.request('POST', '/v1.4/containers/create',
body=mox.IgnoreArg(),
headers={'Content-Type': 'application/json'})
mock_conn.request('POST', '/v1.7/containers/create?name={0}'.format(
expected_uuid),
body=mox.IgnoreArg(),
headers={'Content-Type': 'application/json'})
response = FakeResponse(201, data='{"ping": "pong"}',
headers={'Content-Type': 'application/json'})
mock_conn.getresponse().AndReturn(response)
@@ -146,24 +155,26 @@ class DockerHTTPClientTestCase(test.NoDBTestCase):
self.mox.ReplayAll()
client = nova.virt.docker.client.DockerHTTPClient(mock_conn)
container_id = client.create_container({})
container_id = client.create_container({}, expected_uuid)
self.assertIsNone(container_id)
self.mox.VerifyAll()
def test_create_container_bad_return_code(self):
mock_conn = self.mox.CreateMockAnything()
expected_uuid = uuid.uuid4()
mock_conn.request('POST', '/v1.4/containers/create',
body=mox.IgnoreArg(),
headers={'Content-Type': 'application/json'})
mock_conn.request('POST', '/v1.7/containers/create?name={0}'.format(
expected_uuid),
body=mox.IgnoreArg(),
headers={'Content-Type': 'application/json'})
response = FakeResponse(400)
mock_conn.getresponse().AndReturn(response)
self.mox.ReplayAll()
client = nova.virt.docker.client.DockerHTTPClient(mock_conn)
container_id = client.create_container({})
container_id = client.create_container({}, expected_uuid)
self.assertIsNone(container_id)
self.mox.VerifyAll()

View File

@@ -38,9 +38,9 @@ class DockerDriverTestCase(_VirtDriverTestCase, test.TestCase):
def setUp(self):
super(DockerDriverTestCase, self).setUp()
self.stubs.Set(nova.virt.docker.driver.DockerDriver,
'docker',
nova.tests.virt.docker.mock_client.MockClient())
self.mock_client = nova.tests.virt.docker.mock_client.MockClient()
self.stubs.Set(nova.virt.docker.driver.DockerDriver, 'docker',
self.mock_client)
def fake_setup_network(self, instance, network_info):
return
@@ -152,6 +152,8 @@ class DockerDriverTestCase(_VirtDriverTestCase, test.TestCase):
self.connection.spawn(self.context, instance_href, image_info,
'fake_files', 'fake_password')
self._assert_cpu_shares(instance_href)
self.assertEqual(self.mock_client.name, "nova-{0}".format(
instance_href['uuid']))
def test_create_container_vcpus_2(self, image_info=None):
flavor = utils.get_test_flavor(options={
@@ -167,6 +169,8 @@ class DockerDriverTestCase(_VirtDriverTestCase, test.TestCase):
self.connection.spawn(self.context, instance_href, image_info,
'fake_files', 'fake_password')
self._assert_cpu_shares(instance_href, vcpus=2)
self.assertEqual(self.mock_client.name, "nova-{0}".format(
instance_href['uuid']))
def _assert_cpu_shares(self, instance_href, vcpus=4):
container_id = self.connection.find_container_by_name(

View File

@@ -110,7 +110,7 @@ class DockerHTTPClient(object):
'/v1.4/containers/ps?all={0}&limit=50'.format(int(_all)))
return resp.json
def create_container(self, args):
def create_container(self, args, name):
data = {
'Hostname': '',
'User': '',
@@ -133,7 +133,7 @@ class DockerHTTPClient(object):
data.update(args)
resp = self.make_request(
'POST',
'/v1.4/containers/create',
'/v1.7/containers/create?name={0}'.format(name),
body=jsonutils.dumps(data))
if resp.code != 201:
return

View File

@@ -289,7 +289,7 @@ class DockerDriver(driver.ComputeDriver):
default_cmd = self._get_default_cmd(image_name)
if default_cmd:
args['Cmd'] = default_cmd
container_id = self.docker.create_container(args)
container_id = self._create_container(instance, args)
if not container_id:
msg = _('Image name "{0}" does not exist, fetching it...')
LOG.info(msg.format(image_name))
@@ -298,7 +298,7 @@ class DockerDriver(driver.ComputeDriver):
raise exception.InstanceDeployFailure(
_('Cannot pull missing image'),
instance_id=instance['name'])
container_id = self.docker.create_container(args)
container_id = self._create_container(instance, args)
if not container_id:
raise exception.InstanceDeployFailure(
_('Cannot create container'),
@@ -406,3 +406,7 @@ class DockerDriver(driver.ComputeDriver):
"""
flavor = flavors.extract_flavor(instance)
return int(flavor['vcpus']) * 1024
def _create_container(self, instance, args):
name = "nova-" + instance['uuid']
return self.docker.create_container(args, name)
Reference in New Issue
openstack/nova
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.

The note is not visible to the blocked user.