Skip to content

Commit

Permalink
add ContainerResourceSourceType for hpa metrics and reduce cyclomatic…
Browse files Browse the repository at this point in the history
… complexity
  • Loading branch information
whitebear009 committed Sep 13, 2022
1 parent 20ef8a7 commit 9e50da3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
52 changes: 27 additions & 25 deletions internal/store/horizontalpodautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,45 +134,38 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat

// The variable maps the type of metric to the corresponding value
metricMap := make(map[metricTargetType]float64)

metricTarget := autoscaling.MetricTarget{}
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,
Expand Down Expand Up @@ -221,6 +214,15 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
if m.Resource.Current.AverageValue != nil {
metricMap[average] = float64(m.Resource.Current.AverageValue.MilliValue()) / 1000
}
case autoscaling.ContainerResourceMetricSourceType:
metricName = string(m.ContainerResource.Name)

if m.ContainerResource.Current.AverageUtilization != nil {
metricMap[utilization] = float64(*m.ContainerResource.Current.AverageUtilization)
}
if m.ContainerResource.Current.AverageValue != nil {
metricMap[average] = float64(m.ContainerResource.Current.AverageValue.MilliValue()) / 1000
}
case autoscaling.ExternalMetricSourceType:
metricName = m.External.Metric.Name

Expand Down
24 changes: 24 additions & 0 deletions internal/store/horizontalpodautoscaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9e50da3

Please sign in to comment.