示例#1
0
    def get(self, request):
        k8s.load_auth(request=self.request)
        apps_api = client.AppsV1Api()
        dp_name = request.GET.get('name', None)
        namespace = request.GET.get('namespace', None)
        data = list()
        for rs in apps_api.list_namespaced_replica_set(
                namespace=namespace).items:
            current_dp_name = rs.metadata.owner_references[0].name
            rs_name = rs.metadata.name
            if dp_name == current_dp_name:
                namespace = rs.metadata.namespace
                replicas = rs.status.replicas
                available_replicas = rs.status.available_replicas
                ready_replicas = rs.status.ready_replicas
                revision = rs.metadata.annotations[
                    "deployment.kubernetes.io/revision"]
                create_time = k8s.dt_format(rs.metadata.creation_timestamp)

                containers = {}
                for c in rs.spec.template.spec.containers:
                    containers[c.name] = c.image

                rs = dict(name=rs_name,
                          namespace=namespace,
                          replicas=replicas,
                          available_replicas=available_replicas,
                          ready_replicas=ready_replicas,
                          revision=revision,
                          containers=containers,
                          create_time=create_time)
                data.append(rs)
        count = len(data)
        result = dict(code=0, msg='', count=count, data=data)
        return JsonResponse(result)
示例#2
0
 def get(self, request):
     k8s.load_auth(request=self.request)
     core_api = client.CoreV1Api()
     search_key = self.request.GET.get('search_key', None)
     data = list()
     try:
         for ns in core_api.list_namespace().items:
             name = ns.metadata.name
             labels = ns.metadata.labels
             create_time = k8s.dt_format(ns.metadata.creation_timestamp)
             namespace = {
                 "name": name,
                 "labels": labels,
                 "create_time": create_time
             }
             if search_key:
                 if search_key in name:
                     data.append(namespace)
             else:
                 data.append(namespace)
         code, msg = 0, '数据返回成功!'
     except client.exceptions.ApiException as e:
         code = 1
         if e.status == 403:
             msg = '没有访问权限!,默认使用default空间'
         else:
             msg = '获取数据失败!'
     count = len(data)
     page = self.request.GET.get('page', None)
     limit = self.request.GET.get('limit', None)
     if limit and page:
         data = k8s.paging_data(page=page, limit=limit, data=data)
     result = {'code': code, 'msg': msg, 'count': count, 'data': data}
     return JsonResponse(result)
示例#3
0
 def get(self, request):
     k8s.load_auth(request=self.request)
     core_api = client.CoreV1Api()
     search_key = self.request.GET.get('search_key', None)
     data = list()
     try:
         for pv in core_api.list_persistent_volume().items:
             name = pv.metadata.name
             capacity = pv.spec.capacity["storage"]
             access_modes = pv.spec.access_modes
             if access_modes[0] == 'ReadWriteMany':
                 access_modes = '多节点读写'
             elif access_modes[0] == 'ReadOnlyMany':
                 access_modes = '多节点只读'
             elif access_modes[0] == 'ReadWriteOnce':
                 access_modes = '单节点读写'
             reclaim_policy = pv.spec.persistent_volume_reclaim_policy
             if reclaim_policy == 'Retain':
                 reclaim_policy = '回收后保留'
             elif reclaim_policy == 'Delete':
                 reclaim_policy = '回收后删除'
             status = pv.status.phase
             if status == 'Available':
                 status = '可用'
             if pv.spec.claim_ref is not None:
                 pvc_ns = pv.spec.claim_ref.namespace
                 pvc_name = pv.spec.claim_ref.name
                 pvc = "%s / %s" % (pvc_ns, pvc_name)
             else:
                 pvc = "未绑定"
             storage_class = pv.spec.storage_class_name
             create_time = k8s.dt_format(pv.metadata.creation_timestamp)
             pv = dict(name=name,
                       capacity=capacity,
                       access_modes=access_modes,
                       reclaim_policy=reclaim_policy,
                       status=status,
                       pvc=pvc,
                       storage_class=storage_class,
                       create_time=create_time)
             if search_key:
                 if search_key in name:
                     data.append(pv)
             else:
                 data.append(pv)
         code, msg = 0, '数据返回成功!'
     except client.exceptions.ApiException as e:
         code = 1
         if e.status == 403:
             msg = '没有访问权限!,默认使用default空间'
         else:
             msg = '获取数据失败!'
     count = len(data)
     page = self.request.GET.get('page', None)
     limit = self.request.GET.get('limit', None)
     if limit and page:
         data = k8s.paging_data(page=page, limit=limit, data=data)
     result = {'code': code, 'msg': msg, 'count': count, 'data': data}
     return JsonResponse(result)
