Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 新增获取集群详情 #4404

Open
wants to merge 45 commits into
base: feat/k8s-v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
2d66378
minor: 添加注释
SevenDreamer Nov 28, 2024
3f8a5b5
Merge branch 'feat/add_test_unit/#1010158081120562130' of github.com:…
SevenDreamer Nov 29, 2024
94f2859
minor: 游优化接口文档
SevenDreamer Nov 29, 2024
e017a5a
minor: 提添加only_fields
SevenDreamer Nov 29, 2024
64522b4
minor: 完善单元测试
SevenDreamer Nov 29, 2024
0248403
minor: 添加类属性装饰器
SevenDreamer Dec 2, 2024
4403120
minor: 完善Container单元测试
SevenDreamer Dec 2, 2024
d83fe48
minor: .gitignore add .fleet
SevenDreamer Dec 2, 2024
19cdcdb
minor: 优化接口文档
SevenDreamer Dec 2, 2024
58aa376
minor: 删除文档表格多余的列
SevenDreamer Dec 2, 2024
18de1a7
minor: 新增获取资源详情的接口文档
SevenDreamer Dec 5, 2024
92f57aa
minor: 添加显性类型注释
SevenDreamer Dec 5, 2024
233c595
feat: 新增获取资源详情的接口
SevenDreamer Dec 5, 2024
7d8dae6
Merge branch 'feat/k8s-v2' of github.com:TencentBlueKing/bk-monitor i…
SevenDreamer Dec 5, 2024
b759b5e
minor: 单词拼错修正
SevenDreamer Dec 5, 2024
4d4235b
minor: 新增获取资源详情接口的单元测试
SevenDreamer Dec 5, 2024
db843f4
minor: 新增获取集群下workload_kind列表和count的接口文档
SevenDreamer Dec 9, 2024
e8ff8b4
minor: 添加类型注释
SevenDreamer Dec 9, 2024
cd0a0d8
feat: 优化WrokloadFilter
SevenDreamer Dec 9, 2024
1edb015
feat: 新增获取集群下workload_king列表的接口
SevenDreamer Dec 9, 2024
93b6c69
feat: 获取k8s资源列表新增分页字段
SevenDreamer Dec 9, 2024
34fae82
Merge branch 'feat/k8s-v2' of github.com:TencentBlueKing/bk-monitor i…
SevenDreamer Dec 9, 2024
282e61e
minor: 更新接口文档
SevenDreamer Dec 9, 2024
a3e5df2
minor: 丰富接口文档
SevenDreamer Dec 10, 2024
0248564
feat: ListK8sResources 新增page_type参数,用来区分传统分页和滚动分页
SevenDreamer Dec 10, 2024
e4256f5
minor: readme.md 移除接口文档中ListK8sResources 暂未实现的node部分
SevenDreamer Dec 10, 2024
a4ad87c
minor: readme.md 调整ListK8sResource返回container的响应示例数据结构
SevenDreamer Dec 10, 2024
3be82f0
Merge branch 'feat/k8s-v2' of github.com:TencentBlueKing/bk-monitor i…
SevenDreamer Dec 10, 2024
14cb212
Merge branch 'feat/k8s-v2' of github.com:TencentBlueKing/bk-monitor i…
SevenDreamer Dec 10, 2024
8439f75
minor: readme.md WorkloadOverview 添加响应数据为0的示例
SevenDreamer Dec 10, 2024
14a32e6
feat: 新增WorkloadOverview的单元测试
SevenDreamer Dec 10, 2024
89ae598
Merge branch 'feat/k8s-v2' of github.com:TencentBlueKing/bk-monitor i…
SevenDreamer Dec 11, 2024
835c386
fix: WorkloadOverview中query_string的过滤条件name__icontains
SevenDreamer Dec 11, 2024
ceda332
minor: 添加对WrokloadFilter().filter_dict的单元测试
SevenDreamer Dec 11, 2024
d3a2a6a
minor: 调整WorkloadOverview的单元测试
SevenDreamer Dec 11, 2024
60fe013
Merge branch 'feat/k8s-v2' of github.com:TencentBlueKing/bk-monitor i…
SevenDreamer Dec 11, 2024
89cf87f
fix: 修复缺失请求参数
SevenDreamer Dec 11, 2024
7cd01f3
minor: 添加对分页功能的单元测试
SevenDreamer Dec 11, 2024
6b6d57b
minor: 为K8sNamespaceMeta.filter.filter_queryset新增count()和order_by()方法
SevenDreamer Dec 11, 2024
e074048
minor: 修复单元测试
SevenDreamer Dec 11, 2024
cc5316c
Merge branch 'feat/k8s-v2' of github.com:TencentBlueKing/bk-monitor i…
SevenDreamer Dec 12, 2024
632c5a9
fix: ListK8sResource获取数据报错
SevenDreamer Dec 12, 2024
d906820
Merge branch 'feat/k8s-v2' of github.com:TencentBlueKing/bk-monitor i…
SevenDreamer Dec 13, 2024
c2dda92
fix: ListK8sResource filter_dict添加workload时查询报错
SevenDreamer Dec 13, 2024
391ba2a
feat: GetResourceDetail新增获取集群的详细信息
SevenDreamer Dec 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bkmonitor/packages/monitor_web/k8s/core/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def transform_filter_dict(self, filter_obj) -> Dict:
else:
field_name, condition = parsed_token
# 字段映射, prometheus数据字段 映射到 ORM中的 模型字段
field_name = resource_meta.column_mapping.get(field_name, field_name)
field_name = self.meta.column_mapping.get(field_name, field_name)
# 重新组装特殊查询条件
new_key = field_name if len(parsed_token) == 1 else f"{field_name}__{condition}"
orm_filter_dict[new_key] = value
Expand Down
44 changes: 19 additions & 25 deletions bkmonitor/packages/monitor_web/k8s/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class RequestSerializer(serializers.Serializer):
bk_biz_id: int = serializers.IntegerField(required=True)
namespace: str = serializers.CharField(required=True)
resource_type: str = serializers.ChoiceField(
required=True, choices=["pod", "workload", "container"], label="资源类型"
required=True, choices=["pod", "workload", "container", "cluster"], label="资源类型"
)
# 私有参数
pod_name: str = serializers.CharField(required=False, allow_null=True)
Expand All @@ -132,6 +132,9 @@ def validate_request_data(self, request_data: Dict):
elif resource_type == "container":
fields = ["pod_name", "container_name"]
self.validate_field_exist(resource_type, fields, request_data)
elif resource_type == "cluster":
# 只有 bk_biz_id, bcs_cluster_id 是必传的
pass

