def _memory_in_MB()

in libcloud/compute/drivers/kubevirt.py [0:0]


def _memory_in_MB(memory):  # type: (Union[str, int]) -> int
    """
    parse k8s memory resource units to MiB or MB (depending on input)

    Note:

    - 1 MiB = 1024 KiB = 1024 * 1024 B = 1048576 B
    -  1 MB =  1000 KB = 1000 * 1000 B = 1000000 B

    Reference: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#meaning-of-memory

    :param memory: Limits and requests for memory are measured in bytes.
                   You can express memory as a plain integer or as a fixed-point integer using one of these suffixes:
                   E, P, T, G, M, K, Ei, Pi, Ti, Gi, Mi, Ki
                   For example, the following represent roughly the same value:
                   128974848, 129e6, 129M, 123Mi
    :type memory: ``str`` or ``int``

    :return: memory in MiB (if input is `int` bytes or `str` with suffix `?i`)
             or in MB (if input is `str` with suffix `?`)
    :rtype: ``int``
    """

    try:
        mem_bytes = int(memory)

        return mem_bytes // 1024 // 1024
    except ValueError:
        pass

    if not isinstance(memory, str):
        raise ValueError("memory must be int or str")

    if memory.endswith("Ei"):
        return int(memory.rstrip("Ei")) * 1024 * 1024 * 1024 * 1024
    elif memory.endswith("Pi"):
        return int(memory.rstrip("Pi")) * 1024 * 1024 * 1024
    elif memory.endswith("Ti"):
        return int(memory.rstrip("Ti")) * 1024 * 1024
    elif memory.endswith("Gi"):
        return int(memory.rstrip("Gi")) * 1024
    elif memory.endswith("Mi"):
        return int(memory.rstrip("Mi"))
    elif memory.endswith("Ki"):
        return int(memory.rstrip("Ki")) // 1024
    elif memory.endswith("E"):
        return int(memory.rstrip("E")) * 1000 * 1000 * 1000 * 1000
    elif memory.endswith("P"):
        return int(memory.rstrip("P")) * 1000 * 1000 * 1000
    elif memory.endswith("T"):
        return int(memory.rstrip("T")) * 1000 * 1000
    elif memory.endswith("G"):
        return int(memory.rstrip("G")) * 1000
    elif memory.endswith("M"):
        return int(memory.rstrip("M"))
    elif memory.endswith("K"):
        return int(memory.rstrip("K")) // 1000
    else:
        raise ValueError("memory unit not supported {}".format(memory))