示例#4
0
    def get(self, request):
        k8s.load_auth(request=self.request)
        apps_api = client.AppsV1Api()
        namespace = self.request.GET.get('namespace')
        search_key = self.request.GET.get('search_key', None)
        data = list()
        try:
            for sts in apps_api.list_namespaced_stateful_set(namespace).items:
                name = sts.metadata.name
                namespace = sts.metadata.namespace
                labels = sts.metadata.labels
                selector = sts.spec.selector.match_labels
                replicas = sts.spec.replicas
                ready_replicas = ("0" if sts.status.ready_replicas is None else
                                  sts.status.ready_replicas)
                # current_replicas = sts.status.current_replicas
                service_name = sts.spec.service_name
                containers = {}
                for c in sts.spec.template.spec.containers:
                    containers[c.name] = c.image
                create_time = k8s.dt_format(sts.metadata.creation_timestamp)

                ds = {
                    "name": name,
                    "namespace": namespace,
                    "labels": labels,
                    "replicas": replicas,
                    "ready_replicas": ready_replicas,
                    "service_name": service_name,
                    "selector": selector,
                    "containers": containers,
                    "create_time": create_time
                }
                if search_key:
                    if search_key in name:
                        data.append(ds)
                else:
                    data.append(ds)
            code, msg = 0, '数据返回成功!'
        except client.exceptions.ApiValueError as e:
            code, msg = 1, '{}'.format(e)
        except client.exceptions.ApiException as e:
            code = 1
            if e.status == 403:
                msg = '没有访问权限!,默认使用default空间'
            else:
                msg = '获取数据失败!'
        count = len(data)
        page = self.request.GET.get('page', None)
        limit = self.request.GET.get('limit', None)
        if limit and page:
            data = k8s.paging_data(page=page, limit=limit, data=data)
        result = {'code': code, 'msg': msg, 'count': count, 'data': data}
        return JsonResponse(result)
示例#5
0
    def get(self, request):
        k8s.load_auth(request=self.request)
        core_api = client.CoreV1Api()
        namespace = self.request.GET.get('namespace')
        search_key = self.request.GET.get('search_key', None)
        data = list()
        try:
            for pvc in core_api.list_namespaced_persistent_volume_claim(
                    namespace=namespace).items:
                name = pvc.metadata.name
                namespace = pvc.metadata.namespace
                labels = pvc.metadata.labels
                storage_class_name = pvc.spec.storage_class_name
                access_modes = pvc.spec.access_modes
                capacity = (pvc.status.capacity if pvc.status.capacity is None
                            else pvc.status.capacity["storage"])
                volume_name = pvc.spec.volume_name
                status = pvc.status.phase
                create_time = k8s.dt_format(pvc.metadata.creation_timestamp)

                pvc = {
                    "name": name,
                    "namespace": namespace,
                    "lables": labels,
                    "storage_class_name": storage_class_name,
                    "access_modes": access_modes,
                    "capacity": capacity,
                    "volume_name": volume_name,
                    "status": status,
                    "create_time": create_time
                }
                if search_key:
                    if search_key in name:
                        data.append(pvc)
                else:
                    data.append(pvc)
            code, msg = 0, '数据返回成功!'
        except client.exceptions.ApiValueError as e:
            code, msg = 1, '{}'.format(e)
        except client.exceptions.ApiException as e:
            code = 1
            if e.status == 403:
                msg = '没有访问权限!,默认使用default空间'
            else:
                msg = '获取数据失败!'
        count = len(data)
        page = self.request.GET.get('page', None)
        limit = self.request.GET.get('limit', None)
        if limit and page:
            data = k8s.paging_data(page=page, limit=limit, data=data)
        result = {'code': code, 'msg': msg, 'count': count, 'data': data}
        return JsonResponse(result)
