Esempio n. 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})
Esempio n. 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})
Esempio n. 3
0
 def get(self, request, *args, **kwargs):
     project_name = self.kwargs['project_name']
     cluster_data = []
     restart_pods = []
     warn_containers = []
     if project_name == 'all':
         clusters = Cluster.objects.filter(~Q(
             status=Cluster.CLUSTER_STATUS_READY))
         for c in clusters:
             cluster_monitor = ClusterMonitor(c)
             res = cluster_monitor.list_cluster_data()
             restart_pods = restart_pods + res['restart_pods']
             warn_containers = warn_containers + res['warn_containers']
             cluster_data.append(json.dumps(res))
         restart_pods = ClusterMonitor(
             clusters[0]).quick_sort_pods(restart_pods)
     else:
         cluster = Cluster.objects.get(name=project_name)
         if cluster.status != Cluster.CLUSTER_STATUS_READY:
             cluster_monitor = ClusterMonitor(cluster)
             res = cluster_monitor.list_cluster_data()
             restart_pods = res['restart_pods']
             warn_containers = res['warn_containers']
             cluster_data.append(json.dumps(res))
     return Response(
         data={
             'data': cluster_data,
             'warnContainers': warn_containers,
             'restartPods': restart_pods
         })
Esempio n. 4
0
 def post(self, request, *args, **kwargs):
     id = kwargs['cluster_id']
     cluster = Cluster.objects.get(id=id)
     cluster_monitor = ClusterMonitor(cluster)
     cis_thread = CisThread(cluster_monitor.create_kube_bench)
     cis_thread.start()
     response = HttpResponse(content_type='application/json')
     response.write(json.dumps({'msg': '已运行检查 请稍后查看结果'}))
     return response
