コード例 #1
0
 def list_pods(self):
     try:
         pods = self.api_instance.list_pod_for_all_namespaces()
         podList = []
         for p in pods.items:
             status = p.status
             containers = []
             restart_count = 0
             for c in status.container_statuses:
                 restart_count = restart_count + c.restart_count
                 container = Container(name=c.name,
                                       ready=c.ready,
                                       restart_count=c.restart_count,
                                       pod_name=p.metadata.name)
                 if container.ready == False:
                     self.warn_containers.append(container.__dict__)
                 containers.append(container.__dict__)
             host = Host.objects.get(ip=status.host_ip)
             hostname = (host.name if host.name is not None else None)
             pod = Pod(name=p.metadata.name,
                       cluster_name=self.cluster.name,
                       restart_count=restart_count,
                       status=status.phase,
                       namespace=p.metadata.namespace,
                       host_ip=status.host_ip,
                       pod_ip=status.pod_ip,
                       host_name=hostname,
                       containers=containers)
             if restart_count > 0:
                 self.restart_pods.append(pod.__dict__)
             podList.append(pod.__dict__)
         return podList
     except ApiException as e:
         raise Exception('list pod failed!' + e.reason)
コード例 #2
0
 def list_pods(self):
     podList = []
     try:
         pods = self.api_instance.list_pod_for_all_namespaces()
         for p in pods.items:
             status = p.status
             containers = []
             restart_count = 0
             hostname = None
             host_ip = None
             if status.container_statuses is not None:
                 for c in status.container_statuses:
                     restart_count = restart_count + c.restart_count
                     container = Container(name=c.name, ready=c.ready, restart_count=c.restart_count,
                                           pod_name=p.metadata.name)
                     if container.ready == False:
                         self.warn_containers.append(container.__dict__)
                     containers.append(container.__dict__)
             if status.host_ip is not None:
                 host = Host.objects.get(ip=status.host_ip)
                 hostname = (host.name if host.name is not None else None)
                 host_ip = status.host_ip
             pod_ip = (status.pod_ip if status.pod_ip is not None else None)
             pod = Pod(name=p.metadata.name, cluster_name=self.cluster.name, restart_count=restart_count,
                       status=status.phase,
                       namespace=p.metadata.namespace,
                       host_ip=host_ip, pod_ip=pod_ip, host_name=hostname, containers=containers)
             if restart_count > 0:
                 self.restart_pods.append(pod.__dict__)
             if status.phase != 'Running' and status.phase != 'Succeeded':
                 self.error_pods.append(pod.__dict__)
             podList.append(pod.__dict__)
     except ApiException as e:
         logger.error(msg='list pod error ' + e.reason, exc_info=True)
     return podList