Skip to content

Commit

Permalink
Refactor parseMetricSpecFor func (#45)
Browse files Browse the repository at this point in the history
Change-Id: Iccc12302bb27639844d7a3a5e126cfadfcc9aee4
  • Loading branch information
caoyingjunz authored Nov 19, 2024
1 parent a353065 commit abc8760
Showing 1 changed file with 81 additions and 83 deletions.
164 changes: 81 additions & 83 deletions pkg/controller/controller_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,90 +187,10 @@ func parseMetricSpecs(annotations map[string]string) ([]autoscalingv2.MetricSpec
return nil, err
}

var metricSpec autoscalingv2.MetricSpec
switch target {
case targetAverageUtilization:
averageUtilization, err := extractAverageUtilization(metricValue)
if err != nil {
return nil, err
}

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
}
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
case memory:
metricSpec.Resource.Name = v1.ResourceMemory
metricSpec, err := parseMetricSpec(target, metricType, metricValue, annotations)
if err != nil {
return nil, err
}

metricSpecs = append(metricSpecs, metricSpec)
}

Expand All @@ -281,6 +201,84 @@ func parseMetricSpecs(annotations map[string]string) ([]autoscalingv2.MetricSpec
return metricSpecs, nil
}

func parseMetricSpec(target string, metricType string, metricValue string, annotations map[string]string) (autoscalingv2.MetricSpec, error) {
if metricType == prometheus {
return parseMetricSpecForPrometheus(target, metricType, metricValue, annotations)
}

return parseMetricSpecFor(target, metricType, metricValue, annotations)
}

func parseMetricSpecFor(target string, metricType string, metricValue string, annotations map[string]string) (autoscalingv2.MetricSpec, error) {
metricSpec := autoscalingv2.MetricSpec{
Type: autoscalingv2.ResourceMetricSourceType,
}

switch target {
case targetAverageUtilization:
averageUtilization, err := extractAverageUtilization(metricValue)
if err != nil {
return autoscalingv2.MetricSpec{}, err
}
metricSpec.Resource = &autoscalingv2.ResourceMetricSource{
Target: autoscalingv2.MetricTarget{
Type: autoscalingv2.UtilizationMetricType, AverageUtilization: utilpointer.Int32Ptr(averageUtilization),
},
}
case targetAverageValue:
averageValue, err := resource.ParseQuantity(metricValue)
if err != nil {
return autoscalingv2.MetricSpec{}, err
}
metricSpec.Resource = &autoscalingv2.ResourceMetricSource{
Target: autoscalingv2.MetricTarget{
Type: autoscalingv2.AverageValueMetricType, AverageValue: &averageValue,
},
}
}

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

return metricSpec, nil
}

func parseMetricSpecForPrometheus(target string, metricType string, metricValue string, annotations map[string]string) (autoscalingv2.MetricSpec, error) {
name, ok := annotations[prometheusCustomMetric]
if !ok {
return autoscalingv2.MetricSpec{}, fmt.Errorf("failed to get targetCustomMetric from annotations")
}
averageValue, err := resource.ParseQuantity(metricValue)
if err != nil {
return autoscalingv2.MetricSpec{}, nil
}

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
}

return metricSpec, nil
}

func IsOwnerReference(uid types.UID, ownerReferences []metav1.OwnerReference) bool {
var isOwnerRef bool
for _, ownerReference := range ownerReferences {
Expand Down

0 comments on commit abc8760

Please sign in to comment.