From a3530652a0d6a90f8e1382f95fdfb4df30651182 Mon Sep 17 00:00:00 2001 From: puzhihao <127116692+puzhihao@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:14:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=9F=BA=E4=BA=8EPrometheus?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E6=8C=87=E6=A0=87=E7=9A=84=E6=89=A9?= =?UTF-8?q?=E7=BC=A9=E5=AE=B9=E5=8A=9F=E8=83=BD=20(#38)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: caoyingjunz --- pkg/controller/controller_utils.go | 78 +++++++++++++++++++++++------- pkg/controller/types.go | 4 +- 2 files changed, 63 insertions(+), 19 deletions(-) diff --git a/pkg/controller/controller_utils.go b/pkg/controller/controller_utils.go index 5172882..7a8785d 100644 --- a/pkg/controller/controller_utils.go +++ b/pkg/controller/controller_utils.go @@ -162,7 +162,7 @@ func getMetricTarget(metricName string) (string, string, error) { return "", "", fmt.Errorf("invalied metric item %s", metricName) } metricType := metricTypeSlice[0] - if metricType != cpu && metricType != memory { + if metricType != cpu && metricType != memory && metricType != prometheus { return "", "", fmt.Errorf("unsupprted metric resource name: %s", metricType) } @@ -195,14 +195,38 @@ func parseMetricSpecs(annotations map[string]string) ([]autoscalingv2.MetricSpec return nil, err } - metricSpec = autoscalingv2.MetricSpec{ - Type: autoscalingv2.ResourceMetricSourceType, - Resource: &autoscalingv2.ResourceMetricSource{ - Target: autoscalingv2.MetricTarget{ - Type: autoscalingv2.UtilizationMetricType, - AverageUtilization: utilpointer.Int32Ptr(averageUtilization), + if metricType == prometheus { + averageValue, err := resource.ParseQuantity(metricValue) + if err != nil { + return nil, err + } + name, ok := annotations[prometheusCustomMetric] + if !ok { + return nil, fmt.Errorf("failed to get targetCustomMetric from annotations") + } + + metricSpec = autoscalingv2.MetricSpec{ + Type: autoscalingv2.PodsMetricSourceType, + Pods: &autoscalingv2.PodsMetricSource{ + Metric: autoscalingv2.MetricIdentifier{ + Name: name, + }, + Target: autoscalingv2.MetricTarget{ + AverageValue: &averageValue, + Type: autoscalingv2.UtilizationMetricType, + }, }, - }, + } + } else { + metricSpec = autoscalingv2.MetricSpec{ + Type: autoscalingv2.ResourceMetricSourceType, + Resource: &autoscalingv2.ResourceMetricSource{ + Target: autoscalingv2.MetricTarget{ + Type: autoscalingv2.UtilizationMetricType, + AverageUtilization: utilpointer.Int32Ptr(averageUtilization), + }, + }, + } } case targetAverageValue: @@ -210,18 +234,36 @@ func parseMetricSpecs(annotations map[string]string) ([]autoscalingv2.MetricSpec if err != nil { return nil, err } - - metricSpec = autoscalingv2.MetricSpec{ - Type: autoscalingv2.ResourceMetricSourceType, - Resource: &autoscalingv2.ResourceMetricSource{ - Target: autoscalingv2.MetricTarget{ - Type: autoscalingv2.AverageValueMetricType, - AverageValue: &averageValue, + if metricType == prometheus { + name, ok := annotations[prometheusCustomMetric] + if !ok { + return nil, fmt.Errorf("failed to get targetCustomMetric from annotations") + } + + metricSpec = autoscalingv2.MetricSpec{ + Type: autoscalingv2.PodsMetricSourceType, + Pods: &autoscalingv2.PodsMetricSource{ + Metric: autoscalingv2.MetricIdentifier{ + Name: name, + }, + Target: autoscalingv2.MetricTarget{ + AverageValue: &averageValue, + Type: autoscalingv2.AverageValueMetricType, + }, }, - }, + } + } else { + metricSpec = autoscalingv2.MetricSpec{ + Type: autoscalingv2.ResourceMetricSourceType, + Resource: &autoscalingv2.ResourceMetricSource{ + Target: autoscalingv2.MetricTarget{ + Type: autoscalingv2.AverageValueMetricType, + AverageValue: &averageValue, + }, + }, + } } } - switch metricType { case cpu: metricSpec.Resource.Name = v1.ResourceCPU @@ -306,7 +348,7 @@ type Empty struct{} func NewItems() map[string]Empty { items := make(map[string]Empty) - for _, k := range []string{cpuAverageUtilization, memoryAverageUtilization, cpuAverageValue, memoryAverageValue} { + for _, k := range []string{cpuAverageUtilization, memoryAverageUtilization, prometheusAverageUtilization, cpuAverageValue, memoryAverageValue, prometheusAverageValue} { items[k] = Empty{} } diff --git a/pkg/controller/types.go b/pkg/controller/types.go index 7b4c7a6..8ddaedc 100644 --- a/pkg/controller/types.go +++ b/pkg/controller/types.go @@ -28,7 +28,6 @@ const ( targetAverageUtilization string = "targetAverageUtilization" targetAverageValue string = "targetAverageValue" - // TODO: prometheus is not supported for now. cpu string = "cpu" memory string = "memory" prometheus string = "prometheus" @@ -37,6 +36,9 @@ const ( memoryAverageUtilization = "memory." + PixiuRootPrefix + PixiuSeparator + targetAverageUtilization prometheusAverageUtilization = "prometheus." + PixiuRootPrefix + PixiuSeparator + targetAverageUtilization + // 指标来着 prometheus 时,需要指定指标名称 + prometheusCustomMetric = PixiuRootPrefix + PixiuSeparator + "targetCustomMetric" + // CPU, in cores. (500m = .5 cores) // Memory, in bytes. (500Gi = 500GiB = 500 * 1024 * 1024 * 1024) cpuAverageValue = "cpu." + PixiuRootPrefix + PixiuSeparator + targetAverageValue