return super().validate_request_data(request_data)

Expand All @@ -158,32 +161,23 @@ def link_to_string(cls, item: Dict):
def perform_request(self, validated_request_data):
bk_biz_id = validated_request_data["bk_biz_id"]
bcs_cluster_id = validated_request_data["bcs_cluster_id"]
namespace = validated_request_data["namespace"]

resource_type = validated_request_data["resource_type"]
if resource_type == "pod":
items: List[Dict] = resource.scene_view.get_kubernetes_pod(
bk_biz_id=bk_biz_id,
bcs_cluster_id=bcs_cluster_id,
namespace=namespace,
pod_name=validated_request_data["pod_name"],
)
elif resource_type == "workload":
items: List[Dict] = resource.scene_view.get_kubernetes_workload(
bk_biz_id=bk_biz_id,
bcs_cluster_id=bcs_cluster_id,
namespace=namespace,
workload_name=validated_request_data["workload_name"],
workload_type=validated_request_data["workload_type"],
)
elif resource_type == "container":
items: List[Dict] = resource.scene_view.get_kubernetes_container(
bk_biz_id=bk_biz_id,
bcs_cluster_id=bcs_cluster_id,
namespace=namespace,
pod_name=validated_request_data["pod_name"],
container_name=validated_request_data["container_name"],
)

