Skip to content

Commit 0e8fc63

Browse files
committed
Merge pull request #532 from aanand/labels
Labels
2 parents 645c84e + bd72bd1 commit 0e8fc63

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

docker/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
if not six.PY3:
3535
import websocket
3636

37-
DEFAULT_DOCKER_API_VERSION = '1.17'
37+
DEFAULT_DOCKER_API_VERSION = '1.18'
3838
DEFAULT_TIMEOUT_SECONDS = 60
3939
STREAM_HEADER_SIZE_BYTES = 8
4040

@@ -444,7 +444,7 @@ def create_container(self, image, command=None, hostname=None, user=None,
444444
network_disabled=False, name=None, entrypoint=None,
445445
cpu_shares=None, working_dir=None, domainname=None,
446446
memswap_limit=0, cpuset=None, host_config=None,
447-
mac_address=None):
447+
mac_address=None, labels=None):
448448

449449
if isinstance(volumes, six.string_types):
450450
volumes = [volumes, ]
@@ -458,7 +458,7 @@ def create_container(self, image, command=None, hostname=None, user=None,
458458
self._version, image, command, hostname, user, detach, stdin_open,
459459
tty, mem_limit, ports, environment, dns, volumes, volumes_from,
460460
network_disabled, entrypoint, cpu_shares, working_dir, domainname,
461-
memswap_limit, cpuset, host_config, mac_address
461+
memswap_limit, cpuset, host_config, mac_address, labels
462462
)
463463
return self.create_container_from_config(config, name)
464464

docker/utils/utils.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,8 @@ def create_container_config(
443443
stdin_open=False, tty=False, mem_limit=0, ports=None, environment=None,
444444
dns=None, volumes=None, volumes_from=None, network_disabled=False,
445445
entrypoint=None, cpu_shares=None, working_dir=None, domainname=None,
446-
memswap_limit=0, cpuset=None, host_config=None, mac_address=None
446+
memswap_limit=0, cpuset=None, host_config=None, mac_address=None,
447+
labels=None
447448
):
448449
if isinstance(command, six.string_types):
449450
command = shlex.split(str(command))
@@ -453,6 +454,14 @@ def create_container_config(
453454
for k, v in six.iteritems(environment)
454455
]
455456

457+
if labels is not None and compare_version('1.18', version) < 0:
458+
raise errors.DockerException(
459+
'labels were only introduced in API version 1.18'
460+
)
461+
462+
if isinstance(labels, list):
463+
labels = dict((lbl, six.text_type('')) for lbl in labels)
464+
456465
if isinstance(mem_limit, six.string_types):
457466
mem_limit = parse_bytes(mem_limit)
458467
if isinstance(memswap_limit, six.string_types):
@@ -532,5 +541,6 @@ def create_container_config(
532541
'WorkingDir': working_dir,
533542
'MemorySwap': memswap_limit,
534543
'HostConfig': host_config,
535-
'MacAddress': mac_address
544+
'MacAddress': mac_address,
545+
'Labels': labels
536546
}

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ from. Optionally a single string joining container id's with commas
209209
* memswap_limit (int):
210210
* host_config (dict): A [HostConfig](hostconfig.md) dictionary
211211
* mac_address (str): The Mac Address to assign the container
212+
* labels (dict or list): A dictionary of name-value labels (e.g. `{"label1": "value1", "label2": "value2"}`) or a list of names of labels to set with empty values (e.g. `["label1", "label2"]`)
212213

213214
**Returns** (dict): A dictionary with an image 'Id' key and a 'Warnings' key.
214215

tests/fake_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import fake_stat
1616

17-
CURRENT_VERSION = 'v1.17'
17+
CURRENT_VERSION = 'v1.18'
1818

1919
FAKE_CONTAINER_ID = '3cc2351ab11b'
2020
FAKE_IMAGE_ID = 'e9aa60c60128'
@@ -33,7 +33,7 @@
3333
def get_fake_raw_version():
3434
status_code = 200
3535
response = {
36-
"ApiVersion": "1.17",
36+
"ApiVersion": "1.18",
3737
"GitCommit": "fake-commit",
3838
"GoVersion": "go1.3.3",
3939
"Version": "1.5.0"

tests/test.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,54 @@ def test_create_container_with_devices(self):
13051305
args[1]['timeout'], docker.client.DEFAULT_TIMEOUT_SECONDS
13061306
)
13071307

1308+
def test_create_container_with_labels_dict(self):
1309+
labels_dict = {
1310+
six.text_type('foo'): six.text_type('1'),
1311+
six.text_type('bar'): six.text_type('2'),
1312+
}
1313+
try:
1314+
self.client.create_container(
1315+
'busybox', 'true',
1316+
labels=labels_dict,
1317+
)
1318+
except Exception as e:
1319+
self.fail('Command should not raise exception: {0}'.format(e))
1320+
args = fake_request.call_args
1321+
self.assertEqual(args[0][0], url_prefix + 'containers/create')
1322+
self.assertEqual(json.loads(args[1]['data'])['Labels'], labels_dict)
1323+
self.assertEqual(
1324+
args[1]['headers'], {'Content-Type': 'application/json'}
1325+
)
1326+
self.assertEqual(
1327+
args[1]['timeout'], docker.client.DEFAULT_TIMEOUT_SECONDS
1328+
)
1329+
1330+
def test_create_container_with_labels_list(self):
1331+
labels_list = [
1332+
six.text_type('foo'),
1333+
six.text_type('bar'),
1334+
]
1335+
labels_dict = {
1336+
six.text_type('foo'): six.text_type(),
1337+
six.text_type('bar'): six.text_type(),
1338+
}
1339+
try:
1340+
self.client.create_container(
1341+
'busybox', 'true',
1342+
labels=labels_list,
1343+
)
1344+
except Exception as e:
1345+
self.fail('Command should not raise exception: {0}'.format(e))
1346+
args = fake_request.call_args
1347+
self.assertEqual(args[0][0], url_prefix + 'containers/create')
1348+
self.assertEqual(json.loads(args[1]['data'])['Labels'], labels_dict)
1349+
self.assertEqual(
1350+
args[1]['headers'], {'Content-Type': 'application/json'}
1351+
)
1352+
self.assertEqual(
1353+
args[1]['timeout'], docker.client.DEFAULT_TIMEOUT_SECONDS
1354+
)
1355+
13081356
def test_resize_container(self):
13091357
try:
13101358
self.client.resize(

0 commit comments

Comments
 (0)