示例#6
0
    def get(self, request):
        k8s.load_auth(request=self.request)
        apps_api = client.AppsV1Api()
        namespace = self.request.GET.get('namespace')
        search_key = self.request.GET.get('search_key', None)
        data = list()
        try:
            for ds in apps_api.list_namespaced_daemon_set(namespace).items:
                name = ds.metadata.name
                namespace = ds.metadata.namespace
                desired_number = ds.status.desired_number_scheduled
                available_number = ds.status.number_available
                labels = ds.metadata.labels
                selector = ds.spec.selector.match_labels
                containers = {}
                for c in ds.spec.template.spec.containers:
                    containers[c.name] = c.image
                create_time = k8s.dt_format(ds.metadata.creation_timestamp)

                ds = {
                    "name": name,
                    "namespace": namespace,
                    "labels": labels,
                    "desired_number": desired_number,
                    "available_number": available_number,
                    "selector": selector,
                    "containers": containers,
                    "create_time": create_time
                }
                if search_key:
                    if search_key in name:
                        data.append(ds)
                else:
                    data.append(ds)
            code, msg = 0, '数据返回成功!'
        except client.exceptions.ApiValueError as e:
            code, msg = 1, '{}'.format(e)
        except client.exceptions.ApiException as e:
            code = 1
            if e.status == 403:
                msg = '没有访问权限!,默认使用default空间'
            else:
                msg = '获取数据失败!'
        count = len(data)
        page = self.request.GET.get('page', None)
        limit = self.request.GET.get('limit', None)
        if limit and page:
            data = k8s.paging_data(page=page, limit=limit, data=data)
        result = {'code': code, 'msg': msg, 'count': count, 'data': data}
        return JsonResponse(result)
示例#7
0
    def get(self, request):
        k8s.load_auth(request=self.request)
        core_api = client.CoreV1Api()
        search_key = self.request.GET.get('search_key', None)
        data = list()
        try:

            for node in core_api.list_node_with_http_info()[0].items:
                name = node.metadata.name
                labels = node.metadata.labels
                status = node.status.conditions[-1].status
                scheduler = ("是" if node.spec.unschedulable is None else "否")
                cpu = node.status.capacity['cpu']
                memory = k8s.memory_convert(node.status.capacity['memory'])
                kebelet_version = node.status.node_info.kubelet_version
                cri_version = node.status.node_info.container_runtime_version
                create_time = k8s.dt_format(node.metadata.creation_timestamp)
                node = {
                    "name": name,
                    "labels": labels,
                    "status": status,
                    "scheduler": scheduler,
                    "cpu": cpu,
                    "memory": memory,
                    "kebelet_version": kebelet_version,
                    "cri_version": cri_version,
                    "create_time": create_time
                }
                if search_key:
                    if search_key in name:
                        data.append(node)
                else:
                    data.append(node)
        except client.exceptions.ApiException as e:
            code = 1
            if e.status == 403:
                msg = '没有访问权限!,默认使用default空间'
            else:
                msg = '获取数据失败!'
            result = {'code': code, 'msg': msg}
        else:
            code, msg = 0, '数据返回成功!'
            count = len(data)
            page = self.request.GET.get('page', None)
            limit = self.request.GET.get('limit', None)
            if limit and page:
                data = k8s.paging_data(page=page, limit=limit, data=data)
            result = {'code': code, 'msg': msg, 'count': count, 'data': data}
        return JsonResponse(result)
