Skip to content

Commit c04de9a

Browse files
committed
remove ok variable and change the type of v variable to map
1 parent eece675 commit c04de9a

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

internal/store/horizontalpodautoscaler.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ const (
3636
value metricTargetType = iota
3737
utilization
3838
average
39-
40-
metricTargetTypeCount // Used as a length argument to arrays
4139
)
4240

4341
func (m metricTargetType) String() string {
@@ -134,55 +132,53 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
134132
for _, m := range a.Spec.Metrics {
135133
var metricName string
136134

137-
var v [metricTargetTypeCount]float64
138-
var ok [metricTargetTypeCount]bool
135+
// The variable maps the type of metric to the corresponding value
136+
metricMap := make(map[metricTargetType]float64)
139137

140138
switch m.Type {
141139
case autoscaling.ObjectMetricSourceType:
142140
metricName = m.Object.Metric.Name
143141

144142
if m.Object.Target.Value != nil {
145-
v[value], ok[value] = float64(m.Object.Target.Value.MilliValue())/1000, true
143+
metricMap[value] = float64(m.Object.Target.Value.MilliValue()) / 1000
146144
}
147145
if m.Object.Target.AverageValue != nil {
148-
v[average], ok[average] = float64(m.Object.Target.AverageValue.MilliValue())/1000, true
146+
metricMap[average] = float64(m.Object.Target.AverageValue.MilliValue()) / 1000
149147
}
150148
case autoscaling.PodsMetricSourceType:
151149
metricName = m.Pods.Metric.Name
152150

153-
v[average], ok[average] = float64(m.Pods.Target.AverageValue.MilliValue())/1000, true
151+
metricMap[average] = float64(m.Pods.Target.AverageValue.MilliValue()) / 1000
154152
case autoscaling.ResourceMetricSourceType:
155153
metricName = string(m.Resource.Name)
156154

157155
if m.Resource.Target.AverageUtilization != nil {
158-
v[utilization], ok[utilization] = float64(*m.Resource.Target.AverageUtilization), true
156+
metricMap[utilization] = float64(*m.Resource.Target.AverageUtilization)
159157
}
160158

161159
if m.Resource.Target.AverageValue != nil {
162-
v[average], ok[average] = float64(m.Resource.Target.AverageValue.MilliValue())/1000, true
160+
metricMap[average] = float64(m.Resource.Target.AverageValue.MilliValue()) / 1000
163161
}
164162
case autoscaling.ExternalMetricSourceType:
165163
metricName = m.External.Metric.Name
166164

167165
if m.External.Target.Value != nil {
168-
v[value], ok[value] = float64(m.External.Target.Value.MilliValue())/1000, true
166+
metricMap[value] = float64(m.External.Target.Value.MilliValue()) / 1000
169167
}
170168
if m.External.Target.AverageValue != nil {
171-
v[average], ok[average] = float64(m.External.Target.AverageValue.MilliValue())/1000, true
169+
metricMap[average] = float64(m.External.Target.AverageValue.MilliValue()) / 1000
172170
}
173171
default:
174172
// Skip unsupported metric type
175173
continue
176174
}
177175

178-
for i := range ok {
179-
if ok[i] {
180-
ms = append(ms, &metric.Metric{
181-
LabelKeys: targetMetricLabels,
182-
LabelValues: []string{metricName, metricTargetType(i).String()},
183-
Value: v[i],
184-
})
185-
}
176+
for metricTypeIndex, metricValue := range metricMap {
177+
ms = append(ms, &metric.Metric{
178+
LabelKeys: targetMetricLabels,
179+
LabelValues: []string{metricName, metricTypeIndex.String()},
180+
Value: metricValue,
181+
})
186182
}
187183
}
188184
return &metric.Family{Metrics: ms}

0 commit comments

Comments
 (0)