# 不同的 resource_type 对应不同要调用的接口,并且记录额外要传递的参数
resource_router: Dict[str, List[Dict]] = {
"cluster": [resource.scene_view.get_kubernetes_cluster, []],
"pod": [resource.scene_view.get_kubernetes_pod, ["namespace", "pod_name"]],
"workload": [resource.scene_view.get_kubernetes_workload, ["namespace", "workload_name", "workload_type"]],
"container": [resource.scene_view.get_kubernetes_container, ["namespace", "pod_name", "container_name"]],
}
# 构建同名字典 -> {"field":validated_request_data["field"]}
extra_request_arg = {key: validated_request_data[key] for key in resource_router[resource_type][1]}

# 调用对应的资源类型的接口,返回对应的接口数据
items = resource_router[resource_type][0](
**{"bk_biz_id": bk_biz_id, "bcs_cluster_id": bcs_cluster_id, **extra_request_arg}
)

for item in items:
self.link_to_string(item)
Expand Down
85 changes: 85 additions & 0 deletions bkmonitor/packages/monitor_web/tests/k8s/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from django.utils import timezone
from humanize import naturaldelta

from bkmonitor.models import BCSCluster
from monitor_web.k8s.core.filters import (
ContainerFilter,
NamespaceFilter,
Expand Down Expand Up @@ -195,6 +196,23 @@ def setUp(self):
created_at=timezone.now(),
last_synced_at=timezone.now(),
).save()
BCSCluster(
bk_biz_id=2,
bcs_cluster_id="BCS-K8S-00000",
name="",
area_name="",
project_name="",
environment="正式",
updated_at=timezone.now(),
node_count=18,
cpu_usage_ratio=19.22,
memory_usage_ratio=65.36,
disk_usage_ratio=51.45,
created_at=timezone.now(),
status="RUNNING",
monitor_status="success",
last_synced_at=timezone.now(),
).save()

def test_link_to_string(self):
items = [
Expand Down Expand Up @@ -525,6 +543,73 @@ def test_with_container(self):
]
self.assertEqual(result, expect_data)

def test_with_cluster(self):
validated_request_data = {
"bk_biz_id": 2,
"bcs_cluster_id": "BCS-K8S-00000",
"namespace": "bkmonitor",
"resource_type": "cluster",
}
age = naturaldelta(
datetime.utcnow().replace(tzinfo=timezone.utc) - datetime.utcnow().replace(tzinfo=timezone.utc)
)
with mock.patch("bkmonitor.models.BCSCluster.update_monitor_status") as mock_update_monitor_status:
mock_update_monitor_status.return_value = None

actual_data = GetResourceDetail()(validated_request_data)
expect_data = [
{
"key": "bcs_cluster_id",
"name": "集群ID",
"type": "string",
"value": "BCS-K8S-00000",
},
{"key": "name", "name": "集群名称", "type": "string", "value": ""},
{"key": "status", "name": "运行状态", "type": "string", "value": "RUNNING"},
{
"key": "monitor_status",
"name": "采集状态",
"type": "status",
"value": {"type": "success", "text": "正常"},
},
{"key": "environment", "name": "环境", "type": "string", "value": "正式"},
{"key": "node_count", "name": "节点数量", "type": "number", "value": 18},
{
"key": "cpu_usage_ratio",
"name": "CPU使用率",
"type": "progress",
"value": {"value": 19.22, "label": "19.22%", "status": "SUCCESS"},
},
{
"key": "memory_usage_ratio",
"name": "内存使用率",
"type": "progress",
"value": {"value": 65.36, "label": "65.36%", "status": "SUCCESS"},
},
{
"key": "disk_usage_ratio",
"name": "磁盘使用率",
"type": "progress",
"value": {"value": 51.45, "label": "51.45%", "status": "SUCCESS"},
},
{"key": "area_name", "name": "区域", "type": "string", "value": ""},
{
"key": "created_at",
"name": "创建时间",
"type": "string",
"value": age,
},
{
"key": "updated_at",
"name": "更新时间",
"type": "string",
"value": age,
},
{"key": "project_name", "name": "所属项目", "type": "string", "value": ""},
{"key": "description", "name": "描述", "type": "string", "value": ""},
]
self.assertEqual(expect_data, actual_data)


class TestWorkloadOverview(TestCase):
databases = {"default", "monitor_api"}
Expand Down
Loading