Skip to content

Commit

Permalink
algo: support pods metric query as workload external query
Browse files Browse the repository at this point in the history
Signed-off-by: Zach Zhu <zzqshu@126.com>
  • Loading branch information
zqzten committed Dec 14, 2023
1 parent 3057d4d commit 7b84ffa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
34 changes: 19 additions & 15 deletions algorithm/kapacity/metric/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'])
Expand Down
7 changes: 3 additions & 4 deletions algorithm/kapacity/portrait/horizontal/predictive/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7b84ffa

Please sign in to comment.