def patch_container_attribute()

in mysqloperator/controller/innodbcluster/logs/logs_types_api.py [0:0]


def patch_container_attribute(sts: Union[dict, api_client.V1StatefulSet], patcher: 'InnoDBClusterObjectModifier', patch: dict, attr: str, add: bool) -> None:
    #print(f"\npatch_container_attribute attr={attr} add={add} patch={patch}")
    attr_c = snail_to_camel(attr)
    for container_idx in range(0, len(patch["containers"])):
        container_name = patch["containers"][container_idx]["name"]
        changed_obj_names = [ attr_v["name"] for attr_v in patch["containers"][container_idx][attr_c] ]
        found = False
        if isinstance(sts, dict):
            #print("\npatch_container_attribute dict\n")
            # first filter out
            for container in sts["spec"]["template"]["spec"]["containers"]:
                if get_object_name(container) == container_name:
                    current_value = get_object_attr(container, attr) if has_object_attr(container, attr) else []
                    #print(f"\t\t\t\tcurrent_value({container_name}.{attr})={current_value}")
                    new_value = [v for v in current_value if (v and (get_object_name(v) not in changed_obj_names))]
                    if add:
                        new_value += patch["containers"][container_idx][attr_c]
                    #print(f"\t\t\t\tnew_value({container_name}.{attr})={new_value}")
                    set_object_attr(container, attr, new_value)
                    found = True
                    break
            if found == False and add:
                utils.merge_patch_object(sts["spec"]["template"]["spec"], patch)
        elif isinstance(sts, api_client.V1StatefulSet):
            #print("\npatch_container_attribute V1StatefulSet\n")
            for container in sts.spec["template"]["spec"]["containers"]:
                if get_object_name(container) == container_name:
                    current_value = get_object_attr(container, attr) if has_object_attr(container, attr) else []
                    #print(f"\t\t\t\tcurrent_value({container_name}.{attr})={current_value}")
                    new_value = [v for v in current_value if (v and (get_object_name(v) not in changed_obj_names))]
                    #print(f"\new_value={new_value}")
                    if add:
                        new_value += patch["containers"][container_idx][attr_c]
                    #print(f"\t\t\t\tnew_value({container_name}.{attr})={new_value}")
                    #print(f"\tset_object_attr={new_value}")
                    set_object_attr(container, attr, new_value)
                    found = True
                    break
            if found == False and add:
                #print(f"patch_container_attribute: STS is V1StatefulSet. Patching with {patch}")
                patcher.patch_sts({"spec":{"template":{"spec": patch}}})