Fix panic when trying to add key to nil map

Bug was introduced trying to solved the following issue:
> When scenarios where two HPAs reference the same object but have
different metric calculation (e.g. ingresses with different weights),
kube-metrics-adapter calculates the two metrics but always overrides
one of the metrics when saving it to the store.

This commit fixes a issue where without the added `return` the system
was continuing in an invalid states where `labels2metric` wasn't
properly initialized, causing the system to panic.

Signed-off-by: Katyanna Moura <amelie.kn@gmail.com>
This commit is contained in:
Katyanna Moura
2022-01-12 10:56:03 +01:00
parent 4d4c70c553
commit 71a8e99d1f
2 changed files with 64 additions and 0 deletions

View File

@ -185,6 +185,7 @@ func (s *MetricStore) insertCustomMetric(value custom_metrics.MetricValue) {
object2label[object] = labelsHashToCustomMetricStore{
labelsKey: customMetric,
}
return
}
labels2metric[labelsKey] = customMetric

View File

@ -894,6 +894,69 @@ func TestCustomMetricsStorageErrors(t *testing.T) {
},
},
},
// Regression test covering https://github.com/zalando-incubator/kube-metrics-adapter/issues/379
{
test: "test multiple metrics with label added to metric name",
insert: []collector.CollectedMetric{
{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(0, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
Namespace: "default",
Kind: "Deployment",
APIVersion: "apps/v1",
},
},
},
{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(1, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject-000",
Namespace: "default",
Kind: "Deployment",
APIVersion: "apps/v1",
},
},
},
},
list: []provider.CustomMetricInfo{
{
GroupResource: schema.GroupResource{},
Namespaced: true,
Metric: "metric-per-unit",
},
},
byName: struct {
name types.NamespacedName
info provider.CustomMetricInfo
}{
name: types.NamespacedName{Name: "metricObject-000", Namespace: "default"},
info: provider.CustomMetricInfo{
GroupResource: schema.GroupResource{},
Namespaced: true,
Metric: "metric-per-unit",
},
},
byLabel: struct {
namespace string
selector labels.Selector
info provider.CustomMetricInfo
}{
namespace: "default",
selector: labels.Everything(),
info: provider.CustomMetricInfo{
GroupResource: schema.GroupResource{},
Namespaced: true,
Metric: "metric-per-unit",
},
},
},
}
for _, tc := range multiValueTests {