Skip to content

Commit

Permalink
Improve check for duplicate tag versions of all image components (#2147)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhoherd committed Mar 20, 2024
1 parent d0b2fe2 commit 189ba75
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 59 deletions.
19 changes: 11 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ exclude: '(venv|\.vscode|tests/k8s_schema)' # regex
repos:
- repo: local
hooks:
- id: verify-image-tags
name: Verify image tags
- id: check-image-tags
language: python
additional_dependencies: ["jinja2", "PyYAML"]
name: Ensure unique tags per image
entry: python3
args: [bin/verify_image_tags.py]
language: system
files: ^.*\.(tpl|yaml)$
args: ["bin/show-docker-images.py", "--private-registry", "--with-houston", "-c"]
files: '(config.yml|config.yml.j2|generate_circleci_config.py|yaml|tpl)$'
pass_filenames: false
- id: circle-config-yaml
name: Checks for consistency between config.yml and config.yml.j2
language: python
files: '(config.yml|config.yml.j2|generate_circleci_config.py|values.yaml|metadata.yaml)$'
entry: bin/generate_circleci_config.py
additional_dependencies: ["jinja2", "PyYAML"]
name: Checks for consistency between config.yml and config.yml.j2
entry: bin/generate_circleci_config.py
files: '(config.yml|config.yml.j2|generate_circleci_config.py|yaml|tpl)$'
pass_filenames: false
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
Expand Down
22 changes: 22 additions & 0 deletions bin/show-docker-images.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import sys
import json

from collections import defaultdict


def get_containers_from_spec(spec):
"""Return a list of images used in a kubernetes pod spec."""
Expand Down Expand Up @@ -120,6 +122,12 @@ def main():
action="store_true",
help="include images from houston configmap",
)
parser.add_argument(
"-c",
"--check-tags",
action="store_true",
help="only check for multiple tags for the same image, do not print the images",
)
args = parser.parse_args()

docs = helm_template(args)
Expand All @@ -140,6 +148,20 @@ def main():
case _:
pass

if args.check_tags:
image_tag_counts = defaultdict(int)
for x, y in [
(tag.rpartition(":")[0], tag.rpartition(":")[2]) for tag in containers
]:
image_tag_counts[x] += 1
for image, count in image_tag_counts.items():
if count > 1:
print(
f"ALERT: {image} has {count} different tags: {', '.join([c.rpartition(':')[-1] for c in containers if image in c])}"
)
# Exit with a return code equal to the number of images with multiple tags
raise SystemExit(len([y for x, y in image_tag_counts.items() if y > 1]))

print_results(sorted(containers))


Expand Down
51 changes: 0 additions & 51 deletions bin/verify_image_tags.py

This file was deleted.

0 comments on commit 189ba75

Please sign in to comment.