Skip to content

Commit

Permalink
支持Prometheus指标
Browse files Browse the repository at this point in the history
  • Loading branch information
puzhihao committed Nov 18, 2024
1 parent 7cee531 commit fbea7ba
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
31 changes: 29 additions & 2 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 @@ -227,6 +227,33 @@ func parseMetricSpecs(annotations map[string]string) ([]autoscalingv2.MetricSpec
metricSpec.Resource.Name = v1.ResourceCPU
case memory:
metricSpec.Resource.Name = v1.ResourceMemory
case 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,
},
},
}
switch target {
case targetAverageUtilization:
metricSpec.Pods.Target.Type = autoscalingv2.UtilizationMetricType
case targetAverageValue:
metricSpec.Pods.Target.Type = autoscalingv2.AverageValueMetricType
}
}

metricSpecs = append(metricSpecs, metricSpec)
Expand Down Expand Up @@ -306,7 +333,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
3 changes: 3 additions & 0 deletions pkg/controller/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ const (
memoryAverageUtilization = "memory." + PixiuRootPrefix + PixiuSeparator + targetAverageUtilization
prometheusAverageUtilization = "prometheus." + PixiuRootPrefix + PixiuSeparator + targetAverageUtilization

// When the metrics come from Prometheus, the metric name needs to be specified.
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 fbea7ba

Please sign in to comment.