示例#8
0
def node_info(core_api, n_name=None):
    node_dic: dict = dict()
    for node in core_api.list_node().items:
        node_name: str = node.metadata.name
        node_dic[node_name] = dict(node_name='',
                                   hostname='',
                                   internal_ip='',
                                   os="",
                                   cpu_arch='',
                                   kernel='',
                                   pod_cidr='',
                                   container_runtime_version='',
                                   kubelet_version='',
                                   kube_proxy_version='',
                                   labels='',
                                   unschedulable='',
                                   taints='',
                                   create_time='')
        node_dic[node_name]['node_name'] = node_name
        for i in node.status.addresses:
            if i.type == 'InternalIP':
                node_dic[node_name]['internal_ip'] = i.address
            elif i.type == 'Hostname':
                node_dic[node_name]['hostname'] = i.address
        node_dic[node_name]['pod_cidr'] = node.spec.pod_cidr
        node_dic[node_name]["os"] = node.status.node_info.os_image
        node_dic[node_name]["kernel"] = node.status.node_info.kernel_version
        node_dic[node_name]["cpu_arch"] = node.status.node_info.architecture
        node_dic[node_name][
            "container_runtime_version"] = node.status.node_info.container_runtime_version
        node_dic[node_name][
            "kubelet_version"] = node.status.node_info.kubelet_version
        node_dic[node_name][
            "kube_proxy_version"] = node.status.node_info.kube_proxy_version
        node_dic[node_name]["unschedulable"] = ("是" if node.spec.unschedulable
                                                else "否")
        node_dic[node_name]["labels"] = node.metadata.labels
        node_dic[node_name]["taints"] = node.spec.taints
        node_dic[node_name]["create_time"] = k8s.dt_format(
            node.metadata.creation_timestamp)

    if n_name is None:
        return node_dic
    else:
        return node_dic[n_name]
示例#9
0
    def get(self, request):
        k8s.load_auth(request=self.request)
        core_api = client.CoreV1Api()
        namespace = self.request.GET.get('namespace')
        search_key = self.request.GET.get('search_key', None)
        data = list()
        try:
            for secret in core_api.list_namespaced_secret(
                    namespace=namespace).items:
                name = secret.metadata.name
                namespace = secret.metadata.namespace
                data_length = (len(secret.data)
                               if secret.data is not None else "空")
                create_time = k8s.dt_format(secret.metadata.creation_timestamp)

                se = {
                    "name": name,
                    "namespace": namespace,
                    "data_length": data_length,
                    "create_time": create_time
                }
                if search_key:
                    if search_key in name:
                        data.append(se)
                else:
                    data.append(se)
            code, msg = 0, '数据返回成功!'
        except client.exceptions.ApiValueError as e:
            code, msg = 1, '{}'.format(e)
        except client.exceptions.ApiException as e:
            code = 1
            if e.status == 403:
                msg = '没有访问权限!,默认使用default空间'
            else:
                msg = '获取数据失败!'
        count = len(data)
        page = self.request.GET.get('page', None)
        limit = self.request.GET.get('limit', None)
        if limit and page:
            data = k8s.paging_data(page=page, limit=limit, data=data)
        result = {'code': code, 'msg': msg, 'count': count, 'data': data}
        return JsonResponse(result)
