def status(self): """Return information about Kubernetes cluster.""" try: kubernetes = KubernetesAPI(cluster=self) out = { 'addons': kubernetes.list_services(filter_addons=True), 'deployments': kubernetes.list_deployments(), 'namespaces': kubernetes.list_namespaces(), 'nodes': kubernetes.list_nodes(), 'nodes_pods': kubernetes.count_pods_by_node(), 'persistent_volumes': kubernetes.list_persistent_volumes(), 'persistent_volume_claims': kubernetes.list_persistent_volume_claims(), 'pods': kubernetes.list_pods(), 'replica_sets': kubernetes.list_replica_sets(), 'services': kubernetes.list_services(), 'version': kubernetes.get_version(), } except Exception as e: logger.exception(e) out = {} return out
def topology_data(self): """ Return information about Kubernetes cluster in the format used in visual processing. """ kubernetes = KubernetesAPI(cluster=self) def set_kind(objects, kind): for obj in objects: obj['kind'] = kind nodes = kubernetes.list_nodes() set_kind(nodes, 'Node') pods = kubernetes.list_pods(False) set_kind(pods, 'Pod') namespaces = kubernetes.list_namespaces() set_kind(namespaces, 'Namespace') services = kubernetes.list_services(False) set_kind(services, 'Service') deployments = kubernetes.list_deployments(False) set_kind(deployments, 'Deployment') replica_sets = kubernetes.list_replica_sets(False) replica_set_dict = { datum['metadata']['uid']: datum for datum in replica_sets } raw_data = nodes + pods + services + deployments + namespaces resources = {datum['metadata']['uid']: datum for datum in raw_data} relations = [] namespace_name_2_uid = {} node_name_2_uid = {} service_select_run_2_uid = {} service_select_app_2_uid = {} for resource_id, resource in resources.items(): # Add node name to uid mapping if resource['kind'] == 'Node': node_name_2_uid[resource['metadata']['name']] = resource_id # Add node name to uid mapping if resource['kind'] == 'Namespace': namespace_name_2_uid[resource['metadata'] ['name']] = resource_id # Add service run selector to uid_mapping if resource['kind'] == 'Service' and resource['spec'].get( 'selector', {}) is not None: if resource['spec'].get('selector', {}).get('run', False): service_select_run_2_uid[resource['spec']['selector'] ['run']] = resource_id if resource['spec'].get('selector', {}).get('app', False): service_select_app_2_uid[resource['spec']['selector'] ['app']] = resource_id # Add Containers as top-level resource """ if resource['kind'] == 'Pod': for container in resource['spec']['containers']: container_id = "{1}-{2}".format( resource['metadata']['uid'], container['name']) resources[container_id] = { 'metadata': container, 'kind': 'Container' } relations.append({ 'source': resource_id, 'target': container_id, }) """ for resource_id, resource in resources.items(): if resource['kind'] not in ('Node', 'Namespace'): relations.append({ 'source': resource_id, 'target': namespace_name_2_uid[resource['metadata']['namespace']] }) if resource['kind'] == 'Pod': # Define the relationship between pods and nodes if resource['spec']['node_name'] is not None: relations.append({ 'source': resource_id, 'target': node_name_2_uid[resource['spec']['node_name']] }) # Define relationships between pods and rep sets and # replication controllers if resource['metadata'].get('owner_references', False): if resource['metadata']['owner_references'][0][ 'kind'] == 'ReplicaSet': def get_uid(x): return x['metadata']['owner_references'][0]['uid'] rep_set_id = get_uid(resource) deploy_id = get_uid(replica_set_dict[rep_set_id]) relations.append({ 'source': deploy_id, 'target': resource_id }) # Relation between pods and services if resource.get('metadata', {}).get('labels', {}).get('run', False): relations.append({ 'source': resource_id, 'target': service_select_run_2_uid[resource['metadata']['labels'] ['run']] }) if resource.get('metadata', {}).get('labels', {}).get('app', False): try: app_id = service_select_app_2_uid[resource['metadata'] ['labels']['app']] relations.append({ 'source': resource_id, 'target': app_id }) except Exception as e: pass out = { 'items': resources, 'relations': relations, 'kinds': { 'Pod': '', } } return out
def topology_data(self): """ Return information about Kubernetes cluster in format used in visual processing. """ raw_data = [] kubernetes = KubernetesAPI(cluster=self) nodes = kubernetes.list_nodes() for node in nodes: node['kind'] = 'Node' pods = kubernetes.list_pods(False) for pod in pods: pod['kind'] = 'Pod' namespaces = kubernetes.list_namespaces() for namespace in namespaces: namespace['kind'] = 'Namespace' services = kubernetes.list_services(False) for service in services: service['kind'] = 'Service' deployments = kubernetes.list_deployments(False) for deployment in deployments: deployment['kind'] = 'Deployment' replica_sets = kubernetes.list_replica_sets(False) replica_set_dict = { datum['metadata']['uid']: datum for datum in replica_sets } raw_data = nodes + pods + services + deployments + namespaces resources = {datum['metadata']['uid']: datum for datum in raw_data} relations = [] namespace_name_2_uid = {} node_name_2_uid = {} service_select_run_2_uid = {} service_select_app_2_uid = {} for resource_id, resource in resources.items(): # Add node name to uid mapping if resource['kind'] == 'Node': node_name_2_uid[resource['metadata']['name']] = resource_id # Add node name to uid mapping if resource['kind'] == 'Namespace': namespace_name_2_uid[resource['metadata'] ['name']] = resource_id # Add service run selector to uid_mapping if resource['kind'] == 'Service' and resource['spec'].get( 'selector', {}) is not None: if resource['spec'].get('selector', {}).get('run', False): service_select_run_2_uid[resource['spec']['selector'] ['run']] = resource_id if resource['spec'].get('selector', {}).get('app', False): service_select_app_2_uid[resource['spec']['selector'] ['app']] = resource_id # Add Containers as top-level resource """ if resource['kind'] == 'Pod': for container in resource['spec']['containers']: container_id = "{1}-{2}".format( resource['metadata']['uid'], container['name']) resources[container_id] = { 'metadata': container, 'kind': 'Container' } relations.append({ 'source': resource_id, 'target': container_id, }) """ for resource_id, resource in resources.items(): if resource['kind'] not in ('Node', 'Namespace'): relations.append({ 'source': resource_id, 'target': namespace_name_2_uid[resource['metadata']['namespace']] }) if resource['kind'] == 'Pod': # define relationship between pods and nodes if resource['spec']['node_name'] is not None: relations.append({ 'source': resource_id, 'target': node_name_2_uid[resource['spec']['node_name']] }) # define relationships between pods and rep sets and # replication controllers if resource['metadata'].get('owner_references', False): if resource['metadata']['owner_references'][0][ 'kind'] == 'ReplicaSet': rep_set_id = resource['metadata']['owner_references'][ 0]['uid'] deploy_id = replica_set_dict[rep_set_id]['metadata'][ 'owner_references'][0]['uid'] relations.append({ 'source': deploy_id, 'target': resource_id }) # rel'n between pods and services if resource.get('metadata', {}).get('labels', {}).get('run', False): relations.append({ 'source': resource_id, 'target': service_select_run_2_uid[resource['metadata']['labels'] ['run']] }) if resource.get('metadata', {}).get('labels', {}).get('app', False): try: relations.append({ 'source': resource_id, 'target': service_select_app_2_uid[resource['metadata'] ['labels']['app']] }) except Exception: pass out = { 'items': resources, 'relations': relations, 'kinds': { 'Pod': '', } } # except Exception: # out = { # 'items': [], # 'relations': [] # } return out
def test_list_deployments(self, cluster): api = KubernetesAPI(cluster=cluster) deployments = api.list_deployments() assert isinstance(deployments, list)