@@ -36,8 +36,6 @@ const (
36
36
value metricTargetType = iota
37
37
utilization
38
38
average
39
-
40
- metricTargetTypeCount // Used as a length argument to arrays
41
39
)
42
40
43
41
func (m metricTargetType ) String () string {
@@ -134,55 +132,53 @@ func hpaMetricFamilies(allowAnnotationsList, allowLabelsList []string) []generat
134
132
for _ , m := range a .Spec .Metrics {
135
133
var metricName string
136
134
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 )
139
137
140
138
switch m .Type {
141
139
case autoscaling .ObjectMetricSourceType :
142
140
metricName = m .Object .Metric .Name
143
141
144
142
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
146
144
}
147
145
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
149
147
}
150
148
case autoscaling .PodsMetricSourceType :
151
149
metricName = m .Pods .Metric .Name
152
150
153
- v [average ], ok [ average ] = float64 (m .Pods .Target .AverageValue .MilliValue ())/ 1000 , true
151
+ metricMap [average ] = float64 (m .Pods .Target .AverageValue .MilliValue ()) / 1000
154
152
case autoscaling .ResourceMetricSourceType :
155
153
metricName = string (m .Resource .Name )
156
154
157
155
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 )
159
157
}
160
158
161
159
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
163
161
}
164
162
case autoscaling .ExternalMetricSourceType :
165
163
metricName = m .External .Metric .Name
166
164
167
165
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
169
167
}
170
168
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
172
170
}
173
171
default :
174
172
// Skip unsupported metric type
175
173
continue
176
174
}
177
175
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
+ })
186
182
}
187
183
}
188
184
return & metric.Family {Metrics : ms }
0 commit comments