Skip to content

Commit

Permalink
k8s add pod/container count (Tencent#563)
Browse files Browse the repository at this point in the history
* k8s add pod/container count
* fix namespace permission
  • Loading branch information
ifooth committed Aug 4, 2020
1 parent f2c91c9 commit 20983b5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 9 additions & 0 deletions bcs-app/backend/apps/metric/views/performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,21 @@ def overview(self, request, project_id, cluster_id):
memory_usage = prometheus.get_node_memory_usage(cluster_id, ip)
disk_usage = prometheus.get_node_disk_usage(cluster_id, ip)
diskio_usage = prometheus.get_node_diskio_usage(cluster_id, ip)
container_pod_count = prometheus.get_container_pod_count(cluster_id, ip)

data = {
"cpu_usage": cpu_usage,
"memory_usage": memory_usage,
"disk_usage": disk_usage,
"diskio_usage": diskio_usage,
"container_count": "0",
"pod_count": "0",
}

for count in container_pod_count.get("result") or []:
for k, v in count["metric"].items():
if k == "metric_name" and count["value"]:
data[v] = count["value"][1]
return response.Response(data)

def info(self, request, project_id, cluster_id):
Expand Down
4 changes: 2 additions & 2 deletions bcs-app/backend/apps/metric/views/servicemonitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def filter_no_perm(self, data):
def _validate_namespace_use_perm(self, request, project_id, namespace_list):
"""检查是否有命名空间的使用权限
"""
namespace_map = self._get_cluster_map(project_id)
namespace_map = self._get_namespace_map(project_id)
for namespace in namespace_list:
if namespace in self.NO_PERM_NS:
raise error_codes.APIError(_("namespace operation is not allowed"))
Expand Down Expand Up @@ -185,7 +185,7 @@ def _get_namespace_map(self, project_id):

def list(self, request, project_id, cluster_id):
cluster_map = self._get_cluster_map(project_id)
namespace_map = self._get_cluster_map(project_id)
namespace_map = self._get_namespace_map(project_id)
data = []

if cluster_id not in cluster_map:
Expand Down
11 changes: 11 additions & 0 deletions bcs-app/backend/components/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,17 @@ def get_node_info(cluster_id, ip):
return resp.get("data") or {}


def get_container_pod_count(cluster_id, ip):
"""获取K8S节点容器/Pod数量
"""
prom_query = f"""
label_replace(sum by (instance) ({{__name__="kubelet_running_container_count", cluster_id="{cluster_id}", instance=~"{ip}:\\\\d+"}}), "metric_name", "container_count", "instance", ".*") or
label_replace(sum by (instance) ({{__name__="kubelet_running_pod_count", cluster_id="{cluster_id}", instance=~"{ip}:\\\\d+"}}), "metric_name", "pod_count", "instance", ".*")
""" # noqa
resp = query(prom_query)
return resp.get("data") or {}


def get_node_cpu_usage(cluster_id, ip):
"""获取CPU总使用率
"""
Expand Down

0 comments on commit 20983b5

Please sign in to comment.