Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null labels #51

Merged
merged 2 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v1.2.3
rev: v2.2.1
hooks:
- id: check-added-large-files
- id: check-docstring-first
Expand All @@ -14,7 +14,7 @@ repos:
- id: requirements-txt-fixer
- id: trailing-whitespace
- repo: git://github.com/Yelp/detect-secrets
rev: 0.9.1
rev: v0.12.2
hooks:
- id: detect-secrets
args: ['--baseline', '.secrets.baseline']
Expand Down
9 changes: 6 additions & 3 deletions .secrets.baseline
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"exclude_regex": "tests/.*",
"generated_at": "2018-07-12T23:43:31Z",
"exclude": {
"files": "tests/.*",
"lines": null
},
"generated_at": "2019-04-24T14:36:38Z",
"plugins_used": [
{
"base64_limit": 4.5,
Expand All @@ -15,5 +18,5 @@
}
],
"results": {},
"version": "0.9.1"
"version": "0.12.2"
}
34 changes: 19 additions & 15 deletions docker_custodian/docker_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,25 @@ def include_container(container):


def should_exclude_container_with_labels(container, exclude_container_labels):
for exclude_label in exclude_container_labels:
if exclude_label.value:
matching_keys = fnmatch.filter(
container['Labels'].keys(),
exclude_label.key,
)
label_values_to_check = [
container['Labels'][matching_key]
for matching_key in matching_keys
]
if fnmatch.filter(label_values_to_check, exclude_label.value):
return True
else:
if fnmatch.filter(container['Labels'].keys(), exclude_label.key):
return True
if container['Labels']:
for exclude_label in exclude_container_labels:
if exclude_label.value:
matching_keys = fnmatch.filter(
container['Labels'].keys(),
exclude_label.key,
)
label_values_to_check = [
container['Labels'][matching_key]
for matching_key in matching_keys
]
if fnmatch.filter(label_values_to_check, exclude_label.value):
return True
else:
if fnmatch.filter(
container['Labels'].keys(),
exclude_label.key
):
return True
return False


Expand Down
13 changes: 11 additions & 2 deletions tests/docker_gc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def test_filter_excluded_containers():
{'Labels': {'too': 'lol'}},
{'Labels': {'toots': 'lol'}},
{'Labels': {'foo': 'bar'}},
{'Labels': None},
]
result = docker_gc.filter_excluded_containers(mock_containers, None)
assert mock_containers == list(result)
Expand All @@ -86,15 +87,23 @@ def test_filter_excluded_containers():
mock_containers,
exclude_labels,
)
assert [mock_containers[0], mock_containers[2]] == list(result)
assert [
mock_containers[0],
mock_containers[2],
mock_containers[4]
] == list(result)
exclude_labels = [
docker_gc.ExcludeLabel(key='too*', value='lol'),
]
result = docker_gc.filter_excluded_containers(
mock_containers,
exclude_labels,
)
assert [mock_containers[0], mock_containers[3]] == list(result)
assert [
mock_containers[0],
mock_containers[3],
mock_containers[4]
] == list(result)


def test_cleanup_images(mock_client, now):
Expand Down