Skip to content

Add option to build Kolla container images by service groups instead of pattern matching#2206

Draft
seunghun1ee wants to merge 5 commits intostackhpc/2025.1from
group-container-image-build
Draft

Add option to build Kolla container images by service groups instead of pattern matching#2206
seunghun1ee wants to merge 5 commits intostackhpc/2025.1from
group-container-image-build

Conversation

@seunghun1ee
Copy link
Member

Currently we build Kolla container images with regex filter which will limit the image getting built with pattern matching.
This brings the problem of Kolla building images from unrelated service or not building related images because of image naming.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new get-service-images command to the kolla-images.py script, allowing users to retrieve a list of Kolla container images based on service groups. The implementation involves refactoring the existing check_hierarchy function for better code reuse. My review focuses on improving type safety and enhancing the readability of the new logic. I've suggested using more precise type hints and refactoring the main logic to be more concise and easier to understand.

Comment on lines +342 to +343
def get_hierarchy(kolla_ansible_path: str) -> yaml:
"""Return the tag variable hierarchy against Kolla Ansible variables"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The return type hint yaml is incorrect as yaml is a module, not a type. This function returns a dictionary, so Dict[str, str] would be the correct type hint. Also, I've added a period at the end of the docstring for consistency with others in the file.

Suggested change
def get_hierarchy(kolla_ansible_path: str) -> yaml:
"""Return the tag variable hierarchy against Kolla Ansible variables"""
def get_hierarchy(kolla_ansible_path: str) -> Dict[str, str]:
"""Return the tag variable hierarchy against Kolla Ansible variables."""

Comment on lines +376 to +411
def get_service_images(kolla_ansible_path: str, services: str):
"""Get space separated list of images used by selected services in Kolla Ansible"""
hierarchy = get_hierarchy(kolla_ansible_path)
services_list = []
is_filtered = False
if services:
services_list = services.split(" ")
is_filtered = True
images_list = []
child_re = re.compile(r"^([a-z0-9_]+)_tag$")
parent_re = re.compile(r"{{[\s]*([a-z0-9_]+)_tag[\s]*}}")
parents_no_child_set = set()
for child, parent in hierarchy.items():
child_name = child_re.match(child).group(1)
parent_name = parent_re.match(parent).group(1)
# This is parent
if parent_name == "openstack":
# And part of the query or no services specified
if is_filtered and child_name in services_list or not is_filtered:
parents_no_child_set.add(child_name) # Add to parent list
continue # Then move on
# This service is not part of the query
if is_filtered and parent_name not in services_list:
continue # ignore
# Child found
if parent_name in parents_no_child_set:
parents_no_child_set.discard(parent_name) # Remove parent that has child
images_list.append(child_name) # Add the child to the list
# Add parent with no child
images_list += list(parents_no_child_set)
# NOTE(seunghun1ee): Currently K-A has inconsistency on mariadb tag on 2025.1 release
# Adding manually
if is_filtered and "mariadb" in services_list or not is_filtered:
images_list.append("mariadb")
images_str = " ".join(images_list).replace("_", "-")
print(images_str)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This function can be simplified for better readability and maintainability:

  1. The services argument can be None, so its type hint should be Optional[str].
  2. The is_filtered boolean flag can be removed by checking services_list directly, which simplifies the conditional logic.

Here is a suggested refactoring that applies these improvements:

def get_service_images(kolla_ansible_path: str, services: Optional[str]):
    """Get space separated list of images used by selected services in Kolla Ansible"""
    hierarchy = get_hierarchy(kolla_ansible_path)
    services_list = services.split(" ") if services else []
    images_list = []
    child_re = re.compile(r"^([a-z0-9_]+)_tag$")
    parent_re = re.compile(r"{{[\s]*([a-z0-9_]+)_tag[\s]*}}")
    parents_no_child_set = set()
    for child, parent in hierarchy.items():
        child_name = child_re.match(child).group(1)
        parent_name = parent_re.match(parent).group(1)
        # This is parent
        if parent_name == "openstack":
            # And part of the query or no services specified
            if not services_list or child_name in services_list:
                parents_no_child_set.add(child_name)  # Add to parent list
            continue  # Then move on
        # This service is not part of the query
        if services_list and parent_name not in services_list:
            continue  # ignore
        # Child found
        if parent_name in parents_no_child_set:
            parents_no_child_set.discard(parent_name)  # Remove parent that has child
        images_list.append(child_name)  # Add the child to the list
    # Add parent with no child
    images_list.extend(parents_no_child_set)
    # NOTE(seunghun1ee): Currently K-A has inconsistency on mariadb tag on 2025.1 release
    # Adding manually
    if not services_list or "mariadb" in services_list:
        images_list.append("mariadb")
    images_str = " ".join(images_list).replace("_", "-")
    print(images_str)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant