def _do_compute_pod(cluster_guid, pod, g): assert utilities.valid_string(cluster_guid) assert utilities.is_wrapped_object(pod, 'Pod') assert isinstance(g, ContextGraph) pod_id = pod['id'] pod_guid = 'Pod:' + pod_id g.add_resource(pod_guid, pod['annotations'], 'Pod', pod['timestamp'], pod['properties']) # pod.properties.spec.nodeName may be missing if the pod is waiting # (not running yet). node_id = utilities.get_attribute(pod, ['properties', 'spec', 'nodeName']) if utilities.valid_string(node_id): # Pod is running. node_guid = 'Node:' + node_id g.add_relation(node_guid, pod_guid, 'runs') # Node runs Pod else: # Pod is not running. g.add_relation(cluster_guid, pod_guid, 'contains') # Cluster contains Pod for container in kubernetes.get_containers_from_pod(pod): metrics.annotate_container(container, pod) _do_compute_container(pod_guid, container, g)
def get_containers_with_metrics(gs, docker_host): """Gets the list of all containers in 'docker_host' with metric annotations. Args: gs: global state. docker_host: the Docker host running the containers. Returns: list of wrapped container objects. Each element in the list is the result of utilities.wrap_object(container, 'Container', ...) Raises: CollectorError: in case of failure to fetch data from Docker. Other exceptions may be raised due to exectution errors. """ # Create a lookup table from pod IDs to pods. # This lookup table is needed when annotating containers with # metrics. Also compute the project's name. containers_list = get_containers(gs, docker_host) if not containers_list: return [] pod_id_to_pod = {} project_id = '_unknown_' # Populate the pod ID to pod lookup table. # Compute the project_id from the name of the first pod. for pod in kubernetes.get_pods(gs, docker_host): assert utilities.is_wrapped_object(pod, 'Pod') pod_id_to_pod[pod['id']] = pod if project_id != '_unknown_': continue pod_hostname = utilities.get_attribute( pod, ['properties', 'spec', 'host']) if utilities.valid_string(pod_hostname): project_id = utilities.node_id_to_project_id(pod_hostname) # We know that there are containers in this docker_host. if not pod_id_to_pod: # there are no pods in this docker_host. msg = 'Docker host %s has containers but no pods' % docker_host gs.logger_exception(msg) raise collector_error.CollectorError(msg) # Annotate the containers with their metrics. for container in containers_list: assert utilities.is_wrapped_object(container, 'Container') parent_pod_id = utilities.get_parent_pod_id(container) if not utilities.valid_string(parent_pod_id): msg = ('missing or invalid parent pod ID in container %s' % container['id']) gs.logger_error(msg) raise collector_error.CollectorError(msg) if parent_pod_id not in pod_id_to_pod: msg = ('could not locate parent pod %s for container %s' % (parent_pod_id, container['id'])) gs.logger_error(msg) raise collector_error.CollectorError(msg) # Note that the project ID may be '_unknown_'. # This is not a big deal, because the aggregator knows the project ID. metrics.annotate_container( project_id, container, pod_id_to_pod[parent_pod_id]) return containers_list
def get_containers_with_metrics(gs, docker_host): """Gets the list of all containers in 'docker_host' with metric annotations. Args: gs: global state. docker_host: the Docker host running the containers. Returns: list of wrapped container objects. Each element in the list is the result of utilities.wrap_object(container, 'Container', ...) Raises: CollectorError: in case of failure to fetch data from Docker. Other exceptions may be raised due to exectution errors. """ # Create a lookup table from pod IDs to pods. # This lookup table is needed when annotating containers with # metrics. Also compute the project's name. containers_list = get_containers(gs, docker_host) if not containers_list: return [] pod_id_to_pod = {} project_id = '_unknown_' # Populate the pod ID to pod lookup table. # Compute the project_id from the name of the first pod. for pod in kubernetes.get_pods(gs, docker_host): assert utilities.is_wrapped_object(pod, 'Pod') pod_id_to_pod[pod['id']] = pod if project_id != '_unknown_': continue pod_hostname = utilities.get_attribute(pod, ['properties', 'spec', 'host']) if utilities.valid_string(pod_hostname): project_id = utilities.node_id_to_project_id(pod_hostname) # We know that there are containers in this docker_host. if not pod_id_to_pod: # there are no pods in this docker_host. msg = 'Docker host %s has containers but no pods' % docker_host gs.logger_exception(msg) raise collector_error.CollectorError(msg) # Annotate the containers with their metrics. for container in containers_list: assert utilities.is_wrapped_object(container, 'Container') parent_pod_id = utilities.get_parent_pod_id(container) if not utilities.valid_string(parent_pod_id): msg = ('missing or invalid parent pod ID in container %s' % container['id']) gs.logger_error(msg) raise collector_error.CollectorError(msg) if parent_pod_id not in pod_id_to_pod: msg = ('could not locate parent pod %s for container %s' % (parent_pod_id, container['id'])) gs.logger_error(msg) raise collector_error.CollectorError(msg) # Note that the project ID may be '_unknown_'. # This is not a big deal, because the aggregator knows the project ID. metrics.annotate_container(project_id, container, pod_id_to_pod[parent_pod_id]) return containers_list