Skip to content

Commit 6181e80

Browse files
committed
kolla-images.py: Add a check-image-map command
This command checks the image mapping against Kolla Ansible variables. The *_image variables in Kolla Ansible define the mapping between containers and images. Ensure that the mapping defined in this script matches the one in Kolla Ansible.
1 parent 97c5743 commit 6181e80

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

tools/kolla-images.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ def parse_args() -> argparse.Namespace:
9696
parser.add_argument("--base-distros", default=",".join(SUPPORTED_BASE_DISTROS), choices=SUPPORTED_BASE_DISTROS)
9797
subparsers = parser.add_subparsers(dest="command", required=True)
9898

99+
subparser = subparsers.add_parser("check-image-map", help="Check image mapping against kolla-ansible")
100+
subparser.add_argument("--kolla-ansible-path", required=True, help="Path to kolla-ansible repostory checked out to correct branch")
101+
99102
subparser = subparsers.add_parser("check-hierarchy", help="Check tag variable hierarchy against kolla-ansible")
100103
subparser.add_argument("--kolla-ansible-path", required=True, help="Path to kolla-ansible repostory checked out to correct branch")
101104

@@ -277,6 +280,45 @@ def check_tags(base_distros: List[str], kolla_image_tags: KollaImageTags, regist
277280
sys.exit(1)
278281

279282

283+
def check_image_map(kolla_ansible_path: str):
284+
"""Check the image mapping against Kolla Ansible variables.
285+
286+
The *_image variables in Kolla Ansible define the mapping between
287+
containers and images. Ensure that the mapping defined in this script
288+
matches the one in Kolla Ansible.
289+
"""
290+
supported_images = read_images("etc/kayobe/pulp.yml")
291+
assert supported_images
292+
# Build a map from container to image name.
293+
cmd = """git grep -h '^[a-z0-9_]*_image:' ansible/roles/*/defaults/main.yml"""
294+
image_map_str = subprocess.check_output(cmd, shell=True, cwd=os.path.realpath(kolla_ansible_path))
295+
image_map = yaml.safe_load(image_map_str)
296+
image_var_re = re.compile(r"^([a-z0-9_]+)_image$")
297+
image_map = {
298+
image_var_re.match(image_var).group(1): image.split("/")[-1]
299+
for image_var, image in image_map.items()
300+
}
301+
# Filter out unsupported images.
302+
image_map = {
303+
container: image
304+
for container, image in image_map.items()
305+
if image in supported_images
306+
}
307+
assert image_map
308+
errors = []
309+
# Check that our mapping is correct.
310+
for container, image in image_map.items():
311+
containers = get_containers(image)
312+
if container not in containers:
313+
errors.append((container, image))
314+
if errors:
315+
print("Errors:")
316+
for tag_var, image in errors:
317+
print(f"Expected {tag_var} container to use {image} image")
318+
if errors:
319+
sys.exit(1)
320+
321+
280322
def check_hierarchy(kolla_ansible_path: str):
281323
"""Check the tag variable hierarchy against Kolla Ansible variables."""
282324
cmd = """git grep -h '^[a-z0-9_]*_tag:' ansible/roles/*/defaults/main.yml"""
@@ -352,7 +394,9 @@ def main():
352394

353395
validate(kolla_image_tags)
354396

355-
if args.command == "check-hierarchy":
397+
if args.command == "check-image-map":
398+
check_image_map(args.kolla_ansible_path)
399+
elif args.command == "check-hierarchy":
356400
check_hierarchy(args.kolla_ansible_path)
357401
elif args.command == "check-tags":
358402
check_tags(base_distros, kolla_image_tags, args.registry, args.namespace)

0 commit comments

Comments
 (0)