Add patterns support for exclude-image-file

This commit is contained in:
Viktor Shlapakov 2016-10-03 16:41:14 +03:00
parent 2375af1513
commit cd4121cb59
2 changed files with 46 additions and 1 deletions

View File

@ -4,6 +4,7 @@ Remove old docker containers and images that are no longer in use.
"""
import argparse
import fnmatch
import logging
import sys
@ -88,7 +89,10 @@ def filter_excluded_images(images, exclude_set):
image_tags = image_summary.get('RepoTags')
if no_image_tags(image_tags):
return True
return not set(image_tags) & exclude_set
for exclude_pattern in exclude_set:
if fnmatch.filter(image_tags, exclude_pattern):
return False
return True
return filter(include_image, images)

View File

@ -177,6 +177,47 @@ def test_filter_excluded_images():
assert list(actual) == expected
def test_filter_excluded_images_advanced():
exclude_set = set([
'user/one:*',
'user/foo:tag*',
'user/repo-*:tag',
])
images = [
{
'RepoTags': ['<none>:<none>'],
'Id': 'babababababaabababab'
},
{
'RepoTags': ['user/one:latest', 'user/one:abcd']
},
{
'RepoTags': ['user/foo:test']
},
{
'RepoTags': ['user/foo:tag123']
},
{
'RepoTags': ['user/repo-1:tag']
},
{
'RepoTags': ['user/repo-2:tag']
},
]
expected = [
{
'RepoTags': ['<none>:<none>'],
'Id': 'babababababaabababab'
},
{
'RepoTags': ['user/foo:test'],
},
]
actual = docker_gc.filter_excluded_images(images, exclude_set)
assert list(actual) == expected
def test_is_image_old(image, now):
assert docker_gc.is_image_old(image, now)