示例#10
0
    def get(self, request):
        k8s.load_auth(request=self.request)
        core_api = client.CoreV1Api()
        namespace = self.request.GET.get('namespace', None)
        search_key = self.request.GET.get('search_key', None)
        node_name = self.request.GET.get('node_name', None)
        try:

            if namespace is not None:
                data = list()
                for po in core_api.list_namespaced_pod(namespace).items:
                    name = po.metadata.name
                    namespace = po.metadata.namespace
                    labels = po.metadata.labels
                    pod_ip = po.status.pod_ip

                    containers = []  # [{},{},{}]
                    status = "None"
                    # 只为None说明Pod没有创建(不能调度或者正在下载镜像)
                    if po.status.container_statuses is None:
                        status = po.status.conditions[-1].reason
                    else:
                        for c in po.status.container_statuses:
                            c_name = c.name
                            c_image = c.image

                            # 获取重启次数
                            restart_count = c.restart_count

                            # 获取容器状态
                            c_status = "None"
                            if c.ready is True:
                                c_status = "Running"
                            elif c.ready is False:
                                if c.state.waiting is not None:
                                    c_status = c.state.waiting.reason
                                elif c.state.terminated is not None:
                                    c_status = c.state.terminated.reason
                                elif c.state.last_state.terminated is not None:
                                    c_status = c.last_state.terminated.reason

                            c = {
                                'c_name': c_name,
                                'c_image': c_image,
                                'restart_count': restart_count,
                                'c_status': c_status
                            }
                            containers.append(c)

                    create_time = k8s.dt_format(po.metadata.creation_timestamp)
                    po = {
                        "name": name,
                        "namespace": namespace,
                        "pod_ip": pod_ip,
                        "labels": labels,
                        "containers": containers,
                        "status": status,
                        "create_time": create_time
                    }
                    if search_key:
                        if search_key in name:
                            data.append(po)
                    else:
                        data.append(po)

            elif node_name is not None:
                data = node_data.node_pods(core_api, node_name)
            else:
                raise client.exceptions.ApiException
            code, msg = 0, '数据返回成功!'
        except client.exceptions.ApiValueError as e:
            code, msg = 1, '{}'.format(e)
            result = {'code': code, 'msg': msg}
        except client.exceptions.ApiException as e:
            code = 1
            if e.status == 403:
                msg = '没有访问权限!,默认使用default空间'
            else:
                msg = '获取数据失败!'
            result = {'code': code, 'msg': msg}
        else:
            count = len(data)
            page = self.request.GET.get('page', None)
            limit = self.request.GET.get('limit', None)
            if limit and page:
                data = k8s.paging_data(page=page, limit=limit, data=data)
            result = {'code': code, 'msg': msg, 'count': count, 'data': data}
        return JsonResponse(result)
