Beispiel #1
0
 def test_get_memory_bytes(self):
     self.assertEqual(1024000.0, utils.get_memory_bytes('1000Ki'))
     self.assertEqual(0.001, utils.get_memory_bytes('1E-3'))
     self.assertEqual(0.5, utils.get_memory_bytes('0.0005k'))
     self.assertEqual(1300000.0, utils.get_memory_bytes('1.3E+6'))
     self.assertEqual(1300000.0, utils.get_memory_bytes('1.3E6'))
     self.assertRaises(exception.UnsupportedK8sMemoryFormat,
                       utils.get_memory_bytes, '1E1E')
Beispiel #2
0
    def _parse_pod_info(self, pods):
        """Parse pods and retrieve memory details about each pod

        :param pods: The output of k8s_api.list_namespaced_pods()
        For example:
        {
            'items': [{
                'status': {
                    'container_statuses': None,
                    'pod_ip': None,
                    'phase': 'Pending',
                    'message': None,
                    'conditions': None,
                },
                'spec': {
                    'containers': [{
                        'image': 'nginx',
                        'resources': {'requests': None,
                              'limits': "{u'memory': u'1280e3'}"},
                    }],
                },
                'api_version': None,
            }],
            'kind': 'PodList',
        }

        The above output is the dict form of:
        magnum.common.pythonk8sclient.swagger_client.models.v1_pod_list.
        V1PodList object

        :return: Memory size of each pod. Example:
            [{'Memory': 1280000.0},
             {'Memory': 1280000.0}]
        """
        pods = pods.items
        parsed_containers = []
        for pod in pods:
            containers = pod.spec.containers
            for container in containers:
                memory = 0
                resources = container.resources
                limits = resources.limits
                if limits is not None:
                    # Output of resources.limits is string
                    # for example:
                    # limits = "{'memory': '1000Ki'}"
                    limits = ast.literal_eval(limits)
                    if limits.get('memory', ''):
                        memory = utils.get_memory_bytes(limits['memory'])
                container_dict = {
                    'Memory': memory
                }
                parsed_containers.append(container_dict)
        return parsed_containers
Beispiel #3
0
    def _parse_pod_info(self, pods):
        """Parse pods and retrieve memory details about each pod

        :param pods: The output of k8s_api.list_namespaced_pods()
        For example:
        {
            'items': [{
                'status': {
                    'container_statuses': None,
                    'pod_ip': None,
                    'phase': 'Pending',
                    'message': None,
                    'conditions': None,
                },
                'spec': {
                    'containers': [{
                        'image': 'nginx',
                        'resources': {'requests': None,
                              'limits': "{u'memory': u'1280e3'}"},
                    }],
                },
                'api_version': None,
            }],
            'kind': 'PodList',
        }

        The above output is the dict form of:
        magnum.common.pythonk8sclient.swagger_client.models.v1_pod_list.
        V1PodList object

        :return: Memory size of each pod. Example:
            [{'Memory': 1280000.0},
             {'Memory': 1280000.0}]
        """
        pods = pods.items
        parsed_containers = []
        for pod in pods:
            containers = pod.spec.containers
            for container in containers:
                memory = 0
                resources = container.resources
                limits = resources.limits
                if limits is not None:
                    # Output of resources.limits is string
                    # for example:
                    # limits = "{'memory': '1000Ki'}"
                    limits = ast.literal_eval(limits)
                    if limits.get('memory', ''):
                        memory = utils.get_memory_bytes(limits['memory'])
                container_dict = {'Memory': memory}
                parsed_containers.append(container_dict)
        return parsed_containers
Beispiel #4
0
    def _parse_node_info(self, nodes):
        """Parse nodes to retrieve memory of each node

        :param nodes: The output of k8s_api.list_namespaced_node()
        For example:
        {
            'items': [{
                'status': {
                    'phase': None,
                    'capacity': "{u'memory': u'2049852Ki'}",
                },
            },
            'api_version': None,
            }],
            'kind': 'NodeList',
            'api_version': 'v1',
        }

        The above output is the dict form of:
        magnum.common.pythonk8sclient.swagger_client.models.v1_node_list.
        V1NodeList object

        :return: Memory size of each node. Excample:
            [{'Memory': 1024.0},
             {'Memory': 1024.0}]

        """
        nodes = nodes.items
        parsed_nodes = []
        for node in nodes:
            # Output of node.status.capacity is strong
            # for example:
            # capacity = "{'memory': '1000Ki'}"
            capacity = ast.literal_eval(node.status.capacity)
            memory = utils.get_memory_bytes(capacity['memory'])
            parsed_nodes.append({'Memory': memory})

        return parsed_nodes
Beispiel #5
0
    def _parse_node_info(self, nodes):
        """Parse nodes to retrieve memory of each node

        :param nodes: The output of k8s_api.list_namespaced_node()
        For example:
        {
            'items': [{
                'status': {
                    'phase': None,
                    'capacity': "{u'memory': u'2049852Ki'}",
                },
            },
            'api_version': None,
            }],
            'kind': 'NodeList',
            'api_version': 'v1',
        }

        The above output is the dict form of:
        magnum.common.pythonk8sclient.swagger_client.models.v1_node_list.
        V1NodeList object

        :return: Memory size of each node. Excample:
            [{'Memory': 1024.0},
             {'Memory': 1024.0}]

        """
        nodes = nodes.items
        parsed_nodes = []
        for node in nodes:
            # Output of node.status.capacity is strong
            # for example:
            # capacity = "{'memory': '1000Ki'}"
            capacity = ast.literal_eval(node.status.capacity)
            memory = utils.get_memory_bytes(capacity['memory'])
            parsed_nodes.append({'Memory': memory})

        return parsed_nodes