Exemple #1
0
 def get(self, request, *args, **kwargs):
     project_name = kwargs['project_name']
     cluster_data = []
     restart_pods = []
     warn_containers = []
     error_loki_containers = []
     error_pods = []
     if project_name == 'all':
         clusters = Cluster.objects.filter(~Q(status=Cluster.CLUSTER_STATUS_READY),
                                           ~Q(status=Cluster.CLUSTER_STATUS_INSTALLING),
                                           ~Q(status=Cluster.CLUSTER_STATUS_DELETING))
         for c in clusters:
             cluster_monitor = ClusterMonitor(c)
             res = cluster_monitor.list_cluster_data()
             if len(res) != 0:
                 restart_pods = restart_pods + res.get('restart_pods', [])
                 warn_containers = warn_containers + res.get('warn_containers', [])
                 error_loki_containers = error_loki_containers + res.get('error_loki_containers', [])
                 error_pods = error_pods + res.get('error_pods', [])
                 cluster_data.append(json.dumps(res))
         restart_pods = kubeops_api.cluster_monitor.quick_sort_pods(restart_pods)
         error_loki_containers = kubeops_api.cluster_monitor.quick_sort_error_loki_container(error_loki_containers)
     else:
         cluster = Cluster.objects.get(name=project_name)
         if cluster.status != Cluster.CLUSTER_STATUS_READY and cluster.status != Cluster.CLUSTER_STATUS_INSTALLING and cluster.status != Cluster.CLUSTER_STATUS_DELETING:
             cluster_monitor = ClusterMonitor(cluster)
             res = cluster_monitor.list_cluster_data()
             if len(res) != 0:
                 restart_pods = res.get('restart_pods', [])
                 warn_containers = res.get('warn_containers', [])
                 error_loki_containers = res.get('error_loki_containers', [])
                 error_pods = res.get('error_pods', [])
                 cluster_data.append(json.dumps(res))
     return Response(data={'data': cluster_data, 'warnContainers': warn_containers, 'restartPods': restart_pods,
                           'errorLokiContainers': error_loki_containers, 'errorPods': error_pods})
Exemple #2
0
    def get(self, request, *args, **kwargs):
        project_name = kwargs['project_name']
        item_name = kwargs['item_name']
        cluster_data = []
        restart_pods = []
        warn_containers = []
        error_loki_containers = []
        error_pods = []

        if len(request.user.profile.items) == 0 and request.user.is_superuser is False:
            return Response(data={'data': cluster_data, 'warnContainers': warn_containers, 'restartPods': restart_pods,
                              'errorLokiContainers': error_loki_containers, 'errorPods': error_pods})

        if project_name == 'all':
            item_ids = []
            if item_name == 'all':
                item_ids = Item.objects.all().values_list('id')
            else:
                item = Item.objects.get(name=item_name)
                item_ids.append(item.id)
            resourceIds = ItemResource.objects.filter(item_id__in=item_ids).values_list('resource_id')
            clusters = Cluster.objects.filter(~Q(status=Cluster.CLUSTER_STATUS_READY),
                                              ~Q(status=Cluster.CLUSTER_STATUS_INSTALLING),
                                              ~Q(status=Cluster.CLUSTER_STATUS_DELETING)).filter(id__in=resourceIds)
            for c in clusters:
                cluster_monitor = ClusterMonitor(c)
                res = cluster_monitor.list_cluster_data()
                if len(res) != 0:
                    restart_pods = restart_pods + res.get('restart_pods', [])
                    warn_containers = warn_containers + res.get('warn_containers', [])
                    error_loki_containers = error_loki_containers + res.get('error_loki_containers', [])
                    error_pods = error_pods + res.get('error_pods', [])
                    cluster_data.append(json.dumps(res))
            restart_pods = kubeops_api.cluster_monitor.quick_sort_pods(restart_pods)
            error_loki_containers = kubeops_api.cluster_monitor.quick_sort_error_loki_container(error_loki_containers)
        else:
            cluster = Cluster.objects.get(name=project_name)
            if cluster.status != Cluster.CLUSTER_STATUS_READY and cluster.status != Cluster.CLUSTER_STATUS_INSTALLING and cluster.status != Cluster.CLUSTER_STATUS_DELETING:
                cluster_monitor = ClusterMonitor(cluster)
                res = cluster_monitor.list_cluster_data()
                if len(res) != 0:
                    restart_pods = res.get('restart_pods', [])
                    warn_containers = res.get('warn_containers', [])
                    error_loki_containers = res.get('error_loki_containers', [])
                    error_pods = res.get('error_pods', [])
                    cluster_data.append(json.dumps(res))
        return Response(data={'data': cluster_data, 'warnContainers': warn_containers, 'restartPods': restart_pods,
                              'errorLokiContainers': error_loki_containers, 'errorPods': error_pods})