示例#11
0
    def get(self, request):
        namespace = self.request.GET.get('namespace', None)
        dp_name = self.request.GET.get('name', None)
        if not namespace or not dp_name:
            return redirect('deployment')
        k8s.load_auth(request=self.request)
        core_api = client.CoreV1Api()
        apps_api = client.AppsV1Api()
        networking_api = client.NetworkingV1beta1Api()

        dp_info = list()
        for dp in apps_api.list_namespaced_deployment(
                namespace=namespace).items:
            if dp_name == dp.metadata.name:
                name = dp.metadata.name
                namespace = dp.metadata.namespace
                replicas = dp.spec.replicas
                available_replicas = (0 if dp.status.available_replicas is None
                                      else dp.status.available_replicas)
                labels = dp.metadata.labels
                selector = dp.spec.selector.match_labels

                # 通过deployment反向查询对应service
                service = list()
                svc_name = None
                for svc in core_api.list_namespaced_service(
                        namespace=namespace).items:
                    if svc.spec.selector == selector:
                        svc_name = svc.metadata.name
                        svc_type = svc.spec.type
                        cluster_ip = svc.spec.cluster_ip
                        ports = svc.spec.ports

                        service.append({
                            'type': svc_type,
                            'cluster_ip': cluster_ip,
                            'ports': ports
                        })
                # service没有创建,ingress也没有
                ingress = {'rules': None, 'tls': None}
                for ing in networking_api.list_namespaced_ingress(
                        namespace=namespace).items:
                    for r in ing.spec.rules:
                        for b in r.http.paths:
                            if b.backend.service_name == svc_name:
                                ingress['rules'] = ing.spec.rules
                                ingress['tls'] = ing.spec.tls

                containers = list()
                for c in dp.spec.template.spec.containers:
                    c_name = c.name
                    image = c.image
                    liveness_probe = c.liveness_probe
                    readiness_probe = c.readiness_probe
                    resources = c.resources  # 在前端处理
                    env = c.env
                    ports = c.ports
                    volume_mounts = c.volume_mounts
                    args = c.args
                    command = c.command

                    container = {
                        "name": c_name,
                        "image": image,
                        "liveness_probe": liveness_probe,
                        "readiness_probe": readiness_probe,
                        "resources": resources,
                        "env": env,
                        "ports": ports,
                        "volume_mounts": volume_mounts,
                        "args": args,
                        "command": command
                    }
                    containers.append(container)

                tolerations = dp.spec.template.spec.tolerations
                rolling_update = dp.spec.strategy.rolling_update
                volumes = []
                if dp.spec.template.spec.volumes is not None:
                    for v in dp.spec.template.spec.volumes:
                        volume = {}
                        if v.config_map is not None:
                            volume["config_map"] = v.config_map
                        elif v.secret is not None:
                            volume["secret"] = v.secret
                        elif v.empty_dir is not None:
                            volume["empty_dir"] = v.empty_dir
                        elif v.host_path is not None:
                            volume["host_path"] = v.host_path
                        elif v.config_map is not None:
                            volume["downward_api"] = v.downward_api
                        elif v.config_map is not None:
                            volume["glusterfs"] = v.glusterfs
                        elif v.cephfs is not None:
                            volume["cephfs"] = v.cephfs
                        elif v.rbd is not None:
                            volume["rbd"] = v.rbd
                        elif v.persistent_volume_claim is not None:
                            volume[
                                "persistent_volume_claim"] = v.persistent_volume_claim
                        else:
                            volume["unknown"] = "unknown"
                        volumes.append(volume)

                rs_number = dp.spec.revision_history_limit
                create_time = k8s.dt_format(dp.metadata.creation_timestamp)

                dp_info = {
                    "name": name,
                    "namespace": namespace,
                    "replicas": replicas,
                    "available_replicas": available_replicas,
                    "labels": labels,
                    "selector": selector,
                    "containers": containers,
                    "rs_number": rs_number,
                    "rolling_update": rolling_update,
                    "create_time": create_time,
                    "volumes": volumes,
                    "tolerations": tolerations,
                    "service": service,
                    "ingress": ingress
                }
        result = {
            'dp_name': dp_name,
            'namespace': namespace,
            'dp_info': dp_info
        }
        return render(request, 'workload/deployment_details.html', result)
示例#12
0
    def get(self, request):
        k8s.load_auth(request=self.request)
        apps_api = client.AppsV1Api()
        namespace = self.request.GET.get('namespace')
        search_key = self.request.GET.get('search_key', None)
        data = list()
        try:
            for dp in apps_api.list_namespaced_deployment(
                    namespace=namespace).items:
                name = dp.metadata.name
                namespace = dp.metadata.namespace
                replicas = dp.spec.replicas
                available_replicas = (0 if dp.status.available_replicas is None
                                      else dp.status.available_replicas)
                labels = dp.metadata.labels
                selector = dp.spec.selector.match_labels
                if len(dp.spec.template.spec.containers) > 1:
                    images = ""
                    n = 1
                    for c in dp.spec.template.spec.containers:
                        status = ("运行中" if dp.status.conditions[0].status
                                  == "True" else "异常")
                        image = c.image
                        images += "[%s]: %s / %s" % (n, image, status)
                        images += "<br>"
                        n += 1
                else:
                    status = ("运行中" if dp.status.conditions[0].status == "True"
                              else "异常")
                    image = dp.spec.template.spec.containers[0].image
                    images = "%s / %s" % (image, status)

                create_time = k8s.dt_format(dp.metadata.creation_timestamp)

                dp = {
                    "name": name,
                    "namespace": namespace,
                    "replicas": replicas,
                    "available_replicas": available_replicas,
                    "labels": labels,
                    "selector": selector,
                    "images": images,
                    "create_time": create_time
                }
                if search_key:
                    if search_key in name:
                        data.append(dp)
                else:
                    data.append(dp)
            code, msg = 0, '数据返回成功!'
        except client.exceptions.ApiValueError as e:
            code, msg = 1, '{}'.format(e)
        except client.exceptions.ApiException as e:
            code = 1
            if e.status == 403:
                msg = '没有访问权限!,默认使用default空间'
            else:
                msg = '获取数据失败!'
        count = len(data)
        page = self.request.GET.get('page', None)
        limit = self.request.GET.get('limit', None)
        if limit and page:
            data = k8s.paging_data(page=page, limit=limit, data=data)
        result = {'code': code, 'msg': msg, 'count': count, 'data': data}
        return JsonResponse(result)
