dockerng keeps restarting privileged container
Description of Issue/Question
Setup
container_calico_node:
__env__: base
__sls__: docker.containers
dockerng:
- name: calico_node
- environment:
- ... stripped ...
- image: quay.io/calico/node:latest
- labels:
- net
- infra
- restart_policy: always
- network_mode: host
- binds:
- /var/log/calico:/var/log/calico
- /var/run/calico:/var/run/calico
- /lib/modules:/lib/modules
- /run/docker/plugins:/run/docker/plugins
- /var/run/docker.sock:/var/run/docker.sock
- memory: 1G
- detach: true
- privileged: true
- require:
- pip: dockerng_requirements
- service: docker-engine-service
- running
- order: 10015
Steps to Reproduce Issue
Docker (1.13.1) container created using the above SLS (with privileged: true) has label=disable security option set.
"SecurityOpt": [
"label=disable"
],
This causes dockerng state to consider the container in need of change (removal of the security-opt) and recreates the container. This ends in error:
Comment: Failed to apply configuration for the following parameters:
Minion log:
2017-02-16 12:19:29,845 [salt.minion ][DEBUG ][1214] Minion of "salt.fg.local" is handling event tag '_minion_mine'
2017-02-16 12:19:29,845 [salt.transport.zeromq][DEBUG ][1214] Initializing new AsyncZeroMQReqChannel for ('/etc/salt/pki/minion', 'fg-cml-prod-1', 'tcp://192.168.93.188:4506', 'aes')
2017-02-16 12:19:29,846 [salt.crypt ][DEBUG ][1214] Initializing new AsyncAuth for ('/etc/salt/pki/minion', 'fg-cml-prod-1', 'tcp://192.168.93.188:4506')
2017-02-16 12:19:30,354 [requests.packages.urllib3.connectionpool][DEBUG ][44476] "GET /v1.26/containers/calico_node/json HTTP/1.1" 200 None
2017-02-16 12:19:30,358 [requests.packages.urllib3.connectionpool][DEBUG ][44476] "GET /v1.26/images/sha256:6c01375f3fad33e4aadf93785a1393df68a18acc6b363864305005d4ba107d2d/json HTTP/1.1" 200 1970
2017-02-16 12:19:30,367 [salt.loaded.int.states.dockerng][DEBUG ][44476] dockerng.running: Analysis of container 'calico_node' after creation/replacement reveals the following changes still need to be made: {'security_opt': {'new': None, 'old': [u'label=disable']}}
2017-02-16 12:19:30,368 [salt.state ][ERROR ][44476] {'diff': {}, 'removed': [u'48123d0430beb71b58a895f686ef3970916871fad9290e6bd5dcb8b77dcc3701'], 'added': {'Time_Elapsed': 0.06841683387756348, 'Name': 'calico_node', u'Id': u'2b144a4994976a580a7f3e48d1efa745138d056220c09d5812d682c3a546ca4e', u'Warnings': None}}
2017-02-16 12:19:30,368 [salt.state ][INFO ][44476] Completed state [calico_node] at time 12:19:30.368324 duration_in_ms=2964.466
I tried to work around by manually adding - security_opt: label=disable to the SLS, but this causes the container to be created with two label=disable security options.
"SecurityOpt": [
"label=disable",
"label=disable"
],
And dockerng then tries to recreate the container with just one label=disable.
Versions Report
Both Salt (master and minion), and Docker are installed from official repositories.
Salt Version:
Salt: 2016.11.2
Dependency Versions:
cffi: Not Installed
cherrypy: 3.5.0
dateutil: 2.5.3
gitdb: 2.0.0
gitpython: 2.1.1
ioflo: Not Installed
Jinja2: 2.9.5
libgit2: 0.24.5
libnacl: Not Installed
M2Crypto: 0.24.0
Mako: Not Installed
msgpack-pure: Not Installed
msgpack-python: 0.4.8
mysql-python: Not Installed
pycparser: Not Installed
pycrypto: 2.6.1
pygit2: 0.24.2
Python: 2.7.13 (default, Jan 19 2017, 14:48:08)
python-gnupg: Not Installed
PyYAML: 3.12
PyZMQ: 16.0.2
RAET: Not Installed
smmap: 2.0.1
timelib: Not Installed
Tornado: 4.4.2
ZMQ: 4.2.1
System Versions:
dist: Ubuntu 17.04 zesty
machine: x86_64
release: 4.9.0-15-generic
system: Linux
version: Ubuntu 17.04 zesty
btw. creating the container without - security_opt: label=disable and then adding it after, stops the restarting, until salt recreates the container again (for different reasons).
Hi @Foxlik If it is a side effect of privileged=True, then the diff maker needs to handle this corner case.
check salt.states.dockerng._compare function. you will have to add a new entry
elif item == 'security_opt':
...
@Foxlik were you able to see @ticosax 's comments?
@Ch3LL yeah, it's what I thought, but i've been out of office for the day. Will look into the code tomorrow to see if I can figure out a PR myself.
Well, this is probably just a hack around, but I can't figure out a clean way of doing this:
elif item == 'security_opt':
if actual_data is None:
actual_data = []
if data is None:
data = []
actual_data = sorted(set(actual_data))
desired_data = sorted(set(data))
log.trace('dockerng.running ({0}): munged actual value: {1}'
.format(item, actual_data))
log.trace('dockerng.running ({0}): munged desired value: {1}'
.format(item, desired_data))
if actual_data != desired_data:
ret.update({item: {'old': actual_data,
'new': desired_data}})
continue
Issue with this solution is that it effectively does not care about security_opt being specified multiple times. To be honest, I have no idea if that could make any difference whatsoever. (my guess is that it won't) It also requires, that security_opt: label=disable be specified whenever privileged is used. If one wanted to use dockerng state for removal of duplicate security_opt, this would definitely break things.
Other solutions I could come up with:
- Figuring out which versions of docker add security_opt to privileged containers and on those, add
label=disabledtodata. Though, i don't like the idea of version specific code. - Whenever there is
privileged=true, add"label=disabled"todata. That could restart privileged containers on different versions of docker, and possibly break things, if it's not compatible. - Whenever there is
privileged=true, remove"label=disabled"from bothdataandactual_data, but that could, again, break things for other people/with other versions/other use cases.
But maybe, there is a better solution ...
Taking a bet. I would write it like this
if privileged and 'label=disabled' not in data and 'label=disabled' in actual_data:
actual_data.remove('label=disabled')
Because this is what you observed. docker inserts 'label=disabled' when privileged is True on our behalf, thus we take it out before comparing.
The goal is to be compliant with user input. I think we are achieving it with that little trick, because we don't touch data, except for set conversion.
Having repeated security_opt values is probably not a problem. So I agree with conversion of the list to a set.
I resubmitted @Foxlik's fix against 2016.3 to avoid backporting, and to avoid conflicts with https://github.com/saltstack/salt/pull/39474.
@ticosax If we need to make further changes before this is ready, let me know in the comments for https://github.com/saltstack/salt/pull/39498. Also, I'd love your input on https://github.com/saltstack/salt/pull/39474.
@terminalmage Yes it looks good. this can go in.
I've merged the resubmitted PR into 2016.3. It will be merged forward into 2016.11 and then into the develop branch in the next few days.
Thank you @Foxlik and @ticosax!