From 386eafd05fa46bde9fd2fb83d10df0a959370564 Mon Sep 17 00:00:00 2001 From: whitebear009 Date: Tue, 13 Sep 2022 17:16:33 +0800 Subject: [PATCH] add ContainerResourceSourceType for hpa metrics and reduce cyclomatic complexity --- internal/store/horizontalpodautoscaler.go | 86 ++++++++----------- .../store/horizontalpodautoscaler_test.go | 24 ++++++ 2 files changed, 60 insertions(+), 50 deletions(-) diff --git a/internal/store/horizontalpodautoscaler.go b/internal/store/horizontalpodautoscaler.go index 1b0de5241a..ba069a816b 100644 --- a/internal/store/horizontalpodautoscaler.go +++ b/internal/store/horizontalpodautoscaler.go @@ -131,48 +131,41 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat ms := make([]*metric.Metric, 0, len(a.Spec.Metrics)) for _, m := range a.Spec.Metrics { var metricName string - + var metricTarget autoscaling.MetricTarget // The variable maps the type of metric to the corresponding value metricMap := make(map[metricTargetType]float64) switch m.Type { case autoscaling.ObjectMetricSourceType: metricName = m.Object.Metric.Name - - if m.Object.Target.Value != nil { - metricMap[value] = float64(m.Object.Target.Value.MilliValue()) / 1000 - } - if m.Object.Target.AverageValue != nil { - metricMap[average] = float64(m.Object.Target.AverageValue.MilliValue()) / 1000 - } + metricTarget = m.Object.Target case autoscaling.PodsMetricSourceType: metricName = m.Pods.Metric.Name - - metricMap[average] = float64(m.Pods.Target.AverageValue.MilliValue()) / 1000 + metricTarget = m.Pods.Target case autoscaling.ResourceMetricSourceType: metricName = string(m.Resource.Name) - - if m.Resource.Target.AverageUtilization != nil { - metricMap[utilization] = float64(*m.Resource.Target.AverageUtilization) - } - - if m.Resource.Target.AverageValue != nil { - metricMap[average] = float64(m.Resource.Target.AverageValue.MilliValue()) / 1000 - } + metricTarget = m.Resource.Target + case autoscaling.ContainerResourceMetricSourceType: + metricName = string(m.ContainerResource.Name) + metricTarget = m.ContainerResource.Target case autoscaling.ExternalMetricSourceType: metricName = m.External.Metric.Name - - if m.External.Target.Value != nil { - metricMap[value] = float64(m.External.Target.Value.MilliValue()) / 1000 - } - if m.External.Target.AverageValue != nil { - metricMap[average] = float64(m.External.Target.AverageValue.MilliValue()) / 1000 - } + metricTarget = m.External.Target default: // Skip unsupported metric type continue } + if metricTarget.Value != nil { + metricMap[value] = float64(metricTarget.Value.MilliValue()) / 1000 + } + if metricTarget.AverageValue != nil { + metricMap[average] = float64(metricTarget.AverageValue.MilliValue()) / 1000 + } + if metricTarget.AverageUtilization != nil { + metricMap[utilization] = float64(*metricTarget.AverageUtilization) + } + for metricTypeIndex, metricValue := range metricMap { ms = append(ms, &metric.Metric{ LabelKeys: targetMetricLabels, @@ -193,48 +186,41 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat ms := make([]*metric.Metric, 0, len(a.Status.CurrentMetrics)) for _, m := range a.Status.CurrentMetrics { var metricName string - + var currentMetric autoscaling.MetricValueStatus // The variable maps the type of metric to the corresponding value metricMap := make(map[metricTargetType]float64) switch m.Type { case autoscaling.ObjectMetricSourceType: metricName = m.Object.Metric.Name - - if m.Object.Current.Value != nil { - metricMap[value] = float64(m.Object.Current.Value.MilliValue()) / 1000 - } - if m.Object.Current.AverageValue != nil { - metricMap[average] = float64(m.Object.Current.AverageValue.MilliValue()) / 1000 - } + currentMetric = m.Object.Current case autoscaling.PodsMetricSourceType: metricName = m.Pods.Metric.Name - - metricMap[average] = float64(m.Pods.Current.AverageValue.MilliValue()) / 1000 + currentMetric = m.Pods.Current case autoscaling.ResourceMetricSourceType: metricName = string(m.Resource.Name) - - if m.Resource.Current.AverageUtilization != nil { - metricMap[utilization] = float64(*m.Resource.Current.AverageUtilization) - } - - if m.Resource.Current.AverageValue != nil { - metricMap[average] = float64(m.Resource.Current.AverageValue.MilliValue()) / 1000 - } + currentMetric = m.Resource.Current + case autoscaling.ContainerResourceMetricSourceType: + metricName = string(m.ContainerResource.Name) + currentMetric = m.ContainerResource.Current case autoscaling.ExternalMetricSourceType: metricName = m.External.Metric.Name - - if m.External.Current.Value != nil { - metricMap[value] = float64(m.External.Current.Value.MilliValue()) / 1000 - } - if m.External.Current.AverageValue != nil { - metricMap[average] = float64(m.External.Current.AverageValue.MilliValue()) / 1000 - } + currentMetric = m.External.Current default: // Skip unsupported metric type continue } + if currentMetric.Value != nil { + metricMap[value] = float64(currentMetric.Value.MilliValue()) / 1000 + } + if currentMetric.AverageValue != nil { + metricMap[average] = float64(currentMetric.AverageValue.MilliValue()) / 1000 + } + if currentMetric.AverageUtilization != nil { + metricMap[utilization] = float64(*currentMetric.AverageUtilization) + } + for metricTypeIndex, metricValue := range metricMap { ms = append(ms, &metric.Metric{ LabelKeys: targetMetricLabels, diff --git a/internal/store/horizontalpodautoscaler_test.go b/internal/store/horizontalpodautoscaler_test.go index d573113a70..41c8d9c49b 100644 --- a/internal/store/horizontalpodautoscaler_test.go +++ b/internal/store/horizontalpodautoscaler_test.go @@ -128,6 +128,16 @@ func TestHPAStore(t *testing.T) { }, }, }, + { + Type: autoscaling.ContainerResourceMetricSourceType, + ContainerResource: &autoscaling.ContainerResourceMetricSource{ + Name: "cpu", + Container: "container1", + Target: autoscaling.MetricTarget{ + AverageUtilization: int32ptr(80), + }, + }, + }, // No targets, this metric should be ignored { Type: autoscaling.ResourceMetricSourceType, @@ -211,6 +221,7 @@ func TestHPAStore(t *testing.T) { kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="hits",metric_target_type="value",namespace="ns1"} 10 kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="connections",metric_target_type="average",namespace="ns1"} 0.7 kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="connections",metric_target_type="value",namespace="ns1"} 0.5 + kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="cpu",metric_target_type="utilization",namespace="ns1"} 80 kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="memory",metric_target_type="average",namespace="ns1"} 819200 kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="memory",metric_target_type="utilization",namespace="ns1"} 80 kube_horizontalpodautoscaler_spec_target_metric{horizontalpodautoscaler="hpa1",metric_name="sqs_jobs",metric_target_type="value",namespace="ns1"} 30 @@ -338,6 +349,17 @@ func TestHPAStore(t *testing.T) { }, }, }, + { + Type: "ContainerResource", + ContainerResource: &autoscaling.ContainerResourceMetricStatus{ + Name: "cpu", + Container: "container1", + Current: autoscaling.MetricValueStatus{ + AverageValue: resourcePtr(resource.MustParse("80m")), + AverageUtilization: int32ptr(10), + }, + }, + }, { Type: "External", External: &autoscaling.ExternalMetricStatus{ @@ -379,6 +401,8 @@ func TestHPAStore(t *testing.T) { kube_horizontalpodautoscaler_status_target_metric{horizontalpodautoscaler="hpa2",metric_name="memory",metric_target_type="utilization",namespace="ns1"} 28 kube_horizontalpodautoscaler_status_target_metric{horizontalpodautoscaler="hpa2",metric_name="cpu",metric_target_type="average",namespace="ns1"} 0.062 kube_horizontalpodautoscaler_status_target_metric{horizontalpodautoscaler="hpa2",metric_name="cpu",metric_target_type="utilization",namespace="ns1"} 6 + kube_horizontalpodautoscaler_status_target_metric{horizontalpodautoscaler="hpa2",metric_name="cpu",metric_target_type="average",namespace="ns1"} 0.08 + kube_horizontalpodautoscaler_status_target_metric{horizontalpodautoscaler="hpa2",metric_name="cpu",metric_target_type="utilization",namespace="ns1"} 10 kube_horizontalpodautoscaler_status_target_metric{horizontalpodautoscaler="hpa2",metric_name="traefik_backend_requests_per_second",metric_target_type="value",namespace="ns1"} 0 kube_horizontalpodautoscaler_status_target_metric{horizontalpodautoscaler="hpa2",metric_name="traefik_backend_requests_per_second",metric_target_type="average",namespace="ns1"} 2.9 kube_horizontalpodautoscaler_status_target_metric{horizontalpodautoscaler="hpa2",metric_name="traefik_backend_errors_per_second",metric_target_type="value",namespace="ns1"} 0