示例#13
0
    def get(self, request):
        k8s.load_auth(request=self.request)
        core_api = client.CoreV1Api()
        namespace = self.request.GET.get('namespace')
        search_key = self.request.GET.get('search_key', None)
        data = list()
        try:
            for svc in core_api.list_namespaced_service(
                    namespace=namespace).items:
                name = svc.metadata.name
                namespace = svc.metadata.namespace
                labels = svc.metadata.labels
                types = svc.spec.type
                cluster_ip = svc.spec.cluster_ip
                ports = []
                for p in svc.spec.ports:  # 不是序列,不能直接返回
                    port_name = p.name
                    port = p.port
                    target_port = p.target_port
                    protocol = p.protocol
                    node_port = ""
                    if type == "NodePort":
                        node_port = "NodePort: %s <br>" % p.node_port

                    port = {
                        'port_name': port_name,
                        'port': port,
                        'protocol': protocol,
                        'target_port': target_port,
                        'node_port': node_port
                    }
                    ports.append(port)

                selector = svc.spec.selector
                create_time = k8s.dt_format(svc.metadata.creation_timestamp)
                # 确认是否关联Pod
                endpoint = ""
                for ep in core_api.list_namespaced_endpoints(
                        namespace=namespace).items:
                    if ep.metadata.name == name and ep.subsets is None:
                        endpoint = "未关联"
                    else:
                        endpoint = "已关联"

                svc = {
                    "name": name,
                    "namespace": namespace,
                    "type": types,
                    "cluster_ip": cluster_ip,
                    "ports": ports,
                    "labels": labels,
                    "selector": selector,
                    "endpoint": endpoint,
                    "create_time": create_time
                }
                if search_key:
                    if search_key in name:
                        data.append(svc)
                else:
                    data.append(svc)
            code, msg = 0, '数据返回成功!'
        except client.exceptions.ApiValueError as e:
            code, msg = 1, '{}'.format(e)
        except client.exceptions.ApiException as e:
            code = 1
            if e.status == 403:
                msg = '没有访问权限!,默认使用default空间'
            else:
                msg = '获取数据失败!'
        count = len(data)
        page = self.request.GET.get('page', None)
        limit = self.request.GET.get('limit', None)
        if limit and page:
            data = k8s.paging_data(page=page, limit=limit, data=data)
        result = {'code': code, 'msg': msg, 'count': count, 'data': data}
        return JsonResponse(result)
