Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/prometheus-adapter' into prometh…
Browse files Browse the repository at this point in the history
…eus-adapter

# Conflicts:
#	pkg/controller/controller_utils.go
  • Loading branch information
puzhihao committed Nov 18, 2024
2 parents 6432968 + 31c77d5 commit 76827f8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 46 deletions.
70 changes: 25 additions & 45 deletions pkg/controller/controller_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,14 @@ func parseMetricSpecs(annotations map[string]string) ([]autoscalingv2.MetricSpec
},
},
}
}

case podsTargetAverageUtilization:
switch metricType {
case cpu:
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
Expand All @@ -239,42 +245,17 @@ func parseMetricSpecs(annotations map[string]string) ([]autoscalingv2.MetricSpec
},
Target: autoscalingv2.MetricTarget{
AverageValue: &averageValue,
Type: autoscalingv2.UtilizationMetricType,
},
},
}

case podsTargetAverageValue:
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.AverageValueMetricType,
},
},
switch target {
case targetAverageUtilization:
metricSpec.Pods.Target.Type = autoscalingv2.UtilizationMetricType
case targetAverageValue:
metricSpec.Pods.Target.Type = autoscalingv2.AverageValueMetricType
}
}

switch metricType {
case cpu:
metricSpec.Resource.Name = v1.ResourceCPU
case memory:
metricSpec.Resource.Name = v1.ResourceMemory
}

metricSpecs = append(metricSpecs, metricSpec)
}

Expand Down Expand Up @@ -310,28 +291,27 @@ func ManageByPixiuController(hpa *autoscalingv2.HorizontalPodAutoscaler) bool {
}

func extractReplicas(annotations map[string]string, replicasType string) (int32, error) {
var (
Replicas string
exists bool
)
var Replicas int64
var err error
switch replicasType {
case MinReplicas:
Replicas, exists = annotations[MinReplicas]
minReplicas, exists := annotations[MinReplicas]
if !exists {
return 1, nil // Default minReplicas is 1
// Default minReplicas is 1
return 1, nil
}
if Replicas, err = strconv.ParseInt(minReplicas, 10, 32); err != nil {
return 0, err
}
case MaxReplicas:
Replicas, exists = annotations[MaxReplicas]
maxReplicas, exists := annotations[MaxReplicas]
if !exists {
return 6, nil // Default maxReplicas is 6
return 0, fmt.Errorf("%s is required", MaxReplicas)
}
Replicas, err = strconv.ParseInt(maxReplicas, 10, 32)
}

targetReplicas, err := strconv.ParseInt(Replicas, 10, 32)
if err != nil {
return 0, err
}
return int32(targetReplicas), err
return int32(Replicas), err
}

func extractAverageUtilization(averageUtilization string) (int32, error) {
Expand All @@ -352,7 +332,7 @@ type Empty struct{}

func NewItems() map[string]Empty {
items := make(map[string]Empty)
for _, k := range []string{cpuAverageUtilization, memoryAverageUtilization, prometheusAverageUtilization, cpuAverageValue, memoryAverageValue, prometheusAverageValue} {
for _, k := range []string{cpuAverageUtilization, memoryAverageUtilization, prometheusAverageUtilization, prometheusAverageValue, cpuAverageValue, memoryAverageValue} {
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 @@ -30,7 +30,6 @@ const (
podsTargetAverageUtilization string = "podsTargetAverageUtilization"
podsTargetAverageValue string = "podsTargetAverageValue"

// TODO: prometheus is not supported for now.
cpu string = "cpu"
memory string = "memory"
prometheus string = "prometheus"
Expand All @@ -42,6 +41,9 @@ const (
// When the metrics come from Prometheus, the metric name needs to be specified.
prometheusCustomMetric = PixiuRootPrefix + PixiuSeparator + "targetCustomMetric"

Check failure on line 42 in pkg/controller/types.go

View workflow job for this annotation

GitHub Actions / golang-lint

previous declaration

Check failure on line 42 in pkg/controller/types.go

View workflow job for this annotation

GitHub Actions / golang-lint

previous declaration

Check failure on line 42 in pkg/controller/types.go

View workflow job for this annotation

GitHub Actions / golang-lint

previous declaration

// 指标来着 prometheus 时,需要指定指标名称
prometheusCustomMetric = PixiuRootPrefix + PixiuSeparator + "targetCustomMetric"

Check failure on line 45 in pkg/controller/types.go

View workflow job for this annotation

GitHub Actions / golang-lint

prometheusCustomMetric redeclared in this block

Check failure on line 45 in pkg/controller/types.go

View workflow job for this annotation

GitHub Actions / golang-lint

prometheusCustomMetric redeclared in this block

Check failure on line 45 in pkg/controller/types.go

View workflow job for this annotation

GitHub Actions / golang-lint

prometheusCustomMetric redeclared in this block

// 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 76827f8

Please sign in to comment.