Merge pull request #376 from zalando-incubator/custommetricsselector

Use labels hash in the custom metrics store and metrics store 💅
This commit is contained in:
Katyanna Moura 2021-11-25 10:24:44 +01:00 committed by GitHub
commit 2ccd6903d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 230 additions and 94 deletions

View File

@ -269,7 +269,7 @@ func (p *HPAProvider) collectMetrics(ctx context.Context) {
// GetMetricByName gets a single metric by name.
func (p *HPAProvider) GetMetricByName(name types.NamespacedName, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValue, error) {
metric := p.metricStore.GetMetricsByName(name, info)
metric := p.metricStore.GetMetricsByName(name, info, metricSelector)
if metric == nil {
return nil, provider.NewMetricNotFoundForError(info.GroupResource, info.Metric, name.Name)
}
@ -279,7 +279,7 @@ func (p *HPAProvider) GetMetricByName(name types.NamespacedName, info provider.C
// GetMetricBySelector returns metrics for namespaced resources by
// label selector.
func (p *HPAProvider) GetMetricBySelector(namespace string, selector labels.Selector, info provider.CustomMetricInfo, metricSelector labels.Selector) (*custom_metrics.MetricValueList, error) {
return p.metricStore.GetMetricsBySelector(namespace, selector, info), nil
return p.metricStore.GetMetricsBySelector(objectNamespace(namespace), selector, info), nil
}
// ListAllMetrics list all available metrics from the provicer.
@ -288,7 +288,7 @@ func (p *HPAProvider) ListAllMetrics() []provider.CustomMetricInfo {
}
func (p *HPAProvider) GetExternalMetric(namespace string, metricSelector labels.Selector, info provider.ExternalMetricInfo) (*external_metrics.ExternalMetricValueList, error) {
return p.metricStore.GetExternalMetric(namespace, metricSelector, info)
return p.metricStore.GetExternalMetric(objectNamespace(namespace), metricSelector, info)
}
func (p *HPAProvider) ListAllExternalMetrics() []provider.ExternalMetricInfo {

File diff suppressed because it is too large Load Diff

View File

@ -18,8 +18,7 @@ import (
"k8s.io/metrics/pkg/apis/external_metrics"
)
func newMetricIdentifier(metricName string) custom_metrics.MetricIdentifier {
selector := metav1.LabelSelector{}
func newMetricIdentifier(metricName string, selector metav1.LabelSelector) custom_metrics.MetricIdentifier {
return custom_metrics.MetricIdentifier{
Name: metricName,
Selector: &selector,
@ -46,7 +45,7 @@ func TestInternalMetricStorage(t *testing.T) {
insert: collector.CollectedMetric{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(0, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -94,7 +93,7 @@ func TestInternalMetricStorage(t *testing.T) {
insert: collector.CollectedMetric{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(0, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -151,7 +150,7 @@ func TestInternalMetricStorage(t *testing.T) {
insert: collector.CollectedMetric{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("scalingschedulename"),
Metric: newMetricIdentifier("scalingschedulename", metav1.LabelSelector{}),
Value: *resource.NewQuantity(10, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -208,7 +207,7 @@ func TestInternalMetricStorage(t *testing.T) {
insert: collector.CollectedMetric{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("clusterscalingschedulename"),
Metric: newMetricIdentifier("clusterscalingschedulename", metav1.LabelSelector{}),
Value: *resource.NewQuantity(10, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -265,7 +264,7 @@ func TestInternalMetricStorage(t *testing.T) {
insert: collector.CollectedMetric{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(0, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -307,12 +306,72 @@ func TestInternalMetricStorage(t *testing.T) {
},
},
},
{
test: "insert/list/get an Ingress metric with match labels",
insert: collector.CollectedMetric{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{
MatchLabels: map[string]string{
"select_key": "select_value",
},
}),
Value: *resource.NewQuantity(0, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
Kind: "Ingress",
APIVersion: "extensions/v1beta1",
},
},
},
expectedFound: true,
list: []provider.CustomMetricInfo{
{
GroupResource: schema.GroupResource{
Group: "extensions",
Resource: "ingresses",
},
Namespaced: false,
Metric: "metric-per-unit",
},
},
byName: struct {
name types.NamespacedName
info provider.CustomMetricInfo
}{
name: types.NamespacedName{Name: "metricObject", Namespace: ""},
info: provider.CustomMetricInfo{
GroupResource: schema.GroupResource{
Group: "extensions",
Resource: "ingresses",
},
Namespaced: false,
Metric: "metric-per-unit",
},
},
byLabel: struct {
namespace string
selector labels.Selector
info provider.CustomMetricInfo
}{
namespace: "",
selector: labels.SelectorFromSet(labels.Set{"select_key": "select_value"}),
info: provider.CustomMetricInfo{
GroupResource: schema.GroupResource{
Group: "extensions",
Resource: "ingresses",
},
Namespaced: false,
Metric: "metric-per-unit",
},
},
},
{
test: "insert/list/get an Ingress metric",
insert: collector.CollectedMetric{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(0, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -368,7 +427,7 @@ func TestInternalMetricStorage(t *testing.T) {
insert: collector.CollectedMetric{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(0, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -436,18 +495,15 @@ func TestInternalMetricStorage(t *testing.T) {
require.Equal(t, tc.list, metricInfos)
// Get the metric by name
metric := metricsStore.GetMetricsByName(tc.byName.name, tc.byName.info)
metric := metricsStore.GetMetricsByName(tc.byName.name, tc.byName.info, tc.byLabel.selector)
if tc.expectedFound {
require.Equal(t, tc.insert.Custom, *metric)
metrics := metricsStore.GetMetricsBySelector(tc.byLabel.namespace, tc.byLabel.selector, tc.byLabel.info)
metrics := metricsStore.GetMetricsBySelector(objectNamespace(tc.byLabel.namespace), tc.byLabel.selector, tc.byLabel.info)
require.Equal(t, tc.insert.Custom, metrics.Items[0])
} else {
require.Nil(t, metric)
metrics := metricsStore.GetMetricsBySelector(tc.byLabel.namespace, tc.byLabel.selector, tc.byLabel.info)
metrics := metricsStore.GetMetricsBySelector(objectNamespace(tc.byLabel.namespace), tc.byLabel.selector, tc.byLabel.info)
require.Len(t, metrics.Items, 0)
}
})
}
@ -474,7 +530,7 @@ func TestMultipleMetricValues(t *testing.T) {
{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(0, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -486,7 +542,7 @@ func TestMultipleMetricValues(t *testing.T) {
{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(1, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -543,7 +599,7 @@ func TestMultipleMetricValues(t *testing.T) {
{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(0, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -556,7 +612,7 @@ func TestMultipleMetricValues(t *testing.T) {
{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(1, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -612,11 +668,11 @@ func TestMultipleMetricValues(t *testing.T) {
metricsStore.Insert(insert)
// Get the metric by name
metric := metricsStore.GetMetricsByName(tc.byName.name, tc.byName.info)
metric := metricsStore.GetMetricsByName(tc.byName.name, tc.byName.info, tc.byLabel.selector)
require.Equal(t, insert.Custom, *metric)
// Get the metric by label
metrics := metricsStore.GetMetricsBySelector(tc.byLabel.namespace, tc.byLabel.selector, tc.byLabel.info)
metrics := metricsStore.GetMetricsBySelector(objectNamespace(tc.byLabel.namespace), tc.byLabel.selector, tc.byLabel.info)
require.Equal(t, insert.Custom, metrics.Items[0])
}
@ -677,7 +733,7 @@ func TestCustomMetricsStorageErrors(t *testing.T) {
insert: collector.CollectedMetric{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(0, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -741,10 +797,10 @@ func TestCustomMetricsStorageErrors(t *testing.T) {
require.Equal(t, tc.list, metricInfos)
// Get the metric by name
metric := metricsStore.GetMetricsByName(tc.byName.name, tc.byName.info)
metric := metricsStore.GetMetricsByName(tc.byName.name, tc.byName.info, tc.byLabel.selector)
require.Nil(t, metric)
metrics := metricsStore.GetMetricsBySelector(tc.byLabel.namespace, tc.byLabel.selector, tc.byLabel.info)
metrics := metricsStore.GetMetricsBySelector(objectNamespace(tc.byLabel.namespace), tc.byLabel.selector, tc.byLabel.info)
require.Equal(t, &custom_metrics.MetricValueList{}, metrics)
})
@ -769,7 +825,7 @@ func TestCustomMetricsStorageErrors(t *testing.T) {
{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(0, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -782,7 +838,7 @@ func TestCustomMetricsStorageErrors(t *testing.T) {
{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(1, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -795,7 +851,7 @@ func TestCustomMetricsStorageErrors(t *testing.T) {
{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(1, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -934,7 +990,7 @@ func TestExternalMetricStorage(t *testing.T) {
require.Equal(t, tc.list, metricInfos[0])
// Get the metric by name
metrics, err := metricsStore.GetExternalMetric(tc.get.namespace, tc.get.selector, tc.get.info)
metrics, err := metricsStore.GetExternalMetric(objectNamespace(tc.get.namespace), tc.get.selector, tc.get.info)
require.NoError(t, err)
require.Equal(t, tc.insert.External, metrics.Items[0])
@ -1102,7 +1158,7 @@ func TestMultipleExternalMetricStorage(t *testing.T) {
}
// Get the metric by name
metrics, err := metricsStore.GetExternalMetric(tc.get.namespace, tc.get.selector, tc.get.info)
metrics, err := metricsStore.GetExternalMetric(objectNamespace(tc.get.namespace), tc.get.selector, tc.get.info)
require.NoError(t, err)
require.Len(t, metrics.Items, 1)
require.Contains(t, metrics.Items, tc.insert[tc.expectedIdx].External)
@ -1128,7 +1184,7 @@ func TestMetricsExpiration(t *testing.T) {
customMetric := collector.CollectedMetric{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(0, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",
@ -1167,7 +1223,7 @@ func TestMetricsNonExpiration(t *testing.T) {
customMetric := collector.CollectedMetric{
Type: autoscalingv2.MetricSourceType("Object"),
Custom: custom_metrics.MetricValue{
Metric: newMetricIdentifier("metric-per-unit"),
Metric: newMetricIdentifier("metric-per-unit", metav1.LabelSelector{}),
Value: *resource.NewQuantity(0, ""),
DescribedObject: custom_metrics.ObjectReference{
Name: "metricObject",