Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default labels to networks and volumes created by Compose #4220

Merged
merged 1 commit into from
Dec 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compose/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
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'
Expand Down
18 changes: 16 additions & 2 deletions compose/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
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__)
Expand Down Expand Up @@ -72,8 +75,8 @@ def ensure(self):
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):
Expand All @@ -93,6 +96,17 @@ def full_name(self):
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:
Expand Down
16 changes: 15 additions & 1 deletion compose/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
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__)

Expand All @@ -23,7 +26,7 @@ def __init__(self, client, project, name, driver=None, driver_opts=None,

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):
Expand Down Expand Up @@ -53,6 +56,17 @@ def full_name(self):
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):

Expand Down
8 changes: 4 additions & 4 deletions tests/acceptance/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,8 @@ def test_up_with_network_labels(self):
]

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):
Expand All @@ -870,8 +870,8 @@ def test_up_with_volume_labels(self):
]

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):
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/network_test.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions tests/integration/project_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,8 +942,8 @@ def test_project_up_with_network_label(self):
]

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):
Expand Down Expand Up @@ -1009,7 +1009,8 @@ def test_project_up_with_volume_labels(self):

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):
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/volume_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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


Expand Down Expand Up @@ -94,3 +96,11 @@ def test_exists_external_aliased(self):
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