From 7b84ffa2640d713627b407206d9aac9b88ca395d Mon Sep 17 00:00:00 2001 From: Zach Zhu Date: Thu, 14 Dec 2023 16:09:48 +0800 Subject: [PATCH] algo: support pods metric query as workload external query Signed-off-by: Zach Zhu --- algorithm/kapacity/metric/query.py | 34 +++++++++++-------- .../portrait/horizontal/predictive/main.py | 7 ++-- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/algorithm/kapacity/metric/query.py b/algorithm/kapacity/metric/query.py index 0758ad2..a36afa4 100644 --- a/algorithm/kapacity/metric/query.py +++ b/algorithm/kapacity/metric/query.py @@ -42,8 +42,12 @@ def fetch_metrics(addr, namespace, metric, scale_target, start, end): start=start, end=end) elif metric_type == 'Pods': - # TODO: support pods metric type - raise RuntimeError('UnsupportedMetricType') + return fetch_pod_metric_history(addr=addr, + namespace=namespace, + metric=metric, + scale_target=scale_target, + start=start, + end=end) elif metric_type == 'Object': return fetch_object_metric_history(addr=addr, namespace=namespace, @@ -71,19 +75,6 @@ def compute_history_range(history_len): return start, end -def fetch_replicas_metric_history(addr, namespace, metric, scale_target, start, end): - external = metric['external'] - metric_identifier = build_metric_identifier(external['metric']) - name, group_kind = get_obj_name_and_group_kind(scale_target) - workload_external = metric_pb.WorkloadExternalQuery(group_kind=group_kind, - namespace=namespace, - name=name, - metric=metric_identifier) - query = metric_pb.Query(type=metric_pb.WORKLOAD_EXTERNAL, - workload_external=workload_external) - return query_metrics(addr=addr, query=query, start=start, end=end) - - def fetch_resource_metric_history(addr, namespace, metric, scale_target, start, end): resource_name = metric['resource']['name'] name, group_kind = get_obj_name_and_group_kind(scale_target) @@ -113,6 +104,19 @@ def fetch_container_resource_metric_history(addr, namespace, metric, scale_targe return query_metrics(addr=addr, query=query, start=start, end=end) +def fetch_pod_metric_history(addr, namespace, metric, scale_target, start, end): + pods = metric['pods'] + metric_identifier = build_metric_identifier(pods['metric']) + name, group_kind = get_obj_name_and_group_kind(scale_target) + workload_external = metric_pb.WorkloadExternalQuery(group_kind=group_kind, + namespace=namespace, + name=name, + metric=metric_identifier) + query = metric_pb.Query(type=metric_pb.WORKLOAD_EXTERNAL, + workload_external=workload_external) + return query_metrics(addr=addr, query=query, start=start, end=end) + + def fetch_object_metric_history(addr, namespace, metric, start, end): obj = metric['object'] metric_identifier = build_metric_identifier(obj['metric']) diff --git a/algorithm/kapacity/portrait/horizontal/predictive/main.py b/algorithm/kapacity/portrait/horizontal/predictive/main.py index d63b79e..ad8804f 100644 --- a/algorithm/kapacity/portrait/horizontal/predictive/main.py +++ b/algorithm/kapacity/portrait/horizontal/predictive/main.py @@ -190,13 +190,12 @@ def fetch_metrics_history(args, env, hp_cr): metric_ctx.resource_target = compute_resource_target(env.namespace, resource, scale_target) metric_ctx.resource_history = resource_history.rename(columns={'value': resource['name']}) elif i == 1: - if metric_type != 'External': + if metric_type != 'Pods': raise RuntimeError('MetricTypeError') - replica_history = query.fetch_replicas_metric_history(env.metrics_server_addr, env.namespace, metric, - scale_target, start, end) + replica_history = query.fetch_metrics(env.metrics_server_addr, env.namespace, metric, scale_target, start, end) metric_ctx.replicas_history = replica_history.rename(columns={'value': 'replicas'}) else: - if metric_type != 'Object' and metric_type != 'External': + if metric_type != 'Pods' and metric_type != 'Object' and metric_type != 'External': raise RuntimeError('MetricTypeError') metric_name = metric['name'] traffic_history = query.fetch_metrics(env.metrics_server_addr, env.namespace, metric, scale_target, start, end)