Esempio n. 5
0
 def get(self, request, *args, **kwargs):
     project_name = kwargs['project_name']
     cluster = Cluster.objects.get(name=project_name)
     if cluster.status == Cluster.CLUSTER_STATUS_READY or cluster.status == Cluster.CLUSTER_STATUS_INSTALLING:
         return Response(data={'msg': ': 集群未创建'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     cluster_monitor = ClusterMonitor(cluster)
     result = cluster_monitor.list_storage_class()
     response = HttpResponse(content_type='application/json')
     response.write(json.dumps(result))
     return response
Esempio n. 6
0
 def get(self, request, *args, **kwargs):
     project_name = self.kwargs['project_name']
     cluster = Cluster.objects.get(name=project_name)
     response = HttpResponse(content_type='application/json')
     if cluster.status == Cluster.CLUSTER_STATUS_READY or cluster.status == Cluster.CLUSTER_STATUS_INSTALLING:
         return Response(data={'msg': ': 集群未创建'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     cluster_monitor = ClusterMonitor(cluster)
     try:
         result = cluster_monitor.get_kubernetes_status()
     except Exception as e:
         logger.error(e, exc_info=True)
         return Response(data={'msg': ': 数据读取失败!'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     response.write(json.dumps(result))
     return response
Esempio n. 7
0
 def get(self, request, *args, **kwargs):
     project_name = self.kwargs['project_name']
     cluster = Cluster.objects.get(name=project_name)
     response = HttpResponse(content_type='application/json')
     if cluster.status == Cluster.CLUSTER_STATUS_READY or cluster.status == Cluster.CLUSTER_STATUS_INSTALLING:
         return Response(data={'msg': ': 集群未创建'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     cluster_monitor = ClusterMonitor(cluster)
     # host = "prometheus.apps." + cluster.name + "." + cluster.cluster_doamin_suffix
     # config = {
     #     'host': host
     # }
     # prometheus_client = PrometheusClient(config)
     # result = prometheus_client.handle_targets_message(prometheus_client.targets())
     try:
         result = cluster_monitor.get_kubernetes_status()
     except Exception as e:
         logger.error(e, exc_info=True)
         return Response(data={'msg': ': 数据读取失败!'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
     response.write(json.dumps(result))
     return response
Esempio n. 8
0
 def get(self, request, *args, **kwargs):
     project_name = self.kwargs['project_name']
     cluster = Cluster.objects.get(name=project_name)
     cluster_monitor = ClusterMonitor(cluster)
     res = cluster_monitor.list_pods()
     print(res)
Esempio n. 9
0
    def start(self):
        result = {"raw": {}, "summary": {}}
        pre_deploy_execution_start.send(self.__class__, execution=self)
        cluster = self.get_cluster()
        settings = Setting.get_settings()
        extra_vars = {
            "cluster_name": cluster.name,
        }
        extra_vars.update(settings)
        extra_vars.update(cluster.configs)
        ignore_errors = False
        return_running = False

        if self.operation == "install":
            logger.info(
                msg="cluster: {} exec: {} ".format(cluster, self.operation))
            cluster.change_status(Cluster.CLUSTER_STATUS_INSTALLING)
            result = self.on_install(extra_vars)
            cluster.change_status(Cluster.CLUSTER_STATUS_RUNNING)
        elif self.operation == 'uninstall':
            logger.info(
                msg="cluster: {} exec: {} ".format(cluster, self.operation))
            cluster.change_status(Cluster.CLUSTER_STATUS_DELETING)
            result = self.on_uninstall(extra_vars)
            cluster_monitor = ClusterMonitor(cluster)
            cluster_monitor.delete_cluster_redis_data()
            cluster.change_status(Cluster.CLUSTER_STATUS_READY)
        elif self.operation == 'bigip-config':
            logger.info(
                msg="cluster: {} exec: {} ".format(cluster, self.operation))
            ignore_errors = True
            result = self.on_f5_config(extra_vars)
        elif self.operation == 'upgrade':
            logger.info(
                msg="cluster: {} exec: {} ".format(cluster, self.operation))
            cluster.change_status(Cluster.CLUSTER_STATUS_UPGRADING)
            package_name = self.params.get('package', None)
            package = Package.objects.get(name=package_name)
            extra_vars.update(package.meta.get('vars'))
            result = self.on_upgrade(extra_vars)
            cluster.upgrade_package(package_name)
            cluster.change_status(Cluster.CLUSTER_STATUS_RUNNING)
        elif self.operation == 'scale':
            logger.info(
                msg="cluster: {} exec: {} ".format(cluster, self.operation))
            ignore_errors = True
            return_running = True
            cluster.change_status(Cluster.CLUSTER_DEPLOY_TYPE_SCALING)
            result = self.on_scaling(extra_vars)
            cluster.exit_new_node()
            cluster.change_status(Cluster.CLUSTER_STATUS_RUNNING)
        elif self.operation == 'add-worker':
            logger.info(
                msg="cluster: {} exec: {} ".format(cluster, self.operation))
            ignore_errors = True
            return_running = True
            cluster.change_status(Cluster.CLUSTER_DEPLOY_TYPE_SCALING)
            result = self.on_add_worker(extra_vars)
            cluster.exit_new_node()
            cluster.change_status(Cluster.CLUSTER_STATUS_RUNNING)
        elif self.operation == 'remove-worker':
            logger.info(
                msg="cluster: {} exec: {} ".format(cluster, self.operation))
            ignore_errors = True
            return_running = True
            cluster.change_status(Cluster.CLUSTER_DEPLOY_TYPE_SCALING)
            result = self.on_remove_worker(extra_vars)
            if not result.get('summary', {}).get('success', False):
                cluster.exit_new_node()
            else:
                node_name = self.params.get('node', None)
                cluster.change_to()
                node = Node.objects.get(name=node_name)
                node.delete()
                cluster.change_status(Cluster.CLUSTER_STATUS_RUNNING)
        elif self.operation == 'restore':
            logger.info(
                msg="cluster: {} exec: {} ".format(cluster, self.operation))
            cluster.change_status(Cluster.CLUSTER_STATUS_RESTORING)
            cluster_backup_id = self.params.get('clusterBackupId', None)
            result = self.on_restore(extra_vars, cluster_backup_id)
            cluster.change_status(Cluster.CLUSTER_STATUS_RUNNING)
        elif self.operation == 'backup':
            logger.info(
                msg="cluster: {} exec: {} ".format(cluster, self.operation))
            cluster.change_status(Cluster.CLUSTER_STATUS_BACKUP)
            cluster_storage_id = self.params.get('backupStorageId', None)
            result = self.on_backup(extra_vars)
            self.on_upload_backup_file(cluster_storage_id)
            cluster.change_status(Cluster.CLUSTER_STATUS_RUNNING)
        if not result.get('summary', {}).get('success', False):
            if not ignore_errors:
                cluster.change_status(Cluster.CLUSTER_STATUS_ERROR)
            if return_running:
                cluster.change_status(Cluster.CLUSTER_STATUS_RUNNING)
            logger.error(msg=":cluster {} exec {} error".format(
                cluster, self.operation),
                         exc_info=True)
        post_deploy_execution_start.send(self.__class__,
                                         execution=self,
                                         result=result,
                                         ignore_errors=ignore_errors)
        return result
Esempio n. 10
0
 def get(self, request, *args, **kwargs):
     project_name = self.kwargs['project_name']
     cluster = Cluster.objects.get(name=project_name)
     cluster_monitor = ClusterMonitor(cluster)
     res = cluster_monitor.list_cluster_data()
     return JsonResponse({'data': json.dumps(res)})