Skip to content

Commit 18e61e8

Browse files
committed
Add support for secrets in ContainerSpec
Signed-off-by: Joffrey F <[email protected]>
1 parent f0d8fe0 commit 18e61e8

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

docker/models/services.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ def create(self, image, command=None, **kwargs):
109109
the service to. Default: ``None``.
110110
resources (Resources): Resource limits and reservations.
111111
restart_policy (RestartPolicy): Restart policy for containers.
112+
secrets (list of :py:class:`docker.types.SecretReference`): List
113+
of secrets accessible to containers for this service.
112114
stop_grace_period (int): Amount of time to wait for
113115
containers to terminate before forcefully killing them.
114116
update_config (UpdateConfig): Specification for the update strategy
@@ -179,6 +181,7 @@ def list(self, **kwargs):
179181
'labels',
180182
'mounts',
181183
'stop_grace_period',
184+
'secrets',
182185
]
183186

184187
# kwargs to copy straight over to TaskTemplate

docker/types/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
from .networks import EndpointConfig, IPAMConfig, IPAMPool, NetworkingConfig
55
from .services import (
66
ContainerSpec, DriverConfig, EndpointSpec, Mount, Resources, RestartPolicy,
7-
ServiceMode, TaskTemplate, UpdateConfig
7+
SecretReference, ServiceMode, TaskTemplate, UpdateConfig
88
)
99
from .swarm import SwarmSpec, SwarmExternalCA

docker/types/services.py

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

33
from .. import errors
44
from ..constants import IS_WINDOWS_PLATFORM
5-
from ..utils import format_environment, split_command
5+
from ..utils import check_resource, format_environment, split_command
66

77

88
class TaskTemplate(dict):
@@ -79,9 +79,12 @@ class ContainerSpec(dict):
7979
:py:class:`~docker.types.Mount` class for details.
8080
stop_grace_period (int): Amount of time to wait for the container to
8181
terminate before forcefully killing it.
82+
secrets (list of py:class:`SecretReference`): List of secrets to be
83+
made available inside the containers.
8284
"""
8385
def __init__(self, image, command=None, args=None, env=None, workdir=None,
84-
user=None, labels=None, mounts=None, stop_grace_period=None):
86+
user=None, labels=None, mounts=None, stop_grace_period=None,
87+
secrets=None):
8588
self['Image'] = image
8689

8790
if isinstance(command, six.string_types):
@@ -109,6 +112,11 @@ def __init__(self, image, command=None, args=None, env=None, workdir=None,
109112
if stop_grace_period is not None:
110113
self['StopGracePeriod'] = stop_grace_period
111114

115+
if secrets is not None:
116+
if not isinstance(secrets, list):
117+
raise TypeError('secrets must be a list')
118+
self['Secrets'] = secrets
119+
112120

113121
class Mount(dict):
114122
"""
@@ -410,3 +418,31 @@ def replicas(self):
410418
if self.mode != 'replicated':
411419
return None
412420
return self['replicated'].get('Replicas')
421+
422+
423+
class SecretReference(dict):
424+
"""
425+
Secret reference to be used as part of a :py:class:`ContainerSpec`.
426+
Describes how a secret is made accessible inside the service's
427+
containers.
428+
429+
Args:
430+
secret_id (string): Secret's ID
431+
secret_name (string): Secret's name as defined at its creation.
432+
filename (string): Name of the file containing the secret. Defaults
433+
to the secret's name if not specified.
434+
uid (string): UID of the secret file's owner. Default: 0
435+
gid (string): GID of the secret file's group. Default: 0
436+
mode (int): File access mode inside the container. Default: 0o444
437+
"""
438+
@check_resource
439+
def __init__(self, secret_id, secret_name, filename=None, uid=None,
440+
gid=None, mode=0o444):
441+
self['SecretName'] = secret_name
442+
self['SecretID'] = secret_id
443+
self['File'] = {
444+
'Name': filename or secret_name,
445+
'UID': uid or '0',
446+
'GID': gid or '0',
447+
'Mode': mode
448+
}

docker/utils/decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def wrapped(self, resource_id=None, *args, **kwargs):
1616
resource_id = resource_id.get('Id', resource_id.get('ID'))
1717
if not resource_id:
1818
raise errors.NullResource(
19-
'image or container param is undefined'
19+
'Resource ID was not provided'
2020
)
2121
return f(self, resource_id, *args, **kwargs)
2222
return wrapped

0 commit comments

Comments
 (0)