Skip to content

Commit

Permalink
支持基于Prometheus自定义指标的扩缩容功能 (#38)
Browse files Browse the repository at this point in the history
Co-authored-by: caoyingjunz <[email protected]>
  • Loading branch information
puzhihao and caoyingjunz authored Nov 19, 2024
1 parent 7cee531 commit a353065
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 19 deletions.
78 changes: 60 additions & 18 deletions pkg/controller/controller_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -195,33 +195,75 @@ 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:
averageValue, err := resource.ParseQuantity(metricValue)
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
Expand Down Expand Up @@ -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{}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit a353065

Please sign in to comment.