mirror of https://github.com/docker/compose.git
Add default labels to networks and volumes created by Compose
Signed-off-by: Joffrey F <joffrey@docker.com>
This commit is contained in:
parent
e6b2949edc
commit
ba47fb99ba
|
@ -11,7 +11,9 @@ LABEL_CONTAINER_NUMBER = 'com.docker.compose.container-number'
|
|||
LABEL_ONE_OFF = 'com.docker.compose.oneoff'
|
||||
LABEL_PROJECT = 'com.docker.compose.project'
|
||||
LABEL_SERVICE = 'com.docker.compose.service'
|
||||
LABEL_NETWORK = 'com.docker.compose.network'
|
||||
LABEL_VERSION = 'com.docker.compose.version'
|
||||
LABEL_VOLUME = 'com.docker.compose.volume'
|
||||
LABEL_CONFIG_HASH = 'com.docker.compose.config-hash'
|
||||
|
||||
COMPOSEFILE_V1 = '1'
|
||||
|
|
|
@ -7,8 +7,11 @@ from docker.errors import NotFound
|
|||
from docker.types import IPAMConfig
|
||||
from docker.types import IPAMPool
|
||||
from docker.utils import version_gte
|
||||
from docker.utils import version_lt
|
||||
|
||||
from .config import ConfigurationError
|
||||
from .const import LABEL_NETWORK
|
||||
from .const import LABEL_PROJECT
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
@ -72,8 +75,8 @@ class Network(object):
|
|||
ipam=self.ipam,
|
||||
internal=self.internal,
|
||||
enable_ipv6=self.enable_ipv6,
|
||||
labels=self.labels,
|
||||
attachable=version_gte(self.client._version, '1.24') or None
|
||||
labels=self._labels,
|
||||
attachable=version_gte(self.client._version, '1.24') or None,
|
||||
)
|
||||
|
||||
def remove(self):
|
||||
|
@ -93,6 +96,17 @@ class Network(object):
|
|||
return self.external_name
|
||||
return '{0}_{1}'.format(self.project, self.name)
|
||||
|
||||
@property
|
||||
def _labels(self):
|
||||
if version_lt(self.client._version, '1.23'):
|
||||
return None
|
||||
labels = self.labels.copy() if self.labels else {}
|
||||
labels.update({
|
||||
LABEL_PROJECT: self.project,
|
||||
LABEL_NETWORK: self.name,
|
||||
})
|
||||
return labels
|
||||
|
||||
|
||||
def create_ipam_config_from_dict(ipam_dict):
|
||||
if not ipam_dict:
|
||||
|
|
|
@ -4,8 +4,11 @@ from __future__ import unicode_literals
|
|||
import logging
|
||||
|
||||
from docker.errors import NotFound
|
||||
from docker.utils import version_lt
|
||||
|
||||
from .config import ConfigurationError
|
||||
from .const import LABEL_PROJECT
|
||||
from .const import LABEL_VOLUME
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
@ -23,7 +26,7 @@ class Volume(object):
|
|||
|
||||
def create(self):
|
||||
return self.client.create_volume(
|
||||
self.full_name, self.driver, self.driver_opts, labels=self.labels
|
||||
self.full_name, self.driver, self.driver_opts, labels=self._labels
|
||||
)
|
||||
|
||||
def remove(self):
|
||||
|
@ -53,6 +56,17 @@ class Volume(object):
|
|||
return self.external_name
|
||||
return '{0}_{1}'.format(self.project, self.name)
|
||||
|
||||
@property
|
||||
def _labels(self):
|
||||
if version_lt(self.client._version, '1.23'):
|
||||
return None
|
||||
labels = self.labels.copy() if self.labels else {}
|
||||
labels.update({
|
||||
LABEL_PROJECT: self.project,
|
||||
LABEL_VOLUME: self.name,
|
||||
})
|
||||
return labels
|
||||
|
||||
|
||||
class ProjectVolumes(object):
|
||||
|
||||
|
|
|
@ -850,8 +850,8 @@ class CLITestCase(DockerClientTestCase):
|
|||
]
|
||||
|
||||
assert [n['Name'] for n in networks] == [network_with_label]
|
||||
|
||||
assert networks[0]['Labels'] == {'label_key': 'label_val'}
|
||||
assert 'label_key' in networks[0]['Labels']
|
||||
assert networks[0]['Labels']['label_key'] == 'label_val'
|
||||
|
||||
@v2_1_only()
|
||||
def test_up_with_volume_labels(self):
|
||||
|
@ -870,8 +870,8 @@ class CLITestCase(DockerClientTestCase):
|
|||
]
|
||||
|
||||
assert [v['Name'] for v in volumes] == [volume_with_label]
|
||||
|
||||
assert volumes[0]['Labels'] == {'label_key': 'label_val'}
|
||||
assert 'label_key' in volumes[0]['Labels']
|
||||
assert volumes[0]['Labels']['label_key'] == 'label_val'
|
||||
|
||||
@v2_only()
|
||||
def test_up_no_services(self):
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from .testcases import DockerClientTestCase
|
||||
from compose.const import LABEL_NETWORK
|
||||
from compose.const import LABEL_PROJECT
|
||||
from compose.network import Network
|
||||
|
||||
|
||||
class NetworkTest(DockerClientTestCase):
|
||||
def test_network_default_labels(self):
|
||||
net = Network(self.client, 'composetest', 'foonet')
|
||||
net.ensure()
|
||||
net_data = net.inspect()
|
||||
labels = net_data['Labels']
|
||||
assert labels[LABEL_NETWORK] == net.name
|
||||
assert labels[LABEL_PROJECT] == net.project
|
|
@ -942,8 +942,8 @@ class ProjectTest(DockerClientTestCase):
|
|||
]
|
||||
|
||||
assert [n['Name'] for n in networks] == ['composetest_{}'.format(network_name)]
|
||||
|
||||
assert networks[0]['Labels'] == {'label_key': 'label_val'}
|
||||
assert 'label_key' in networks[0]['Labels']
|
||||
assert networks[0]['Labels']['label_key'] == 'label_val'
|
||||
|
||||
@v2_only()
|
||||
def test_project_up_volumes(self):
|
||||
|
@ -1009,7 +1009,8 @@ class ProjectTest(DockerClientTestCase):
|
|||
|
||||
assert [v['Name'] for v in volumes] == ['composetest_{}'.format(volume_name)]
|
||||
|
||||
assert volumes[0]['Labels'] == {'label_key': 'label_val'}
|
||||
assert 'label_key' in volumes[0]['Labels']
|
||||
assert volumes[0]['Labels']['label_key'] == 'label_val'
|
||||
|
||||
@v2_only()
|
||||
def test_project_up_logging_with_multiple_files(self):
|
||||
|
|
|
@ -4,6 +4,8 @@ from __future__ import unicode_literals
|
|||
from docker.errors import DockerException
|
||||
|
||||
from .testcases import DockerClientTestCase
|
||||
from compose.const import LABEL_PROJECT
|
||||
from compose.const import LABEL_VOLUME
|
||||
from compose.volume import Volume
|
||||
|
||||
|
||||
|
@ -94,3 +96,11 @@ class VolumeTest(DockerClientTestCase):
|
|||
assert vol.exists() is False
|
||||
vol.create()
|
||||
assert vol.exists() is True
|
||||
|
||||
def test_volume_default_labels(self):
|
||||
vol = self.create_volume('volume01')
|
||||
vol.create()
|
||||
vol_data = vol.inspect()
|
||||
labels = vol_data['Labels']
|
||||
assert labels[LABEL_VOLUME] == vol.name
|
||||
assert labels[LABEL_PROJECT] == vol.project
|
||||
|
|
Loading…
Reference in New Issue