示例#14
0
    def get(self, request):
        k8s.load_auth(request=self.request)
        networking_api = client.NetworkingV1beta1Api()
        namespace = self.request.GET.get('namespace')
        search_key = self.request.GET.get('search_key', None)
        data = list()
        try:
            for ing in networking_api.list_namespaced_ingress(
                    namespace=namespace).items:
                name = ing.metadata.name
                namespace = ing.metadata.namespace
                labels = ing.metadata.labels
                service = "None"
                http_hosts = "None"
                for h in ing.spec.rules:
                    host = h.host
                    path = ("/" if h.http.paths[0].path is None else
                            h.http.paths[0].path)
                    service_name = h.http.paths[0].backend.service_name
                    service_port = h.http.paths[0].backend.service_port
                    http_hosts = {
                        'host': host,
                        'path': path,
                        'service_name': service_name,
                        'service_port': service_port
                    }

                https_hosts = "None"
                if ing.spec.tls is None:
                    https_hosts = ing.spec.tls
                else:
                    for tls in ing.spec.tls:
                        host = tls.hosts[0]
                        secret_name = tls.secret_name
                        https_hosts = {
                            'host': host,
                            'secret_name': secret_name
                        }

                create_time = k8s.dt_format(ing.metadata.creation_timestamp)

                ing = {
                    "name": name,
                    "namespace": namespace,
                    "labels": labels,
                    "http_hosts": http_hosts,
                    "https_hosts": https_hosts,
                    "service": service,
                    "create_time": create_time
                }
                if search_key:
                    if search_key in name:
                        data.append(ing)
                else:
                    data.append(ing)
            code, msg = 0, '数据返回成功!'
        except client.exceptions.ApiValueError as e:
            code, msg = 1, '{}'.format(e)
        except ValueError as e:
            code, msg = 1, '{}'.format(e)
        except client.exceptions.ApiException as e:
            code = 1
            if e.status == 403:
                msg = '没有访问权限!,默认使用default空间'
            else:
                msg = '获取数据失败!'
        count = len(data)
        page = self.request.GET.get('page', None)
        limit = self.request.GET.get('limit', None)
        if limit and page:
            data = k8s.paging_data(page=page, limit=limit, data=data)
        result = {'code': code, 'msg': msg, 'count': count, 'data': data}
        return JsonResponse(result)
示例#15
0
def node_pods(core_api, node_name):
    data = list()
    for pod in core_api.list_pod_for_all_namespaces().items:
        name = pod.spec.node_name
        pod_name = pod.metadata.name
        namespace = pod.metadata.namespace
        status = ("运行中" if pod.status.conditions[-1].status else "异常")
        host_network = pod.spec.host_network
        pod_ip = ("主机网络" if host_network else pod.status.pod_ip)
        create_time = k8s.dt_format(pod.metadata.creation_timestamp)

        if name == node_name:
            if len(pod.spec.containers) == 1:
                cpu_requests = "0"
                cpu_limits = "0"
                memory_requests = "0"
                memory_limits = "0"
                for c in pod.spec.containers:
                    # c_name = c.name
                    # c_image= c.image
                    cpu_requests = "0"
                    cpu_limits = "0"
                    memory_requests = "0"
                    memory_limits = "0"
                    if c.resources.requests is not None:
                        if "cpu" in c.resources.requests:
                            cpu_requests = c.resources.requests["cpu"]
                        if "memory" in c.resources.requests:
                            memory_requests = c.resources.requests["memory"]
                    if c.resources.limits is not None:
                        if "cpu" in c.resources.limits:
                            cpu_limits = c.resources.limits["cpu"]
                        if "memory" in c.resources.limits:
                            memory_limits = c.resources.limits["memory"]
            else:
                c_r = "0"
                c_l = "0"
                m_r = "0"
                m_l = "0"
                cpu_requests = ""
                cpu_limits = ""
                memory_requests = ""
                memory_limits = ""
                for c in pod.spec.containers:
                    c_name = c.name
                    # c_image= c.image
                    if c.resources.requests is not None:
                        if "cpu" in c.resources.requests:
                            c_r = c.resources.requests["cpu"]
                        if "memory" in c.resources.requests:
                            m_r = c.resources.requests["memory"]
                    if c.resources.limits is not None:
                        if "cpu" in c.resources.limits:
                            c_l = c.resources.limits["cpu"]
                        if "memory" in c.resources.limits:
                            m_l = c.resources.limits["memory"]

                    cpu_requests += "%s=%s<br>" % (c_name, c_r)
                    cpu_limits += "%s=%s<br>" % (c_name, c_l)
                    memory_requests += "%s=%s<br>" % (c_name, m_r)
                    memory_limits += "%s=%s<br>" % (c_name, m_l)

            pod = {
                "pod_name": pod_name,
                "namespace": namespace,
                "status": status,
                "pod_ip": pod_ip,
                "cpu_requests": cpu_requests,
                "cpu_limits": cpu_limits,
                "memory_requests": memory_requests,
                "memory_limits": memory_limits,
                "create_time": create_time
            }
            data.append(pod)
    return data