Merge pull request #5585 from docker/nginth-4904-label-config-validation

Labels validation and basic type conversion
This commit is contained in:
Joffrey F 2018-01-19 14:14:07 -08:00 committed by GitHub
commit b2cc8a290a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 316 additions and 56 deletions

View File

@ -78,7 +78,7 @@
"hostname": {"type": "string"},
"image": {"type": "string"},
"ipc": {"type": "string"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"links": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
"log_driver": {"type": "string"},
"log_opt": {"type": "object"},
@ -166,6 +166,21 @@
]
},
"labels": {
"oneOf": [
{
"type": "object",
"patternProperties": {
".+": {
"type": "string"
}
},
"additionalProperties": false
},
{"type": "array", "items": {"type": "string"}, "uniqueItems": true}
]
},
"constraints": {
"service": {
"id": "#/definitions/constraints/service",

View File

@ -158,7 +158,7 @@
"hostname": {"type": "string"},
"image": {"type": "string"},
"ipc": {"type": "string"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"links": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
"logging": {
@ -355,6 +355,21 @@
]
},
"labels": {
"oneOf": [
{
"type": "object",
"patternProperties": {
".+": {
"type": "string"
}
},
"additionalProperties": false
},
{"type": "array", "items": {"type": "string"}, "uniqueItems": true}
]
},
"blkio_limit": {
"type": "object",
"properties": {

View File

@ -88,7 +88,7 @@
"context": {"type": "string"},
"dockerfile": {"type": "string"},
"args": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
}
@ -183,7 +183,7 @@
"image": {"type": "string"},
"ipc": {"type": "string"},
"isolation": {"type": "string"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"links": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
"logging": {
@ -351,7 +351,7 @@
},
"internal": {"type": "boolean"},
"enable_ipv6": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"name": {"type": "string"}
},
"additionalProperties": false
@ -375,7 +375,7 @@
},
"additionalProperties": false
},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"name": {"type": "string"}
},
"additionalProperties": false
@ -409,6 +409,21 @@
]
},
"labels": {
"oneOf": [
{
"type": "object",
"patternProperties": {
".+": {
"type": "string"
}
},
"additionalProperties": false
},
{"type": "array", "items": {"type": "string"}, "uniqueItems": true}
]
},
"blkio_limit": {
"type": "object",
"properties": {

View File

@ -88,7 +88,7 @@
"context": {"type": "string"},
"dockerfile": {"type": "string"},
"args": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"cache_from": {"$ref": "#/definitions/list_of_strings"},
"network": {"type": "string"}
},
@ -189,7 +189,7 @@
"init": {"type": ["boolean", "string"]},
"ipc": {"type": "string"},
"isolation": {"type": "string"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"links": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
"logging": {
@ -358,7 +358,7 @@
},
"internal": {"type": "boolean"},
"enable_ipv6": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"name": {"type": "string"}
},
"additionalProperties": false
@ -382,7 +382,7 @@
},
"additionalProperties": false
},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"name": {"type": "string"}
},
"additionalProperties": false
@ -416,6 +416,21 @@
]
},
"labels": {
"oneOf": [
{
"type": "object",
"patternProperties": {
".+": {
"type": "string"
}
},
"additionalProperties": false
},
{"type": "array", "items": {"type": "string"}, "uniqueItems": true}
]
},
"blkio_limit": {
"type": "object",
"properties": {

View File

@ -88,7 +88,7 @@
"context": {"type": "string"},
"dockerfile": {"type": "string"},
"args": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"cache_from": {"$ref": "#/definitions/list_of_strings"},
"network": {"type": "string"},
"target": {"type": "string"},
@ -192,7 +192,7 @@
"init": {"type": ["boolean", "string"]},
"ipc": {"type": "string"},
"isolation": {"type": "string"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"links": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
"logging": {
@ -395,7 +395,7 @@
},
"internal": {"type": "boolean"},
"enable_ipv6": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"name": {"type": "string"}
},
"additionalProperties": false
@ -419,7 +419,7 @@
},
"additionalProperties": false
},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"name": {"type": "string"}
},
"additionalProperties": false
@ -453,6 +453,21 @@
]
},
"labels": {
"oneOf": [
{
"type": "object",
"patternProperties": {
".+": {
"type": "string"
}
},
"additionalProperties": false
},
{"type": "array", "items": {"type": "string"}, "uniqueItems": true}
]
},
"blkio_limit": {
"type": "object",
"properties": {

View File

@ -105,7 +105,7 @@
"hostname": {"type": "string"},
"image": {"type": "string"},
"ipc": {"type": "string"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"links": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
"logging": {
@ -223,7 +223,7 @@
"properties": {
"mode": {"type": "string"},
"replicas": {"type": "integer"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"update_config": {
"type": "object",
"properties": {
@ -310,7 +310,7 @@
"additionalProperties": false
},
"internal": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -333,7 +333,7 @@
},
"additionalProperties": false
},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -366,6 +366,21 @@
]
},
"labels": {
"oneOf": [
{
"type": "object",
"patternProperties": {
".+": {
"type": "string"
}
},
"additionalProperties": false
},
{"type": "array", "items": {"type": "string"}, "uniqueItems": true}
]
},
"constraints": {
"service": {
"id": "#/definitions/constraints/service",

View File

@ -116,7 +116,7 @@
"hostname": {"type": "string"},
"image": {"type": "string"},
"ipc": {"type": "string"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"links": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
"logging": {
@ -252,7 +252,7 @@
"properties": {
"mode": {"type": "string"},
"replicas": {"type": "integer"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"update_config": {
"type": "object",
"properties": {
@ -339,7 +339,7 @@
"additionalProperties": false
},
"internal": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -362,7 +362,7 @@
},
"additionalProperties": false
},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -378,7 +378,7 @@
"name": {"type": "string"}
}
},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -411,6 +411,21 @@
]
},
"labels": {
"oneOf": [
{
"type": "object",
"patternProperties": {
".+": {
"type": "string"
}
},
"additionalProperties": false
},
{"type": "array", "items": {"type": "string"}, "uniqueItems": true}
]
},
"constraints": {
"service": {
"id": "#/definitions/constraints/service",

View File

@ -118,7 +118,7 @@
"hostname": {"type": "string"},
"image": {"type": "string"},
"ipc": {"type": "string"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"links": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
"logging": {
@ -299,7 +299,7 @@
"mode": {"type": "string"},
"endpoint_mode": {"type": "string"},
"replicas": {"type": "integer"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"update_config": {
"type": "object",
"properties": {
@ -387,7 +387,7 @@
},
"internal": {"type": "boolean"},
"attachable": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -410,7 +410,7 @@
},
"additionalProperties": false
},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -426,7 +426,7 @@
"name": {"type": "string"}
}
},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -459,6 +459,21 @@
]
},
"labels": {
"oneOf": [
{
"type": "object",
"patternProperties": {
".+": {
"type": "string"
}
},
"additionalProperties": false
},
{"type": "array", "items": {"type": "string"}, "uniqueItems": true}
]
},
"constraints": {
"service": {
"id": "#/definitions/constraints/service",

View File

@ -83,7 +83,7 @@
"context": {"type": "string"},
"dockerfile": {"type": "string"},
"args": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"cache_from": {"$ref": "#/definitions/list_of_strings"}
},
"additionalProperties": false
@ -151,7 +151,7 @@
"hostname": {"type": "string"},
"image": {"type": "string"},
"ipc": {"type": "string"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"links": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
"logging": {
@ -332,7 +332,7 @@
"mode": {"type": "string"},
"endpoint_mode": {"type": "string"},
"replicas": {"type": "integer"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"update_config": {
"type": "object",
"properties": {
@ -430,7 +430,7 @@
},
"internal": {"type": "boolean"},
"attachable": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -453,7 +453,7 @@
},
"additionalProperties": false
},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -469,7 +469,7 @@
"name": {"type": "string"}
}
},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -485,7 +485,7 @@
"name": {"type": "string"}
}
},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -518,6 +518,21 @@
]
},
"labels": {
"oneOf": [
{
"type": "object",
"patternProperties": {
".+": {
"type": "string"
}
},
"additionalProperties": false
},
{"type": "array", "items": {"type": "string"}, "uniqueItems": true}
]
},
"constraints": {
"service": {
"id": "#/definitions/constraints/service",

View File

@ -85,7 +85,7 @@
"context": {"type": "string"},
"dockerfile": {"type": "string"},
"args": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"cache_from": {"$ref": "#/definitions/list_of_strings"},
"network": {"type": "string"},
"target": {"type": "string"}
@ -155,7 +155,7 @@
"hostname": {"type": "string"},
"image": {"type": "string"},
"ipc": {"type": "string"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"links": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
"logging": {
@ -337,7 +337,7 @@
"mode": {"type": "string"},
"endpoint_mode": {"type": "string"},
"replicas": {"type": "integer"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"update_config": {
"type": "object",
"properties": {
@ -438,7 +438,7 @@
},
"internal": {"type": "boolean"},
"attachable": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -462,7 +462,7 @@
},
"additionalProperties": false
},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -478,7 +478,7 @@
"name": {"type": "string"}
}
},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -494,7 +494,7 @@
"name": {"type": "string"}
}
},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -527,6 +527,21 @@
]
},
"labels": {
"oneOf": [
{
"type": "object",
"patternProperties": {
".+": {
"type": "string"
}
},
"additionalProperties": false
},
{"type": "array", "items": {"type": "string"}, "uniqueItems": true}
]
},
"constraints": {
"service": {
"id": "#/definitions/constraints/service",

View File

@ -84,7 +84,7 @@
"context": {"type": "string"},
"dockerfile": {"type": "string"},
"args": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"cache_from": {"$ref": "#/definitions/list_of_strings"},
"network": {"type": "string"},
"target": {"type": "string"},
@ -156,7 +156,7 @@
"image": {"type": "string"},
"ipc": {"type": "string"},
"isolation": {"type": "string"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"links": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
"logging": {
@ -338,7 +338,7 @@
"mode": {"type": "string"},
"endpoint_mode": {"type": "string"},
"replicas": {"type": "integer"},
"labels": {"$ref": "#/definitions/list_or_dict"},
"labels": {"$ref": "#/definitions/labels"},
"update_config": {
"type": "object",
"properties": {
@ -464,7 +464,7 @@
},
"internal": {"type": "boolean"},
"attachable": {"type": "boolean"},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -488,7 +488,7 @@
},
"additionalProperties": false
},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -505,7 +505,7 @@
"name": {"type": "string"}
}
},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -522,7 +522,7 @@
"name": {"type": "string"}
}
},
"labels": {"$ref": "#/definitions/list_or_dict"}
"labels": {"$ref": "#/definitions/labels"}
},
"additionalProperties": false
},
@ -555,6 +555,21 @@
]
},
"labels": {
"oneOf": [
{
"type": "object",
"patternProperties": {
".+": {
"type": "string"
}
},
"additionalProperties": false
},
{"type": "array", "items": {"type": "string"}, "uniqueItems": true}
]
},
"constraints": {
"service": {
"id": "#/definitions/constraints/service",

View File

@ -84,7 +84,7 @@ def recursive_interpolate(obj, interpolator, config_path):
)
if isinstance(obj, list):
return [recursive_interpolate(val, interpolator, config_path) for val in obj]
return obj
return converter.convert(config_path, obj)
class TemplateWithDefaults(Template):
@ -160,10 +160,11 @@ class UnsetRequiredSubstitution(Exception):
PATH_JOKER = '[^.]+'
FULL_JOKER = '.+'
def re_path(*args):
return re.compile('^{}$'.format('.'.join(args)))
return re.compile('^{}$'.format('\.'.join(args)))
def re_path_basic(section, name):
@ -175,6 +176,8 @@ def service_path(*args):
def to_boolean(s):
if not isinstance(s, six.string_types):
return s
s = s.lower()
if s in ['y', 'yes', 'true', 'on']:
return True
@ -184,6 +187,9 @@ def to_boolean(s):
def to_int(s):
if not isinstance(s, six.string_types):
return s
# We must be able to handle octal representation for `mode` values notably
if six.PY3 and re.match('^0[0-9]+$', s.strip()):
s = '0o' + s[1:]
@ -194,27 +200,39 @@ def to_int(s):
def to_float(s):
if not isinstance(s, six.string_types):
return s
try:
return float(s)
except ValueError:
raise ValueError('"{}" is not a valid float'.format(s))
def to_str(o):
if isinstance(o, (bool, float, int)):
return '{}'.format(o)
return o
class ConversionMap(object):
map = {
service_path('blkio_config', 'weight'): to_int,
service_path('blkio_config', 'weight_device', 'weight'): to_int,
service_path('build', 'labels', FULL_JOKER): to_str,
service_path('cpus'): to_float,
service_path('cpu_count'): to_int,
service_path('configs', 'mode'): to_int,
service_path('secrets', 'mode'): to_int,
service_path('healthcheck', 'retries'): to_int,
service_path('healthcheck', 'disable'): to_boolean,
service_path('deploy', 'labels', PATH_JOKER): to_str,
service_path('deploy', 'replicas'): to_int,
service_path('deploy', 'update_config', 'parallelism'): to_int,
service_path('deploy', 'update_config', 'max_failure_ratio'): to_float,
service_path('deploy', 'restart_policy', 'max_attempts'): to_int,
service_path('mem_swappiness'): to_int,
service_path('labels', FULL_JOKER): to_str,
service_path('oom_kill_disable'): to_boolean,
service_path('oom_score_adj'): to_int,
service_path('ports', 'target'): to_int,
@ -232,9 +250,13 @@ class ConversionMap(object):
re_path_basic('network', 'attachable'): to_boolean,
re_path_basic('network', 'external'): to_boolean,
re_path_basic('network', 'internal'): to_boolean,
re_path('network', PATH_JOKER, 'labels', FULL_JOKER): to_str,
re_path_basic('volume', 'external'): to_boolean,
re_path('volume', PATH_JOKER, 'labels', FULL_JOKER): to_str,
re_path_basic('secret', 'external'): to_boolean,
re_path('secret', PATH_JOKER, 'labels', FULL_JOKER): to_str,
re_path_basic('config', 'external'): to_boolean,
re_path('config', PATH_JOKER, 'labels', FULL_JOKER): to_str,
}
def convert(self, path, value):

View File

@ -563,7 +563,7 @@ class ConfigTest(unittest.TestCase):
'services': {
'web': {
'build': {
'context': '.',
'context': os.getcwd(),
'args': None,
},
},
@ -959,7 +959,7 @@ class ConfigTest(unittest.TestCase):
).services[0]
assert 'labels' in service['build']
assert 'label1' in service['build']['labels']
assert service['build']['labels']['label1'] == 42
assert service['build']['labels']['label1'] == '42'
assert service['build']['labels']['label2'] == 'foobar'
def test_load_build_labels_list(self):
@ -2747,6 +2747,62 @@ class ConfigTest(unittest.TestCase):
]
assert service_sort(service_dicts) == service_sort(expected)
def test_config_convertible_label_types(self):
config_details = build_config_details(
{
'version': '3.5',
'services': {
'web': {
'build': {
'labels': {'testbuild': True},
'context': os.getcwd()
},
'labels': {
"key": 12345
}
},
},
'networks': {
'foo': {
'labels': {'network.ips.max': 1023}
}
},
'volumes': {
'foo': {
'labels': {'volume.is_readonly': False}
}
},
'secrets': {
'foo': {
'labels': {'secret.data.expires': 1546282120}
}
},
'configs': {
'foo': {
'labels': {'config.data.correction.value': -0.1412}
}
}
}
)
loaded_config = config.load(config_details)
assert loaded_config.services[0]['build']['labels'] == {'testbuild': 'True'}
assert loaded_config.services[0]['labels'] == {'key': '12345'}
assert loaded_config.networks['foo']['labels']['network.ips.max'] == '1023'
assert loaded_config.volumes['foo']['labels']['volume.is_readonly'] == 'False'
assert loaded_config.secrets['foo']['labels']['secret.data.expires'] == '1546282120'
assert loaded_config.configs['foo']['labels']['config.data.correction.value'] == '-0.1412'
def test_config_invalid_label_types(self):
config_details = build_config_details({
'version': '2.3',
'volumes': {
'foo': {'labels': [1, 2, 3]}
}
})
with pytest.raises(ConfigurationError):
config.load(config_details)
def test_service_volume_invalid_config(self):
config_details = build_config_details(
{
@ -2766,14 +2822,31 @@ class ConfigTest(unittest.TestCase):
}
}
]
},
},
}
}
}
)
with pytest.raises(ConfigurationError) as exc:
config.load(config_details)
assert "services.web.volumes contains unsupported option: 'garbage'" in exc.exconly()
def test_config_valid_service_label_validation(self):
config_details = build_config_details(
{
'version': '3.5',
'services': {
'web': {
'image': 'busybox',
'labels': {
"key": "string"
}
},
},
}
)
config.load(config_details)
class NetworkModeTest(unittest.TestCase):

View File

@ -109,7 +109,7 @@ def test_interpolate_environment_variables_in_secrets(mock_env):
'secretservice': {
'file': 'bar',
'labels': {
'max': 2,
'max': '2',
'user': 'jenny'
}
},